diff --git a/suitleds-keiran.ino b/suitleds-keiran.ino index 37560a0..a4524b9 100644 --- a/suitleds-keiran.ino +++ b/suitleds-keiran.ino @@ -14,7 +14,7 @@ #define BT_SCAN_MS 50 #define MIN_FIRMWARE "0.7.0" #define FACTORYRESET true -#define DIM_FACTOR 80 +#define DIM_FACTOR 32 #define SHIFTING_STOPPED 0 #define SHIFTING_FORWARD 1 @@ -132,7 +132,7 @@ class Strip { uint8_t num_pixels; unsigned long last; uint8_t offset = num_pixels; - uint8_t shifting = SHIFTING_FORWARD; + uint8_t shifting = SHIFTING_REVERSE; Adafruit_NeoPixel pixel; public: @@ -159,10 +159,8 @@ class Strip { } void set_shift_direction(uint8_t new_shift){ - if (new_shift == SHIFTING_FORWARD){ - // FIXME: convert offset - } else if (new_shift == SHIFTING_REVERSE){ - // FIXME: convert offset + if (new_shift == SHIFTING_FORWARD || new_shift == SHIFTING_REVERSE){ + offset = num_pixels - offset - 1; } else { offset = 0; } @@ -196,13 +194,13 @@ class Strip { if (offset == 0){ return index; } - if (shifting = SHIFTING_FORWARD){ + if (shifting == SHIFTING_FORWARD){ uint16_t loc = offset + index; if (loc >= num_pixels){ loc -= num_pixels; } return (uint8_t)loc; - } else if (shifting = SHIFTING_REVERSE){ + } else if (shifting == SHIFTING_REVERSE){ uint16_t loc = num_pixels - offset + index; if (loc >= num_pixels){ loc -= num_pixels; @@ -211,41 +209,34 @@ class Strip { } return index; // SHIFTING_STOPPED } + void set_pixel(uint8_t index, uint8_t rgb[]){ + pixel.setPixelColor( + get_offset_loc(index), + pixel.Color(rgb[0]/DIM_FACTOR, rgb[1]/DIM_FACTOR, rgb[2]/DIM_FACTOR) + ); + } void rainbow(void){ - uint8_t r = 255; - uint8_t g = 0; - uint8_t b = 0; - pixel.begin(); - for (uint8_t index=0; index yellow shifting - g += 10; - } else if (index < 48){ // yellow -> green shifting - if (index == 24){ - g = 255; - } - r -= 10; - } else if (index < 72){ // green -> cyan shifting - if (index == 48){ - r = 0; - } - b += 10; - } else if (index < 96){ // cyan -> blue shifting - if (index == 72){ - b = 255; - } - g -= 10; - } else if (index < 120){ // blue -> magenta shifting - if (index == 96){ - g = 0; - } - r += 10; - } else { // magenta -> red shifting - if (index == 120){ - r = 255; - } - b -= 10; + uint8_t rgb[] = {255, 0, 0}; // starting RGB value + uint8_t phase_len = num_pixels / 6; // how long each rainbow phase is + uint8_t bump = 255 / phase_len; // how much to increase between pixels + set_pixel(0, rgb); // first pixel + for (uint8_t index=1; index < num_pixels; index++){ + uint8_t phase = index / phase_len; // which section we're in + if (phase % 2 == 0){ // even-index phases add; odd-indexed subtracts + // (r,g,b) index for each phase in order is 1,0,2,1,0, + rgb[(6 - phase + 1) % 3] += bump; + } else { + rgb[(6 - phase + 1) % 3] -= bump; } + if (index % phase_len == 0) { // this is the transition between phases + // max out the change from the previous phase + if (phase % 2 == 0){ + rgb[(6 - phase + 2) % 3] = 0; + } else { + rgb[(6 - phase + 2) % 3] = 255; + } + } + set_pixel(index, rgb); } pixel.show(); }