diff --git a/suitleds-keiran.ino b/suitleds-keiran.ino index c608f65..0f79488 100644 --- a/suitleds-keiran.ino +++ b/suitleds-keiran.ino @@ -10,18 +10,33 @@ #include "BluefruitConfig.h" -#define REQUIRE_SERIAL true -#define BT_SCAN_MS 200 +#define REQUIRE_SERIAL false +#define BT_SCAN_MS 100 #define MIN_FIRMWARE "0.7.0" #define FACTORYRESET true #define DIM_FACTOR 80 // Updated with bluetooth connection state volatile bool conn = false; -volatile char* to_rx; -const char pattern_rainbow[] PROGMEM = "pattern rainbow"; -const char* const msg_pattern_rainbow PROGMEM = pattern_rainbow; +// pgm_read_word hates having msg_0, etc passed directly, but a secondary lookup table works +// so long as that table has const set twice (?) +#define MSG_PATTERN_RAINBOW 0 +const char msg_0[] PROGMEM = "pattern rainbow"; +#define MSG_PATTERN_OFF 1 +const char msg_1[] PROGMEM = "pattern off"; +#define MSG_PATTERN_INVALID 2 +const char msg_2[] PROGMEM = "pattern invalid"; +#define MSG_SPEED_INVALID 3 +const char msg_3[] PROGMEM = "out of range (0,200)"; +#define MSG_SPEED_CHANGED 4 +const char msg_4[] PROGMEM = "speed changed"; +const char *const msg_table[] PROGMEM = {msg_0, msg_1, msg_2, msg_3, msg_4}; +#define MSG_UNSET 5 + +// Interrupts are needed to TX without deadlocking, so instead of doing it +// inside another interrupt, save the MSG_ ID here for loop() to do it +volatile int to_rx = MSG_UNSET; Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); @@ -228,37 +243,43 @@ void setWait(char data[], uint16_t len){ uint16_t wait_int = wait.toInt(); if (wait_int >= 0 && wait_int <= 200){ strip.wait = wait_int; - Serial.println(F("delay set")); + to_rx = MSG_SPEED_CHANGED; } else { - Serial.println(F("out of range (0,200)")); + to_rx = MSG_SPEED_INVALID; } } void setPattern(char data[], uint16_t len){ if (len != 1){ - Serial.println(F("pattern invalid")); + to_rx = MSG_PATTERN_INVALID; } else if (data[0] == 'r'){ strip.pattern = 'r'; - to_rx = msg_pattern_rainbow; - Serial.println(F("pattern rainbow")); + to_rx = MSG_PATTERN_RAINBOW; } else if (data[0] == 'o') { strip.pattern = 'o'; - Serial.println(F("pattern off")); + to_rx = MSG_PATTERN_OFF; } else { - Serial.println(F("pattern invalid")); + to_rx = MSG_PATTERN_INVALID; } } void do_rx_send(void){ - if (to_rx == NULL){ + if (to_rx == MSG_UNSET){ return; } char buffer[20]; - strcpy_P(buffer, (char *)pgm_read_word(&(msg_pattern_rainbow))); - Serial.println("attempt"); - Serial.print(buffer); - Serial.println(); - to_rx = NULL; + strcpy_P(buffer, (char *)pgm_read_word(&(msg_table[to_rx]))); + to_rx = MSG_UNSET; + if (conn){ + Serial.print(F("[TX] ")); + Serial.print(buffer); + Serial.println(); + ble.write(buffer); + } else { + Serial.println(F("Disconnected. Couldn't TX: ")); + Serial.print(buffer); + Serial.println(); + } } void setup(void) {