@ -7,13 +7,10 @@
https : //www.arduino.cc/en/Tutorial/BuiltInExamples/DigitalReadSerial
*/
# include "CRC16.h"
# include <SPI.h>
# include <Wire.h>
volatile uint8_t control_flags = 0xC9 ;
volatile uint32_t timer_end = 0x00000000 ;
volatile uint32_t length = 0x00000000 ;
volatile uint32_t last_timer_sent = 0x00000000 ;
CRC16 crc = CRC16 ( 0x1021 , 0xFFFF , 0x0000 , false , false ) ;
# include "CRC16.h"
int read_line = 22 ;
@ -68,17 +65,7 @@ typedef struct __attribute__((packed)) {
uint16_t crc ;
} pkt_set_poll_rate_t ;
typedef struct __attribute__ ( ( packed ) ) {
uint8_t type ;
uint8_t unknown_1 ;
uint16_t system_status ;
uint8_t unknown_2 [ 2 ] ;
uint8_t field_status ;
uint8_t unknown_3 [ 11 ] ;
uint8_t name [ 10 ] ;
uint8_t version [ 2 ] ;
uint16_t crc ;
} pkt_poll_t ;
CRC16 crc = CRC16 ( 0x1021 , 0xFFFF , 0x0000 , false , false ) ;
void parse_discovery ( uint8_t * pkt , size_t len ) {
pkt_discovery_t parsed ;
@ -155,9 +142,9 @@ void parse_get_poll_rate(uint8_t* pkt, size_t len, uint8_t poll_ms) {
uint16_t calc_crc = crc . calc ( ) ;
if ( parsed_crc ! = calc_crc ) {
Serial . printf ( " Got get_poll_rate packet with bad crc(%04x != %04x\n " , calc_crc , parsed_crc ) ;
Serial . printf ( " Got init_3 packet with bad crc(%04x != %04x\n " , calc_crc , parsed_crc ) ;
} else {
Serial . printf ( " Got get_poll_rate packet with matching crc(%04x)\n " , calc_crc ) ;
Serial . printf ( " Got init_3 packet with matching crc(%04x)\n " , calc_crc ) ;
}
pkt_set_poll_rate_t response ;
@ -203,10 +190,10 @@ void send_state(uint8_t control_flags, uint32_t timer) {
response [ 1 ] = control_flags ;
response [ 2 ] = 0x00 ;
response [ 3 ] = 0x00 ;
response [ 4 ] = ( ( timer > > 0 ) & 0xFF ) ;
response [ 5 ] = ( ( timer > > 8 ) & 0xFF ) ;
response [ 6 ] = ( ( timer > > 16 ) & 0xFF ) ;
response [ 7 ] = ( ( timer > > 24 ) & 0xFF ) ;
response [ 4 ] = ( timer > > 24 & 0xFF ) ;
response [ 5 ] = ( timer > > 16 & 0xFF ) ;
response [ 6 ] = ( timer > > 8 & 0xFF ) ;
response [ 7 ] = ( timer > > 0 & 0xFF ) ;
response [ 8 ] = 0x03 ;
crc . restart ( ) ;
@ -222,6 +209,22 @@ void send_state(uint8_t control_flags, uint32_t timer) {
digitalWrite ( read_line , LOW ) ;
}
typedef struct __attribute__ ( ( packed ) ) {
uint8_t type ;
uint8_t unknown_1 ;
uint16_t system_status ;
uint8_t unknown_2 [ 2 ] ;
uint8_t field_status ;
uint8_t unknown_3 [ 11 ] ;
uint8_t name [ 10 ] ;
uint8_t version [ 2 ] ;
uint16_t crc ;
} pkt_poll_t ;
volatile uint8_t control_flags = 0xC9 ;
volatile uint32_t timer = 0x00002000 ;
# define POLL_COUNT_MAX 5
void parse_poll ( uint8_t * pkt , size_t len ) {
pkt_poll_t * parsed = ( pkt_poll_t * ) pkt ;
crc . restart ( ) ;
@ -234,22 +237,10 @@ void parse_poll(uint8_t* pkt, size_t len) {
//Serial.printf("parsed poll with valid CRC 0x%04x, system status 0x%04x, field status 0x%02x, team name %s\n", htons(parsed->crc), htons(parsed->system_status), parsed->field_status, parsed->name);
}
uint32_t now = millis ( ) ;
uint32_t current_timer = 0 ;
if ( now < timer_end ) {
current_timer = ( timer_end - millis ( ) ) & 0xFFFFFF00 ;
} else {
current_timer = length ;
control_flags = 0xC9 ;
}
if ( ( parsed - > field_status = = control_flags ) & & ( current_timer = = last_timer_sent ) ) {
if ( parsed - > field_status = = control_flags ) {
send_ack ( 0xA7 , 0x01 ) ;
} else if ( control_flags = = 0xC9 ) {
send_state ( 0xC9 , length ) ;
} else {
last_timer_sent = current_timer ;
send_state ( control_flags , current_timer ) ;
} {
send_state ( control_flags , timer ) ;
}
}
@ -260,9 +251,8 @@ void on_rx(void) {
return ;
}
int read = Serial . readBytes ( pkt , n ) ;
sscanf ( ( const char * ) pkt , " 0x%2hhx%8x " , & control_flags , & length ) ;
timer_end = millis ( ) + length ;
Serial . printf ( " Parsed new control flags: 0x%02x, length 0x%08x \n " , control_flags , length ) ;
sscanf ( ( const char * ) pkt , " 0x%2hhx " , & control_flags ) ;
Serial . printf ( " Parsed new control flags: 0x%02x \n " , control_flags ) ;
}
void on_rx_2 ( void ) {
@ -283,7 +273,7 @@ void on_rx_2(void) {
parse_init ( pkt , read ) ;
break ;
case 0x11 :
parse_get_poll_rate ( pkt , read , 2 0) ;
parse_get_poll_rate ( pkt , read , 5 0) ;
break ;
case 0x02 :
switch ( pkt [ 1 ] ) {