bzr branch
http://bzr.ed.am/elec/quadcopter
18
by Tim Marston
completed (hopefully) the RC interface software |
1 |
// |
2 |
// comms.cc |
|
3 |
// |
|
4 |
||
5 |
||
6 |
#include "comms.h" |
|
7 |
#include "config.h" |
|
8 |
#include <Arduino.h> |
|
9 |
||
10 |
||
11 |
// the width of the display of a single channel (in chars) |
|
12 |
#define GRAPH_SIZE 7 |
|
13 |
||
14 |
||
15 |
void Comms::setup() |
|
16 |
{ |
|
17 |
Serial.begin( 9600 ); |
|
18 |
} |
|
19 |
||
20 |
||
21 |
static void draw_graph( int channel_values[] ) |
|
22 |
{ |
|
23 |
// init graph |
|
24 |
static char graph[ GRAPH_SIZE + 2 ]; |
|
25 |
static char inited_graph = false; |
|
26 |
if( !inited_graph ) { |
|
27 |
for( int a = 1; a < GRAPH_SIZE + 1; a++ ) |
|
28 |
graph[ a ] = '_'; |
|
29 |
graph[ 0 ] = '|'; |
|
30 |
graph[ GRAPH_SIZE + 1 ] = 0; |
|
31 |
inited_graph = true; |
|
32 |
} |
|
33 |
||
34 |
// draw channels |
|
35 |
for( int a = 0; a < NUM_CHANNELS; a++ ) { |
|
36 |
unsigned long value = min( channel_values[ a ], MAX_CHANNEL_VALUE ); |
|
37 |
int pos = ( GRAPH_SIZE ) * value / MAX_CHANNEL_VALUE; |
|
38 |
graph[ pos + 1 ] = '^'; |
|
39 |
Serial.print( graph ); |
|
40 |
graph[ pos + 1 ] = '_'; |
|
41 |
} |
|
42 |
Serial.println( "|" ); |
|
43 |
} |
|
44 |
||
45 |
||
46 |
static void write_raw_channel_data( int channel_values[] ) |
|
47 |
{ |
|
48 |
Serial.write( -1 ); |
|
49 |
Serial.write( -1 ); |
|
50 |
unsigned char *data = reinterpret_cast< unsigned char * >( channel_values ); |
|
51 |
Serial.write( data, sizeof( channel_values ) * NUM_CHANNELS ); |
|
52 |
} |
|
53 |
||
54 |
||
55 |
void Comms::write_channels( int channel_values[] ) |
|
56 |
{ |
|
57 |
// draw_graph( channel_values ); |
|
58 |
write_raw_channel_data( channel_values ); |
|
59 |
} |
|
60 |