/
RC-Test.c
executable file
·191 lines (143 loc) · 4.46 KB
/
RC-Test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/****************************************************************************
*
* 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 "Hardware.h"
#include "Timer.h"
#include "UART.h"
#include "lcd.h"
#include "Log.h"
#include "Delay.h"
#include "RCInput.h"
#define NUM_CHANNELS 2
#define NUM_AVG_SAMPLES 8
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 <= 2 ))
{
uint8_t chanIdx = channel - 1;
gPulseAggr.channel[ chanIdx ] -= gPulseWidth[ gPulseIdx ].channel[ chanIdx ];
gPulseWidth[ gPulseIdx ].channel[ chanIdx ] = pulseWidth;
gPulseAggr.channel[ chanIdx ] += pulseWidth;
if ( channel == 2 )
{
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;
FILE *lcd;
char *spinChar = " .oO";
uint8_t spinIdx = 0;
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, UART0_GetCharStdio );
LogInit( stdout );
lcd = fdevopen( LCD_PutCharStdio, NULL );
printf( "*****\n" );
printf( "***** RCI-Test program\n" );
printf( "*****\n" );
LCD_Init( 4, 20 );
fprintf( lcd, "**** R/C Input ****\n" );
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;
LCD_MoveTo( 0, 1 );
fprintf( lcd, "%c", spinChar[ spinIdx ] );
// Tick rate is 100/sec so waiting for 100 waits for 1 sec
for ( i = 0; i < 10; i++ )
{
int j;
int channel;
LCD_MoveTo( 0, 2 );
for ( channel = 0; channel < 2; channel++ )
{
if ( gPulseAggr.channel[ channel ] == 0 )
{
fprintf( lcd, "%d: No Pulse\n", channel + 1 );
}
else
{
uint16_t pulseWidth = gPulseAggr.channel[ channel ] / NUM_AVG_SAMPLES;
pulseWidth += 5;
pulseWidth /= 10;
fprintf( lcd, "%d: %4d \n", channel + 1, pulseWidth );
}
}
for ( j = 0; j < 10; j++ )
{
LogBufDump();
WaitForTimer0Rollover();
if ( UART0_IsCharAvailable() )
{
char ch = getchar();
printf( "Read: '%c'\n", ch );
if ( ch == ' ' )
{
printf( "*** Press a key to continue\n" );
ch = getchar();
printf( "*** Continuing...\n" );
}
}
}
}
}
return 0;
}