From 46f769043beb017f7a78b22e466537be71542bc1 Mon Sep 17 00:00:00 2001 From: Keiran Date: Wed, 27 Mar 2019 20:25:53 -0400 Subject: [PATCH] allow reverse --- suitleds-keiran.ino | 59 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 12 deletions(-) diff --git a/suitleds-keiran.ino b/suitleds-keiran.ino index 2ba9392..37560a0 100644 --- a/suitleds-keiran.ino +++ b/suitleds-keiran.ino @@ -16,6 +16,10 @@ #define FACTORYRESET true #define DIM_FACTOR 80 +#define SHIFTING_STOPPED 0 +#define SHIFTING_FORWARD 1 +#define SHIFTING_REVERSE 2 + // Updated with bluetooth connection state volatile bool conn = false; @@ -127,7 +131,8 @@ class Strip { uint8_t pin; uint8_t num_pixels; unsigned long last; - uint8_t offset; + uint8_t offset = num_pixels; + uint8_t shifting = SHIFTING_FORWARD; Adafruit_NeoPixel pixel; public: @@ -153,14 +158,22 @@ class Strip { pixel.show(); } + 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 + } else { + offset = 0; + } + shifting = new_shift; + } + void update(void){ unsigned long now = millis(); if (now - wait > last){ last = now; - offset += 1; - if (offset == num_pixels){ - offset = 0; - } + add_offset(); } if (pattern == 'o'){ off(); @@ -170,19 +183,41 @@ class Strip { } private: + void add_offset(void){ + /* Increments the offset counter. See get_offset_loc() */ + offset++; + if (offset == num_pixels){ + offset = 0; + } + } + uint8_t get_offset_loc(uint8_t index){ + /* index is the location on the strip where a pixel would be if offset + * was 0. Offset will be from 0 to num_pixels-1 */ + if (offset == 0){ + return index; + } + 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){ + uint16_t loc = num_pixels - offset + index; + if (loc >= num_pixels){ + loc -= num_pixels; + } + return (uint8_t)loc; + } + return index; // SHIFTING_STOPPED + } void rainbow(void){ uint8_t r = 255; uint8_t g = 0; uint8_t b = 0; pixel.begin(); for (uint8_t index=0; index num_pixels - 1){ - loc = index + offset - num_pixels; - } else { - loc = offset + index; - } - pixel.setPixelColor(loc, pixel.Color(r/DIM_FACTOR, g/DIM_FACTOR, b/DIM_FACTOR)); + pixel.setPixelColor(get_offset_loc(index), 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