better flash memory management
This commit is contained in:
@@ -19,24 +19,15 @@
|
||||
// Updated with bluetooth connection state
|
||||
volatile bool conn = false;
|
||||
|
||||
// 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
|
||||
const char msg_pattern_off[] PROGMEM = "pattern off";
|
||||
const char msg_pattern_rainbow[] PROGMEM = "pattern rainbow";
|
||||
const char msg_pattern_invalid[] PROGMEM = "pattern invalid";
|
||||
const char msg_speed_out_of_range[] PROGMEM = "out of range (0,200)";
|
||||
const char msg_speed_changed[] PROGMEM = "speed changed";
|
||||
|
||||
// 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;
|
||||
volatile int to_rx = NULL;
|
||||
|
||||
Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
|
||||
|
||||
@@ -123,19 +114,14 @@ void BleUartRX(char payload[], uint16_t payload_len){
|
||||
if (cmd_byte == 's'){
|
||||
setWait(data, data_len);
|
||||
} else if (cmd_byte == 'p') {
|
||||
setPattern(data, data_len);
|
||||
set_pattern(data, data_len);
|
||||
} else if (cmd_byte == 'a') {
|
||||
set_amplitude(data, data_len);
|
||||
} else {
|
||||
Serial.println(F("Unrecognized cmd"));
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t pack_color(uint8_t r, uint8_t g, uint8_t b) {
|
||||
return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
|
||||
}
|
||||
uint32_t pack_color(uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
|
||||
return ((uint32_t)w << 24) | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
|
||||
}
|
||||
|
||||
class Strip {
|
||||
private:
|
||||
uint8_t pin;
|
||||
@@ -147,6 +133,7 @@ class Strip {
|
||||
public:
|
||||
uint16_t wait;
|
||||
char pattern;
|
||||
uint8_t amplitude = 0;
|
||||
|
||||
Strip(uint8_t led_pin, uint8_t strip_len) {
|
||||
pattern = 'r';
|
||||
@@ -161,7 +148,7 @@ class Strip {
|
||||
void off(void){
|
||||
pixel.begin();
|
||||
for(uint8_t i=0; i<num_pixels; i++){
|
||||
pixel.setPixelColor(i, pack_color(0,0,0));
|
||||
pixel.setPixelColor(i, pixel.Color(0,0,0));
|
||||
}
|
||||
pixel.show();
|
||||
}
|
||||
@@ -179,10 +166,7 @@ class Strip {
|
||||
} else {
|
||||
loc = offset + index;
|
||||
}
|
||||
pixel.setPixelColor(
|
||||
loc,
|
||||
pack_color(r/DIM_FACTOR, g/DIM_FACTOR, b/DIM_FACTOR)
|
||||
);
|
||||
pixel.setPixelColor(loc, pixel.Color(r/DIM_FACTOR, g/DIM_FACTOR, b/DIM_FACTOR));
|
||||
if (index < 24){ // red -> yellow shifting
|
||||
g += 10;
|
||||
} else if (index < 48){ // yellow -> green shifting
|
||||
@@ -243,43 +227,61 @@ void setWait(char data[], uint16_t len){
|
||||
uint16_t wait_int = wait.toInt();
|
||||
if (wait_int >= 0 && wait_int <= 200){
|
||||
strip.wait = wait_int;
|
||||
to_rx = MSG_SPEED_CHANGED;
|
||||
to_rx = msg_speed_changed;
|
||||
} else {
|
||||
to_rx = MSG_SPEED_INVALID;
|
||||
to_rx = msg_speed_out_of_range;
|
||||
}
|
||||
}
|
||||
|
||||
void setPattern(char data[], uint16_t len){
|
||||
void set_pattern(char data[], uint16_t len){
|
||||
if (len != 1){
|
||||
to_rx = MSG_PATTERN_INVALID;
|
||||
to_rx = msg_pattern_invalid;
|
||||
} else if (data[0] == 'r'){
|
||||
strip.pattern = 'r';
|
||||
to_rx = MSG_PATTERN_RAINBOW;
|
||||
to_rx = msg_pattern_rainbow;
|
||||
} else if (data[0] == 'o') {
|
||||
strip.pattern = 'o';
|
||||
to_rx = MSG_PATTERN_OFF;
|
||||
to_rx = msg_pattern_off;
|
||||
} else {
|
||||
to_rx = MSG_PATTERN_INVALID;
|
||||
to_rx = msg_pattern_invalid;
|
||||
}
|
||||
}
|
||||
|
||||
void set_amplitude(char data[], uint16_t len){
|
||||
if (len == 1){
|
||||
strip.amplitude = data[0];
|
||||
//Serial.println((uint8_t)data[0]);
|
||||
Serial.println(255 - (uint8_t)data[0]);
|
||||
} else {
|
||||
Serial.println(F("invalid len for amplitude"));
|
||||
}
|
||||
}
|
||||
|
||||
void do_rx_send(void){
|
||||
if (to_rx == MSG_UNSET){
|
||||
if (to_rx == NULL){
|
||||
return;
|
||||
}
|
||||
char buffer[20];
|
||||
strcpy_P(buffer, (char *)pgm_read_word(&(msg_table[to_rx])));
|
||||
to_rx = MSG_UNSET;
|
||||
char* msg = flash_msg(to_rx);
|
||||
to_rx = NULL;
|
||||
if (conn){
|
||||
Serial.print(F("[TX] "));
|
||||
Serial.print(buffer);
|
||||
Serial.println();
|
||||
ble.write(buffer);
|
||||
Serial.println(msg);
|
||||
ble.write(msg);
|
||||
} else {
|
||||
Serial.println(F("Disconnected. Couldn't TX: "));
|
||||
Serial.print(buffer);
|
||||
Serial.println();
|
||||
Serial.print(F("Disconnected. Couldn't TX: "));
|
||||
Serial.println(msg);
|
||||
}
|
||||
free(msg);
|
||||
}
|
||||
|
||||
char * flash_msg(const char* addr){
|
||||
/* return a char* array from flash memory; be sure to free() it after */
|
||||
uint8_t buff_size = strlen_P(addr);
|
||||
char * buffer = (char *) malloc (buff_size);
|
||||
for (uint8_t index=0; index < buff_size; index++){
|
||||
buffer[index] = pgm_read_byte_near(addr + index);
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void setup(void) {
|
||||
@@ -288,6 +290,8 @@ void setup(void) {
|
||||
Serial.begin(115200);
|
||||
}
|
||||
bluetooth_setup();
|
||||
strip.wait = 50;
|
||||
strip.off();
|
||||
Serial.println(F("Ready"));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user