├── .gitignore ├── Native ├── .DS_Store ├── Demo │ ├── binary │ │ └── demo.bin │ └── demo.ino ├── Fade │ ├── binary │ │ └── fade.bin │ ├── colors.h │ ├── colors.cpp │ └── fade.ino ├── SnowGlobe │ ├── .DS_Store │ ├── binary │ │ └── snowglobe.bin │ ├── util.h │ ├── cube.h │ ├── colors.h │ ├── util.cpp │ ├── snow.h │ ├── cube.cpp │ ├── snowglobe.cpp │ ├── colors.cpp │ ├── neopixel.h │ ├── snow.cpp │ └── neopixel.cpp ├── Audio │ ├── binary │ │ └── audio.bin │ ├── util.h │ ├── cube.h │ ├── colors.h │ ├── util.cpp │ ├── kiss_fftr.h │ ├── cube.cpp │ ├── colors.cpp │ ├── neopixel.h │ ├── kiss_fft.h │ ├── audio.cpp │ ├── audio.cpp~ │ ├── _kiss_fft_guts.h │ ├── kiss_fftr.c │ ├── kiss_fft.c │ └── neopixel.cpp └── Matrix │ ├── binary │ └── matrix.bin │ ├── matrix.h │ └── matrix.ino ├── Streaming └── Listener │ ├── Listener.bin │ └── Listener.ino ├── Readme.md~ └── Readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Vim swap files 2 | *.swp 3 | *.swo 4 | -------------------------------------------------------------------------------- /Native/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/enjrolas/L3D-Software/HEAD/Native/.DS_Store -------------------------------------------------------------------------------- /Native/Demo/binary/demo.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/enjrolas/L3D-Software/HEAD/Native/Demo/binary/demo.bin -------------------------------------------------------------------------------- /Native/Fade/binary/fade.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/enjrolas/L3D-Software/HEAD/Native/Fade/binary/fade.bin -------------------------------------------------------------------------------- /Native/SnowGlobe/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/enjrolas/L3D-Software/HEAD/Native/SnowGlobe/.DS_Store -------------------------------------------------------------------------------- /Native/Audio/binary/audio.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/enjrolas/L3D-Software/HEAD/Native/Audio/binary/audio.bin -------------------------------------------------------------------------------- /Native/Matrix/binary/matrix.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/enjrolas/L3D-Software/HEAD/Native/Matrix/binary/matrix.bin -------------------------------------------------------------------------------- /Streaming/Listener/Listener.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/enjrolas/L3D-Software/HEAD/Streaming/Listener/Listener.bin -------------------------------------------------------------------------------- /Native/SnowGlobe/binary/snowglobe.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/enjrolas/L3D-Software/HEAD/Native/SnowGlobe/binary/snowglobe.bin -------------------------------------------------------------------------------- /Native/Audio/util.h: -------------------------------------------------------------------------------- 1 | #ifndef H_UTIL 2 | #define H_UTIL 3 | 4 | float frand(float min, float max); 5 | float dist(float x1, float y1, float z1, float x2, float y2, float z2); 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /Native/SnowGlobe/util.h: -------------------------------------------------------------------------------- 1 | #ifndef H_UTIL 2 | #define H_UTIL 3 | 4 | float frand(float min, float max); 5 | float dist(float x1, float y1, float z1, float x2, float y2, float z2); 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /Native/Matrix/matrix.h: -------------------------------------------------------------------------------- 1 | #ifndef matrix_h 2 | #define matrix_h 3 | 4 | typedef struct{ 5 | unsigned char red, green, blue; 6 | } color; 7 | 8 | typedef struct{ 9 | unsigned char x,y; 10 | float z, zInc, zEnd; 11 | } matrix; 12 | 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /Native/Audio/cube.h: -------------------------------------------------------------------------------- 1 | #ifndef H_CUBE 2 | #define H_CUBE 3 | 4 | static color color_dark = { 0, 0, 0 }; 5 | 6 | void cube_init(void); 7 | void cube_update(void); 8 | void setPixel(int x, int y, int z, color* col); 9 | color getPixel(int x, int y, int z); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /Native/SnowGlobe/cube.h: -------------------------------------------------------------------------------- 1 | #ifndef H_CUBE 2 | #define H_CUBE 3 | 4 | static color color_dark = { 0, 0, 0 }; 5 | 6 | void cube_init(void); 7 | void cube_update(void); 8 | void setPixel(int x, int y, int z, color* col); 9 | color getPixel(int x, int y, int z); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /Native/Fade/colors.h: -------------------------------------------------------------------------------- 1 | #ifndef colors_h 2 | #define colors_h 3 | 4 | typedef struct{ 5 | unsigned char red, green, blue; 6 | } color; 7 | 8 | typedef struct{ 9 | int x, y, z; 10 | } point; 11 | 12 | //function definitions 13 | color lerpColor(color* a, color* b, int val, int min, int max); 14 | color colorMap(float val, float min, float max); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /Native/Audio/colors.h: -------------------------------------------------------------------------------- 1 | #ifndef colors_h 2 | #define colors_h 3 | 4 | typedef struct { 5 | unsigned char red, green, blue; 6 | } color; 7 | 8 | typedef struct { 9 | int x, y, z; 10 | } point; 11 | 12 | //function definitions 13 | color lerpColor(color* a, color* b, int val, int min, int max); 14 | color colorMap(float val, float min, float max); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /Native/SnowGlobe/colors.h: -------------------------------------------------------------------------------- 1 | #ifndef colors_h 2 | #define colors_h 3 | 4 | typedef struct { 5 | unsigned char red, green, blue; 6 | } color; 7 | 8 | typedef struct { 9 | int x, y, z; 10 | } point; 11 | 12 | //function definitions 13 | color lerpColor(color* a, color* b, int val, int min, int max); 14 | color colorMap(float val, float min, float max); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /Native/Audio/util.cpp: -------------------------------------------------------------------------------- 1 | #include "stdlib.h" 2 | #include "math.h" 3 | #include "util.h" 4 | 5 | // random float between min and max 6 | float frand(float min, float max) { 7 | float range = max - min; 8 | return min + range * (float)rand() / (float)RAND_MAX; 9 | } 10 | 11 | // distance between 3D points 12 | float dist(float x1, float y1, float z1, float x2, float y2, float z2) { 13 | return sqrt(pow(x2-x1, 2) + pow(y2-y1, 2) + pow(z2-z1, 2)); 14 | } 15 | -------------------------------------------------------------------------------- /Native/SnowGlobe/util.cpp: -------------------------------------------------------------------------------- 1 | #include "stdlib.h" 2 | #include "math.h" 3 | #include "util.h" 4 | 5 | // random float between min and max 6 | float frand(float min, float max) { 7 | float range = max - min; 8 | return min + range * (float)rand() / (float)RAND_MAX; 9 | } 10 | 11 | // distance between 3D points 12 | float dist(float x1, float y1, float z1, float x2, float y2, float z2) { 13 | return sqrt(pow(x2-x1, 2) + pow(y2-y1, 2) + pow(z2-z1, 2)); 14 | } 15 | -------------------------------------------------------------------------------- /Native/SnowGlobe/snow.h: -------------------------------------------------------------------------------- 1 | #ifndef H_SNOW 2 | #define H_SNOW 3 | 4 | typedef struct { 5 | float x, y, z; 6 | } vector; 7 | 8 | enum { 9 | SNOW_STATIC = 1 << 0, // never move 10 | SNOW_STUCK = 1 << 1, // don't move now 11 | SNOW_THICK = 1 << 2 // always render particle at home position 12 | }; 13 | 14 | typedef struct { 15 | uint8_t flags; 16 | float x, y, z; 17 | float vx, vy, vz; 18 | float home_x, home_y, home_z; 19 | } snowflake; 20 | 21 | void flurry(float severity, float strength); 22 | void snowstorm(void); 23 | void render_snow(void); 24 | void render_background(void); 25 | 26 | void update_snow(float ax, float ay, float az); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /Native/Audio/kiss_fftr.h: -------------------------------------------------------------------------------- 1 | #ifndef KISS_FTR_H 2 | #define KISS_FTR_H 3 | 4 | #include "kiss_fft.h" 5 | #ifdef __cplusplus 6 | extern "C" { 7 | #endif 8 | 9 | 10 | /* 11 | 12 | Real optimized version can save about 45% cpu time vs. complex fft of a real seq. 13 | 14 | 15 | 16 | */ 17 | 18 | typedef struct kiss_fftr_state *kiss_fftr_cfg; 19 | 20 | 21 | kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem, size_t * lenmem); 22 | /* 23 | nfft must be even 24 | 25 | If you don't care to allocate space, use mem = lenmem = NULL 26 | */ 27 | 28 | 29 | void kiss_fftr(kiss_fftr_cfg cfg,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata); 30 | /* 31 | input timedata has nfft scalar points 32 | output freqdata has nfft/2+1 complex points 33 | */ 34 | 35 | void kiss_fftri(kiss_fftr_cfg cfg,const kiss_fft_cpx *freqdata,kiss_fft_scalar *timedata); 36 | /* 37 | input freqdata has nfft/2+1 complex points 38 | output timedata has nfft scalar points 39 | */ 40 | 41 | #define kiss_fftr_free free 42 | 43 | #ifdef __cplusplus 44 | } 45 | #endif 46 | #endif 47 | -------------------------------------------------------------------------------- /Native/Audio/cube.cpp: -------------------------------------------------------------------------------- 1 | #include "neopixel.h" 2 | #include "colors.h" 3 | #include "cube.h" 4 | 5 | #define PIXEL_PIN D0 6 | #define PIXEL_COUNT 512 7 | #define PIXEL_TYPE WS2812B 8 | Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE); 9 | 10 | #define SIDE 8 11 | 12 | void cube_init() { 13 | strip.begin(); 14 | } 15 | 16 | void cube_update() { 17 | strip.show(); 18 | } 19 | 20 | void setPixel(int x, int y, int z, color* col) { 21 | if(x >= 0 && x < 8 && y >= 0 && y < 8 && z >= 0 && z < 8) { 22 | int index = (z*SIDE*SIDE) + (x*SIDE) + y; 23 | int rando=10; 24 | 25 | if(col->green>rando/2) 26 | strip.setPixelColor(index,strip.Color(col->red, rand()%rando - rando/2 + col->green, col->blue)); 27 | else 28 | strip.setPixelColor(index,strip.Color(col->red, col->green, col->blue)); 29 | } 30 | } 31 | 32 | color getPixel(int x, int y, int z) { 33 | int index = (z*SIDE*SIDE) + (x*SIDE) + y; 34 | uint32_t col=strip.getPixelColor(index); 35 | color pixelColor; 36 | pixelColor.red=(col>>16)&255; 37 | pixelColor.green=(col>>8)&255; 38 | pixelColor.blue=col&255; 39 | return pixelColor; 40 | } 41 | -------------------------------------------------------------------------------- /Native/SnowGlobe/cube.cpp: -------------------------------------------------------------------------------- 1 | #include "neopixel.h" 2 | #include "colors.h" 3 | #include "cube.h" 4 | 5 | #define PIXEL_PIN D0 6 | #define PIXEL_COUNT 512 7 | #define PIXEL_TYPE WS2812B 8 | Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE); 9 | 10 | #define SIDE 8 11 | 12 | void cube_init() { 13 | strip.begin(); 14 | } 15 | 16 | void cube_update() { 17 | strip.show(); 18 | } 19 | 20 | void setPixel(int x, int y, int z, color* col) { 21 | if(x >= 0 && x < 8 && y >= 0 && y < 8 && z >= 0 && z < 8) { 22 | int index = (z*SIDE*SIDE) + (x*SIDE) + y; 23 | int rando=10; 24 | 25 | if(col->green>rando/2) 26 | strip.setPixelColor(index,strip.Color(col->red, rand()%rando - rando/2 + col->green, col->blue)); 27 | else 28 | strip.setPixelColor(index,strip.Color(col->red, col->green, col->blue)); 29 | } 30 | } 31 | 32 | color getPixel(int x, int y, int z) { 33 | int index = (z*SIDE*SIDE) + (x*SIDE) + y; 34 | uint32_t col=strip.getPixelColor(index); 35 | color pixelColor; 36 | pixelColor.red=(col>>16)&255; 37 | pixelColor.green=(col>>8)&255; 38 | pixelColor.blue=col&255; 39 | return pixelColor; 40 | } 41 | -------------------------------------------------------------------------------- /Native/SnowGlobe/snowglobe.cpp: -------------------------------------------------------------------------------- 1 | #include "application.h" 2 | #include 3 | 4 | #include "colors.h" 5 | #include "math.h" 6 | 7 | #include "cube.h" 8 | #include "snow.h" 9 | 10 | bool tried_connecting = false; 11 | SYSTEM_MODE(SEMI_AUTOMATIC); 12 | #define INTERNET_SWITCH D2 13 | 14 | #define PIN_X 14 15 | #define PIN_Y 15 16 | #define PIN_Z 13 17 | 18 | void setup() { 19 | Serial.begin(115200); 20 | 21 | pinMode(INTERNET_SWITCH, INPUT); 22 | 23 | cube_init(); 24 | 25 | // initialize snowflakes 26 | snowstorm(); 27 | } 28 | 29 | void loop() { 30 | // connect to cloud if internet switch is set 31 | if(digitalRead(INTERNET_SWITCH) && !tried_connecting) { 32 | Spark.connect(); 33 | tried_connecting = true; 34 | } 35 | 36 | // read accelerometer 37 | float accel_x = (float)(analogRead(PIN_X) - 2048) / 2048.0f; 38 | float accel_y = -(float)(analogRead(PIN_Y) - 2048) / 2048.0f; 39 | float accel_z = (float)(analogRead(PIN_Z) - 2048) / 2048.0f; 40 | 41 | // favor movement normal to cube floor 42 | float biased_magnitude = sqrt(pow(accel_x, 2) + pow(accel_z, 2)) + accel_y; 43 | 44 | if(biased_magnitude > -0.08) 45 | flurry(0.1, 0.02); 46 | 47 | update_snow(accel_x, accel_y, accel_z); 48 | 49 | render_background(); 50 | render_snow(); 51 | cube_update(); 52 | } 53 | 54 | -------------------------------------------------------------------------------- /Native/Audio/colors.cpp: -------------------------------------------------------------------------------- 1 | #include "colors.h" 2 | //maxBrightness is the brightness limit for each pixel. All color data will be scaled down 3 | //so that the largest value is maxBrightness 4 | int maxBrightness=50; 5 | 6 | 7 | //returns a color from a set of colors fading from blue to green to red and back again 8 | //the color is returned based on where the parameter *val* falls between the parameters 9 | //*min* and *max*. If *val* is min, the function returns a blue color. If *val* is halfway 10 | //between *min* and *max*, the function returns a yellow color. 11 | color colorMap(float val, float min, float max) 12 | { 13 | float range=1024; 14 | val=range*(val-min)/(max-min); 15 | color colors[6]; 16 | colors[0].red=0; 17 | colors[0].green=0; 18 | colors[0].blue=maxBrightness; 19 | 20 | colors[1].red=0; 21 | colors[1].green=maxBrightness; 22 | colors[1].blue=maxBrightness; 23 | 24 | colors[2].red=0; 25 | colors[2].green=maxBrightness; 26 | colors[2].blue=0; 27 | 28 | colors[3].red=maxBrightness; 29 | colors[3].green=maxBrightness; 30 | colors[3].blue=0; 31 | 32 | colors[4].red=maxBrightness; 33 | colors[4].green=0; 34 | colors[4].blue=0; 35 | 36 | colors[5].red=maxBrightness; 37 | colors[5].green=0; 38 | colors[5].blue=maxBrightness; 39 | 40 | if (val<=range/6) 41 | return(lerpColor(&colors[0], &colors[1], val, 0, range/6)); 42 | else if (val<=2*range/6) 43 | return(lerpColor(&colors[1], &colors[2], val, range/6, 2*range/6)); 44 | else if (val<=3*range/6) 45 | return(lerpColor(&colors[2], &colors[3], val, 2*range/6, 3*range/6)); 46 | else if (val<=4*range/6) 47 | return(lerpColor(&colors[3], &colors[4], val, 3*range/6, 4*range/6)); 48 | else if (val<=5*range/6) 49 | return(lerpColor(&colors[4], &colors[5], val, 4*range/6, 5*range/6)); 50 | else 51 | return(lerpColor(&colors[5], &colors[0], val, 5*range/6, range)); 52 | } 53 | 54 | 55 | //returns a color that's an interpolation between colors a and b. The color 56 | //is controlled by the position of val relative to min and max -- if val is equal to min, 57 | //the resulting color is identical to color a. If it's equal to max, the resulting color 58 | //is identical to color b. If val is (max-min)/2, the resulting color is the average of 59 | //color a and color b 60 | color lerpColor(color* a, color* b, int val, int min, int max) 61 | { 62 | color lerped; 63 | lerped.red=a->red+(b->red-a->red)*(val-min)/(max-min); 64 | lerped.green=a->green+(b->green-a->green)*(val-min)/(max-min); 65 | lerped.blue=a->blue+(b->blue-a->blue)*(val-min)/(max-min); 66 | return lerped; 67 | } 68 | -------------------------------------------------------------------------------- /Native/Fade/colors.cpp: -------------------------------------------------------------------------------- 1 | #include "colors.h" 2 | //maxBrightness is the brightness limit for each pixel. All color data will be scaled down 3 | //so that the largest value is maxBrightness 4 | int maxBrightness=50; 5 | 6 | 7 | //returns a color from a set of colors fading from blue to green to red and back again 8 | //the color is returned based on where the parameter *val* falls between the parameters 9 | //*min* and *max*. If *val* is min, the function returns a blue color. If *val* is halfway 10 | //between *min* and *max*, the function returns a yellow color. 11 | color colorMap(float val, float min, float max) 12 | { 13 | float range=1024; 14 | val=range*(val-min)/(max-min); 15 | color colors[6]; 16 | colors[0].red=0; 17 | colors[0].green=0; 18 | colors[0].blue=maxBrightness; 19 | 20 | colors[1].red=0; 21 | colors[1].green=maxBrightness; 22 | colors[1].blue=maxBrightness; 23 | 24 | colors[2].red=0; 25 | colors[2].green=maxBrightness; 26 | colors[2].blue=0; 27 | 28 | colors[3].red=maxBrightness; 29 | colors[3].green=maxBrightness; 30 | colors[3].blue=0; 31 | 32 | colors[4].red=maxBrightness; 33 | colors[4].green=0; 34 | colors[4].blue=0; 35 | 36 | colors[5].red=maxBrightness; 37 | colors[5].green=0; 38 | colors[5].blue=maxBrightness; 39 | 40 | if (val<=range/6) 41 | return(lerpColor(&colors[0], &colors[1], val, 0, range/6)); 42 | else if (val<=2*range/6) 43 | return(lerpColor(&colors[1], &colors[2], val, range/6, 2*range/6)); 44 | else if (val<=3*range/6) 45 | return(lerpColor(&colors[2], &colors[3], val, 2*range/6, 3*range/6)); 46 | else if (val<=4*range/6) 47 | return(lerpColor(&colors[3], &colors[4], val, 3*range/6, 4*range/6)); 48 | else if (val<=5*range/6) 49 | return(lerpColor(&colors[4], &colors[5], val, 4*range/6, 5*range/6)); 50 | else 51 | return(lerpColor(&colors[5], &colors[0], val, 5*range/6, range)); 52 | } 53 | 54 | 55 | //returns a color that's an interpolation between colors a and b. The color 56 | //is controlled by the position of val relative to min and max -- if val is equal to min, 57 | //the resulting color is identical to color a. If it's equal to max, the resulting color 58 | //is identical to color b. If val is (max-min)/2, the resulting color is the average of 59 | //color a and color b 60 | color lerpColor(color* a, color* b, int val, int min, int max) 61 | { 62 | color lerped; 63 | lerped.red=a->red+(b->red-a->red)*(val-min)/(max-min); 64 | lerped.green=a->green+(b->green-a->green)*(val-min)/(max-min); 65 | lerped.blue=a->blue+(b->blue-a->blue)*(val-min)/(max-min); 66 | return lerped; 67 | } 68 | -------------------------------------------------------------------------------- /Native/SnowGlobe/colors.cpp: -------------------------------------------------------------------------------- 1 | #include "colors.h" 2 | //maxBrightness is the brightness limit for each pixel. All color data will be scaled down 3 | //so that the largest value is maxBrightness 4 | int maxBrightness=50; 5 | 6 | 7 | //returns a color from a set of colors fading from blue to green to red and back again 8 | //the color is returned based on where the parameter *val* falls between the parameters 9 | //*min* and *max*. If *val* is min, the function returns a blue color. If *val* is halfway 10 | //between *min* and *max*, the function returns a yellow color. 11 | color colorMap(float val, float min, float max) 12 | { 13 | float range=1024; 14 | val=range*(val-min)/(max-min); 15 | color colors[6]; 16 | colors[0].red=0; 17 | colors[0].green=0; 18 | colors[0].blue=maxBrightness; 19 | 20 | colors[1].red=0; 21 | colors[1].green=maxBrightness; 22 | colors[1].blue=maxBrightness; 23 | 24 | colors[2].red=0; 25 | colors[2].green=maxBrightness; 26 | colors[2].blue=0; 27 | 28 | colors[3].red=maxBrightness; 29 | colors[3].green=maxBrightness; 30 | colors[3].blue=0; 31 | 32 | colors[4].red=maxBrightness; 33 | colors[4].green=0; 34 | colors[4].blue=0; 35 | 36 | colors[5].red=maxBrightness; 37 | colors[5].green=0; 38 | colors[5].blue=maxBrightness; 39 | 40 | if (val<=range/6) 41 | return(lerpColor(&colors[0], &colors[1], val, 0, range/6)); 42 | else if (val<=2*range/6) 43 | return(lerpColor(&colors[1], &colors[2], val, range/6, 2*range/6)); 44 | else if (val<=3*range/6) 45 | return(lerpColor(&colors[2], &colors[3], val, 2*range/6, 3*range/6)); 46 | else if (val<=4*range/6) 47 | return(lerpColor(&colors[3], &colors[4], val, 3*range/6, 4*range/6)); 48 | else if (val<=5*range/6) 49 | return(lerpColor(&colors[4], &colors[5], val, 4*range/6, 5*range/6)); 50 | else 51 | return(lerpColor(&colors[5], &colors[0], val, 5*range/6, range)); 52 | } 53 | 54 | 55 | //returns a color that's an interpolation between colors a and b. The color 56 | //is controlled by the position of val relative to min and max -- if val is equal to min, 57 | //the resulting color is identical to color a. If it's equal to max, the resulting color 58 | //is identical to color b. If val is (max-min)/2, the resulting color is the average of 59 | //color a and color b 60 | color lerpColor(color* a, color* b, int val, int min, int max) 61 | { 62 | color lerped; 63 | lerped.red=a->red+(b->red-a->red)*(val-min)/(max-min); 64 | lerped.green=a->green+(b->green-a->green)*(val-min)/(max-min); 65 | lerped.blue=a->blue+(b->blue-a->blue)*(val-min)/(max-min); 66 | return lerped; 67 | } 68 | -------------------------------------------------------------------------------- /Native/Audio/neopixel.h: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | Spark Core library to control WS2811/WS2812 based RGB 3 | LED devices such as Adafruit NeoPixel strips. 4 | Currently handles 800 KHz and 400kHz bitstream on Spark Core, 5 | WS2812, WS2812B and WS2811. 6 | 7 | Also supports: 8 | - Radio Shack Tri-Color Strip with TM1803 controller 400kHz bitstream. 9 | - TM1829 pixels 10 | 11 | Written by Phil Burgess / Paint Your Dragon for Adafruit Industries. 12 | Modified to work with Spark Core by Technobly. 13 | Contributions by PJRC and other members of the open source community. 14 | 15 | Adafruit invests time and resources providing this open source code, 16 | please support Adafruit and open-source hardware by purchasing products 17 | from Adafruit! 18 | --------------------------------------------------------------------*/ 19 | 20 | /* ======================= Adafruit_NeoPixel.h ======================= */ 21 | /*-------------------------------------------------------------------- 22 | This file is part of the Adafruit NeoPixel library. 23 | 24 | NeoPixel is free software: you can redistribute it and/or modify 25 | it under the terms of the GNU Lesser General Public License as 26 | published by the Free Software Foundation, either version 3 of 27 | the License, or (at your option) any later version. 28 | 29 | NeoPixel is distributed in the hope that it will be useful, 30 | but WITHOUT ANY WARRANTY; without even the implied warranty of 31 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 32 | GNU Lesser General Public License for more details. 33 | 34 | You should have received a copy of the GNU Lesser General Public 35 | License along with NeoPixel. If not, see 36 | . 37 | --------------------------------------------------------------------*/ 38 | 39 | #ifndef SPARK_NEOPIXEL_H 40 | #define SPARK_NEOPIXEL_H 41 | 42 | #include "application.h" 43 | 44 | // 'type' flags for LED pixels (third parameter to constructor): 45 | #define WS2812 0x02 // 800 KHz datastream (NeoPixel) 46 | #define WS2812B 0x02 // 800 KHz datastream (NeoPixel) 47 | #define WS2811 0x00 // 400 KHz datastream (NeoPixel) 48 | #define TM1803 0x03 // 400 KHz datastream (Radio Shack Tri-Color Strip) 49 | #define TM1829 0x04 // 800 KHz datastream () 50 | 51 | class Adafruit_NeoPixel { 52 | 53 | public: 54 | 55 | // Constructor: number of LEDs, pin number, LED type 56 | Adafruit_NeoPixel(uint16_t n, uint8_t p=2, uint8_t t=WS2812B); 57 | ~Adafruit_NeoPixel(); 58 | 59 | void 60 | begin(void), 61 | show(void) __attribute__((optimize("Ofast"))), 62 | setPin(uint8_t p), 63 | setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b), 64 | setPixelColor(uint16_t n, uint32_t c), 65 | setBrightness(uint8_t); 66 | uint8_t 67 | *getPixels() const; 68 | uint16_t 69 | numPixels(void) const; 70 | static uint32_t 71 | Color(uint8_t r, uint8_t g, uint8_t b); 72 | uint32_t 73 | getPixelColor(uint16_t n) const; 74 | 75 | private: 76 | 77 | const uint16_t 78 | numLEDs, // Number of RGB LEDs in strip 79 | numBytes; // Size of 'pixels' buffer below 80 | const uint8_t 81 | type; // Pixel type flag (400 vs 800 KHz) 82 | uint8_t 83 | pin, // Output pin number 84 | brightness, 85 | *pixels; // Holds LED color values (3 bytes each) 86 | uint32_t 87 | endTime; // Latch timing reference 88 | }; 89 | 90 | #endif // ADAFRUIT_NEOPIXEL_H 91 | -------------------------------------------------------------------------------- /Native/SnowGlobe/neopixel.h: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | Spark Core library to control WS2811/WS2812 based RGB 3 | LED devices such as Adafruit NeoPixel strips. 4 | Currently handles 800 KHz and 400kHz bitstream on Spark Core, 5 | WS2812, WS2812B and WS2811. 6 | 7 | Also supports: 8 | - Radio Shack Tri-Color Strip with TM1803 controller 400kHz bitstream. 9 | - TM1829 pixels 10 | 11 | Written by Phil Burgess / Paint Your Dragon for Adafruit Industries. 12 | Modified to work with Spark Core by Technobly. 13 | Contributions by PJRC and other members of the open source community. 14 | 15 | Adafruit invests time and resources providing this open source code, 16 | please support Adafruit and open-source hardware by purchasing products 17 | from Adafruit! 18 | --------------------------------------------------------------------*/ 19 | 20 | /* ======================= Adafruit_NeoPixel.h ======================= */ 21 | /*-------------------------------------------------------------------- 22 | This file is part of the Adafruit NeoPixel library. 23 | 24 | NeoPixel is free software: you can redistribute it and/or modify 25 | it under the terms of the GNU Lesser General Public License as 26 | published by the Free Software Foundation, either version 3 of 27 | the License, or (at your option) any later version. 28 | 29 | NeoPixel is distributed in the hope that it will be useful, 30 | but WITHOUT ANY WARRANTY; without even the implied warranty of 31 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 32 | GNU Lesser General Public License for more details. 33 | 34 | You should have received a copy of the GNU Lesser General Public 35 | License along with NeoPixel. If not, see 36 | . 37 | --------------------------------------------------------------------*/ 38 | 39 | #ifndef SPARK_NEOPIXEL_H 40 | #define SPARK_NEOPIXEL_H 41 | 42 | #include "application.h" 43 | 44 | // 'type' flags for LED pixels (third parameter to constructor): 45 | #define WS2812 0x02 // 800 KHz datastream (NeoPixel) 46 | #define WS2812B 0x02 // 800 KHz datastream (NeoPixel) 47 | #define WS2811 0x00 // 400 KHz datastream (NeoPixel) 48 | #define TM1803 0x03 // 400 KHz datastream (Radio Shack Tri-Color Strip) 49 | #define TM1829 0x04 // 800 KHz datastream () 50 | 51 | class Adafruit_NeoPixel { 52 | 53 | public: 54 | 55 | // Constructor: number of LEDs, pin number, LED type 56 | Adafruit_NeoPixel(uint16_t n, uint8_t p=2, uint8_t t=WS2812B); 57 | ~Adafruit_NeoPixel(); 58 | 59 | void 60 | begin(void), 61 | show(void) __attribute__((optimize("Ofast"))), 62 | setPin(uint8_t p), 63 | setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b), 64 | setPixelColor(uint16_t n, uint32_t c), 65 | setBrightness(uint8_t); 66 | uint8_t 67 | *getPixels() const; 68 | uint16_t 69 | numPixels(void) const; 70 | static uint32_t 71 | Color(uint8_t r, uint8_t g, uint8_t b); 72 | uint32_t 73 | getPixelColor(uint16_t n) const; 74 | 75 | private: 76 | 77 | const uint16_t 78 | numLEDs, // Number of RGB LEDs in strip 79 | numBytes; // Size of 'pixels' buffer below 80 | const uint8_t 81 | type; // Pixel type flag (400 vs 800 KHz) 82 | uint8_t 83 | pin, // Output pin number 84 | brightness, 85 | *pixels; // Holds LED color values (3 bytes each) 86 | uint32_t 87 | endTime; // Latch timing reference 88 | }; 89 | 90 | #endif // ADAFRUIT_NEOPIXEL_H 91 | -------------------------------------------------------------------------------- /Native/Audio/kiss_fft.h: -------------------------------------------------------------------------------- 1 | #ifndef KISS_FFT_H 2 | #define KISS_FFT_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #ifdef __cplusplus 10 | extern "C" { 11 | #endif 12 | 13 | /* 14 | ATTENTION! 15 | If you would like a : 16 | -- a utility that will handle the caching of fft objects 17 | -- real-only (no imaginary time component ) FFT 18 | -- a multi-dimensional FFT 19 | -- a command-line utility to perform ffts 20 | -- a command-line utility to perform fast-convolution filtering 21 | 22 | Then see kfc.h kiss_fftr.h kiss_fftnd.h fftutil.c kiss_fastfir.c 23 | in the tools/ directory. 24 | */ 25 | 26 | #ifdef USE_SIMD 27 | # include 28 | # define kiss_fft_scalar __m128 29 | #define KISS_FFT_MALLOC(nbytes) _mm_malloc(nbytes,16) 30 | #define KISS_FFT_FREE _mm_free 31 | #else 32 | #define KISS_FFT_MALLOC malloc 33 | #define KISS_FFT_FREE free 34 | #endif 35 | 36 | 37 | #ifdef FIXED_POINT 38 | #include 39 | # if (FIXED_POINT == 32) 40 | # define kiss_fft_scalar int32_t 41 | # else 42 | # define kiss_fft_scalar int16_t 43 | # endif 44 | #else 45 | # ifndef kiss_fft_scalar 46 | /* default is float */ 47 | # define kiss_fft_scalar float 48 | # endif 49 | #endif 50 | 51 | typedef struct { 52 | kiss_fft_scalar r; 53 | kiss_fft_scalar i; 54 | }kiss_fft_cpx; 55 | 56 | typedef struct kiss_fft_state* kiss_fft_cfg; 57 | 58 | /* 59 | * kiss_fft_alloc 60 | * 61 | * Initialize a FFT (or IFFT) algorithm's cfg/state buffer. 62 | * 63 | * typical usage: kiss_fft_cfg mycfg=kiss_fft_alloc(1024,0,NULL,NULL); 64 | * 65 | * The return value from fft_alloc is a cfg buffer used internally 66 | * by the fft routine or NULL. 67 | * 68 | * If lenmem is NULL, then kiss_fft_alloc will allocate a cfg buffer using malloc. 69 | * The returned value should be free()d when done to avoid memory leaks. 70 | * 71 | * The state can be placed in a user supplied buffer 'mem': 72 | * If lenmem is not NULL and mem is not NULL and *lenmem is large enough, 73 | * then the function places the cfg in mem and the size used in *lenmem 74 | * and returns mem. 75 | * 76 | * If lenmem is not NULL and ( mem is NULL or *lenmem is not large enough), 77 | * then the function returns NULL and places the minimum cfg 78 | * buffer size in *lenmem. 79 | * */ 80 | 81 | kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem); 82 | 83 | /* 84 | * kiss_fft(cfg,in_out_buf) 85 | * 86 | * Perform an FFT on a complex input buffer. 87 | * for a forward FFT, 88 | * fin should be f[0] , f[1] , ... ,f[nfft-1] 89 | * fout will be F[0] , F[1] , ... ,F[nfft-1] 90 | * Note that each element is complex and can be accessed like 91 | f[k].r and f[k].i 92 | * */ 93 | void kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout); 94 | 95 | /* 96 | A more generic version of the above function. It reads its input from every Nth sample. 97 | * */ 98 | void kiss_fft_stride(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int fin_stride); 99 | 100 | /* If kiss_fft_alloc allocated a buffer, it is one contiguous 101 | buffer and can be simply free()d when no longer needed*/ 102 | #define kiss_fft_free free 103 | 104 | /* 105 | Cleans up some memory that gets managed internally. Not necessary to call, but it might clean up 106 | your compiler output to call this before you exit. 107 | */ 108 | void kiss_fft_cleanup(void); 109 | 110 | 111 | /* 112 | * Returns the smallest integer k, such that k>=n and k has only "fast" factors (2,3,5) 113 | */ 114 | int kiss_fft_next_fast_size(int n); 115 | 116 | /* for real ffts, we need an even size */ 117 | #define kiss_fftr_next_fast_size_real(n) \ 118 | (kiss_fft_next_fast_size( ((n)+1)>>1)<<1) 119 | 120 | #ifdef __cplusplus 121 | } 122 | #endif 123 | 124 | #endif 125 | -------------------------------------------------------------------------------- /Streaming/Listener/Listener.ino: -------------------------------------------------------------------------------- 1 | #include "neopixel/neopixel.h" 2 | 3 | UDP Udp; 4 | unsigned count = 0; 5 | 6 | char data[512]; 7 | 8 | #define PIXEL_PIN D0 9 | #define PIXEL_COUNT 512 10 | #define PIXEL_TYPE WS2812B 11 | 12 | #define SIDE 8 13 | #define MODE D3 14 | 15 | Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE); 16 | char localIP[24]; 17 | char macAddress[20]; 18 | int port; 19 | int lastUpdated=0; 20 | 21 | typedef struct{ 22 | unsigned char red, green, blue; 23 | } color; 24 | 25 | void setPixel(int x, int y, int z, color col); //sets a pixel at position (x,y,z) to the col parameter's color 26 | int setPort(String _port); 27 | 28 | void setup() 29 | { 30 | port=2222; 31 | strip.begin(); 32 | strip.show(); // Initialize all pixels to 'off' 33 | Udp.begin (port); 34 | pinMode(MODE,INPUT_PULLUP); 35 | if(!digitalRead(MODE)) 36 | WiFi.listen(); 37 | Serial.begin(115200); 38 | Serial.println("initializing..."); 39 | updateNetworkInfo(); 40 | initSparkVariables(); 41 | } 42 | 43 | //initializes the shared variables and functions that are accessible through the spark API 44 | //this makes the core's local IP address, MAC address and port accessible to streaming programs 45 | //that have the access token for the core owner's account 46 | //The function setPort lets a streaming program set the port on which the core will listen for streaming packets 47 | void initSparkVariables() 48 | { 49 | Spark.variable("IPAddress", localIP, STRING); 50 | Spark.variable("MACAddress", macAddress, STRING); 51 | Spark.variable("port", &port, INT); 52 | Spark.function("setPort", setPort); 53 | } 54 | 55 | //updates the local IP address and mac address and stores them in global strings 56 | //if those strings are Spark variables (using the Spark.variable() function), they'll be accessible to the wider world 57 | //This is all kinds of helpful anytime you're working on a project that uses network communicqtion (streaming, etc) 58 | 59 | void updateNetworkInfo() 60 | { 61 | IPAddress myIp = WiFi.localIP(); 62 | sprintf(localIP, "%d.%d.%d.%d", myIp[0], myIp[1], myIp[2], myIp[3]); 63 | byte macAddr[6]; 64 | WiFi.macAddress(macAddr); 65 | sprintf(macAddress, "%02x:%02x:%02x:%02x:%02x:%02x",macAddr[5],macAddr[4],macAddr[3],macAddr[2],macAddr[1],macAddr[0]); 66 | 67 | //print it out the serial port 68 | Serial.print("local IP Address: "); 69 | Serial.println(localIP); 70 | Serial.print("MAC address: "); 71 | Serial.println(macAddress); 72 | } 73 | 74 | int setPort(String _port) 75 | { 76 | port=_port.toInt(); 77 | Udp.begin(port); 78 | return port; 79 | } 80 | 81 | 82 | void loop (void) 83 | { 84 | int32_t bytesrecv = Udp.parsePacket(); 85 | 86 | if(millis()-lastUpdated>60000) //update the network settings every minute 87 | { 88 | updateNetworkInfo(); 89 | lastUpdated=millis(); 90 | } 91 | 92 | if (bytesrecv==PIXEL_COUNT) { 93 | Udp.read(data,bytesrecv); 94 | for(int x=0;x>1, (data[index]&0x1C)<<2, (data[index]&0x03)<<5}; //colors with max brightness set to 128 101 | color pixelColor={ (data[index]&0xE0)>>2, (data[index]&0x1C)<<1, (data[index]&0x03)<<4}; //colors with max brightness set to 64 102 | setPixel(x,y,z,pixelColor); 103 | } 104 | 105 | } 106 | strip.show(); 107 | if(!digitalRead(MODE)) 108 | WiFi.listen(); 109 | } 110 | 111 | //sets a pixel at position (x,y,z) to the col parameter's color 112 | void setPixel(int x, int y, int z, color col) 113 | { 114 | int index = z*64 + x*8 + y; 115 | strip.setPixelColor(index,strip.Color(col.red, col.green, col.blue)); 116 | } 117 | 118 | -------------------------------------------------------------------------------- /Native/Fade/fade.ino: -------------------------------------------------------------------------------- 1 | #include "colors.h" 2 | #include "neopixel/neopixel.h" 3 | 4 | #define PIXEL_PIN D0 5 | #define PIXEL_COUNT 512 6 | #define PIXEL_TYPE WS2812B 7 | Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE); 8 | 9 | #define SIDE 8 10 | SYSTEM_MODE(SEMI_AUTOMATIC); //don't connect to the internet on boot 11 | #define BUTTON D2 //press this button to connect to the internet 12 | #define MODE D3 13 | int colorific; 14 | bool fading=false; 15 | int fadeValue=255; 16 | bool onlinePressed=false; 17 | bool lastOnline=true; 18 | 19 | void setup() { 20 | strip.begin(); 21 | Spark.variable("colorific", &colorific, INT); 22 | initCloudButton(); 23 | 24 | } 25 | 26 | //sets up the online/offline switch 27 | void initCloudButton() 28 | { 29 | //set the input mode for the 'connect to cloud' button 30 | pinMode(BUTTON, INPUT_PULLUP); 31 | pinMode(MODE, INPUT_PULLUP); 32 | if(!digitalRead(MODE)) 33 | WiFi.listen(); 34 | //a.k.a. onlinePressed is HIGH when the switch is set to 'online' and LOW when the switch is set to 'offline' 35 | onlinePressed=digitalRead(BUTTON); 36 | if(onlinePressed) 37 | Spark.connect(); 38 | } 39 | 40 | //checks to see if the 'online/offline' switch is switched 41 | void checkCloudButton() 42 | { 43 | //if the 'connect to cloud' button is pressed, try to connect to wifi. 44 | //otherwise, run the program 45 | //note -- how does this behave when there are no wifi credentials loaded on the spark? 46 | 47 | //onlinePressed is HIGH when the switch is _not_ connected and LOW when the switch is connected 48 | //a.k.a. onlinePressed is HIGH when the switch is set to 'online' and LOW when the switch is set to 'offline' 49 | onlinePressed=digitalRead(BUTTON); 50 | 51 | if((!onlinePressed)&&(lastOnline)) //marked as 'offline' 52 | { 53 | lastOnline=onlinePressed; 54 | Spark.disconnect(); 55 | } 56 | 57 | else if((onlinePressed)&&(!lastOnline)) //marked as 'online' 58 | { 59 | lastOnline=onlinePressed; 60 | Spark.connect(); 61 | } 62 | 63 | lastOnline=onlinePressed; 64 | 65 | if(!digitalRead(MODE)) 66 | WiFi.listen(); 67 | } 68 | 69 | void loop() { 70 | //if the 'connect to cloud' button is pressed, try to connect to wifi. 71 | //otherwise, run the program 72 | checkCloudButton(); 73 | 74 | if(!fading) 75 | { 76 | randomize(); 77 | fading=true; 78 | fadeValue=255; 79 | } 80 | else 81 | { 82 | fadeValue--; 83 | fade(); 84 | //if we're done fading) 85 | if(fadeValue==0) 86 | fading=false; 87 | } 88 | 89 | strip.show(); 90 | if(!fading) 91 | delay(2000); //wait for a few seconds to show off the pretty colors; 92 | 93 | } 94 | 95 | void randomize() 96 | { 97 | color col; 98 | for(int x=0;x0) 123 | pixelColor.red--; 124 | if(pixelColor.green>0) 125 | pixelColor.green--; 126 | if(pixelColor.blue>0) 127 | pixelColor.blue--; 128 | setPixel(x,y,z,&pixelColor); 129 | } 130 | } 131 | 132 | //sets a pixel at position (x,y,z) to the col parameter's color 133 | void setPixel(int x, int y, int z, color* col) 134 | { 135 | int index = (z*SIDE*SIDE) + (x*SIDE) + y; 136 | int rando=10; 137 | if(col->green>rando/2) 138 | strip.setPixelColor(index,strip.Color(col->red, rand()%rando - rando/2 + col->green, col->blue)); 139 | else 140 | strip.setPixelColor(index,strip.Color(col->red, col->green, col->blue)); 141 | } 142 | 143 | //returns the color value currently displayed at the x,y,z location 144 | color getPixel(int x, int y, int z) 145 | { 146 | int index = (z*SIDE*SIDE) + (x*SIDE) + y; 147 | uint32_t col=strip.getPixelColor(index); 148 | color pixelColor; 149 | pixelColor.red=(col>>16)&255; 150 | pixelColor.green=(col>>8)&255; 151 | pixelColor.blue=col&255; 152 | return pixelColor; 153 | } -------------------------------------------------------------------------------- /Native/Audio/audio.cpp: -------------------------------------------------------------------------------- 1 | 2 | /************************************** 3 | 4 | Audio by Owen Trueblood 5 | October 2014 6 | 7 | *************************************/ 8 | 9 | #include "application.h" 10 | #include 11 | #include 12 | 13 | #include "colors.h" 14 | #include "cube.h" 15 | #include "kiss_fftr.h" 16 | 17 | SYSTEM_MODE(SEMI_AUTOMATIC); //don't connect to the internet on boot 18 | #define BUTTON D2 //press this button to connect to the internet 19 | #define MODE D3 20 | bool onlinePressed=false; 21 | bool lastOnline=true; 22 | 23 | #define PIN_X 14 24 | #define PIN_Y 15 25 | #define PIN_Z 13 26 | 27 | #define MICROPHONE 12 28 | #define GAIN_CONTROL 11 29 | #define DIAL 10 30 | 31 | #define FFT_SIZE 256 32 | kiss_fftr_cfg fft_cfg; 33 | kiss_fft_scalar *fft_in; 34 | kiss_fft_cpx *fft_out; 35 | 36 | uint8_t height[8][8]; 37 | 38 | void updateFFT() { 39 | kiss_fft_scalar pt; 40 | 41 | for(int i=0; i < FFT_SIZE; i++) { 42 | fft_in[i] = ((float)analogRead(MICROPHONE))/4096.f; 43 | } 44 | 45 | kiss_fftr(fft_cfg, fft_in, fft_out); 46 | } 47 | void initCloudButton(); 48 | void checkCloudButton(); 49 | 50 | void setup() { 51 | Serial.begin(9600); 52 | initCloudButton(); 53 | pinMode(GAIN_CONTROL, OUTPUT); 54 | analogWrite(GAIN_CONTROL, 45); //put the gain right in the middle, for now 55 | 56 | fft_cfg = kiss_fftr_alloc(FFT_SIZE, FALSE, NULL, NULL); 57 | fft_in = (kiss_fft_scalar*)malloc(FFT_SIZE * sizeof(kiss_fft_scalar)); 58 | fft_out = (kiss_fft_cpx*)malloc(FFT_SIZE / 2 * sizeof(kiss_fft_cpx) + 1); 59 | 60 | cube_init(); 61 | } 62 | 63 | //sets up the online/offline switch 64 | void initCloudButton() 65 | { 66 | //set the input mode for the 'connect to cloud' button 67 | pinMode(BUTTON, INPUT_PULLUP); 68 | pinMode(MODE, INPUT_PULLUP); 69 | if(!digitalRead(MODE)) 70 | WiFi.listen(); 71 | //a.k.a. onlinePressed is HIGH when the switch is set to 'online' and LOW when the switch is set to 'offline' 72 | onlinePressed=digitalRead(BUTTON); 73 | if(onlinePressed) 74 | Spark.connect(); 75 | } 76 | 77 | //checks to see if the 'online/offline' switch is switched 78 | void checkCloudButton() 79 | { 80 | //if the 'connect to cloud' button is pressed, try to connect to wifi. 81 | //otherwise, run the program 82 | //note -- how does this behave when there are no wifi credentials loaded on the spark? 83 | 84 | //onlinePressed is HIGH when the switch is _not_ connected and LOW when the switch is connected 85 | //a.k.a. onlinePressed is HIGH when the switch is set to 'online' and LOW when the switch is set to 'offline' 86 | onlinePressed=digitalRead(BUTTON); 87 | 88 | if((!onlinePressed)&&(lastOnline)) //marked as 'offline' 89 | { 90 | lastOnline=onlinePressed; 91 | Spark.disconnect(); 92 | } 93 | 94 | else if((onlinePressed)&&(!lastOnline)) //marked as 'online' 95 | { 96 | lastOnline=onlinePressed; 97 | Spark.connect(); 98 | } 99 | 100 | lastOnline=onlinePressed; 101 | 102 | if(!digitalRead(MODE)) 103 | WiFi.listen(); 104 | } 105 | 106 | 107 | void palettize(color* col, int x) { 108 | /* 109 | col->red = 256*sin(M_PI * x / 32); 110 | col->green = 256*sin(M_PI * x / 64); 111 | col->blue = 256*sin(M_PI * x / 128); 112 | */ 113 | 114 | col->red = 256*sin(M_PI * x / 128); 115 | col->green = 256*sin(M_PI * x / 128); 116 | col->blue = 0; 117 | } 118 | 119 | void loop() { 120 | checkCloudButton(); 121 | 122 | updateFFT(); 123 | 124 | /*for(int i=0; i < FFT_SIZE; i++) { 125 | Serial.print(log_pwr_fft[i]); 126 | Serial.print(",\t"); 127 | } 128 | Serial.println();*/ 129 | 130 | color col = { 55, 55, 55 }; 131 | 132 | // shift the previous slices 133 | for(int y=7; y >= 1; y--) { 134 | for(int x=0; x < 8; x++) { 135 | for(int z=0; z < 8; z++) { 136 | color below = getPixel(x, y-1, z); 137 | setPixel(x, y, z, &below); 138 | } 139 | } 140 | } 141 | 142 | // add the next slice 143 | uint8_t x = 4; 144 | uint8_t z = 4; 145 | uint8_t magic[4] = {0, 1, 0, -1}; 146 | bool done = false; 147 | int count = 0; 148 | for(int i=1; count < 64; i++) { 149 | for(int j=0; j<2 && count < 64; j++) { 150 | for(int k=0; k 11 | #include 12 | 13 | #include "colors.h" 14 | #include "cube.h" 15 | #include "kiss_fftr.h" 16 | 17 | bool tried_connecting = false; 18 | SYSTEM_MODE(SEMI_AUTOMATIC); //don't connect to the internet on boot 19 | #define BUTTON D2 //press this button to connect to the internet 20 | #define MODE D3 21 | bool onlinePressed=false; 22 | bool lastOnline=true; 23 | 24 | #define PIN_X 14 25 | #define PIN_Y 15 26 | #define PIN_Z 13 27 | 28 | #define MICROPHONE 12 29 | #define GAIN_CONTROL 11 30 | #define DIAL 10 31 | 32 | #define FFT_SIZE 256 33 | kiss_fftr_cfg fft_cfg; 34 | kiss_fft_scalar *fft_in; 35 | kiss_fft_cpx *fft_out; 36 | 37 | uint8_t height[8][8]; 38 | 39 | void updateFFT() { 40 | kiss_fft_scalar pt; 41 | 42 | for(int i=0; i < FFT_SIZE; i++) { 43 | fft_in[i] = ((float)analogRead(MICROPHONE))/4096.f; 44 | } 45 | 46 | kiss_fftr(fft_cfg, fft_in, fft_out); 47 | } 48 | void initCloudButton(); 49 | void checkCloudButton(); 50 | 51 | void setup() { 52 | Serial.begin(9600); 53 | initCloudButton(); 54 | pinMode(GAIN_CONTROL, OUTPUT); 55 | analogWrite(GAIN_CONTROL, 45); //put the gain right in the middle, for now 56 | 57 | fft_cfg = kiss_fftr_alloc(FFT_SIZE, FALSE, NULL, NULL); 58 | fft_in = (kiss_fft_scalar*)malloc(FFT_SIZE * sizeof(kiss_fft_scalar)); 59 | fft_out = (kiss_fft_cpx*)malloc(FFT_SIZE / 2 * sizeof(kiss_fft_cpx) + 1); 60 | 61 | cube_init(); 62 | } 63 | 64 | //sets up the online/offline switch 65 | void initCloudButton() 66 | { 67 | //set the input mode for the 'connect to cloud' button 68 | pinMode(BUTTON, INPUT_PULLUP); 69 | pinMode(MODE, INPUT_PULLUP); 70 | if(!digitalRead(MODE)) 71 | WiFi.listen(); 72 | //a.k.a. onlinePressed is HIGH when the switch is set to 'online' and LOW when the switch is set to 'offline' 73 | onlinePressed=digitalRead(BUTTON); 74 | if(onlinePressed) 75 | Spark.connect(); 76 | } 77 | 78 | //checks to see if the 'online/offline' switch is switched 79 | void checkCloudButton() 80 | { 81 | //if the 'connect to cloud' button is pressed, try to connect to wifi. 82 | //otherwise, run the program 83 | //note -- how does this behave when there are no wifi credentials loaded on the spark? 84 | 85 | //onlinePressed is HIGH when the switch is _not_ connected and LOW when the switch is connected 86 | //a.k.a. onlinePressed is HIGH when the switch is set to 'online' and LOW when the switch is set to 'offline' 87 | onlinePressed=digitalRead(BUTTON); 88 | 89 | if((!onlinePressed)&&(lastOnline)) //marked as 'offline' 90 | { 91 | lastOnline=onlinePressed; 92 | Spark.disconnect(); 93 | } 94 | 95 | else if((onlinePressed)&&(!lastOnline)) //marked as 'online' 96 | { 97 | lastOnline=onlinePressed; 98 | Spark.connect(); 99 | } 100 | 101 | lastOnline=onlinePressed; 102 | 103 | if(!digitalRead(MODE)) 104 | WiFi.listen(); 105 | } 106 | 107 | 108 | void palettize(color* col, int x) { 109 | /* 110 | col->red = 256*sin(M_PI * x / 32); 111 | col->green = 256*sin(M_PI * x / 64); 112 | col->blue = 256*sin(M_PI * x / 128); 113 | */ 114 | 115 | col->red = 256*sin(M_PI * x / 128); 116 | col->green = 256*sin(M_PI * x / 128); 117 | col->blue = 0; 118 | } 119 | 120 | void loop() { 121 | checkCloudButton(); 122 | 123 | updateFFT(); 124 | 125 | /*for(int i=0; i < FFT_SIZE; i++) { 126 | Serial.print(log_pwr_fft[i]); 127 | Serial.print(",\t"); 128 | } 129 | Serial.println();*/ 130 | 131 | color col = { 55, 55, 55 }; 132 | 133 | // shift the previous slices 134 | for(int y=7; y >= 1; y--) { 135 | for(int x=0; x < 8; x++) { 136 | for(int z=0; z < 8; z++) { 137 | color below = getPixel(x, y-1, z); 138 | setPixel(x, y, z, &below); 139 | } 140 | } 141 | } 142 | 143 | // add the next slice 144 | uint8_t x = 4; 145 | uint8_t z = 4; 146 | uint8_t magic[4] = {0, 1, 0, -1}; 147 | bool done = false; 148 | int count = 0; 149 | for(int i=1; count < 64; i++) { 150 | for(int j=0; j<2 && count < 64; j++) { 151 | for(int k=0; k 7 | 8 | #define PIXEL_PIN D0 9 | #define PIXEL_COUNT 512 10 | #define PIXEL_TYPE WS2812B 11 | Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE); 12 | 13 | SYSTEM_MODE(SEMI_AUTOMATIC); //don't connect to the internet on boot 14 | #define BUTTON D2 //press this button to connect to the internet 15 | #define MODE D3 16 | bool onlinePressed=false; 17 | bool lastOnline=true; 18 | 19 | //sets a pixel at position (x,y,z) to the col parameter's color 20 | void setPixel(int x, int y, int z, color* col); 21 | //function definitions 22 | void newMatrix(matrix* mat); 23 | void updateMatrix(matrix* mat); 24 | void drawMatrix(matrix* mat); 25 | 26 | matrix matrices[MATRIX_STRANDS]; 27 | color black, matrixTip, matrixStrand; 28 | 29 | double num; 30 | void setup() 31 | { 32 | 33 | Spark.variable("num", &num, DOUBLE); 34 | // seed the random number generator. THINGS WILL NEVER BE THE SAME AGAIN 35 | uint32_t seed = millis(); 36 | srand(seed); 37 | 38 | //set up some colors that we'll use in the program 39 | black.red=0; 40 | black.green=0; 41 | black.blue=0; 42 | 43 | matrixTip.red=50; 44 | matrixTip.green=160; 45 | matrixTip.blue=40; 46 | 47 | matrixStrand.red=14; 48 | matrixStrand.green=70; 49 | matrixStrand.blue=16; 50 | 51 | 52 | initCloudButton(); 53 | 54 | //initialize all the matrix 'strands' 55 | for(int i=0;igreen>rando/2) 125 | strip.setPixelColor(index,strip.Color(col->red, rand()%rando - rando/2 + col->green, col->blue)); 126 | else 127 | strip.setPixelColor(index,strip.Color(col->red, col->green, col->blue)); 128 | 129 | } 130 | 131 | 132 | void newMatrix(matrix* mat) 133 | { 134 | mat->x=rand()%SIDE; 135 | mat->y=rand()%SIDE; 136 | mat->zInc=rand()%150; 137 | mat->zInc/=100; 138 | mat->zInc-=0.75; 139 | num=mat->zInc; 140 | if(abs(mat->zInc)<0.01) 141 | { 142 | if(mat->zInc>0) 143 | mat->zInc+=0.01; 144 | else 145 | mat->zInc-=0.01; 146 | } 147 | if(mat->zInc<0) 148 | { 149 | mat->zEnd=-1*rand()%20; 150 | mat->z=rand()%20; 151 | } 152 | else 153 | { 154 | mat->zEnd=rand()%20; 155 | mat->z=-1*rand()%20; 156 | } 157 | } 158 | 159 | void updateMatrix(matrix* mat) 160 | { 161 | mat->z+=mat->zInc; 162 | if(abs(mat->z-mat->zEnd)<1) 163 | newMatrix(mat); 164 | } 165 | 166 | void drawMatrix(matrix* mat) 167 | { 168 | if(mat->zInc<1) 169 | { 170 | if(mat->zmat->z)&&(i>=0));i--) 173 | setPixel(mat->x, i, mat->y,&matrixStrand); 174 | if(mat->z>=0) 175 | setPixel(mat->x, mat->z, mat->y, &matrixTip); 176 | } 177 | } 178 | else 179 | { 180 | if(mat->zz)&&(ix, i, mat->y,&matrixStrand); 184 | if(mat->z>=0) 185 | setPixel(mat->x, mat->z, mat->y, &matrixTip); 186 | } 187 | } 188 | } 189 | 190 | -------------------------------------------------------------------------------- /Readme.md~: -------------------------------------------------------------------------------- 1 | This is a set of demo software for the L3D cube. There are two types of programs in this repository: Native programs, 2 | that run on the [spark processor](https://www.spark.io/) in the cube, and don't require a wifi connection to work, 3 | and Streaming programs, that stream data to the cube from a computer or phone over wifi. 4 | 5 | For streaming programs, you'll need to load the Listener program(located in the /Streaming directory) onto the Spark, 6 | and then run the program on your computer that generates the pattern for the cube. Data is streamed via UDP multicast on port 7 | 6000, which most home routers support. If you're not seeing any data come through, it's possible that your router is blocking 8 | multicast packets. If you can fiddle with the router's settings, check to see if it's blocking packets on port 6000, and if 9 | it's disabling multicast. 10 | 11 | If you have a network that requires you to click through a form to get access to the web, the processor in the cube 12 | 13 | * Getting started 14 | 15 | You’ll need to do some quick setup so that you can load programs onto the processor in your cube. You’ll need to tell the processor the credentials for your wifi network, so that it can connect to the internet and receive new programs. First, turn your cube on its side, and put your processor into *listening mode* by pressing and holding the ‘mode’ button for three seconds, until it starts flashing blue. More instructions are available [here](http://docs.spark.io/connect/) 16 | 17 | Follow the instructions [here](http://docs.spark.io/start/) to use your phone or tablet to set up the processor. 18 | 19 | If you prefer to work on the command line, you’ll need to plug the USB cable into your computer and follow the setup instructions [here](http://docs.spark.io/cli/) to install the command-line interface for the processor. Once it’s installed, run the command ‘spark setup’, and a prompt will walk you through the process of creating a (free) account with Spark, loading your wifi credentials onto the processor and linking the processor to your account. 20 | 21 | Once your processor is set up, you’re ready to load a program onto it. We prefer to work from the [Spark web interface](https://www.spark.io/build) — all the code is written in the browser, the compiler is in the cloud, and as long as your cube has a wifi connection, you load a new program onto the cube just by clicking a button. To get started, check out the [example included in our L3D library](LINK GOES HERE), and try flashing it onto your cube. 22 | 23 | Unfortunately, Spark doesn’t have a great setup yet for sharing code online. We’re working with them to set something up, but until then, the best way to ‘fork’ our code and start editing it is to create a new app in the spark editor, and copy-paste the code from the github repository into the app. 24 | 25 | To further complicate matters, the spark interface has a couple quirks that make copy-pasting a little tricky. Here’s the best way we’ve found to paste in some new code: 26 | 27 | Let’s say that there’s a native program that has three files: 28 | 29 | demo.ino — this is the main file in the app. Includes the spark library ‘Neopixel’ 30 | colors.cpp — a couple functions for handling colors 31 | colors.h — defines some datatypes 32 | 33 | 1. Create a new app in the spark editor — you can call it anything you like, but for this example, let’s say you call it ‘myApp’ 34 | ![creating a new app](http://cl.ly/image/161p0M0j2z3C/Screen%20Shot%202014-10-01%20at%2012.29.23%20PM.png) 35 | ![naming the app](http://cl.ly/image/0i2a1J2U331u/Screen%20Shot%202014-10-01%20at%2012.29.29%20PM.png) 36 | 37 | 2. The editor will open up with a single file, myApp.ino Copy all the code in your local copy of demo.ino and paste it into the myApp.ino file in your browser. Once it’s pasted, ** be sure to click ‘save’ **. 38 | ![pasting in new code](http://cl.ly/image/39392f402n1a/Screen%20Shot%202014-10-01%20at%2012.30.12%20PM.png) 39 | 40 | 3. Next, you’ll need to add the spark neopixel library. Click on the ‘libraries’ icon: 41 | ![library icon](http://cl.ly/image/1g3a3t0P3z3k/Screen%20Shot%202014-10-01%20at%2012.30.24%20PM.png) 42 | 43 | Next, find the ‘neopixel’ library in the list of libraries (it’s quite popular, so it’s usually at the very top), and click on it. 44 | ![neopixel library](http://cl.ly/image/3C450h1x0L39/Screen%20Shot%202014-10-01%20at%2012.30.29%20PM.png) 45 | 46 | Scroll down on the left side of the screen, and click the ‘include in app’ button: 47 | 48 | ![include in app](http://cl.ly/image/0X1A1l281q2G/Screen%20Shot%202014-10-01%20at%2012.30.40%20PM.png) 49 | 50 | It will display a list of all your apps. Scroll to the bottom and select ‘myApp’ 51 | 52 | ![select your app](http://cl.ly/image/0q1B2x3i2f1T/Screen%20Shot%202014-10-01%20at%2012.30.46%20PM.png) 53 | 54 | Scroll down to the bottom of a list again, and click ‘add to this app’ 55 | 56 | ![add to this app](http://cl.ly/image/1y1K3M0N0X2u/Screen%20Shot%202014-10-01%20at%2012.30.54%20PM.png) 57 | 58 | Great! That’s all done 59 | 60 | 4. Finally, you need to add the colors.h and colors.cpp files to the sketch. Click the ‘+’ icon on the top-right of the web interface. 61 | 62 | ![add files](http://cl.ly/image/0N2b0Z1m3O2J/Screen%20Shot%202014-10-01%20at%2012.59.25%20PM.png) 63 | 64 | It will create a .cpp and .h file, and prompt you to enter a name for the files. Type in ‘colors.h’, and it will automatically rename both files. 65 | 66 | ![renaming files](http://cl.ly/image/3r3u1N362W2J/Screen%20Shot%202014-10-01%20at%2012.59.28%20PM.png) 67 | 68 | Paste in the code from both colors.h and colors.cpp 69 | 70 | ![pasting code](http://cl.ly/image/0G253T2n2g45/Screen%20Shot%202014-10-01%20at%2012.59.38%20PM.png) 71 | 72 | Aaaand you’re good to go! 73 | 74 | 5. To load the code onto your spark, just click the ‘flash program’ icon on the top left of the interface 75 | 76 | ![flash program](http://cl.ly/image/3d18301k1N3E/Screen%20Shot%202014-10-01%20at%201.01.49%20PM.png) 77 | 78 | The color LED on your spark should quickly turn purple and begin flashing to show that the program is loading. It usually takes about 20 seconds to load a new program, although we’ve seen it take up to a minute when we’re working outside of the US. 79 | 80 | 81 | ------ 82 | 83 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | This is a set of demo software for the L3D cube. There are two types of programs in this repository: Native programs, 2 | that run on the [spark processor](https://www.spark.io/) in the cube, and don't require a wifi connection to work, 3 | and Streaming programs, that stream data to the cube from a computer or phone over wifi. 4 | 5 | For streaming programs, you'll need to load the Listener program(located in the /Streaming directory) onto the Spark, 6 | and then run the program on your computer that generates the pattern for the cube. Data is streamed via UDP multicast on port 7 | 2222, which most home routers support. If you're not seeing any data come through, it's possible that your router is blocking 8 | multicast packets. If you can fiddle with the router's settings, check to see if it's blocking packets on port 2222, and if 9 | it's disabling multicast. 10 | 11 | If you have a network that requires you to click through a form to get access to the web, the processor in the cube 12 | 13 | * Getting started 14 | 15 | You’ll need to do some quick setup so that you can load programs onto the processor in your cube. You’ll need to tell the processor the credentials for your wifi network, so that it can connect to the internet and receive new programs. First, turn your cube on its side, and put your processor into *listening mode* by pressing and holding the ‘mode’ button for three seconds, until it starts flashing blue. More instructions are available [here](http://docs.spark.io/connect/) 16 | 17 | Follow the instructions [here](http://docs.spark.io/start/) to use your phone or tablet to set up the processor. 18 | 19 | If you prefer to work on the command line, you’ll need to plug the USB cable into your computer and follow the setup instructions [here](http://docs.spark.io/cli/) to install the command-line interface for the processor. Once it’s installed, run the command ‘spark setup’, and a prompt will walk you through the process of creating a (free) account with Spark, loading your wifi credentials onto the processor and linking the processor to your account. 20 | 21 | Once your processor is set up, you’re ready to load a program onto it. We prefer to work from the [Spark web interface](https://www.spark.io/build) — all the code is written in the browser, the compiler is in the cloud, and as long as your cube has a wifi connection, you load a new program onto the cube just by clicking a button. To get started, check out the [example included in our L3D library](https://github.com/enjrolas/L3D-library), and try flashing it onto your cube. 22 | 23 | Unfortunately, Spark doesn’t have a great setup yet for sharing code online. We’re working with them to set something up, but until then, the best way to ‘fork’ our code and start editing it is to create a new app in the spark editor, and copy-paste the code from the github repository into the app. 24 | 25 | To further complicate matters, the spark interface has a couple quirks that make copy-pasting a little tricky. Here’s the best way we’ve found to paste in some new code: 26 | 27 | Let’s say that there’s a native program that has three files: 28 | 29 | demo.ino — this is the main file in the app. Includes the spark library ‘Neopixel’ 30 | colors.cpp — a couple functions for handling colors 31 | colors.h — defines some datatypes 32 | 33 | 1. Create a new app in the spark editor — you can call it anything you like, but for this example, let’s say you call it ‘myApp’ 34 | ![creating a new app](http://cl.ly/image/161p0M0j2z3C/Screen%20Shot%202014-10-01%20at%2012.29.23%20PM.png) 35 | ![naming the app](http://cl.ly/image/0i2a1J2U331u/Screen%20Shot%202014-10-01%20at%2012.29.29%20PM.png) 36 | 37 | 2. The editor will open up with a single file, myApp.ino Copy all the code in your local copy of demo.ino and paste it into the myApp.ino file in your browser. Once it’s pasted, ** be sure to click ‘save’ **. 38 | ![pasting in new code](http://cl.ly/image/39392f402n1a/Screen%20Shot%202014-10-01%20at%2012.30.12%20PM.png) 39 | 40 | 3. Next, you’ll need to add the spark neopixel library. Click on the ‘libraries’ icon: 41 | ![library icon](http://cl.ly/image/1g3a3t0P3z3k/Screen%20Shot%202014-10-01%20at%2012.30.24%20PM.png) 42 | 43 | Next, find the ‘neopixel’ library in the list of libraries (it’s quite popular, so it’s usually at the very top), and click on it. 44 | ![neopixel library](http://cl.ly/image/3C450h1x0L39/Screen%20Shot%202014-10-01%20at%2012.30.29%20PM.png) 45 | 46 | Scroll down on the left side of the screen, and click the ‘include in app’ button: 47 | 48 | ![include in app](http://cl.ly/image/0X1A1l281q2G/Screen%20Shot%202014-10-01%20at%2012.30.40%20PM.png) 49 | 50 | It will display a list of all your apps. Scroll to the bottom and select ‘myApp’ 51 | 52 | ![select your app](http://cl.ly/image/0q1B2x3i2f1T/Screen%20Shot%202014-10-01%20at%2012.30.46%20PM.png) 53 | 54 | Scroll down to the bottom of a list again, and click ‘add to this app’ 55 | 56 | ![add to this app](http://cl.ly/image/1y1K3M0N0X2u/Screen%20Shot%202014-10-01%20at%2012.30.54%20PM.png) 57 | 58 | Great! That’s all done 59 | 60 | 4. Finally, you need to add the colors.h and colors.cpp files to the sketch. Click the ‘+’ icon on the top-right of the web interface. 61 | 62 | ![add files](http://cl.ly/image/0N2b0Z1m3O2J/Screen%20Shot%202014-10-01%20at%2012.59.25%20PM.png) 63 | 64 | It will create a .cpp and .h file, and prompt you to enter a name for the files. Type in ‘colors.h’, and it will automatically rename both files. 65 | 66 | ![renaming files](http://cl.ly/image/3r3u1N362W2J/Screen%20Shot%202014-10-01%20at%2012.59.28%20PM.png) 67 | 68 | Paste in the code from both colors.h and colors.cpp 69 | 70 | ![pasting code](http://cl.ly/image/0G253T2n2g45/Screen%20Shot%202014-10-01%20at%2012.59.38%20PM.png) 71 | 72 | Aaaand you’re good to go! 73 | 74 | 5. To load the code onto your spark, just click the ‘flash program’ icon on the top left of the interface 75 | 76 | ![flash program](http://cl.ly/image/3d18301k1N3E/Screen%20Shot%202014-10-01%20at%201.01.49%20PM.png) 77 | 78 | The color LED on your spark should quickly turn purple and begin flashing to show that the program is loading. It usually takes about 20 seconds to load a new program, although we’ve seen it take up to a minute when we’re working outside of the US. 79 | 80 | 81 | ------ 82 | 83 | -------------------------------------------------------------------------------- /Native/Audio/_kiss_fft_guts.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2003-2010, Mark Borgerding 3 | 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 10 | * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. 11 | 12 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 13 | */ 14 | 15 | /* kiss_fft.h 16 | defines kiss_fft_scalar as either short or a float type 17 | and defines 18 | typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */ 19 | #include "kiss_fft.h" 20 | #include 21 | 22 | #define MAXFACTORS 32 23 | /* e.g. an fft of length 128 has 4 factors 24 | as far as kissfft is concerned 25 | 4*4*4*2 26 | */ 27 | 28 | struct kiss_fft_state{ 29 | int nfft; 30 | int inverse; 31 | int factors[2*MAXFACTORS]; 32 | kiss_fft_cpx twiddles[1]; 33 | }; 34 | 35 | /* 36 | Explanation of macros dealing with complex math: 37 | 38 | C_MUL(m,a,b) : m = a*b 39 | C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise 40 | C_SUB( res, a,b) : res = a - b 41 | C_SUBFROM( res , a) : res -= a 42 | C_ADDTO( res , a) : res += a 43 | * */ 44 | #ifdef FIXED_POINT 45 | #if (FIXED_POINT==32) 46 | # define FRACBITS 31 47 | # define SAMPPROD int64_t 48 | #define SAMP_MAX 2147483647 49 | #else 50 | # define FRACBITS 15 51 | # define SAMPPROD int32_t 52 | #define SAMP_MAX 32767 53 | #endif 54 | 55 | #define SAMP_MIN -SAMP_MAX 56 | 57 | #if defined(CHECK_OVERFLOW) 58 | # define CHECK_OVERFLOW_OP(a,op,b) \ 59 | if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \ 60 | fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) ); } 61 | #endif 62 | 63 | 64 | # define smul(a,b) ( (SAMPPROD)(a)*(b) ) 65 | # define sround( x ) (kiss_fft_scalar)( ( (x) + (1<<(FRACBITS-1)) ) >> FRACBITS ) 66 | 67 | # define S_MUL(a,b) sround( smul(a,b) ) 68 | 69 | # define C_MUL(m,a,b) \ 70 | do{ (m).r = sround( smul((a).r,(b).r) - smul((a).i,(b).i) ); \ 71 | (m).i = sround( smul((a).r,(b).i) + smul((a).i,(b).r) ); }while(0) 72 | 73 | # define DIVSCALAR(x,k) \ 74 | (x) = sround( smul( x, SAMP_MAX/k ) ) 75 | 76 | # define C_FIXDIV(c,div) \ 77 | do { DIVSCALAR( (c).r , div); \ 78 | DIVSCALAR( (c).i , div); }while (0) 79 | 80 | # define C_MULBYSCALAR( c, s ) \ 81 | do{ (c).r = sround( smul( (c).r , s ) ) ;\ 82 | (c).i = sround( smul( (c).i , s ) ) ; }while(0) 83 | 84 | #else /* not FIXED_POINT*/ 85 | 86 | # define S_MUL(a,b) ( (a)*(b) ) 87 | #define C_MUL(m,a,b) \ 88 | do{ (m).r = (a).r*(b).r - (a).i*(b).i;\ 89 | (m).i = (a).r*(b).i + (a).i*(b).r; }while(0) 90 | # define C_FIXDIV(c,div) /* NOOP */ 91 | # define C_MULBYSCALAR( c, s ) \ 92 | do{ (c).r *= (s);\ 93 | (c).i *= (s); }while(0) 94 | #endif 95 | 96 | #ifndef CHECK_OVERFLOW_OP 97 | # define CHECK_OVERFLOW_OP(a,op,b) /* noop */ 98 | #endif 99 | 100 | #define C_ADD( res, a,b)\ 101 | do { \ 102 | CHECK_OVERFLOW_OP((a).r,+,(b).r)\ 103 | CHECK_OVERFLOW_OP((a).i,+,(b).i)\ 104 | (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \ 105 | }while(0) 106 | #define C_SUB( res, a,b)\ 107 | do { \ 108 | CHECK_OVERFLOW_OP((a).r,-,(b).r)\ 109 | CHECK_OVERFLOW_OP((a).i,-,(b).i)\ 110 | (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \ 111 | }while(0) 112 | #define C_ADDTO( res , a)\ 113 | do { \ 114 | CHECK_OVERFLOW_OP((res).r,+,(a).r)\ 115 | CHECK_OVERFLOW_OP((res).i,+,(a).i)\ 116 | (res).r += (a).r; (res).i += (a).i;\ 117 | }while(0) 118 | 119 | #define C_SUBFROM( res , a)\ 120 | do {\ 121 | CHECK_OVERFLOW_OP((res).r,-,(a).r)\ 122 | CHECK_OVERFLOW_OP((res).i,-,(a).i)\ 123 | (res).r -= (a).r; (res).i -= (a).i; \ 124 | }while(0) 125 | 126 | 127 | #ifdef FIXED_POINT 128 | # define KISS_FFT_COS(phase) floor(.5+SAMP_MAX * cos (phase)) 129 | # define KISS_FFT_SIN(phase) floor(.5+SAMP_MAX * sin (phase)) 130 | # define HALF_OF(x) ((x)>>1) 131 | #elif defined(USE_SIMD) 132 | # define KISS_FFT_COS(phase) _mm_set1_ps( cos(phase) ) 133 | # define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) ) 134 | # define HALF_OF(x) ((x)*_mm_set1_ps(.5)) 135 | #else 136 | # define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase) 137 | # define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase) 138 | # define HALF_OF(x) ((x)*.5) 139 | #endif 140 | 141 | #define kf_cexp(x,phase) \ 142 | do{ \ 143 | (x)->r = KISS_FFT_COS(phase);\ 144 | (x)->i = KISS_FFT_SIN(phase);\ 145 | }while(0) 146 | 147 | 148 | /* a debugging function */ 149 | #define pcpx(c)\ 150 | fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) ) 151 | 152 | 153 | #ifdef KISS_FFT_USE_ALLOCA 154 | // define this to allow use of alloca instead of malloc for temporary buffers 155 | // Temporary buffers are used in two case: 156 | // 1. FFT sizes that have "bad" factors. i.e. not 2,3 and 5 157 | // 2. "in-place" FFTs. Notice the quotes, since kissfft does not really do an in-place transform. 158 | #include 159 | #define KISS_FFT_TMP_ALLOC(nbytes) alloca(nbytes) 160 | #define KISS_FFT_TMP_FREE(ptr) 161 | #else 162 | #define KISS_FFT_TMP_ALLOC(nbytes) KISS_FFT_MALLOC(nbytes) 163 | #define KISS_FFT_TMP_FREE(ptr) KISS_FFT_FREE(ptr) 164 | #endif 165 | -------------------------------------------------------------------------------- /Native/Audio/kiss_fftr.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2003-2004, Mark Borgerding 3 | 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 10 | * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. 11 | 12 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 13 | */ 14 | 15 | #include "kiss_fftr.h" 16 | #include "_kiss_fft_guts.h" 17 | 18 | struct kiss_fftr_state{ 19 | kiss_fft_cfg substate; 20 | kiss_fft_cpx * tmpbuf; 21 | kiss_fft_cpx * super_twiddles; 22 | #ifdef USE_SIMD 23 | void * pad; 24 | #endif 25 | }; 26 | 27 | kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem) 28 | { 29 | int i; 30 | kiss_fftr_cfg st = NULL; 31 | size_t subsize, memneeded; 32 | 33 | if (nfft & 1) { 34 | //fprintf(stderr,"Real FFT optimization must be even.\n"); 35 | return NULL; 36 | } 37 | nfft >>= 1; 38 | 39 | kiss_fft_alloc (nfft, inverse_fft, NULL, &subsize); 40 | memneeded = sizeof(struct kiss_fftr_state) + subsize + sizeof(kiss_fft_cpx) * ( nfft * 3 / 2); 41 | 42 | if (lenmem == NULL) { 43 | st = (kiss_fftr_cfg) KISS_FFT_MALLOC (memneeded); 44 | } else { 45 | if (*lenmem >= memneeded) 46 | st = (kiss_fftr_cfg) mem; 47 | *lenmem = memneeded; 48 | } 49 | if (!st) 50 | return NULL; 51 | 52 | st->substate = (kiss_fft_cfg) (st + 1); /*just beyond kiss_fftr_state struct */ 53 | st->tmpbuf = (kiss_fft_cpx *) (((char *) st->substate) + subsize); 54 | st->super_twiddles = st->tmpbuf + nfft; 55 | kiss_fft_alloc(nfft, inverse_fft, st->substate, &subsize); 56 | 57 | for (i = 0; i < nfft/2; ++i) { 58 | double phase = 59 | -3.14159265358979323846264338327 * ((double) (i+1) / nfft + .5); 60 | if (inverse_fft) 61 | phase *= -1; 62 | kf_cexp (st->super_twiddles+i,phase); 63 | } 64 | return st; 65 | } 66 | 67 | void kiss_fftr(kiss_fftr_cfg st,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata) 68 | { 69 | /* input buffer timedata is stored row-wise */ 70 | int k,ncfft; 71 | kiss_fft_cpx fpnk,fpk,f1k,f2k,tw,tdc; 72 | 73 | if ( st->substate->inverse) { 74 | //fprintf(stderr,"kiss fft usage error: improper alloc\n"); 75 | exit(1); 76 | } 77 | 78 | ncfft = st->substate->nfft; 79 | 80 | /*perform the parallel fft of two real signals packed in real,imag*/ 81 | kiss_fft( st->substate , (const kiss_fft_cpx*)timedata, st->tmpbuf ); 82 | /* The real part of the DC element of the frequency spectrum in st->tmpbuf 83 | * contains the sum of the even-numbered elements of the input time sequence 84 | * The imag part is the sum of the odd-numbered elements 85 | * 86 | * The sum of tdc.r and tdc.i is the sum of the input time sequence. 87 | * yielding DC of input time sequence 88 | * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1... 89 | * yielding Nyquist bin of input time sequence 90 | */ 91 | 92 | tdc.r = st->tmpbuf[0].r; 93 | tdc.i = st->tmpbuf[0].i; 94 | C_FIXDIV(tdc,2); 95 | CHECK_OVERFLOW_OP(tdc.r ,+, tdc.i); 96 | CHECK_OVERFLOW_OP(tdc.r ,-, tdc.i); 97 | freqdata[0].r = tdc.r + tdc.i; 98 | freqdata[ncfft].r = tdc.r - tdc.i; 99 | #ifdef USE_SIMD 100 | freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps(0); 101 | #else 102 | freqdata[ncfft].i = freqdata[0].i = 0; 103 | #endif 104 | 105 | for ( k=1;k <= ncfft/2 ; ++k ) { 106 | fpk = st->tmpbuf[k]; 107 | fpnk.r = st->tmpbuf[ncfft-k].r; 108 | fpnk.i = - st->tmpbuf[ncfft-k].i; 109 | C_FIXDIV(fpk,2); 110 | C_FIXDIV(fpnk,2); 111 | 112 | C_ADD( f1k, fpk , fpnk ); 113 | C_SUB( f2k, fpk , fpnk ); 114 | C_MUL( tw , f2k , st->super_twiddles[k-1]); 115 | 116 | freqdata[k].r = HALF_OF(f1k.r + tw.r); 117 | freqdata[k].i = HALF_OF(f1k.i + tw.i); 118 | freqdata[ncfft-k].r = HALF_OF(f1k.r - tw.r); 119 | freqdata[ncfft-k].i = HALF_OF(tw.i - f1k.i); 120 | } 121 | } 122 | 123 | void kiss_fftri(kiss_fftr_cfg st,const kiss_fft_cpx *freqdata,kiss_fft_scalar *timedata) 124 | { 125 | /* input buffer timedata is stored row-wise */ 126 | int k, ncfft; 127 | 128 | if (st->substate->inverse == 0) { 129 | //fprintf (stderr, "kiss fft usage error: improper alloc\n"); 130 | exit (1); 131 | } 132 | 133 | ncfft = st->substate->nfft; 134 | 135 | st->tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r; 136 | st->tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r; 137 | C_FIXDIV(st->tmpbuf[0],2); 138 | 139 | for (k = 1; k <= ncfft / 2; ++k) { 140 | kiss_fft_cpx fk, fnkc, fek, fok, tmp; 141 | fk = freqdata[k]; 142 | fnkc.r = freqdata[ncfft - k].r; 143 | fnkc.i = -freqdata[ncfft - k].i; 144 | C_FIXDIV( fk , 2 ); 145 | C_FIXDIV( fnkc , 2 ); 146 | 147 | C_ADD (fek, fk, fnkc); 148 | C_SUB (tmp, fk, fnkc); 149 | C_MUL (fok, tmp, st->super_twiddles[k-1]); 150 | C_ADD (st->tmpbuf[k], fek, fok); 151 | C_SUB (st->tmpbuf[ncfft - k], fek, fok); 152 | #ifdef USE_SIMD 153 | st->tmpbuf[ncfft - k].i *= _mm_set1_ps(-1.0); 154 | #else 155 | st->tmpbuf[ncfft - k].i *= -1; 156 | #endif 157 | } 158 | kiss_fft (st->substate, st->tmpbuf, (kiss_fft_cpx *) timedata); 159 | } 160 | -------------------------------------------------------------------------------- /Native/SnowGlobe/snow.cpp: -------------------------------------------------------------------------------- 1 | #include "application.h" 2 | 3 | #include "math.h" 4 | #include "colors.h" 5 | #include "cube.h" 6 | #include "util.h" 7 | 8 | #include "snow.h" 9 | 10 | #define WITH_TREE // enable tree 11 | 12 | #define NUM_SNOWFLAKES 100 // # snowflakes to allocate memory for 13 | #define MOVEABLE_PERCENT 30 // % snowflakes non-static 14 | #define GRAVITY_FACTOR 0.05 // accelerometer multiplier 15 | #define AIR_FRICTION 0.8 // per-frame velocity multiplier 16 | #define HOMING_LIKELIHOOD 1 // if rand() % 100 < this then put particle back home 17 | 18 | color color_snow = { 54, 54, 54 }; 19 | color stroke = { 200, 200, 200 }; 20 | 21 | const uint8_t profile_tree[5] = { 2, 2, 4, 6, 6 }; // size of the squares making up the tree 22 | const uint8_t profile_snow[5] = { 0, 4, 6, 0, 0 }; // size of the snow rings for each tree level 23 | 24 | snowflake* snow[NUM_SNOWFLAKES]; 25 | unsigned int snowcount = 0; // # added snow particles 26 | bool snowed = false; 27 | 28 | void place_snow(float x, float y, float z, bool thick) { 29 | if(snowcount < NUM_SNOWFLAKES) { 30 | snowflake* flake = (snowflake*)calloc(1, sizeof(snowflake)); 31 | 32 | flake->x = x; 33 | flake->y = y; 34 | flake->z = z; 35 | 36 | // home is where it was first placed 37 | flake->home_x = x; 38 | flake->home_y = y; 39 | flake->home_z = z; 40 | 41 | // initially cannot move 42 | flake->flags = 1 << SNOW_STUCK; 43 | 44 | // some never move 45 | if(rand() % 100 >= MOVEABLE_PERCENT) 46 | flake->flags |= 1 << SNOW_STATIC; 47 | 48 | // thick snow doesn't disappear 49 | if(thick) 50 | flake->flags |= 1 << SNOW_THICK; 51 | 52 | snow[snowcount] = flake; 53 | snowcount++; 54 | } 55 | } 56 | 57 | // puts a layer of snow on the scene 58 | void snowstorm() { 59 | // if it snowed before then don't add more snow 60 | // just move the snow back to where it started 61 | if(snowed) { 62 | for(unsigned int i=0; i < snowcount; i++) { 63 | snow[i]->x = snow[i]->home_x; 64 | snow[i]->y = snow[i]->home_y; 65 | snow[i]->z = snow[i]->home_z; 66 | } 67 | 68 | return; 69 | } 70 | 71 | // snow on ground 72 | for(int z=0; z < 8; z++) { 73 | for(int x=0; x < 8; x++) { 74 | #ifdef WITH_TREE 75 | if(x >= 3 && x <= 4 && z >= 3 && z <= 4) 76 | // skip the tree trunk 77 | continue; 78 | #endif 79 | 80 | place_snow(x, 0, z, true); 81 | } 82 | } 83 | 84 | #ifdef WITH_TREE 85 | // snow on leaves 86 | for(int i=0; i < 5; i++) { 87 | uint8_t p = profile_snow[i]; 88 | 89 | if(p > 0) { 90 | // if there is somewhere on the tree for the snow to land 91 | uint8_t offset = 4 - p / 2; 92 | 93 | for(int sx=0; sx < p; sx++) { 94 | place_snow(sx + offset, 7-i, offset, false); 95 | place_snow(sx + offset, 7-i, 7 - offset, false); 96 | } 97 | 98 | for(int sz=0; sz < p; sz++) { 99 | place_snow(offset, 7-i, sz + offset, false); 100 | place_snow(7 - offset, 7-i, sz + offset, false); 101 | } 102 | } 103 | } 104 | #endif 105 | 106 | snowed = true; 107 | } 108 | 109 | // randomize velocities for a fraction of the snowflakes 110 | void flurry(float severity, float strength) { 111 | for(unsigned int i=0; i < snowcount; i++) { 112 | snowflake* flake = snow[i]; 113 | 114 | if(rand() % 100 < (100.0 * severity)) { 115 | if(flake->flags & (1<flags &= ~(1<vx = frand(-strength, strength); 120 | flake->vy = frand(0, strength); 121 | flake->vz = frand(-strength, strength); 122 | } 123 | } 124 | } 125 | } 126 | 127 | // do physics for every snowflake 128 | void update_snow(float ax, float ay, float az) { 129 | for(unsigned int i=0; i < snowcount; i++) { 130 | snowflake* flake = snow[i]; 131 | 132 | if(((flake->flags & (1<flags & (1<x = flake->home_x; 137 | flake->y = flake->home_y; 138 | flake->z = flake->home_z; 139 | 140 | flake->vx = 0; 141 | flake->vy = 0; 142 | flake->vz = 0; 143 | 144 | flake->flags |= 1 << SNOW_STUCK; 145 | } else { 146 | // gravity 147 | flake->vx += ax * GRAVITY_FACTOR; 148 | flake->vy += ay * GRAVITY_FACTOR; 149 | flake->vz += az * GRAVITY_FACTOR; 150 | 151 | // air friction 152 | flake->vx *= AIR_FRICTION; 153 | flake->vy *= AIR_FRICTION; 154 | flake->vz *= AIR_FRICTION; 155 | 156 | // movement 157 | flake->x += flake->vx; 158 | flake->y += flake->vy; 159 | flake->z += flake->vz; 160 | 161 | // boundaries just wrap 162 | if(flake->x < 0) flake->x = 7; 163 | if(flake->x > 7) flake->x = 0; 164 | if(flake->y < 0) flake->y = 7; 165 | if(flake->y > 7) flake->y = 0; 166 | if(flake->z < 0) flake->z = 7; 167 | if(flake->z > 7) flake->z = 0; 168 | } 169 | } 170 | } 171 | } 172 | 173 | void render_snow() { 174 | for(unsigned int i=0; i < snowcount; i++) { 175 | snowflake* flake = snow[i]; 176 | setPixel((int)flake->x, (int)flake->y, (int)flake->z, &color_snow); 177 | 178 | if(flake->flags & (1<home_x, (int)flake->home_y, (int)flake->home_z, &color_snow); 180 | } 181 | } 182 | 183 | void render_background() { 184 | // clear cube 185 | for(int z=0; z < 8; z++) { 186 | for(int y=0; y < 8; y++) { 187 | for(int x=0; x < 8; x++) { 188 | setPixel(x, y, z, &color_dark); 189 | } 190 | } 191 | } 192 | 193 | #ifdef WITH_TREE 194 | { // tree 195 | stroke.red = 0; 196 | stroke.green = 54; 197 | stroke.blue = 0; 198 | 199 | // leaves 200 | for(int i=0; i < 5; i++) { 201 | uint8_t p = profile_tree[i]; 202 | uint8_t offset = 4 - p / 2; 203 | 204 | for(int tz=0; tz < p; tz++) { 205 | for(int tx=0; tx < p; tx++) { 206 | setPixel(tx + offset, 7-i, tz + offset, &stroke); 207 | } 208 | } 209 | } 210 | 211 | // trunk 212 | stroke.red = 239/4; 213 | stroke.green = 69/4; 214 | stroke.blue = 19/4; 215 | 216 | for(int y=0; y < 3; y++) { 217 | for(int z=3; z <= 4; z++) { 218 | for(int x=3; x <= 4; x++) { 219 | setPixel(x, y, z, &stroke); 220 | } 221 | } 222 | } 223 | } 224 | #endif 225 | } 226 | -------------------------------------------------------------------------------- /Native/Audio/kiss_fft.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2003-2010, Mark Borgerding 3 | 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 10 | * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. 11 | 12 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 13 | */ 14 | 15 | 16 | #include "_kiss_fft_guts.h" 17 | /* The guts header contains all the multiplication and addition macros that are defined for 18 | fixed or floating point complex numbers. It also delares the kf_ internal functions. 19 | */ 20 | 21 | static void kf_bfly2( 22 | kiss_fft_cpx * Fout, 23 | const size_t fstride, 24 | const kiss_fft_cfg st, 25 | int m 26 | ) 27 | { 28 | kiss_fft_cpx * Fout2; 29 | kiss_fft_cpx * tw1 = st->twiddles; 30 | kiss_fft_cpx t; 31 | Fout2 = Fout + m; 32 | do{ 33 | C_FIXDIV(*Fout,2); C_FIXDIV(*Fout2,2); 34 | 35 | C_MUL (t, *Fout2 , *tw1); 36 | tw1 += fstride; 37 | C_SUB( *Fout2 , *Fout , t ); 38 | C_ADDTO( *Fout , t ); 39 | ++Fout2; 40 | ++Fout; 41 | }while (--m); 42 | } 43 | 44 | static void kf_bfly4( 45 | kiss_fft_cpx * Fout, 46 | const size_t fstride, 47 | const kiss_fft_cfg st, 48 | const size_t m 49 | ) 50 | { 51 | kiss_fft_cpx *tw1,*tw2,*tw3; 52 | kiss_fft_cpx scratch[6]; 53 | size_t k=m; 54 | const size_t m2=2*m; 55 | const size_t m3=3*m; 56 | 57 | 58 | tw3 = tw2 = tw1 = st->twiddles; 59 | 60 | do { 61 | C_FIXDIV(*Fout,4); C_FIXDIV(Fout[m],4); C_FIXDIV(Fout[m2],4); C_FIXDIV(Fout[m3],4); 62 | 63 | C_MUL(scratch[0],Fout[m] , *tw1 ); 64 | C_MUL(scratch[1],Fout[m2] , *tw2 ); 65 | C_MUL(scratch[2],Fout[m3] , *tw3 ); 66 | 67 | C_SUB( scratch[5] , *Fout, scratch[1] ); 68 | C_ADDTO(*Fout, scratch[1]); 69 | C_ADD( scratch[3] , scratch[0] , scratch[2] ); 70 | C_SUB( scratch[4] , scratch[0] , scratch[2] ); 71 | C_SUB( Fout[m2], *Fout, scratch[3] ); 72 | tw1 += fstride; 73 | tw2 += fstride*2; 74 | tw3 += fstride*3; 75 | C_ADDTO( *Fout , scratch[3] ); 76 | 77 | if(st->inverse) { 78 | Fout[m].r = scratch[5].r - scratch[4].i; 79 | Fout[m].i = scratch[5].i + scratch[4].r; 80 | Fout[m3].r = scratch[5].r + scratch[4].i; 81 | Fout[m3].i = scratch[5].i - scratch[4].r; 82 | }else{ 83 | Fout[m].r = scratch[5].r + scratch[4].i; 84 | Fout[m].i = scratch[5].i - scratch[4].r; 85 | Fout[m3].r = scratch[5].r - scratch[4].i; 86 | Fout[m3].i = scratch[5].i + scratch[4].r; 87 | } 88 | ++Fout; 89 | }while(--k); 90 | } 91 | 92 | static void kf_bfly3( 93 | kiss_fft_cpx * Fout, 94 | const size_t fstride, 95 | const kiss_fft_cfg st, 96 | size_t m 97 | ) 98 | { 99 | size_t k=m; 100 | const size_t m2 = 2*m; 101 | kiss_fft_cpx *tw1,*tw2; 102 | kiss_fft_cpx scratch[5]; 103 | kiss_fft_cpx epi3; 104 | epi3 = st->twiddles[fstride*m]; 105 | 106 | tw1=tw2=st->twiddles; 107 | 108 | do{ 109 | C_FIXDIV(*Fout,3); C_FIXDIV(Fout[m],3); C_FIXDIV(Fout[m2],3); 110 | 111 | C_MUL(scratch[1],Fout[m] , *tw1); 112 | C_MUL(scratch[2],Fout[m2] , *tw2); 113 | 114 | C_ADD(scratch[3],scratch[1],scratch[2]); 115 | C_SUB(scratch[0],scratch[1],scratch[2]); 116 | tw1 += fstride; 117 | tw2 += fstride*2; 118 | 119 | Fout[m].r = Fout->r - HALF_OF(scratch[3].r); 120 | Fout[m].i = Fout->i - HALF_OF(scratch[3].i); 121 | 122 | C_MULBYSCALAR( scratch[0] , epi3.i ); 123 | 124 | C_ADDTO(*Fout,scratch[3]); 125 | 126 | Fout[m2].r = Fout[m].r + scratch[0].i; 127 | Fout[m2].i = Fout[m].i - scratch[0].r; 128 | 129 | Fout[m].r -= scratch[0].i; 130 | Fout[m].i += scratch[0].r; 131 | 132 | ++Fout; 133 | }while(--k); 134 | } 135 | 136 | static void kf_bfly5( 137 | kiss_fft_cpx * Fout, 138 | const size_t fstride, 139 | const kiss_fft_cfg st, 140 | int m 141 | ) 142 | { 143 | kiss_fft_cpx *Fout0,*Fout1,*Fout2,*Fout3,*Fout4; 144 | int u; 145 | kiss_fft_cpx scratch[13]; 146 | kiss_fft_cpx * twiddles = st->twiddles; 147 | kiss_fft_cpx *tw; 148 | kiss_fft_cpx ya,yb; 149 | ya = twiddles[fstride*m]; 150 | yb = twiddles[fstride*2*m]; 151 | 152 | Fout0=Fout; 153 | Fout1=Fout0+m; 154 | Fout2=Fout0+2*m; 155 | Fout3=Fout0+3*m; 156 | Fout4=Fout0+4*m; 157 | 158 | tw=st->twiddles; 159 | for ( u=0; ur += scratch[7].r + scratch[8].r; 174 | Fout0->i += scratch[7].i + scratch[8].i; 175 | 176 | scratch[5].r = scratch[0].r + S_MUL(scratch[7].r,ya.r) + S_MUL(scratch[8].r,yb.r); 177 | scratch[5].i = scratch[0].i + S_MUL(scratch[7].i,ya.r) + S_MUL(scratch[8].i,yb.r); 178 | 179 | scratch[6].r = S_MUL(scratch[10].i,ya.i) + S_MUL(scratch[9].i,yb.i); 180 | scratch[6].i = -S_MUL(scratch[10].r,ya.i) - S_MUL(scratch[9].r,yb.i); 181 | 182 | C_SUB(*Fout1,scratch[5],scratch[6]); 183 | C_ADD(*Fout4,scratch[5],scratch[6]); 184 | 185 | scratch[11].r = scratch[0].r + S_MUL(scratch[7].r,yb.r) + S_MUL(scratch[8].r,ya.r); 186 | scratch[11].i = scratch[0].i + S_MUL(scratch[7].i,yb.r) + S_MUL(scratch[8].i,ya.r); 187 | scratch[12].r = - S_MUL(scratch[10].i,yb.i) + S_MUL(scratch[9].i,ya.i); 188 | scratch[12].i = S_MUL(scratch[10].r,yb.i) - S_MUL(scratch[9].r,ya.i); 189 | 190 | C_ADD(*Fout2,scratch[11],scratch[12]); 191 | C_SUB(*Fout3,scratch[11],scratch[12]); 192 | 193 | ++Fout0;++Fout1;++Fout2;++Fout3;++Fout4; 194 | } 195 | } 196 | 197 | /* perform the butterfly for one stage of a mixed radix FFT */ 198 | static void kf_bfly_generic( 199 | kiss_fft_cpx * Fout, 200 | const size_t fstride, 201 | const kiss_fft_cfg st, 202 | int m, 203 | int p 204 | ) 205 | { 206 | int u,k,q1,q; 207 | kiss_fft_cpx * twiddles = st->twiddles; 208 | kiss_fft_cpx t; 209 | int Norig = st->nfft; 210 | 211 | kiss_fft_cpx * scratch = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC(sizeof(kiss_fft_cpx)*p); 212 | 213 | for ( u=0; u=Norig) twidx-=Norig; 228 | C_MUL(t,scratch[q] , twiddles[twidx] ); 229 | C_ADDTO( Fout[ k ] ,t); 230 | } 231 | k += m; 232 | } 233 | } 234 | KISS_FFT_TMP_FREE(scratch); 235 | } 236 | 237 | static 238 | void kf_work( 239 | kiss_fft_cpx * Fout, 240 | const kiss_fft_cpx * f, 241 | const size_t fstride, 242 | int in_stride, 243 | int * factors, 244 | const kiss_fft_cfg st 245 | ) 246 | { 247 | kiss_fft_cpx * Fout_beg=Fout; 248 | const int p=*factors++; /* the radix */ 249 | const int m=*factors++; /* stage's fft length/p */ 250 | const kiss_fft_cpx * Fout_end = Fout + p*m; 251 | 252 | #ifdef _OPENMP 253 | // use openmp extensions at the 254 | // top-level (not recursive) 255 | if (fstride==1 && p<=5) 256 | { 257 | int k; 258 | 259 | // execute the p different work units in different threads 260 | # pragma omp parallel for 261 | for (k=0;k floor_sqrt) 324 | p = n; /* no more factors, skip to end */ 325 | } 326 | n /= p; 327 | *facbuf++ = p; 328 | *facbuf++ = n; 329 | } while (n > 1); 330 | } 331 | 332 | /* 333 | * 334 | * User-callable function to allocate all necessary storage space for the fft. 335 | * 336 | * The return value is a contiguous block of memory, allocated with malloc. As such, 337 | * It can be freed with free(), rather than a kiss_fft-specific function. 338 | * */ 339 | kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem ) 340 | { 341 | kiss_fft_cfg st=NULL; 342 | size_t memneeded = sizeof(struct kiss_fft_state) 343 | + sizeof(kiss_fft_cpx)*(nfft-1); /* twiddle factors*/ 344 | 345 | if ( lenmem==NULL ) { 346 | st = ( kiss_fft_cfg)KISS_FFT_MALLOC( memneeded ); 347 | }else{ 348 | if (mem != NULL && *lenmem >= memneeded) 349 | st = (kiss_fft_cfg)mem; 350 | *lenmem = memneeded; 351 | } 352 | if (st) { 353 | int i; 354 | st->nfft=nfft; 355 | st->inverse = inverse_fft; 356 | 357 | for (i=0;iinverse) 361 | phase *= -1; 362 | kf_cexp(st->twiddles+i, phase ); 363 | } 364 | 365 | kf_factor(nfft,st->factors); 366 | } 367 | return st; 368 | } 369 | 370 | 371 | void kiss_fft_stride(kiss_fft_cfg st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int in_stride) 372 | { 373 | if (fin == fout) { 374 | //NOTE: this is not really an in-place FFT algorithm. 375 | //It just performs an out-of-place FFT into a temp buffer 376 | kiss_fft_cpx * tmpbuf = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC( sizeof(kiss_fft_cpx)*st->nfft); 377 | kf_work(tmpbuf,fin,1,in_stride, st->factors,st); 378 | memcpy(fout,tmpbuf,sizeof(kiss_fft_cpx)*st->nfft); 379 | KISS_FFT_TMP_FREE(tmpbuf); 380 | }else{ 381 | kf_work( fout, fin, 1,in_stride, st->factors,st ); 382 | } 383 | } 384 | 385 | void kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout) 386 | { 387 | kiss_fft_stride(cfg,fin,fout,1); 388 | } 389 | 390 | 391 | void kiss_fft_cleanup(void) 392 | { 393 | // nothing needed any more 394 | } 395 | 396 | int kiss_fft_next_fast_size(int n) 397 | { 398 | while(1) { 399 | int m=n; 400 | while ( (m%2) == 0 ) m/=2; 401 | while ( (m%3) == 0 ) m/=3; 402 | while ( (m%5) == 0 ) m/=5; 403 | if (m<=1) 404 | break; /* n is completely factorable by twos, threes, and fives */ 405 | n++; 406 | } 407 | return n; 408 | } 409 | -------------------------------------------------------------------------------- /Native/Audio/neopixel.cpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | Spark Core library to control WS2811/WS2812 based RGB 3 | LED devices such as Adafruit NeoPixel strips. 4 | Currently handles 800 KHz and 400kHz bitstream on Spark Core, 5 | WS2812, WS2812B and WS2811. 6 | 7 | Also supports: 8 | - Radio Shack Tri-Color Strip with TM1803 controller 400kHz bitstream. 9 | - TM1829 pixels 10 | 11 | Written by Phil Burgess / Paint Your Dragon for Adafruit Industries. 12 | Modified to work with Spark Core by Technobly. 13 | Contributions by PJRC and other members of the open source community. 14 | 15 | Adafruit invests time and resources providing this open source code, 16 | please support Adafruit and open-source hardware by purchasing products 17 | from Adafruit! 18 | --------------------------------------------------------------------*/ 19 | 20 | /* ======================= Adafruit_NeoPixel.cpp ======================= */ 21 | /*------------------------------------------------------------------------- 22 | This file is part of the Adafruit NeoPixel library. 23 | 24 | NeoPixel is free software: you can redistribute it and/or modify 25 | it under the terms of the GNU Lesser General Public License as 26 | published by the Free Software Foundation, either version 3 of 27 | the License, or (at your option) any later version. 28 | 29 | NeoPixel is distributed in the hope that it will be useful, 30 | but WITHOUT ANY WARRANTY; without even the implied warranty of 31 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 32 | GNU Lesser General Public License for more details. 33 | 34 | You should have received a copy of the GNU Lesser General Public 35 | License along with NeoPixel. If not, see 36 | . 37 | -------------------------------------------------------------------------*/ 38 | 39 | #include "neopixel.h" 40 | 41 | Adafruit_NeoPixel::Adafruit_NeoPixel(uint16_t n, uint8_t p, uint8_t t) : \ 42 | numLEDs(n), numBytes(n*3), type(t), pin(p), pixels(NULL) 43 | { 44 | if((pixels = (uint8_t *)malloc(numBytes))) { 45 | memset(pixels, 0, numBytes); 46 | } 47 | } 48 | 49 | Adafruit_NeoPixel::~Adafruit_NeoPixel() { 50 | if(pixels) free(pixels); 51 | pinMode(pin, INPUT); 52 | } 53 | 54 | void Adafruit_NeoPixel::begin(void) { 55 | pinMode(pin, OUTPUT); 56 | digitalWrite(pin, LOW); 57 | } 58 | 59 | void Adafruit_NeoPixel::show(void) { 60 | if(!pixels) return; 61 | 62 | // Data latch = 24 or 50 microsecond pause in the output stream. Rather than 63 | // put a delay at the end of the function, the ending time is noted and 64 | // the function will simply hold off (if needed) on issuing the 65 | // subsequent round of data until the latch time has elapsed. This 66 | // allows the mainline code to start generating the next frame of data 67 | // rather than stalling for the latch. 68 | uint32_t wait_time; // wait time in microseconds. 69 | switch(type) { 70 | case TM1803: // TM1803 = 24us reset pulse 71 | wait_time = 24L; 72 | break; 73 | case TM1829: // TM1829 = 500us reset pulse 74 | wait_time = 500L; 75 | break; 76 | case WS2812B: // WS2812 & WS2812B = 50us reset pulse 77 | case WS2811: // WS2811 = 50us reset pulse 78 | default: // default = 50us reset pulse 79 | wait_time = 50L; 80 | break; 81 | } 82 | while((micros() - endTime) < wait_time); 83 | // endTime is a private member (rather than global var) so that multiple 84 | // instances on different pins can be quickly issued in succession (each 85 | // instance doesn't delay the next). 86 | 87 | __disable_irq(); // Need 100% focus on instruction timing 88 | 89 | volatile uint32_t 90 | c, // 24-bit pixel color 91 | mask; // 8-bit mask 92 | volatile uint16_t i = numBytes; // Output loop counter 93 | volatile uint8_t 94 | j, // 8-bit inner loop counter 95 | *ptr = pixels, // Pointer to next byte 96 | g, // Current green byte value 97 | r, // Current red byte value 98 | b; // Current blue byte value 99 | 100 | if(type == WS2812B) { // same as WS2812, 800 KHz bitstream 101 | while(i) { // While bytes left... (3 bytes = 1 pixel) 102 | mask = 0x800000; // reset the mask 103 | i = i-3; // decrement bytes remaining 104 | g = *ptr++; // Next green byte value 105 | r = *ptr++; // Next red byte value 106 | b = *ptr++; // Next blue byte value 107 | c = ((uint32_t)g << 16) | ((uint32_t)r << 8) | b; // Pack the next 3 bytes to keep timing tight 108 | j = 0; // reset the 24-bit counter 109 | do { 110 | PIN_MAP[pin].gpio_peripheral->BSRR = PIN_MAP[pin].gpio_pin; // HIGH 111 | if (c & mask) { // if masked bit is high 112 | // WS2812 spec 700ns HIGH 113 | // Adafruit on Arduino (meas. 812ns) 114 | // This lib on Spark Core (meas. 792ns) 115 | asm volatile( 116 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 117 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 118 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 119 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 120 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 121 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 122 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 123 | "nop" "\n\t" 124 | ::: "r0", "cc", "memory"); 125 | // WS2812 spec 600ns LOW 126 | // Adafruit on Arduino (meas. 436ns) 127 | // This lib on Spark Core (meas. 472ns) 128 | PIN_MAP[pin].gpio_peripheral->BRR = PIN_MAP[pin].gpio_pin; // LOW 129 | asm volatile( 130 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 131 | "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 132 | ::: "r0", "cc", "memory"); 133 | } 134 | else { // else masked bit is low 135 | // WS2812 spec 350ns HIGH 136 | // Adafruit on Arduino (meas. 312ns) 137 | // This lib on Spark Core (meas. 306ns) 138 | asm volatile( 139 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 140 | ::: "r0", "cc", "memory"); 141 | // WS2812 spec 800ns LOW 142 | // Adafruit on Arduino (meas. 938ns) 143 | // This lib on Spark Core (meas. 932ns) 144 | PIN_MAP[pin].gpio_peripheral->BRR = PIN_MAP[pin].gpio_pin; // LOW 145 | asm volatile( 146 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 147 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 148 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 149 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 150 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 151 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 152 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 153 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 154 | ::: "r0", "cc", "memory"); 155 | } 156 | mask >>= 1; 157 | } while ( ++j < 24 ); // ... pixel done 158 | } // end while(i) ... no more pixels 159 | } 160 | else if(type == WS2811) { // WS2811, 400 KHz bitstream 161 | while(i) { // While bytes left... (3 bytes = 1 pixel) 162 | mask = 0x800000; // reset the mask 163 | i = i-3; // decrement bytes remaining 164 | r = *ptr++; // Next red byte value 165 | g = *ptr++; // Next green byte value 166 | b = *ptr++; // Next blue byte value 167 | c = ((uint32_t)r << 16) | ((uint32_t)g << 8) | b; // Pack the next 3 bytes to keep timing tight 168 | j = 0; // reset the 24-bit counter 169 | do { 170 | PIN_MAP[pin].gpio_peripheral->BSRR = PIN_MAP[pin].gpio_pin; // HIGH 171 | if (c & mask) { // if masked bit is high 172 | // WS2811 spec 1.20us HIGH 173 | // Adafruit on Arduino (meas. 1.25us) 174 | // This lib on Spark Core (meas. 1.25us) 175 | asm volatile( 176 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 177 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 178 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 179 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 180 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 181 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 182 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 183 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 184 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 185 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 186 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 187 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 188 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 189 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 190 | ::: "r0", "cc", "memory"); 191 | // WS2811 spec 1.30us LOW 192 | // Adafruit on Arduino (meas. 1.25us) 193 | // This lib on Spark Core (meas. 1.25us) 194 | PIN_MAP[pin].gpio_peripheral->BRR = PIN_MAP[pin].gpio_pin; // LOW 195 | asm volatile( 196 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 197 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 198 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 199 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 200 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 201 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 202 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 203 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 204 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 205 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 206 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 207 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 208 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 209 | ::: "r0", "cc", "memory"); 210 | } 211 | else { // else masked bit is low 212 | // WS2811 spec 500ns HIGH 213 | // Adafruit on Arduino (meas. 500ns) 214 | // This lib on Spark Core (meas. 500ns) 215 | asm volatile( 216 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 217 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 218 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 219 | ::: "r0", "cc", "memory"); 220 | // WS2811 spec 2.000us LOW 221 | // Adafruit on Arduino (meas. 2.000us) 222 | // This lib on Spark Core (meas. 2.000us) 223 | PIN_MAP[pin].gpio_peripheral->BRR = PIN_MAP[pin].gpio_pin; // LOW 224 | asm volatile( 225 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 226 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 227 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 228 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 229 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 230 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 231 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 232 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 233 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 234 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 235 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 236 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 237 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 238 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 239 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 240 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 241 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 242 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 243 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 244 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 245 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 246 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 247 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 248 | ::: "r0", "cc", "memory"); 249 | } 250 | mask >>= 1; 251 | } while ( ++j < 24 ); // ... pixel done 252 | } // end while(i) ... no more pixels 253 | } 254 | else if(type == TM1803) { // TM1803 (Radio Shack Tri-Color Strip), 400 KHz bitstream 255 | while(i) { // While bytes left... (3 bytes = 1 pixel) 256 | mask = 0x800000; // reset the mask 257 | i = i-3; // decrement bytes remaining 258 | r = *ptr++; // Next green byte value 259 | g = *ptr++; // Next red byte value 260 | b = *ptr++; // Next blue byte value 261 | c = ((uint32_t)r << 16) | ((uint32_t)g << 8) | b; // Pack the next 3 bytes to keep timing tight 262 | j = 0; // reset the 24-bit counter 263 | do { 264 | PIN_MAP[pin].gpio_peripheral->BSRR = PIN_MAP[pin].gpio_pin; // HIGH 265 | if (c & mask) { // if masked bit is high 266 | // TM1803 spec 1.36us HIGH 267 | // Pololu on Arduino (meas. 1.31us) 268 | // This lib on Spark Core (meas. 1.36us) 269 | asm volatile( 270 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 271 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 272 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 273 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 274 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 275 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 276 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 277 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 278 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 279 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 280 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 281 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 282 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 283 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 284 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 285 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 286 | ::: "r0", "cc", "memory"); 287 | // TM1803 spec 680ns LOW 288 | // Pololu on Arduino (meas. 1.024us) 289 | // This lib on Spark Core (meas. 670ns) 290 | PIN_MAP[pin].gpio_peripheral->BRR = PIN_MAP[pin].gpio_pin; // LOW 291 | asm volatile( 292 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 293 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 294 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 295 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 296 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 297 | ::: "r0", "cc", "memory"); 298 | } 299 | else { // else masked bit is low 300 | // TM1803 spec 680ns HIGH 301 | // Pololu on Arduino (meas. 374ns) 302 | // This lib on Spark Core (meas. 680ns) 303 | asm volatile( 304 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 305 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 306 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 307 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 308 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 309 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 310 | ::: "r0", "cc", "memory"); 311 | // TM1803 spec 1.36us LOW 312 | // Pololu on Arduino (meas. 2.00us) 313 | // This lib on Spark Core (meas. 1.36us) 314 | PIN_MAP[pin].gpio_peripheral->BRR = PIN_MAP[pin].gpio_pin; // LOW 315 | asm volatile( 316 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 317 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 318 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 319 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 320 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 321 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 322 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 323 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 324 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 325 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 326 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 327 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 328 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 329 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 330 | ::: "r0", "cc", "memory"); 331 | } 332 | mask >>= 1; 333 | } while ( ++j < 24 ); // ... pixel done 334 | } // end while(i) ... no more pixels 335 | } 336 | else { // must be only other option TM1829, 800 KHz bitstream 337 | while(i) { // While bytes left... (3 bytes = 1 pixel) 338 | mask = 0x800000; // reset the mask 339 | i = i-3; // decrement bytes remaining 340 | r = *ptr++; // Next red byte value 341 | b = *ptr++; // Next blue byte value 342 | g = *ptr++; // Next green byte value 343 | c = ((uint32_t)r << 16) | ((uint32_t)b << 8) | g; // Pack the next 3 bytes to keep timing tight 344 | j = 0; // reset the 24-bit counter 345 | PIN_MAP[pin].gpio_peripheral->BRR = PIN_MAP[pin].gpio_pin; // LOW 346 | for( ;; ) { // ... pixel done 347 | if (c & mask) { // if masked bit is high 348 | // TM1829 spec 800ns LOW 349 | // This lib on Spark Core (meas. 792ns) 350 | mask >>= 1; // Do this task during the long delay of this bit 351 | asm volatile( 352 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 353 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 354 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 355 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 356 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 357 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 358 | ::: "r0", "cc", "memory"); 359 | j++; 360 | // TM1829 spec 300ns HIGH 361 | // This lib on Spark Core (meas. 319ns) 362 | PIN_MAP[pin].gpio_peripheral->BSRR = PIN_MAP[pin].gpio_pin; // HIGH 363 | asm volatile( 364 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 365 | ::: "r0", "cc", "memory"); 366 | if(j==24) break; 367 | PIN_MAP[pin].gpio_peripheral->BRR = PIN_MAP[pin].gpio_pin; // LOW 368 | } 369 | else { // else masked bit is low 370 | // TM1829 spec 300ns LOW 371 | // This lib on Spark Core (meas. 306ns) 372 | asm volatile( 373 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 374 | ::: "r0", "cc", "memory"); 375 | // TM1829 spec 800ns HIGH 376 | // This lib on Spark Core (meas. 805ns) 377 | PIN_MAP[pin].gpio_peripheral->BSRR = PIN_MAP[pin].gpio_pin; // HIGH 378 | j++; 379 | mask >>= 1; // Do this task during the long delay of this bit 380 | asm volatile( 381 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 382 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 383 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 384 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 385 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 386 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 387 | ::: "r0", "cc", "memory"); 388 | if(j==24) break; 389 | PIN_MAP[pin].gpio_peripheral->BRR = PIN_MAP[pin].gpio_pin; // LOW 390 | } 391 | } 392 | } // end while(i) ... no more pixels 393 | } 394 | 395 | __enable_irq(); 396 | endTime = micros(); // Save EOD time for latch on next call 397 | } 398 | 399 | // Set the output pin number 400 | void Adafruit_NeoPixel::setPin(uint8_t p) { 401 | pinMode(pin, INPUT); 402 | pin = p; 403 | pinMode(p, OUTPUT); 404 | digitalWrite(p, LOW); 405 | } 406 | 407 | // Set pixel color from separate R,G,B components: 408 | void Adafruit_NeoPixel::setPixelColor( 409 | uint16_t n, uint8_t r, uint8_t g, uint8_t b) { 410 | if(n < numLEDs) { 411 | if(brightness) { // See notes in setBrightness() 412 | r = (r * brightness) >> 8; 413 | g = (g * brightness) >> 8; 414 | b = (b * brightness) >> 8; 415 | } 416 | uint8_t *p = &pixels[n * 3]; 417 | switch(type) { 418 | case WS2812B: // WS2812 & WS2812B is GRB order. 419 | *p++ = g; 420 | *p++ = r; 421 | *p = b; 422 | break; 423 | case TM1829: // TM1829 is special RBG order 424 | if(r == 255) r = 254; // 255 on RED channel causes display to be in a special mode. 425 | *p++ = r; 426 | *p++ = b; 427 | *p = g; 428 | break; 429 | case WS2811: // WS2811 is RGB order 430 | case TM1803: // TM1803 is RGB order 431 | default: // default is RGB order 432 | *p++ = r; 433 | *p++ = g; 434 | *p = b; 435 | break; 436 | } 437 | } 438 | } 439 | 440 | // Set pixel color from 'packed' 32-bit RGB color: 441 | void Adafruit_NeoPixel::setPixelColor(uint16_t n, uint32_t c) { 442 | if(n < numLEDs) { 443 | uint8_t 444 | r = (uint8_t)(c >> 16), 445 | g = (uint8_t)(c >> 8), 446 | b = (uint8_t)c; 447 | if(brightness) { // See notes in setBrightness() 448 | r = (r * brightness) >> 8; 449 | g = (g * brightness) >> 8; 450 | b = (b * brightness) >> 8; 451 | } 452 | uint8_t *p = &pixels[n * 3]; 453 | switch(type) { 454 | case WS2812B: // WS2812 & WS2812B is GRB order. 455 | *p++ = g; 456 | *p++ = r; 457 | *p = b; 458 | break; 459 | case TM1829: // TM1829 is special RBG order 460 | if(r == 255) r = 254; // 255 on RED channel causes display to be in a special mode. 461 | *p++ = r; 462 | *p++ = b; 463 | *p = g; 464 | break; 465 | case WS2811: // WS2811 is RGB order 466 | case TM1803: // TM1803 is RGB order 467 | default: // default is RGB order 468 | *p++ = r; 469 | *p++ = g; 470 | *p = b; 471 | break; 472 | } 473 | } 474 | } 475 | 476 | // Convert separate R,G,B into packed 32-bit RGB color. 477 | // Packed format is always RGB, regardless of LED strand color order. 478 | uint32_t Adafruit_NeoPixel::Color(uint8_t r, uint8_t g, uint8_t b) { 479 | return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b; 480 | } 481 | 482 | // Query color from previously-set pixel (returns packed 32-bit RGB value) 483 | uint32_t Adafruit_NeoPixel::getPixelColor(uint16_t n) const { 484 | 485 | if(n < numLEDs) { 486 | uint16_t ofs = n * 3; 487 | return (uint32_t)(pixels[ofs + 2]) | 488 | ((uint32_t)(pixels[ofs ]) << 8) | 489 | ((uint32_t)(pixels[ofs + 1]) << 16); 490 | } 491 | 492 | return 0; // Pixel # is out of bounds 493 | } 494 | 495 | uint8_t *Adafruit_NeoPixel::getPixels(void) const { 496 | return pixels; 497 | } 498 | 499 | uint16_t Adafruit_NeoPixel::numPixels(void) const { 500 | return numLEDs; 501 | } 502 | 503 | // Adjust output brightness; 0=darkest (off), 255=brightest. This does 504 | // NOT immediately affect what's currently displayed on the LEDs. The 505 | // next call to show() will refresh the LEDs at this level. However, 506 | // this process is potentially "lossy," especially when increasing 507 | // brightness. The tight timing in the WS2811/WS2812 code means there 508 | // aren't enough free cycles to perform this scaling on the fly as data 509 | // is issued. So we make a pass through the existing color data in RAM 510 | // and scale it (subsequent graphics commands also work at this 511 | // brightness level). If there's a significant step up in brightness, 512 | // the limited number of steps (quantization) in the old data will be 513 | // quite visible in the re-scaled version. For a non-destructive 514 | // change, you'll need to re-render the full strip data. C'est la vie. 515 | void Adafruit_NeoPixel::setBrightness(uint8_t b) { 516 | // Stored brightness value is different than what's passed. 517 | // This simplifies the actual scaling math later, allowing a fast 518 | // 8x8-bit multiply and taking the MSB. 'brightness' is a uint8_t, 519 | // adding 1 here may (intentionally) roll over...so 0 = max brightness 520 | // (color values are interpreted literally; no scaling), 1 = min 521 | // brightness (off), 255 = just below max brightness. 522 | uint8_t newBrightness = b + 1; 523 | if(newBrightness != brightness) { // Compare against prior value 524 | // Brightness has changed -- re-scale existing data in RAM 525 | uint8_t c, 526 | *ptr = pixels, 527 | oldBrightness = brightness - 1; // De-wrap old brightness value 528 | uint16_t scale; 529 | if(oldBrightness == 0) scale = 0; // Avoid /0 530 | else if(b == 255) scale = 65535 / oldBrightness; 531 | else scale = (((uint16_t)newBrightness << 8) - 1) / oldBrightness; 532 | for(uint16_t i=0; i> 8; 535 | } 536 | brightness = newBrightness; 537 | } 538 | } 539 | -------------------------------------------------------------------------------- /Native/SnowGlobe/neopixel.cpp: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------- 2 | Spark Core library to control WS2811/WS2812 based RGB 3 | LED devices such as Adafruit NeoPixel strips. 4 | Currently handles 800 KHz and 400kHz bitstream on Spark Core, 5 | WS2812, WS2812B and WS2811. 6 | 7 | Also supports: 8 | - Radio Shack Tri-Color Strip with TM1803 controller 400kHz bitstream. 9 | - TM1829 pixels 10 | 11 | Written by Phil Burgess / Paint Your Dragon for Adafruit Industries. 12 | Modified to work with Spark Core by Technobly. 13 | Contributions by PJRC and other members of the open source community. 14 | 15 | Adafruit invests time and resources providing this open source code, 16 | please support Adafruit and open-source hardware by purchasing products 17 | from Adafruit! 18 | --------------------------------------------------------------------*/ 19 | 20 | /* ======================= Adafruit_NeoPixel.cpp ======================= */ 21 | /*------------------------------------------------------------------------- 22 | This file is part of the Adafruit NeoPixel library. 23 | 24 | NeoPixel is free software: you can redistribute it and/or modify 25 | it under the terms of the GNU Lesser General Public License as 26 | published by the Free Software Foundation, either version 3 of 27 | the License, or (at your option) any later version. 28 | 29 | NeoPixel is distributed in the hope that it will be useful, 30 | but WITHOUT ANY WARRANTY; without even the implied warranty of 31 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 32 | GNU Lesser General Public License for more details. 33 | 34 | You should have received a copy of the GNU Lesser General Public 35 | License along with NeoPixel. If not, see 36 | . 37 | -------------------------------------------------------------------------*/ 38 | 39 | #include "neopixel.h" 40 | 41 | Adafruit_NeoPixel::Adafruit_NeoPixel(uint16_t n, uint8_t p, uint8_t t) : \ 42 | numLEDs(n), numBytes(n*3), type(t), pin(p), pixels(NULL) 43 | { 44 | if((pixels = (uint8_t *)malloc(numBytes))) { 45 | memset(pixels, 0, numBytes); 46 | } 47 | } 48 | 49 | Adafruit_NeoPixel::~Adafruit_NeoPixel() { 50 | if(pixels) free(pixels); 51 | pinMode(pin, INPUT); 52 | } 53 | 54 | void Adafruit_NeoPixel::begin(void) { 55 | pinMode(pin, OUTPUT); 56 | digitalWrite(pin, LOW); 57 | } 58 | 59 | void Adafruit_NeoPixel::show(void) { 60 | if(!pixels) return; 61 | 62 | // Data latch = 24 or 50 microsecond pause in the output stream. Rather than 63 | // put a delay at the end of the function, the ending time is noted and 64 | // the function will simply hold off (if needed) on issuing the 65 | // subsequent round of data until the latch time has elapsed. This 66 | // allows the mainline code to start generating the next frame of data 67 | // rather than stalling for the latch. 68 | uint32_t wait_time; // wait time in microseconds. 69 | switch(type) { 70 | case TM1803: // TM1803 = 24us reset pulse 71 | wait_time = 24L; 72 | break; 73 | case TM1829: // TM1829 = 500us reset pulse 74 | wait_time = 500L; 75 | break; 76 | case WS2812B: // WS2812 & WS2812B = 50us reset pulse 77 | case WS2811: // WS2811 = 50us reset pulse 78 | default: // default = 50us reset pulse 79 | wait_time = 50L; 80 | break; 81 | } 82 | while((micros() - endTime) < wait_time); 83 | // endTime is a private member (rather than global var) so that multiple 84 | // instances on different pins can be quickly issued in succession (each 85 | // instance doesn't delay the next). 86 | 87 | __disable_irq(); // Need 100% focus on instruction timing 88 | 89 | volatile uint32_t 90 | c, // 24-bit pixel color 91 | mask; // 8-bit mask 92 | volatile uint16_t i = numBytes; // Output loop counter 93 | volatile uint8_t 94 | j, // 8-bit inner loop counter 95 | *ptr = pixels, // Pointer to next byte 96 | g, // Current green byte value 97 | r, // Current red byte value 98 | b; // Current blue byte value 99 | 100 | if(type == WS2812B) { // same as WS2812, 800 KHz bitstream 101 | while(i) { // While bytes left... (3 bytes = 1 pixel) 102 | mask = 0x800000; // reset the mask 103 | i = i-3; // decrement bytes remaining 104 | g = *ptr++; // Next green byte value 105 | r = *ptr++; // Next red byte value 106 | b = *ptr++; // Next blue byte value 107 | c = ((uint32_t)g << 16) | ((uint32_t)r << 8) | b; // Pack the next 3 bytes to keep timing tight 108 | j = 0; // reset the 24-bit counter 109 | do { 110 | PIN_MAP[pin].gpio_peripheral->BSRR = PIN_MAP[pin].gpio_pin; // HIGH 111 | if (c & mask) { // if masked bit is high 112 | // WS2812 spec 700ns HIGH 113 | // Adafruit on Arduino (meas. 812ns) 114 | // This lib on Spark Core (meas. 792ns) 115 | asm volatile( 116 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 117 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 118 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 119 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 120 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 121 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 122 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 123 | "nop" "\n\t" 124 | ::: "r0", "cc", "memory"); 125 | // WS2812 spec 600ns LOW 126 | // Adafruit on Arduino (meas. 436ns) 127 | // This lib on Spark Core (meas. 472ns) 128 | PIN_MAP[pin].gpio_peripheral->BRR = PIN_MAP[pin].gpio_pin; // LOW 129 | asm volatile( 130 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 131 | "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 132 | ::: "r0", "cc", "memory"); 133 | } 134 | else { // else masked bit is low 135 | // WS2812 spec 350ns HIGH 136 | // Adafruit on Arduino (meas. 312ns) 137 | // This lib on Spark Core (meas. 306ns) 138 | asm volatile( 139 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 140 | ::: "r0", "cc", "memory"); 141 | // WS2812 spec 800ns LOW 142 | // Adafruit on Arduino (meas. 938ns) 143 | // This lib on Spark Core (meas. 932ns) 144 | PIN_MAP[pin].gpio_peripheral->BRR = PIN_MAP[pin].gpio_pin; // LOW 145 | asm volatile( 146 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 147 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 148 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 149 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 150 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 151 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 152 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 153 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 154 | ::: "r0", "cc", "memory"); 155 | } 156 | mask >>= 1; 157 | } while ( ++j < 24 ); // ... pixel done 158 | } // end while(i) ... no more pixels 159 | } 160 | else if(type == WS2811) { // WS2811, 400 KHz bitstream 161 | while(i) { // While bytes left... (3 bytes = 1 pixel) 162 | mask = 0x800000; // reset the mask 163 | i = i-3; // decrement bytes remaining 164 | r = *ptr++; // Next red byte value 165 | g = *ptr++; // Next green byte value 166 | b = *ptr++; // Next blue byte value 167 | c = ((uint32_t)r << 16) | ((uint32_t)g << 8) | b; // Pack the next 3 bytes to keep timing tight 168 | j = 0; // reset the 24-bit counter 169 | do { 170 | PIN_MAP[pin].gpio_peripheral->BSRR = PIN_MAP[pin].gpio_pin; // HIGH 171 | if (c & mask) { // if masked bit is high 172 | // WS2811 spec 1.20us HIGH 173 | // Adafruit on Arduino (meas. 1.25us) 174 | // This lib on Spark Core (meas. 1.25us) 175 | asm volatile( 176 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 177 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 178 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 179 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 180 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 181 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 182 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 183 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 184 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 185 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 186 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 187 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 188 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 189 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 190 | ::: "r0", "cc", "memory"); 191 | // WS2811 spec 1.30us LOW 192 | // Adafruit on Arduino (meas. 1.25us) 193 | // This lib on Spark Core (meas. 1.25us) 194 | PIN_MAP[pin].gpio_peripheral->BRR = PIN_MAP[pin].gpio_pin; // LOW 195 | asm volatile( 196 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 197 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 198 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 199 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 200 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 201 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 202 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 203 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 204 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 205 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 206 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 207 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 208 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 209 | ::: "r0", "cc", "memory"); 210 | } 211 | else { // else masked bit is low 212 | // WS2811 spec 500ns HIGH 213 | // Adafruit on Arduino (meas. 500ns) 214 | // This lib on Spark Core (meas. 500ns) 215 | asm volatile( 216 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 217 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 218 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 219 | ::: "r0", "cc", "memory"); 220 | // WS2811 spec 2.000us LOW 221 | // Adafruit on Arduino (meas. 2.000us) 222 | // This lib on Spark Core (meas. 2.000us) 223 | PIN_MAP[pin].gpio_peripheral->BRR = PIN_MAP[pin].gpio_pin; // LOW 224 | asm volatile( 225 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 226 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 227 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 228 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 229 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 230 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 231 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 232 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 233 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 234 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 235 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 236 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 237 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 238 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 239 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 240 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 241 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 242 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 243 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 244 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 245 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 246 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 247 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 248 | ::: "r0", "cc", "memory"); 249 | } 250 | mask >>= 1; 251 | } while ( ++j < 24 ); // ... pixel done 252 | } // end while(i) ... no more pixels 253 | } 254 | else if(type == TM1803) { // TM1803 (Radio Shack Tri-Color Strip), 400 KHz bitstream 255 | while(i) { // While bytes left... (3 bytes = 1 pixel) 256 | mask = 0x800000; // reset the mask 257 | i = i-3; // decrement bytes remaining 258 | r = *ptr++; // Next green byte value 259 | g = *ptr++; // Next red byte value 260 | b = *ptr++; // Next blue byte value 261 | c = ((uint32_t)r << 16) | ((uint32_t)g << 8) | b; // Pack the next 3 bytes to keep timing tight 262 | j = 0; // reset the 24-bit counter 263 | do { 264 | PIN_MAP[pin].gpio_peripheral->BSRR = PIN_MAP[pin].gpio_pin; // HIGH 265 | if (c & mask) { // if masked bit is high 266 | // TM1803 spec 1.36us HIGH 267 | // Pololu on Arduino (meas. 1.31us) 268 | // This lib on Spark Core (meas. 1.36us) 269 | asm volatile( 270 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 271 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 272 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 273 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 274 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 275 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 276 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 277 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 278 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 279 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 280 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 281 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 282 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 283 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 284 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 285 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 286 | ::: "r0", "cc", "memory"); 287 | // TM1803 spec 680ns LOW 288 | // Pololu on Arduino (meas. 1.024us) 289 | // This lib on Spark Core (meas. 670ns) 290 | PIN_MAP[pin].gpio_peripheral->BRR = PIN_MAP[pin].gpio_pin; // LOW 291 | asm volatile( 292 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 293 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 294 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 295 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 296 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 297 | ::: "r0", "cc", "memory"); 298 | } 299 | else { // else masked bit is low 300 | // TM1803 spec 680ns HIGH 301 | // Pololu on Arduino (meas. 374ns) 302 | // This lib on Spark Core (meas. 680ns) 303 | asm volatile( 304 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 305 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 306 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 307 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 308 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 309 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 310 | ::: "r0", "cc", "memory"); 311 | // TM1803 spec 1.36us LOW 312 | // Pololu on Arduino (meas. 2.00us) 313 | // This lib on Spark Core (meas. 1.36us) 314 | PIN_MAP[pin].gpio_peripheral->BRR = PIN_MAP[pin].gpio_pin; // LOW 315 | asm volatile( 316 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 317 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 318 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 319 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 320 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 321 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 322 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 323 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 324 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 325 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 326 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 327 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 328 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 329 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 330 | ::: "r0", "cc", "memory"); 331 | } 332 | mask >>= 1; 333 | } while ( ++j < 24 ); // ... pixel done 334 | } // end while(i) ... no more pixels 335 | } 336 | else { // must be only other option TM1829, 800 KHz bitstream 337 | while(i) { // While bytes left... (3 bytes = 1 pixel) 338 | mask = 0x800000; // reset the mask 339 | i = i-3; // decrement bytes remaining 340 | r = *ptr++; // Next red byte value 341 | b = *ptr++; // Next blue byte value 342 | g = *ptr++; // Next green byte value 343 | c = ((uint32_t)r << 16) | ((uint32_t)b << 8) | g; // Pack the next 3 bytes to keep timing tight 344 | j = 0; // reset the 24-bit counter 345 | PIN_MAP[pin].gpio_peripheral->BRR = PIN_MAP[pin].gpio_pin; // LOW 346 | for( ;; ) { // ... pixel done 347 | if (c & mask) { // if masked bit is high 348 | // TM1829 spec 800ns LOW 349 | // This lib on Spark Core (meas. 792ns) 350 | mask >>= 1; // Do this task during the long delay of this bit 351 | asm volatile( 352 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 353 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 354 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 355 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 356 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 357 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 358 | ::: "r0", "cc", "memory"); 359 | j++; 360 | // TM1829 spec 300ns HIGH 361 | // This lib on Spark Core (meas. 319ns) 362 | PIN_MAP[pin].gpio_peripheral->BSRR = PIN_MAP[pin].gpio_pin; // HIGH 363 | asm volatile( 364 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 365 | ::: "r0", "cc", "memory"); 366 | if(j==24) break; 367 | PIN_MAP[pin].gpio_peripheral->BRR = PIN_MAP[pin].gpio_pin; // LOW 368 | } 369 | else { // else masked bit is low 370 | // TM1829 spec 300ns LOW 371 | // This lib on Spark Core (meas. 306ns) 372 | asm volatile( 373 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 374 | ::: "r0", "cc", "memory"); 375 | // TM1829 spec 800ns HIGH 376 | // This lib on Spark Core (meas. 805ns) 377 | PIN_MAP[pin].gpio_peripheral->BSRR = PIN_MAP[pin].gpio_pin; // HIGH 378 | j++; 379 | mask >>= 1; // Do this task during the long delay of this bit 380 | asm volatile( 381 | "mov r0, r0" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 382 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 383 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 384 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 385 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 386 | "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" "nop" "\n\t" 387 | ::: "r0", "cc", "memory"); 388 | if(j==24) break; 389 | PIN_MAP[pin].gpio_peripheral->BRR = PIN_MAP[pin].gpio_pin; // LOW 390 | } 391 | } 392 | } // end while(i) ... no more pixels 393 | } 394 | 395 | __enable_irq(); 396 | endTime = micros(); // Save EOD time for latch on next call 397 | } 398 | 399 | // Set the output pin number 400 | void Adafruit_NeoPixel::setPin(uint8_t p) { 401 | pinMode(pin, INPUT); 402 | pin = p; 403 | pinMode(p, OUTPUT); 404 | digitalWrite(p, LOW); 405 | } 406 | 407 | // Set pixel color from separate R,G,B components: 408 | void Adafruit_NeoPixel::setPixelColor( 409 | uint16_t n, uint8_t r, uint8_t g, uint8_t b) { 410 | if(n < numLEDs) { 411 | if(brightness) { // See notes in setBrightness() 412 | r = (r * brightness) >> 8; 413 | g = (g * brightness) >> 8; 414 | b = (b * brightness) >> 8; 415 | } 416 | uint8_t *p = &pixels[n * 3]; 417 | switch(type) { 418 | case WS2812B: // WS2812 & WS2812B is GRB order. 419 | *p++ = g; 420 | *p++ = r; 421 | *p = b; 422 | break; 423 | case TM1829: // TM1829 is special RBG order 424 | if(r == 255) r = 254; // 255 on RED channel causes display to be in a special mode. 425 | *p++ = r; 426 | *p++ = b; 427 | *p = g; 428 | break; 429 | case WS2811: // WS2811 is RGB order 430 | case TM1803: // TM1803 is RGB order 431 | default: // default is RGB order 432 | *p++ = r; 433 | *p++ = g; 434 | *p = b; 435 | break; 436 | } 437 | } 438 | } 439 | 440 | // Set pixel color from 'packed' 32-bit RGB color: 441 | void Adafruit_NeoPixel::setPixelColor(uint16_t n, uint32_t c) { 442 | if(n < numLEDs) { 443 | uint8_t 444 | r = (uint8_t)(c >> 16), 445 | g = (uint8_t)(c >> 8), 446 | b = (uint8_t)c; 447 | if(brightness) { // See notes in setBrightness() 448 | r = (r * brightness) >> 8; 449 | g = (g * brightness) >> 8; 450 | b = (b * brightness) >> 8; 451 | } 452 | uint8_t *p = &pixels[n * 3]; 453 | switch(type) { 454 | case WS2812B: // WS2812 & WS2812B is GRB order. 455 | *p++ = g; 456 | *p++ = r; 457 | *p = b; 458 | break; 459 | case TM1829: // TM1829 is special RBG order 460 | if(r == 255) r = 254; // 255 on RED channel causes display to be in a special mode. 461 | *p++ = r; 462 | *p++ = b; 463 | *p = g; 464 | break; 465 | case WS2811: // WS2811 is RGB order 466 | case TM1803: // TM1803 is RGB order 467 | default: // default is RGB order 468 | *p++ = r; 469 | *p++ = g; 470 | *p = b; 471 | break; 472 | } 473 | } 474 | } 475 | 476 | // Convert separate R,G,B into packed 32-bit RGB color. 477 | // Packed format is always RGB, regardless of LED strand color order. 478 | uint32_t Adafruit_NeoPixel::Color(uint8_t r, uint8_t g, uint8_t b) { 479 | return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b; 480 | } 481 | 482 | // Query color from previously-set pixel (returns packed 32-bit RGB value) 483 | uint32_t Adafruit_NeoPixel::getPixelColor(uint16_t n) const { 484 | 485 | if(n < numLEDs) { 486 | uint16_t ofs = n * 3; 487 | return (uint32_t)(pixels[ofs + 2]) | 488 | ((uint32_t)(pixels[ofs ]) << 8) | 489 | ((uint32_t)(pixels[ofs + 1]) << 16); 490 | } 491 | 492 | return 0; // Pixel # is out of bounds 493 | } 494 | 495 | uint8_t *Adafruit_NeoPixel::getPixels(void) const { 496 | return pixels; 497 | } 498 | 499 | uint16_t Adafruit_NeoPixel::numPixels(void) const { 500 | return numLEDs; 501 | } 502 | 503 | // Adjust output brightness; 0=darkest (off), 255=brightest. This does 504 | // NOT immediately affect what's currently displayed on the LEDs. The 505 | // next call to show() will refresh the LEDs at this level. However, 506 | // this process is potentially "lossy," especially when increasing 507 | // brightness. The tight timing in the WS2811/WS2812 code means there 508 | // aren't enough free cycles to perform this scaling on the fly as data 509 | // is issued. So we make a pass through the existing color data in RAM 510 | // and scale it (subsequent graphics commands also work at this 511 | // brightness level). If there's a significant step up in brightness, 512 | // the limited number of steps (quantization) in the old data will be 513 | // quite visible in the re-scaled version. For a non-destructive 514 | // change, you'll need to re-render the full strip data. C'est la vie. 515 | void Adafruit_NeoPixel::setBrightness(uint8_t b) { 516 | // Stored brightness value is different than what's passed. 517 | // This simplifies the actual scaling math later, allowing a fast 518 | // 8x8-bit multiply and taking the MSB. 'brightness' is a uint8_t, 519 | // adding 1 here may (intentionally) roll over...so 0 = max brightness 520 | // (color values are interpreted literally; no scaling), 1 = min 521 | // brightness (off), 255 = just below max brightness. 522 | uint8_t newBrightness = b + 1; 523 | if(newBrightness != brightness) { // Compare against prior value 524 | // Brightness has changed -- re-scale existing data in RAM 525 | uint8_t c, 526 | *ptr = pixels, 527 | oldBrightness = brightness - 1; // De-wrap old brightness value 528 | uint16_t scale; 529 | if(oldBrightness == 0) scale = 0; // Avoid /0 530 | else if(b == 255) scale = 65535 / oldBrightness; 531 | else scale = (((uint16_t)newBrightness << 8) - 1) / oldBrightness; 532 | for(uint16_t i=0; i> 8; 535 | } 536 | brightness = newBrightness; 537 | } 538 | } 539 | -------------------------------------------------------------------------------- /Native/Demo/demo.ino: -------------------------------------------------------------------------------- 1 | #include "neopixel/neopixel.h" 2 | #include 3 | //set up the pin that controls the LEDs, the type of LEDs (WS2812B) and the number of LEDs in the cube (8*8*8=512) 4 | #define PIXEL_PIN D0 5 | #define PIXEL_COUNT 512 6 | #define PIXEL_TYPE WS2812B 7 | #define SIDE 8 8 | 9 | SYSTEM_MODE(SEMI_AUTOMATIC); //don't connect to the internet on boot 10 | #define BUTTON D2 //press this button to connect to the internet 11 | #define MODE D3 12 | #define MICROPHONE 12 13 | #define GAIN_CONTROL 11 14 | #define MAX_POINTS 20 15 | #define SPEED 0.22 16 | #define MIN_SALVO_SPACING 100 17 | 18 | bool onlinePressed=false; 19 | bool lastOnline=true; 20 | 21 | #define FIREWORKS 0 22 | #define PLASMA 1 23 | #define SQUARRAL 2 24 | #define PURPLE_RAIN 3 25 | #define DEMO_ROUTINES 4 26 | 27 | /* datatype definitions 28 | */ 29 | 30 | typedef struct{ 31 | unsigned char red, green, blue; 32 | } color; 33 | 34 | typedef struct{ 35 | float x; 36 | float y; 37 | float z; 38 | } point; 39 | 40 | typedef struct{ 41 | point raindrops[MAX_POINTS]; 42 | bool dead; 43 | } salvo; 44 | 45 | /****************************** 46 | * function definitions 47 | * ***************************/ 48 | void background(color col); 49 | color getPixel(int x, int y, int z); 50 | void setPixel(int x, int y, int z, color col); 51 | color colorMap(float val, float min, float max); 52 | color lerpColor(color a, color b, int val, int min, int max); 53 | void add(point& a, point& b); 54 | 55 | 56 | /****************************** 57 | * fireworks variables * 58 | * ****************************/ 59 | color black; 60 | int centerX, centerY, centerZ; 61 | int launchX, launchZ; 62 | int red, green, blue; 63 | int brightness=35; 64 | float radius=0; 65 | float speed; 66 | bool showRocket; 67 | bool exploded; 68 | float xInc, yInc, zInc; 69 | float rocketX, rocketY, rocketZ; 70 | float launchTime; 71 | int maxSize; 72 | color rocketColor, fireworkColor; 73 | 74 | 75 | 76 | /********************************* 77 | * squarral variables * 78 | * ******************************/ 79 | 80 | #define TRAIL_LENGTH 50 81 | 82 | int frame=0; 83 | color pixelColor; 84 | point position, increment, pixel; 85 | point trailPoints[TRAIL_LENGTH]; 86 | int posX, posY, posZ; 87 | int incX, incY, incZ; 88 | int squarral_zInc=1; 89 | int bound=0; 90 | int boundInc=1; 91 | unsigned char axis=0; 92 | bool rainbow=true; 93 | 94 | //maxBrightness is the brightness limit for each pixel. All color data will be scaled down 95 | //so that the largest value is maxBrightness 96 | int maxBrightness=50; 97 | 98 | /******************************** 99 | * zplasma variables * 100 | * *****************************/ 101 | float phase = 0.0; 102 | float phaseIncrement = 0.035; // Controls the speed of the moving points. Higher == faster 103 | float colorStretch = 0.23; // Higher numbers will produce tighter color bands 104 | float plasmaBrightness = 0.2; 105 | color plasmaColor; 106 | 107 | /********************************* 108 | * purple rain variables * 109 | * *******************************/ 110 | int threshhold; 111 | int max; 112 | int min; 113 | float sensitivity=0.5; 114 | int maxAmplitude=0; 115 | bool aboveThreshhold=false; 116 | int timeAboveThreshhold; 117 | color rainColor; 118 | salvo salvos[SIDE]; 119 | 120 | /********************************** 121 | * flip variables * 122 | * ********************************/ 123 | //accelerometer pinout 124 | #define X 13 125 | #define Y 14 126 | #define Z 15 127 | #define AUTOCYCLE_TIME 15000 128 | #define FACEPLANT 2300 129 | #define UPSIDE_DOWN 1850 130 | #define RIGHTSIDE_UP 2400 131 | #define LEFT_SIDE 1800 132 | #define RIGHT_SIDE 2400 133 | #define FLIP_TIMEOUT 3000 134 | #define FLIP_DEBOUNCE 250 135 | 136 | long lastFaceplant=-1*FLIP_TIMEOUT; 137 | bool upsideDown=false; 138 | bool sideways=false; 139 | bool autoCycle=true; //start on autocycle by default 140 | int upsideDownTime=-1*FLIP_TIMEOUT; 141 | long lastAutoCycle=0; 142 | int lastLeft=-1*FLIP_TIMEOUT; 143 | int lastRight=-1*FLIP_TIMEOUT; 144 | int accelerometer[3]; 145 | long lastChange=0; 146 | 147 | 148 | 149 | int demo=FIREWORKS; 150 | 151 | Adafruit_NeoPixel strip=Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE); 152 | 153 | int frameCount=0; 154 | 155 | /******************************* 156 | * fade variables * 157 | * ****************************/ 158 | bool fading=false; 159 | int fadeValue=255; 160 | int fadeSpeed=2; 161 | 162 | 163 | void setup() { 164 | pinMode(7,OUTPUT); 165 | digitalWrite(7, HIGH); 166 | // seed the random number generator. THINGS WILL NEVER BE THE SAME AGAIN 167 | uint32_t seed = millis(); 168 | srand(seed); 169 | // Serial.begin(115200); 170 | initCube(); 171 | initCloudButton(); 172 | initSquarral(); 173 | initFireworks(); 174 | initMicrophone(); 175 | initSalvos(); 176 | 177 | } 178 | 179 | //sets up the online/offline switch 180 | void initCloudButton() 181 | { 182 | //set the input mode for the 'connect to cloud' button 183 | pinMode(BUTTON, INPUT_PULLUP); 184 | pinMode(MODE, INPUT_PULLUP); 185 | if(!digitalRead(MODE)) 186 | WiFi.listen(); 187 | //a.k.a. onlinePressed is HIGH when the switch is set to 'online' and LOW when the switch is set to 'offline' 188 | onlinePressed=digitalRead(BUTTON); 189 | if(onlinePressed) 190 | Spark.connect(); 191 | } 192 | 193 | //checks to see if the 'online/offline' switch is switched 194 | void checkCloudButton() 195 | { 196 | //if the 'connect to cloud' button is pressed, try to connect to wifi. 197 | //otherwise, run the program 198 | //note -- how does this behave when there are no wifi credentials loaded on the spark? 199 | 200 | //onlinePressed is HIGH when the switch is _not_ connected and LOW when the switch is connected 201 | //a.k.a. onlinePressed is HIGH when the switch is set to 'online' and LOW when the switch is set to 'offline' 202 | onlinePressed=digitalRead(BUTTON); 203 | 204 | if((!onlinePressed)&&(lastOnline)) //marked as 'online' 205 | { 206 | lastOnline=onlinePressed; 207 | Spark.connect(); 208 | } 209 | 210 | else if((onlinePressed)&&(!lastOnline)) //marked as 'offline' 211 | { 212 | lastOnline=onlinePressed; 213 | Spark.disconnect(); 214 | } 215 | 216 | lastOnline=onlinePressed; 217 | 218 | if(!digitalRead(MODE)) 219 | WiFi.listen(); 220 | } 221 | 222 | void loop() { 223 | //if the 'connect to cloud' button is pressed, try to connect to wifi. 224 | //otherwise, run the program 225 | checkCloudButton(); 226 | 227 | 228 | if(fading) 229 | fade(); 230 | else 231 | { 232 | background(black); 233 | switch(demo) 234 | { 235 | case(FIREWORKS): 236 | updateFireworks(); 237 | break; 238 | case(PLASMA): 239 | zPlasma(); 240 | break; 241 | case(SQUARRAL): 242 | squarral(); 243 | break; 244 | case(PURPLE_RAIN): 245 | purpleRain(); 246 | break; 247 | 248 | } 249 | frameCount++; 250 | } 251 | //check to see how if the cube has been flipped 252 | checkFlipState(); 253 | 254 | strip.show(); 255 | 256 | if(fading) 257 | { 258 | fadeValue-=fadeSpeed; 259 | //if we're done fading) 260 | if(fadeValue<=0) 261 | { 262 | fading=false; 263 | fadeValue=255; 264 | } 265 | else 266 | fade(); 267 | } 268 | } 269 | 270 | void fade() 271 | { 272 | color pixelColor; 273 | for(int x=0;x0) 279 | pixelColor.red--; 280 | if(pixelColor.green>0) 281 | pixelColor.green--; 282 | if(pixelColor.blue>0) 283 | pixelColor.blue--; 284 | setPixel(x,y,z, pixelColor); 285 | } 286 | } 287 | 288 | 289 | //sets a pixel at position (x,y,z) to the col parameter's color 290 | void setPixel(int x, int y, int z, color col) 291 | { 292 | int index = (z*SIDE*SIDE) + (x*SIDE) + y; 293 | strip.setPixelColor(index,strip.Color(col.red, col.green, col.blue)); 294 | 295 | } 296 | 297 | 298 | //returns the color value currently displayed at the x,y,z location 299 | color getPixel(int x, int y, int z) 300 | { 301 | int index = (z*SIDE*SIDE) + (x*SIDE) + y; 302 | uint32_t col=strip.getPixelColor(index); 303 | color pixelColor; 304 | pixelColor.red=(col>>16)&255; 305 | pixelColor.green=(col>>8)&255; 306 | pixelColor.blue=col&255; 307 | return pixelColor; 308 | } 309 | 310 | void initCube() 311 | { 312 | black.red=0; 313 | black.green=0; 314 | black.blue=0; 315 | } 316 | 317 | 318 | void background(color col) 319 | { 320 | for(int x=0;xmaxSize) 357 | prepRocket(); 358 | if(abs(distance(centerX,centerY,centerZ,rocketX, rocketY, rocketZ)-radius)<2) 359 | { 360 | showRocket=false; 361 | exploded=true; 362 | } 363 | 364 | } 365 | 366 | float distance(float x, float y, float z, float x1, float y1, float z1) 367 | { 368 | return(sqrt(pow(x-x1,2)+pow(y-y1,2)+pow(z-z1,2))); 369 | } 370 | 371 | void prepRocket() 372 | { 373 | radius=0; 374 | centerX=rand()%8; 375 | centerY=rand()%8; 376 | centerZ=rand()%8; 377 | fireworkColor.red=rand()%brightness; 378 | fireworkColor.green=rand()%brightness; 379 | fireworkColor.blue=rand()%brightness; 380 | launchX=rand()%8; 381 | launchZ=rand()%8; 382 | rocketX=launchX; 383 | rocketY=0; 384 | rocketZ=launchZ; 385 | launchTime=15+rand()%25; 386 | xInc=(centerX-rocketX)/launchTime; 387 | yInc=(centerY-rocketY)/launchTime; 388 | zInc=(centerZ-rocketZ)/launchTime; 389 | showRocket=true; 390 | exploded=false; 391 | speed=0.15; 392 | maxSize=2+rand()%6; 393 | //speed=rand()%5; 394 | //speed*=0.1; 395 | } 396 | 397 | void initFireworks() 398 | { 399 | rocketColor.red=255; 400 | rocketColor.green=150; 401 | rocketColor.blue=100; 402 | prepRocket(); 403 | } 404 | 405 | void initSquarral() 406 | { 407 | position={0,0,0}; 408 | increment={1,0,0}; 409 | } 410 | 411 | void squarral() 412 | { 413 | add(position, increment); 414 | if((increment.x==1)&&(position.x==SIDE-1-bound)) 415 | increment={0,1,0}; 416 | if((increment.x==-1)&&(position.x==bound)) 417 | increment={0,-1,0}; 418 | if((increment.y==1)&&(position.y==SIDE-1-bound)) 419 | increment={-1,0,0}; 420 | if((increment.y==-1)&&(position.y==bound)) 421 | { 422 | increment={1,0,0}; 423 | position.z+=squarral_zInc; 424 | bound+=boundInc; 425 | if((position.z==3)&&(squarral_zInc>0)) 426 | boundInc=0; 427 | if((position.z==4)&&(squarral_zInc>0)) 428 | boundInc=-1; 429 | if((position.z==3)&&(squarral_zInc<0)) 430 | boundInc=-1; 431 | if((position.z==4)&&(squarral_zInc<0)) 432 | boundInc=0; 433 | 434 | if((position.z==0)||(position.z==SIDE-1)) 435 | boundInc*=-1; 436 | 437 | if((position.z==SIDE-1)||(position.z==0)) 438 | { 439 | squarral_zInc*=-1; 440 | if(squarral_zInc==1) 441 | { 442 | axis=rand()%6; 443 | if(rand()%5==0) 444 | rainbow=true; 445 | else 446 | rainbow=false; 447 | } 448 | } 449 | } 450 | 451 | posX=position.x; 452 | posY=position.y; 453 | posZ=position.z; 454 | 455 | incX=increment.x; 456 | incY=increment.y; 457 | incZ=increment.z; 458 | 459 | for(int i=TRAIL_LENGTH-1;i>0;i--) 460 | { 461 | trailPoints[i].x=trailPoints[i-1].x; 462 | trailPoints[i].y=trailPoints[i-1].y; 463 | trailPoints[i].z=trailPoints[i-1].z; 464 | } 465 | trailPoints[0].x=pixel.x; 466 | trailPoints[0].y=pixel.y; 467 | trailPoints[0].z=pixel.z; 468 | switch(axis){ 469 | case(0): 470 | pixel.x=position.x; 471 | pixel.y=position.y; 472 | pixel.z=position.z; 473 | break; 474 | case(1): 475 | pixel.x=position.z; 476 | pixel.y=position.x; 477 | pixel.z=position.y; 478 | break; 479 | case(2): 480 | pixel.x=position.y; 481 | pixel.y=position.z; 482 | pixel.z=position.x; 483 | break; 484 | case(3): 485 | pixel.x=position.z; 486 | pixel.y=SIDE-1-position.x; 487 | pixel.z=position.y; 488 | break; 489 | case(4): 490 | pixel.x=position.y; 491 | pixel.y=position.z; 492 | pixel.z=SIDE-1-position.x; 493 | break; 494 | case(5): 495 | pixel.x=position.x; 496 | pixel.y=SIDE-1-position.y; 497 | pixel.z=position.z; 498 | break; 499 | } 500 | 501 | pixelColor=colorMap(frame%1000,0,1000); 502 | setPixel((int)pixel.x, (int)pixel.y, (int)pixel.z, pixelColor); 503 | for(int i=0;imax) 671 | max=mic; 672 | float range=max-min; 673 | int mean=(max-min)/2; 674 | /* 675 | if(minmean) 678 | max--; 679 | */ 680 | threshhold=mean+sensitivity*(range/2); 681 | 682 | if(mic>threshhold) 683 | { 684 | if((!aboveThreshhold)&&((timeAboveThreshhold-millis())>MIN_SALVO_SPACING)) 685 | { 686 | launchRain(mic-threshhold); 687 | aboveThreshhold=true; 688 | timeAboveThreshhold=millis(); 689 | } 690 | } 691 | else 692 | aboveThreshhold=false; 693 | /* 694 | Serial.print(mic); 695 | Serial.print(": "); 696 | Serial.print(threshhold); 697 | Serial.print(" - above threshhold: "); 698 | Serial.println(aboveThreshhold); 699 | */ 700 | } 701 | 702 | void launchRain(int amplitude) 703 | { 704 | int i; 705 | for(i=0;((imaxAmplitude) 710 | maxAmplitude=amplitude; 711 | int numDrops=map(amplitude,0, maxAmplitude,0, MAX_POINTS); 712 | for(int j=0;jFACEPLANT) //if the cube is upside-down, set the upside-down flag and mark the time when it was flipped 803 | { 804 | lastFaceplant=millis(); 805 | // Serial.println("I'm upside-down!"); 806 | } 807 | if(accelerometer[1]RIGHT_SIDE) 813 | { 814 | lastRight=millis(); 815 | // Serial.println("I'm on my right side"); 816 | } 817 | 818 | if(accelerometer[2]>RIGHTSIDE_UP) 819 | { 820 | // Serial.println("whew! I'm rightside-up"); 821 | /* 822 | if(((millis()-upsideDownTime)FLIP_DEBOUNCE)) 823 | { 824 | // Serial.println("turned upside down and back"); 825 | lastChange=millis(); 826 | autoCycle=!autoCycle; 827 | upsideDownTime=millis()-FLIP_TIMEOUT; 828 | lastLeft=millis()-FLIP_TIMEOUT; //clears the left and right turns, in case the user turned it sideways 829 | lastRight=millis()-FLIP_TIMEOUT; //clears the left and right turns, in case the user turned it sideways 830 | } 831 | if(((millis()-lastLeft)FLIP_DEBOUNCE)) 832 | { 833 | // Serial.println("turned to the left and back"); 834 | lastChange=millis(); 835 | decrementDemo(); 836 | lastLeft=millis()-FLIP_TIMEOUT; 837 | } 838 | if(((millis()-lastRight)FLIP_DEBOUNCE)) 839 | { 840 | // Serial.println("turned to the right and back"); 841 | lastChange=millis(); 842 | incrementDemo(); 843 | lastRight=millis()-FLIP_TIMEOUT; 844 | } 845 | } 846 | */ 847 | if(((millis()-lastFaceplant)FLIP_DEBOUNCE)) 848 | { 849 | autoCycle=false; 850 | lastFaceplant=millis()-FLIP_TIMEOUT; 851 | color flash; 852 | flash.red=maxBrightness; 853 | flash.green=maxBrightness; 854 | flash.blue=maxBrightness; 855 | background(flash); 856 | } 857 | if(((millis()-lastLeft)FLIP_DEBOUNCE)) 858 | { 859 | // Serial.println("turned to the left and back"); 860 | autoCycle=false; 861 | lastChange=millis(); 862 | decrementDemo(); 863 | lastLeft=millis()-FLIP_TIMEOUT; 864 | } 865 | if(((millis()-lastRight)FLIP_DEBOUNCE)) 866 | { 867 | // Serial.println("turned to the right and back"); 868 | autoCycle=false; 869 | lastChange=millis(); 870 | incrementDemo(); 871 | lastRight=millis()-FLIP_TIMEOUT; 872 | } 873 | } 874 | 875 | if(autoCycle) 876 | if(millis()-lastAutoCycle>AUTOCYCLE_TIME) //in autocycle, change demos every 15 seconds 877 | { 878 | incrementDemo(); 879 | // Serial.print("autocycling...Demo is "); 880 | // Serial.println(demo); 881 | lastAutoCycle=millis(); 882 | } 883 | 884 | } 885 | 886 | void updateAccelerometer() 887 | { 888 | for(int i=0;i<3;i++) 889 | accelerometer[i]=analogRead(X+i); 890 | } 891 | 892 | void setFadeSpeed() 893 | { 894 | if(autoCycle) 895 | fadeSpeed=2; 896 | else 897 | fadeSpeed=20; 898 | } 899 | 900 | void incrementDemo() 901 | { 902 | demo++; 903 | setFadeSpeed(); 904 | fading=true; 905 | if(demo>=DEMO_ROUTINES) 906 | demo=0; 907 | } 908 | 909 | void decrementDemo() 910 | { 911 | demo--; 912 | setFadeSpeed(); 913 | fading=true; 914 | if(demo<0) 915 | demo=DEMO_ROUTINES-1; 916 | } --------------------------------------------------------------------------------