Compare commits

..

No commits in common. "ffa418eed7d74d280827153997ec5ee92d05df2e" and "2123b9aa14c7328b410f5dec716c63ac53542872" have entirely different histories.

2 changed files with 33 additions and 43 deletions

@ -10,7 +10,7 @@ Power Max485 with 5V and GND connected to the Arduino.
Flash the code onto the Arduino, connect A&B of the smart cable connected to the controller(aka the black and red wires respectively) to the Max485.
Viewing the USB serial of the Arduino, send 0xAB01234567-like hex sequences to update the configured state to 0xAB and the timer to 0x01234567. Note that 0xC9 should be sent with the timer before starting a state.
Viewing the USB serial of the Arduino, send 0xAB-like hex sequences to update the configured state.
# Info

@ -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, 20);
parse_get_poll_rate(pkt, read, 50);
break;
case 0x02:
switch (pkt[1]){