/elec/quadcopter

To get this branch, use:
bzr branch http://bzr.ed.am/elec/quadcopter

« back to all changes in this revision

Viewing changes to src/rc-interface/comms.cc

  • Committer: dan
  • Date: 2016-04-10 13:36:22 UTC
  • Revision ID: dan-20160410133622-eagrslv007bj3loz
Updated Megashield project after long time

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
}
44
44
 
45
45
 
46
 
static void write_raw_channel_data( int channel_values[] )
 
46
void Comms::write_channels( int channel_values[] )
47
47
{
 
48
//      draw_graph( channel_values );
 
49
 
48
50
        Serial.write( -1 );
49
51
        Serial.write( -1 );
50
52
        unsigned char *data = reinterpret_cast< unsigned char * >( channel_values );
52
54
}
53
55
 
54
56
 
55
 
void Comms::write_channels( int channel_values[] )
 
57
bool Comms::read_channels( int channel_values[] )
56
58
{
57
 
        // draw_graph( channel_values );
58
 
        write_raw_channel_data( channel_values );
 
59
        static unsigned char buf[ sizeof( int ) * ( NUM_MOTORS + 1 ) ];
 
60
        static unsigned int buflen = 0;
 
61
 
 
62
        bool frame_complete = false;
 
63
 
 
64
        while( buflen < sizeof( buf ) && Serial.available() )
 
65
        {
 
66
                // read byte
 
67
                buf[ buflen++ ] = Serial.read();
 
68
 
 
69
                // check if we've got a frame
 
70
                if( buflen > 1 &&
 
71
                        buf[ buflen - 1 ] == 0xff && buf[ buflen - 2 ] == 0xff )
 
72
                {
 
73
                        // got a complete frame?
 
74
                        if( buflen == sizeof( buf ) ) {
 
75
                                frame_complete = true;
 
76
 
 
77
                                // copy channel values
 
78
                                unsigned char *data =
 
79
                                        reinterpret_cast< unsigned char * >( channel_values );
 
80
                                memcpy( data, buf, sizeof( int ) * NUM_MOTORS );
 
81
                        }
 
82
 
 
83
                        // reset read buffer
 
84
                        buflen = 0;
 
85
                }
 
86
        }
 
87
 
 
88
        // reset read buffer in the case where the buffer is full, but there's been
 
89
        // no end-of-frame marker (i.e., bad data)
 
90
        if( buflen >= sizeof( buf ) )
 
91
                buflen = 0;
 
92
 
 
93
        return frame_complete;
59
94
}
60