A library for adafruit's neo pixel ring and Addressable LED.

Dependencies:   PololuLedStrip

Committer:
tichise
Date:
Sat Jul 21 06:21:00 2018 +0000
Revision:
0:d7a396a60bdc
new

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tichise 0:d7a396a60bdc 1 #include "TI_NEOPIXEL.h"
tichise 0:d7a396a60bdc 2 #include "mbed.h"
tichise 0:d7a396a60bdc 3
tichise 0:d7a396a60bdc 4 TI_NEOPIXEL::TI_NEOPIXEL(PinName input) : _ledStrip(input) {
tichise 0:d7a396a60bdc 5 }
tichise 0:d7a396a60bdc 6
tichise 0:d7a396a60bdc 7 void TI_NEOPIXEL::switchLightOff(int count) {
tichise 0:d7a396a60bdc 8
tichise 0:d7a396a60bdc 9 rgb_color colors[count];
tichise 0:d7a396a60bdc 10
tichise 0:d7a396a60bdc 11 for(int i = 0; i < count; i++) {
tichise 0:d7a396a60bdc 12 colors[i] = (rgb_color) {0, 0, 0};
tichise 0:d7a396a60bdc 13 }
tichise 0:d7a396a60bdc 14
tichise 0:d7a396a60bdc 15 _ledStrip.write(colors, count);
tichise 0:d7a396a60bdc 16 }
tichise 0:d7a396a60bdc 17
tichise 0:d7a396a60bdc 18 void TI_NEOPIXEL::switchLightOn(int count) {
tichise 0:d7a396a60bdc 19
tichise 0:d7a396a60bdc 20 rgb_color colors[count];
tichise 0:d7a396a60bdc 21
tichise 0:d7a396a60bdc 22 for(int i = 0; i < count; i++) {
tichise 0:d7a396a60bdc 23 colors[i] = (rgb_color) {50, 10, 170};
tichise 0:d7a396a60bdc 24 }
tichise 0:d7a396a60bdc 25
tichise 0:d7a396a60bdc 26 _ledStrip.write(colors, count);
tichise 0:d7a396a60bdc 27 }
tichise 0:d7a396a60bdc 28
tichise 0:d7a396a60bdc 29 void TI_NEOPIXEL::changeColor(int count, rgb_color rgbColor) {
tichise 0:d7a396a60bdc 30
tichise 0:d7a396a60bdc 31 rgb_color colors[count];
tichise 0:d7a396a60bdc 32
tichise 0:d7a396a60bdc 33 for(int i = 0; i < count; i++) {
tichise 0:d7a396a60bdc 34 colors[i] = rgbColor;
tichise 0:d7a396a60bdc 35 }
tichise 0:d7a396a60bdc 36
tichise 0:d7a396a60bdc 37 _ledStrip.write(colors, count);
tichise 0:d7a396a60bdc 38 }
tichise 0:d7a396a60bdc 39
tichise 0:d7a396a60bdc 40 void TI_NEOPIXEL::changePointColor(int count, rgb_color topColor, rgb_color bottomColor) {
tichise 0:d7a396a60bdc 41
tichise 0:d7a396a60bdc 42 rgb_color colors[count];
tichise 0:d7a396a60bdc 43
tichise 0:d7a396a60bdc 44 for(int i = 0; i < count; i++) {
tichise 0:d7a396a60bdc 45 if (i == 0) {
tichise 0:d7a396a60bdc 46 colors[i] = topColor;
tichise 0:d7a396a60bdc 47 } else if (i == count/2) {
tichise 0:d7a396a60bdc 48 colors[i] = bottomColor;
tichise 0:d7a396a60bdc 49 } else {
tichise 0:d7a396a60bdc 50 colors[i] = (rgb_color) {0, 0, 0};
tichise 0:d7a396a60bdc 51 }
tichise 0:d7a396a60bdc 52 }
tichise 0:d7a396a60bdc 53
tichise 0:d7a396a60bdc 54 _ledStrip.write(colors, count);
tichise 0:d7a396a60bdc 55 }
tichise 0:d7a396a60bdc 56
tichise 0:d7a396a60bdc 57 void TI_NEOPIXEL::circle(int count, rgb_color rgbColor) {
tichise 0:d7a396a60bdc 58
tichise 0:d7a396a60bdc 59 for(int j = 0; j < count; j++) {
tichise 0:d7a396a60bdc 60 rgb_color colors[count];
tichise 0:d7a396a60bdc 61
tichise 0:d7a396a60bdc 62 for(int i = 0; i < count; i++) {
tichise 0:d7a396a60bdc 63 if (j >= i) {
tichise 0:d7a396a60bdc 64 colors[i] = rgbColor;
tichise 0:d7a396a60bdc 65 } else {
tichise 0:d7a396a60bdc 66 colors[i] = (rgb_color) {0, 0, 0};
tichise 0:d7a396a60bdc 67 }
tichise 0:d7a396a60bdc 68 }
tichise 0:d7a396a60bdc 69
tichise 0:d7a396a60bdc 70 _ledStrip.write(colors, count);
tichise 0:d7a396a60bdc 71 wait(0.07);
tichise 0:d7a396a60bdc 72 }
tichise 0:d7a396a60bdc 73 }
tichise 0:d7a396a60bdc 74
tichise 0:d7a396a60bdc 75 void TI_NEOPIXEL::circleRainbow(int count) {
tichise 0:d7a396a60bdc 76
tichise 0:d7a396a60bdc 77 for(int j = 0; j < count; j++) {
tichise 0:d7a396a60bdc 78 rgb_color colors[count];
tichise 0:d7a396a60bdc 79
tichise 0:d7a396a60bdc 80 for(int i = 0; i < count; i++) {
tichise 0:d7a396a60bdc 81 if (j >= i) {
tichise 0:d7a396a60bdc 82 uint8_t phase = 256/count*i;
tichise 0:d7a396a60bdc 83 colors[i] = convertHsvToRgb(phase / 256.0, 1.0, 1.0);
tichise 0:d7a396a60bdc 84 } else {
tichise 0:d7a396a60bdc 85 colors[i] = (rgb_color) {0, 0, 0};
tichise 0:d7a396a60bdc 86 }
tichise 0:d7a396a60bdc 87 }
tichise 0:d7a396a60bdc 88
tichise 0:d7a396a60bdc 89 _ledStrip.write(colors, count);
tichise 0:d7a396a60bdc 90 wait(0.07);
tichise 0:d7a396a60bdc 91 }
tichise 0:d7a396a60bdc 92 }
tichise 0:d7a396a60bdc 93
tichise 0:d7a396a60bdc 94 rgb_color TI_NEOPIXEL::convertHsvToRgb(float h, float s, float v)
tichise 0:d7a396a60bdc 95 {
tichise 0:d7a396a60bdc 96 int i = floor(h * 6);
tichise 0:d7a396a60bdc 97 float f = h * 6 - i;
tichise 0:d7a396a60bdc 98 float p = v * (1 - s);
tichise 0:d7a396a60bdc 99 float q = v * (1 - f * s);
tichise 0:d7a396a60bdc 100 float t = v * (1 - (1 - f) * s);
tichise 0:d7a396a60bdc 101 float r = 0, g = 0, b = 0;
tichise 0:d7a396a60bdc 102
tichise 0:d7a396a60bdc 103 switch(i % 6){
tichise 0:d7a396a60bdc 104 case 0: r = v; g = t; b = p; break;
tichise 0:d7a396a60bdc 105 case 1: r = q; g = v; b = p; break;
tichise 0:d7a396a60bdc 106 case 2: r = p; g = v; b = t; break;
tichise 0:d7a396a60bdc 107 case 3: r = p; g = q; b = v; break;
tichise 0:d7a396a60bdc 108 case 4: r = t; g = p; b = v; break;
tichise 0:d7a396a60bdc 109 case 5: r = v; g = p; b = q; break;
tichise 0:d7a396a60bdc 110 }
tichise 0:d7a396a60bdc 111
tichise 0:d7a396a60bdc 112 return (rgb_color){r * 255, g * 255, b * 255};
tichise 0:d7a396a60bdc 113 }