Move strip into a separate .cpp
This commit is contained in:
@@ -1,21 +1,3 @@
|
||||
#define GAMMA_CORRECTION { \
|
||||
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, \
|
||||
2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, \
|
||||
4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, \
|
||||
7, 7, 8, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 12, \
|
||||
12, 13, 13, 13, 14, 14, 15, 15, 15, 16, 16, 17, 17, 18, 18, 19, \
|
||||
19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25, 25, 26, 27, 27, 28, \
|
||||
29, 29, 30, 31, 31, 32, 33, 34, 34, 35, 36, 37, 37, 38, 39, 40, \
|
||||
41, 42, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 52, 53, 54, \
|
||||
56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 69, 70, 71, 72, \
|
||||
73, 75, 76, 77, 78, 80, 81, 82, 84, 85, 86, 88, 89, 90, 92, 93, \
|
||||
95, 96, 98, 99,101,102,104,105,107,108,110,111,113,115,116,118, \
|
||||
120,121,123,125,126,128,130,132,133,135,137,139,141,142,144,146, \
|
||||
148,150,152,154,156,158,160,162,164,166,168,170,172,174,176,179, \
|
||||
181,183,185,187,189,192,194,196,199,201,203,206,208,210,213,215, \
|
||||
217,220,222,225,227,230,232,235,238,240,243,245,248,251,253,255}
|
||||
|
||||
// COMMON SETTINGS
|
||||
// ----------------------------------------------------------------------------------------------
|
||||
// These settings are used in both SW UART, HW UART and SPI mode
|
||||
|
||||
32
Patterns.h
Normal file
32
Patterns.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#define SHIFTING_STOPPED 0
|
||||
#define SHIFTING_FORWARD 1
|
||||
#define SHIFTING_REVERSE 2
|
||||
#define SHIFTING_DEFAULT SHIFTING_REVERSE
|
||||
|
||||
#define PATTERN_OFF 0
|
||||
#define PATTERN_RAINBOW 1
|
||||
#define PATTERN_RED 2
|
||||
#define PATTERN_DEFAULT PATTERN_RAINBOW
|
||||
|
||||
#define OFFSET_INCREMENT_DEFAULT 2
|
||||
|
||||
// FIXME: DIM_FACTOR is for testing only; remove this when ready to embed
|
||||
#define DIM_FACTOR 1
|
||||
|
||||
#define GAMMA_CORRECTION { \
|
||||
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, \
|
||||
2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, \
|
||||
4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, \
|
||||
7, 7, 8, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 12, \
|
||||
12, 13, 13, 13, 14, 14, 15, 15, 15, 16, 16, 17, 17, 18, 18, 19, \
|
||||
19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25, 25, 26, 27, 27, 28, \
|
||||
29, 29, 30, 31, 31, 32, 33, 34, 34, 35, 36, 37, 37, 38, 39, 40, \
|
||||
41, 42, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 52, 53, 54, \
|
||||
56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 69, 70, 71, 72, \
|
||||
73, 75, 76, 77, 78, 80, 81, 82, 84, 85, 86, 88, 89, 90, 92, 93, \
|
||||
95, 96, 98, 99,101,102,104,105,107,108,110,111,113,115,116,118, \
|
||||
120,121,123,125,126,128,130,132,133,135,137,139,141,142,144,146, \
|
||||
148,150,152,154,156,158,160,162,164,166,168,170,172,174,176,179, \
|
||||
181,183,185,187,189,192,194,196,199,201,203,206,208,210,213,215, \
|
||||
217,220,222,225,227,230,232,235,238,240,243,245,248,251,253,255}
|
||||
158
Strip.cpp
Normal file
158
Strip.cpp
Normal file
@@ -0,0 +1,158 @@
|
||||
#include <Arduino.h>
|
||||
#include <Adafruit_NeoPixel.h>
|
||||
#include "Patterns.h"
|
||||
|
||||
const uint8_t PROGMEM gamma[] = GAMMA_CORRECTION;
|
||||
|
||||
class Strip {
|
||||
private:
|
||||
uint8_t pin;
|
||||
uint8_t num_pixels;
|
||||
unsigned long last;
|
||||
uint8_t offset = 0;
|
||||
uint8_t offset_increment = OFFSET_INCREMENT_DEFAULT;
|
||||
uint8_t shifting = SHIFTING_DEFAULT;
|
||||
|
||||
public:
|
||||
Adafruit_NeoPixel pixel;
|
||||
uint16_t wait = 0;
|
||||
char pattern = PATTERN_DEFAULT;
|
||||
uint8_t amplitude = 0;
|
||||
|
||||
Strip(uint8_t led_pin, uint8_t strip_len) {
|
||||
pin = led_pin;
|
||||
num_pixels = strip_len;
|
||||
last = millis();
|
||||
pixel = Adafruit_NeoPixel(num_pixels, pin, NEO_GRBW + NEO_KHZ800);
|
||||
pixel.begin();
|
||||
}
|
||||
|
||||
void set_shift_direction(uint8_t new_shift){
|
||||
if (new_shift == SHIFTING_FORWARD || new_shift == SHIFTING_REVERSE){
|
||||
offset = num_pixels - offset - 1;
|
||||
} else {
|
||||
offset = 0;
|
||||
}
|
||||
shifting = new_shift;
|
||||
}
|
||||
|
||||
void update(void){
|
||||
unsigned long now = millis();
|
||||
if (now - wait > last){
|
||||
last = now;
|
||||
add_offset();
|
||||
}
|
||||
if (pattern == PATTERN_RAINBOW){
|
||||
rainbow();
|
||||
} else if (pattern == PATTERN_RED){
|
||||
red();
|
||||
} else {
|
||||
off();
|
||||
}
|
||||
pixel.show();
|
||||
}
|
||||
|
||||
private:
|
||||
void off(void){
|
||||
for(uint8_t i=0; i<num_pixels; i++){
|
||||
pixel.setPixelColor(i, pixel.Color(0,0,0));
|
||||
}
|
||||
}
|
||||
void add_offset(void){
|
||||
/* Increments the offset counter. See get_offset_loc() */
|
||||
offset += offset_increment;
|
||||
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 set_pixel_rgb(uint8_t index, uint8_t rgb[]){
|
||||
set_pixel_rgb(index, rgb[0], rgb[1], rgb[2]);
|
||||
}
|
||||
void set_pixel_rgb(uint8_t index, uint8_t red, uint8_t green, uint8_t blue){
|
||||
pixel.setPixelColor(
|
||||
get_offset_loc(index),
|
||||
pixel.Color(
|
||||
pgm_read_byte(&gamma[red / DIM_FACTOR]),
|
||||
pgm_read_byte(&gamma[green / DIM_FACTOR]),
|
||||
pgm_read_byte(&gamma[blue / DIM_FACTOR])
|
||||
)
|
||||
);
|
||||
}
|
||||
void set_pixel_rgbw(uint8_t index, uint8_t rgb[]){
|
||||
set_pixel_rgbw(index, rgb[0], rgb[1], rgb[2], rgb[3]);
|
||||
}
|
||||
void set_pixel_rgbw(uint8_t index, uint8_t red, uint8_t green, uint8_t blue, uint8_t white){
|
||||
pixel.setPixelColor(
|
||||
get_offset_loc(index),
|
||||
pixel.Color(
|
||||
pgm_read_byte(&gamma[red / DIM_FACTOR]),
|
||||
pgm_read_byte(&gamma[green / DIM_FACTOR]),
|
||||
pgm_read_byte(&gamma[blue / DIM_FACTOR]),
|
||||
pgm_read_byte(&gamma[white / DIM_FACTOR])
|
||||
)
|
||||
);
|
||||
}
|
||||
void rainbow(void){
|
||||
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_rgb(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_rgb(index, rgb);
|
||||
}
|
||||
}
|
||||
void red(void){
|
||||
float bump = 255.0 / (num_pixels / 2 + 1);
|
||||
uint8_t half_point = num_pixels / 2 + 1;
|
||||
float red = 0;
|
||||
set_pixel_rgb(0, 1, 0, 0);
|
||||
//set_pixel_rgbw(0, 0, 0, 0, 255);
|
||||
for (uint8_t index=1; index < num_pixels; index++){
|
||||
if (index < half_point){
|
||||
red += bump;
|
||||
} else {
|
||||
red -= bump;
|
||||
}
|
||||
uint8_t red_int = (uint8_t)red;
|
||||
//uint8_t white_int = (255 - red_int + 1) / 2;
|
||||
set_pixel_rgb(index, red_int, 0, 0);
|
||||
//set_pixel_rgbw(index, red_int, 0, 0, white_int);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -1,7 +1,7 @@
|
||||
#include <string.h>
|
||||
#include <Arduino.h>
|
||||
#include <SPI.h>
|
||||
#include <Adafruit_NeoPixel.h>
|
||||
//#include <SPI.h>
|
||||
//#include <Adafruit_NeoPixel.h>
|
||||
#include "Adafruit_BluefruitLE_SPI.h"
|
||||
|
||||
#if SOFTWARE_SERIAL_AVAILABLE
|
||||
@@ -10,20 +10,12 @@
|
||||
|
||||
#include "BluefruitConfig.h"
|
||||
|
||||
const uint8_t PROGMEM gamma[] = GAMMA_CORRECTION;
|
||||
|
||||
#define REQUIRE_SERIAL false
|
||||
#define BT_SCAN_MS 50
|
||||
#define MIN_FIRMWARE "0.7.0"
|
||||
#define FACTORYRESET true
|
||||
|
||||
#define SHIFTING_STOPPED 0
|
||||
#define SHIFTING_FORWARD 1
|
||||
#define SHIFTING_REVERSE 2
|
||||
|
||||
#define PATTERN_OFF 0
|
||||
#define PATTERN_RAINBOW 1
|
||||
#define PATTERN_RED 2
|
||||
#include "Strip.cpp"
|
||||
|
||||
// Updated with bluetooth connection state
|
||||
volatile bool conn = false;
|
||||
@@ -131,159 +123,6 @@ void BleUartRX(char payload[], uint16_t payload_len){
|
||||
}
|
||||
}
|
||||
|
||||
class Strip {
|
||||
private:
|
||||
uint8_t pin;
|
||||
uint8_t num_pixels;
|
||||
unsigned long last;
|
||||
uint8_t offset = 0;
|
||||
uint8_t shifting = SHIFTING_REVERSE;
|
||||
|
||||
public:
|
||||
Adafruit_NeoPixel pixel;
|
||||
uint16_t wait = 0;
|
||||
uint8_t dim = 1;
|
||||
char pattern = PATTERN_RAINBOW;
|
||||
uint8_t amplitude = 0;
|
||||
|
||||
Strip(uint8_t led_pin, uint8_t strip_len) {
|
||||
pin = led_pin;
|
||||
num_pixels = strip_len;
|
||||
last = millis();
|
||||
pixel = Adafruit_NeoPixel(num_pixels, pin, NEO_GRBW + NEO_KHZ800);
|
||||
pixel.begin();
|
||||
}
|
||||
|
||||
void set_shift_direction(uint8_t new_shift){
|
||||
if (new_shift == SHIFTING_FORWARD || new_shift == SHIFTING_REVERSE){
|
||||
offset = num_pixels - offset - 1;
|
||||
} else {
|
||||
offset = 0;
|
||||
}
|
||||
shifting = new_shift;
|
||||
}
|
||||
|
||||
void update(void){
|
||||
unsigned long now = millis();
|
||||
if (now - wait > last){
|
||||
last = now;
|
||||
add_offset();
|
||||
}
|
||||
if (pattern == PATTERN_RAINBOW){
|
||||
rainbow();
|
||||
} else if (pattern == PATTERN_RED){
|
||||
red();
|
||||
} else {
|
||||
off();
|
||||
}
|
||||
pixel.show();
|
||||
}
|
||||
|
||||
private:
|
||||
void off(void){
|
||||
for(uint8_t i=0; i<num_pixels; i++){
|
||||
pixel.setPixelColor(i, pixel.Color(0,0,0));
|
||||
}
|
||||
}
|
||||
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 set_pixel_rgb(uint8_t index, uint8_t rgb[]){
|
||||
set_pixel_rgb(index, rgb[0], rgb[1], rgb[2]);
|
||||
}
|
||||
void set_pixel_rgb(uint8_t index, uint8_t red, uint8_t green, uint8_t blue){
|
||||
pixel.setPixelColor(
|
||||
get_offset_loc(index),
|
||||
pixel.Color(
|
||||
pgm_read_byte(&gamma[red / dim]),
|
||||
pgm_read_byte(&gamma[green / dim]),
|
||||
pgm_read_byte(&gamma[blue / dim])
|
||||
)
|
||||
);
|
||||
}
|
||||
void set_pixel_rgbw(uint8_t index, uint8_t rgb[]){
|
||||
set_pixel_rgbw(index, rgb[0], rgb[1], rgb[2], rgb[3]);
|
||||
}
|
||||
void set_pixel_rgbw(uint8_t index, uint8_t red, uint8_t green, uint8_t blue, uint8_t white){
|
||||
pixel.setPixelColor(
|
||||
get_offset_loc(index),
|
||||
pixel.Color(
|
||||
pgm_read_byte(&gamma[red / dim]),
|
||||
pgm_read_byte(&gamma[green / dim]),
|
||||
pgm_read_byte(&gamma[blue / dim]),
|
||||
pgm_read_byte(&gamma[white / dim])
|
||||
)
|
||||
);
|
||||
}
|
||||
void rainbow(void){
|
||||
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_rgb(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_rgb(index, rgb);
|
||||
}
|
||||
}
|
||||
void red(void){
|
||||
float bump = 255.0 / (num_pixels / 2 + 1);
|
||||
uint8_t half_point = num_pixels / 2 + 1;
|
||||
float red = 0;
|
||||
set_pixel_rgb(0, 1, 0, 0);
|
||||
//set_pixel_rgbw(0, 0, 0, 0, 255);
|
||||
for (uint8_t index=1; index < num_pixels; index++){
|
||||
if (index < half_point){
|
||||
red += bump;
|
||||
} else {
|
||||
red -= bump;
|
||||
}
|
||||
uint8_t red_int = (uint8_t)red;
|
||||
//uint8_t white_int = (255 - red_int + 1) / 2;
|
||||
set_pixel_rgb(index, red_int, 0, 0);
|
||||
//set_pixel_rgbw(index, red_int, 0, 0, white_int);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
volatile Strip strip = Strip(6, 144);
|
||||
|
||||
void set_wait(char data[], uint16_t len){
|
||||
|
||||
Reference in New Issue
Block a user