All pastes #413187 Raw Edit

Someone

public text v1 · immutable
#413187 ·published 2007-03-28 05:31 UTC
rendered paste body
/****************************************************************************
*
*   Copyright (c) 2006 Dave Hylands     <dhylands@gmail.com>
*
*   This program is free software; you can redistribute it and/or modify
*   it under the terms of the GNU General Public License version 2 as
*   published by the Free Software Foundation.
*
*   Alternatively, this software may be distributed under the terms of BSD
*   license.
*
*   See README and COPYING for more details.
*
****************************************************************************/

#include <avr/io.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "Hardware.h"
#include "Timer.h"
#include "UART.h"

#include "Log.h"

#include "Delay.h"
#include "RCInput.h"

#define NUM_CHANNELS    4
#define NUM_AVG_SAMPLES 8

char **split(char *string,char delim);
char **split(char *string,char delim)
{
char **a,*pch=NULL;
int element=0,i=0,z=0;

a=(char **)malloc(sizeof(char *));
pch=strchr(string,delim);
//we do this to catch the first element
if(pch!=NULL)
{
        a[element] =(char *) malloc(1);
	z=0;
	for(i=0;i<=(pch-string+1);i++)
		{
		a[element]=(char *)realloc(a[element],sizeof(char *)*(z+1));
		if(string[i]==delim) { a[element][z]='\0'; break; }
		a[element][z]=string[i];
		z++;
		}
element++;
}

//cycle through the rest
while (pch!=NULL)
        {
	a=(char **)realloc((char *)a,sizeof(char *) * (element+1));
//	if(a==(char**)0) perror("Realloc: ");
        a[element] =(char *) malloc(1);
	z=0;
	for(i=(pch-string+1);i<=strlen(string);i++)
		{
		a[element]=(char *)realloc(a[element],sizeof(char *)*(z+1));
		if(string[i]==delim) { a[element][z]='\0'; break; }
		a[element][z]=string[i];
		z++;
		}
	element++;
        pch=strchr(pch+1,',');
        }
// just allocate a little more so we can while it ok
element++;
a=(char **)realloc((char *)a,sizeof(char *) * (element+1));
return (char **)a;
}


typedef struct
{
    uint16_t   channel[NUM_CHANNELS];

} Sample_t;

volatile Sample_t   gPulseWidth[ NUM_AVG_SAMPLES ];
volatile Sample_t   gPulseAggr;
volatile uint8_t    gPulseIdx;

static void PulseDetected( uint8_t channel, uint16_t pulseWidth )
{
    if (( channel >= 1 ) && ( channel <= 4 ))
    {
        uint8_t chanIdx = channel - 1;

        gPulseAggr.channel[ chanIdx ] -= gPulseWidth[ gPulseIdx ].channel[ chanIdx ];

        gPulseWidth[ gPulseIdx ].channel[ chanIdx ] = pulseWidth;

        gPulseAggr.channel[ chanIdx ] += pulseWidth;

        if ( channel == 4 )
        {
            gPulseIdx++;
            gPulseIdx %= NUM_AVG_SAMPLES;
        }
    }

} // PulseDetected

static void ClearMem( volatile void *ptr, uint8_t numBytes )
{
    volatile uint8_t    *dst = (volatile uint8_t *)ptr;

    while ( numBytes > 0 )
    {
        *dst++ = 0;
        numBytes--;
    }
}

static void MissingPulse( void )
{
    ClearMem( gPulseWidth, sizeof( gPulseWidth ));
    ClearMem( &gPulseAggr, sizeof( gPulseAggr ));

} // MissingPulse

int main(void)
{
    int     i;
    int     led = 0;
//    char   *spinChar = " .oO";
    uint8_t spinIdx = 0;

    char newarray[255],oldarray[255];


    InitHardware();

    // The first handle opened for read goes to stdin, and the first handle
    // opened for write goes to stdout. So u0 is stdin, stdout, and stderr

    fdevopen( UART0_PutCharStdio, UART1_GetCharStdio );

    LogInit( stdout );

    RCI_SetPulseCallback( PulseDetected );
    RCI_SetMissingPulseCallback( MissingPulse );
    
    RCI_Init();

    sei();

    while( 1 )
    {
        // Turn all of the LEDs off

        LED_OFF( RED );
        LED_OFF( BLUE );
        LED_OFF( YELLOW );

        switch ( led )
        {
            case    0:  LED_ON( BLUE );     break;
            case    1:  LED_OFF( BLUE );    break;
        }

        if ( ++led > 1 )
        {
            led = 0;
        }

        spinIdx++;
        spinIdx &= 3;

        //printf("%c", spinChar[ spinIdx ] );

        // Tick rate is 100/sec so waiting for 100 waits for 1 sec

        uint16_t pulseList[ 4 ];
 
        for ( i = 0; i < 10; i++ ) 
        {
            int j;
            int channel;


            for ( channel = 0; channel < 10; channel++ ) 
            {
                if ( gPulseAggr.channel[ channel ] == 0 )
                {
		      printf("000000000000\n");
//                    printf( "%d: No Pulse\n", channel + 1 );
                }
                else
                {
                    uint16_t pulseWidth = gPulseAggr.channel[ channel ] / NUM_AVG_SAMPLES;
                    pulseWidth /= 10;
                    pulseWidth -= 24;
                    if ((channel + 1) == 1) {
 		      pulseList[0] = pulseWidth;
                    }
                    if ((channel + 1) == 2) {
 		      pulseList[1] = pulseWidth;
                    }
                    if ((channel + 1) == 3) {
 		      pulseList[2] = pulseWidth;
                    }
                    if ((channel + 1) == 4) {
 		      pulseList[3] = pulseWidth;
                    }
//                    printf( "%d: %4d    \n", channel + 1, pulseWidth );
                    
                }

            }
//            printf("%3d%3d%3d%3d\n", pulseList[0],pulseList[1],pulseList[2],pulseList[3]);

	    int f = 0;
            for ( f = 0; f < 255; f++  ) {
                if ( UART1_IsCharAvailable() )
                {

                    char ch = getchar();

                    if ( ch == '$' )
                    {
		
                        if (newarray[0] == 'G' && newarray[1] == 'P' && newarray[2] == 'G'&& newarray[3] == 'G' && newarray[4] == 'A') {
				//GPGGA
				char currenttime[255];
				char **b;
				(char **)b=split(newarray,',');
if(strlen(b[1]) <sizeof(currenttime) && strlen(b[1]))
{
				strncpy(currenttime,b[1],strlen(b[1]));
				printf("Time: %s\n", currenttime);
}
int x;
while(b[x]!=NULL)
{
    free(b[x]);
    x++;
}
free(b);

                        }



                        printf("%s\n", newarray);
                        memset(newarray, 0, sizeof(newarray));
                        memset(oldarray, 0, sizeof(oldarray));
                    } else if (ch == '\n') {
                       // skip character
                    } else if (ch == '\r') {
                       // skip character
                    } else {
                        snprintf(newarray,sizeof(newarray),"%s%c",oldarray,ch);
                        strncpy(oldarray,newarray,sizeof(oldarray));
                    }

                }

	    }



            for ( j = 0; j < 4; j++  ) 
            {
                LogBufDump();
                WaitForTimer0Rollover();
		

            }
        }
    }

    return 0;
}