1
0

CRC checks and commands

This commit is contained in:
2019-03-16 18:59:36 -04:00
parent 9e85793660
commit fbabccd804

View File

@@ -40,6 +40,7 @@ void bluetooth_setup(){
//ble.echo(false); // disable command echo //ble.echo(false); // disable command echo
//ble.verbose(false); // disable debug info //ble.verbose(false); // disable debug info
ble.sendCommandCheckOK(F("AT+GAPDEVNAME=Suit LEDs")); // Change name displayed in bluetooth scans ble.sendCommandCheckOK(F("AT+GAPDEVNAME=Suit LEDs")); // Change name displayed in bluetooth scans
ble.sendCommandCheckOK(F("AT+HWMODELED=2")); // Show TX/RX activity on LED
//ble.info(); //ble.info();
ble.setConnectCallback(on_connect); ble.setConnectCallback(on_connect);
@@ -49,19 +50,61 @@ void bluetooth_setup(){
} }
void on_connect(void){ void on_connect(void){
// Status light will go solid blue because of AT+HWMODELED=2
Serial.println(F("Bluetooth Connected")); Serial.println(F("Bluetooth Connected"));
} }
void on_disconnect(void){ void on_disconnect(void){
// Blue status light will shut off because of AT+HWMODELED=2
Serial.println(F("Bluetooth Disconnected")); Serial.println(F("Bluetooth Disconnected"));
} }
void BleUartRX(char data[], uint16_t len){ void BleUartRX(char payload[], uint16_t payload_len){
Serial.print( F("[RX] " ) ); // Red status light should flicker because of AT+HWMODELED=2
Serial.write(data, len); if (payload_len < 3){
if (data[0] == 's' && len > 1){ Serial.print("packet length ");
setWait(data, len); Serial.print(payload_len);
Serial.println(" is too short");
return;
} }
// first byte is the command
uint8_t cmd_byte = payload[0];
// last byte is the CRC
uint8_t crc_byte = payload[payload_len-1];
// all other bytes are the data for that command
uint16_t data_len = payload_len-2;
uint8_t data[data_len];
uint16_t payload_index;
uint16_t data_index;
for (payload_index=1, data_index=0; payload_index<payload_len-1; payload_index++, data_index++){
data[data_index] = payload[payload_index];
}
// Print parts
Serial.print(F("[RX] cmd=" ));
Serial.print((char)cmd_byte);
Serial.print(F(" crc="));
Serial.print(crc_byte);
Serial.print(F(" data="));
Serial.write(data, data_len);
Serial.println(); Serial.println();
// check CRC
uint8_t crc = 0;
for (uint16_t i=0; i<payload_len-1; i++){
crc += payload[i];
}
crc = ~crc;
if (crc_byte != crc){
Serial.print("CRC mismatch; expected ");
Serial.println(crc);
return;
}
// run whichever command was requested
if (cmd_byte == 's'){
setWait(data, data_len);
} else if (cmd_byte == 'p') {
setPattern(data, data_len);
} else {
Serial.println(F("Unrecognized cmd"));
}
} }
uint32_t pack_color(uint8_t r, uint8_t g, uint8_t b) { uint32_t pack_color(uint8_t r, uint8_t g, uint8_t b) {
@@ -81,8 +124,10 @@ class Strip {
public: public:
uint16_t wait; uint16_t wait;
char pattern;
Strip(uint8_t led_pin, uint8_t strip_len) { Strip(uint8_t led_pin, uint8_t strip_len) {
pattern = 'r';
pin = led_pin; pin = led_pin;
num_pixels = strip_len; num_pixels = strip_len;
wait = 0; wait = 0;
@@ -91,8 +136,7 @@ class Strip {
pixel = Adafruit_NeoPixel(num_pixels, pin, NEO_GRBW + NEO_KHZ800); pixel = Adafruit_NeoPixel(num_pixels, pin, NEO_GRBW + NEO_KHZ800);
} }
public: void off(void){
void off(){
Serial.println(F("off()")); Serial.println(F("off()"));
pixel.begin(); pixel.begin();
for(uint8_t i=0; i<num_pixels; i++){ for(uint8_t i=0; i<num_pixels; i++){
@@ -102,7 +146,7 @@ class Strip {
} }
private: private:
void rainbow(){ void rainbow(void){
uint8_t r = 255; uint8_t r = 255;
uint8_t g = 0; uint8_t g = 0;
uint8_t b = 0; uint8_t b = 0;
@@ -151,7 +195,7 @@ class Strip {
} }
public: public:
void update(){ void update(void){
unsigned long now = millis(); unsigned long now = millis();
if (now - wait > last){ if (now - wait > last){
last = now; last = now;
@@ -168,12 +212,27 @@ volatile Strip strip = Strip(6, 144);
void setWait(char data[], uint16_t len){ void setWait(char data[], uint16_t len){
String wait = ""; String wait = "";
for (uint16_t index=1; index<len; index++){ for (uint16_t index=0; index<len; index++){
wait += (char)data[index]; wait += (char)data[index];
} }
uint16_t wait_int = wait.toInt(); uint16_t wait_int = wait.toInt();
if (wait_int >= 0 && wait_int < 10000){ if (wait_int > -1 && wait_int < 10000){
strip.wait = wait_int; strip.wait = wait_int;
Serial.println(F("strip.wait set to "));
Serial.print(wait_int);
} else {
Serial.println(F("Speed is out of the allowed range"));
}
}
void setPattern(char data[], uint16_t len){
if (len != 1){
Serial.println(F("Invalid pattern"));
} else if (data[0] == 'r'){
Serial.println(F("pattern set to rainbow"));
strip.pattern = 'r';
} else {
Serial.println(F("Invalid pattern"));
} }
} }