From fbabccd804771065381c51fcb2418c0a020f2b1b Mon Sep 17 00:00:00 2001 From: Keiran Date: Sat, 16 Mar 2019 18:59:36 -0400 Subject: [PATCH] CRC checks and commands --- neopixel-keiran.ino | 83 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 71 insertions(+), 12 deletions(-) diff --git a/neopixel-keiran.ino b/neopixel-keiran.ino index f5639fe..2208728 100644 --- a/neopixel-keiran.ino +++ b/neopixel-keiran.ino @@ -40,6 +40,7 @@ void bluetooth_setup(){ //ble.echo(false); // disable command echo //ble.verbose(false); // disable debug info 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.setConnectCallback(on_connect); @@ -49,19 +50,61 @@ void bluetooth_setup(){ } void on_connect(void){ + // Status light will go solid blue because of AT+HWMODELED=2 Serial.println(F("Bluetooth Connected")); } void on_disconnect(void){ + // Blue status light will shut off because of AT+HWMODELED=2 Serial.println(F("Bluetooth Disconnected")); } -void BleUartRX(char data[], uint16_t len){ - Serial.print( F("[RX] " ) ); - Serial.write(data, len); - if (data[0] == 's' && len > 1){ - setWait(data, len); +void BleUartRX(char payload[], uint16_t payload_len){ + // Red status light should flicker because of AT+HWMODELED=2 + if (payload_len < 3){ + Serial.print("packet length "); + 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 last){ last = now; @@ -168,12 +212,27 @@ volatile Strip strip = Strip(6, 144); void setWait(char data[], uint16_t len){ String wait = ""; - for (uint16_t index=1; index= 0 && wait_int < 10000){ - strip.wait = wait_int; + if (wait_int > -1 && wait_int < 10000){ + 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")); } }