better flash memory management
This commit is contained in:
@@ -19,24 +19,15 @@
|
|||||||
// Updated with bluetooth connection state
|
// Updated with bluetooth connection state
|
||||||
volatile bool conn = false;
|
volatile bool conn = false;
|
||||||
|
|
||||||
// pgm_read_word hates having msg_0, etc passed directly, but a secondary lookup table works
|
const char msg_pattern_off[] PROGMEM = "pattern off";
|
||||||
// so long as that table has const set twice (?)
|
const char msg_pattern_rainbow[] PROGMEM = "pattern rainbow";
|
||||||
#define MSG_PATTERN_RAINBOW 0
|
const char msg_pattern_invalid[] PROGMEM = "pattern invalid";
|
||||||
const char msg_0[] PROGMEM = "pattern rainbow";
|
const char msg_speed_out_of_range[] PROGMEM = "out of range (0,200)";
|
||||||
#define MSG_PATTERN_OFF 1
|
const char msg_speed_changed[] PROGMEM = "speed changed";
|
||||||
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
|
// 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
|
// 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);
|
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'){
|
if (cmd_byte == 's'){
|
||||||
setWait(data, data_len);
|
setWait(data, data_len);
|
||||||
} else if (cmd_byte == 'p') {
|
} 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 {
|
} else {
|
||||||
Serial.println(F("Unrecognized cmd"));
|
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 {
|
class Strip {
|
||||||
private:
|
private:
|
||||||
uint8_t pin;
|
uint8_t pin;
|
||||||
@@ -147,6 +133,7 @@ class Strip {
|
|||||||
public:
|
public:
|
||||||
uint16_t wait;
|
uint16_t wait;
|
||||||
char pattern;
|
char pattern;
|
||||||
|
uint8_t amplitude = 0;
|
||||||
|
|
||||||
Strip(uint8_t led_pin, uint8_t strip_len) {
|
Strip(uint8_t led_pin, uint8_t strip_len) {
|
||||||
pattern = 'r';
|
pattern = 'r';
|
||||||
@@ -161,7 +148,7 @@ class Strip {
|
|||||||
void off(void){
|
void off(void){
|
||||||
pixel.begin();
|
pixel.begin();
|
||||||
for(uint8_t i=0; i<num_pixels; i++){
|
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();
|
pixel.show();
|
||||||
}
|
}
|
||||||
@@ -179,10 +166,7 @@ class Strip {
|
|||||||
} else {
|
} else {
|
||||||
loc = offset + index;
|
loc = offset + index;
|
||||||
}
|
}
|
||||||
pixel.setPixelColor(
|
pixel.setPixelColor(loc, pixel.Color(r/DIM_FACTOR, g/DIM_FACTOR, b/DIM_FACTOR));
|
||||||
loc,
|
|
||||||
pack_color(r/DIM_FACTOR, g/DIM_FACTOR, b/DIM_FACTOR)
|
|
||||||
);
|
|
||||||
if (index < 24){ // red -> yellow shifting
|
if (index < 24){ // red -> yellow shifting
|
||||||
g += 10;
|
g += 10;
|
||||||
} else if (index < 48){ // yellow -> green shifting
|
} else if (index < 48){ // yellow -> green shifting
|
||||||
@@ -243,43 +227,61 @@ void setWait(char data[], uint16_t len){
|
|||||||
uint16_t wait_int = wait.toInt();
|
uint16_t wait_int = wait.toInt();
|
||||||
if (wait_int >= 0 && wait_int <= 200){
|
if (wait_int >= 0 && wait_int <= 200){
|
||||||
strip.wait = wait_int;
|
strip.wait = wait_int;
|
||||||
to_rx = MSG_SPEED_CHANGED;
|
to_rx = msg_speed_changed;
|
||||||
} else {
|
} 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){
|
if (len != 1){
|
||||||
to_rx = MSG_PATTERN_INVALID;
|
to_rx = msg_pattern_invalid;
|
||||||
} else if (data[0] == 'r'){
|
} else if (data[0] == 'r'){
|
||||||
strip.pattern = 'r';
|
strip.pattern = 'r';
|
||||||
to_rx = MSG_PATTERN_RAINBOW;
|
to_rx = msg_pattern_rainbow;
|
||||||
} else if (data[0] == 'o') {
|
} else if (data[0] == 'o') {
|
||||||
strip.pattern = 'o';
|
strip.pattern = 'o';
|
||||||
to_rx = MSG_PATTERN_OFF;
|
to_rx = msg_pattern_off;
|
||||||
} else {
|
} 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){
|
void do_rx_send(void){
|
||||||
if (to_rx == MSG_UNSET){
|
if (to_rx == NULL){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
char buffer[20];
|
char* msg = flash_msg(to_rx);
|
||||||
strcpy_P(buffer, (char *)pgm_read_word(&(msg_table[to_rx])));
|
to_rx = NULL;
|
||||||
to_rx = MSG_UNSET;
|
|
||||||
if (conn){
|
if (conn){
|
||||||
Serial.print(F("[TX] "));
|
Serial.print(F("[TX] "));
|
||||||
Serial.print(buffer);
|
Serial.println(msg);
|
||||||
Serial.println();
|
ble.write(msg);
|
||||||
ble.write(buffer);
|
|
||||||
} else {
|
} else {
|
||||||
Serial.println(F("Disconnected. Couldn't TX: "));
|
Serial.print(F("Disconnected. Couldn't TX: "));
|
||||||
Serial.print(buffer);
|
Serial.println(msg);
|
||||||
Serial.println();
|
|
||||||
}
|
}
|
||||||
|
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) {
|
void setup(void) {
|
||||||
@@ -288,6 +290,8 @@ void setup(void) {
|
|||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
}
|
}
|
||||||
bluetooth_setup();
|
bluetooth_setup();
|
||||||
|
strip.wait = 50;
|
||||||
|
strip.off();
|
||||||
Serial.println(F("Ready"));
|
Serial.println(F("Ready"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user