My Version of The LED_WS2812 Library

Dependencies:   WS2812 PixelArray

Fork of LED_WS2812 by CreaLab

Committer:
garphil
Date:
Wed Oct 03 11:33:02 2018 +0000
Revision:
6:c9955bf67a3e
Parent:
4:15b992a39c77
Child:
7:92f47cf3a6fd
Removed warning on callback;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
garphil 0:999518b1799b 1 #include "LED_WS2812.h"
garphil 0:999518b1799b 2
garphil 0:999518b1799b 3
garphil 0:999518b1799b 4
garphil 0:999518b1799b 5 LED_WS2812::LED_WS2812(PinName _PinOut, int _nbLeds) {
garphil 0:999518b1799b 6 nbLeds = _nbLeds;
garphil 0:999518b1799b 7 double period_ns;
garphil 0:999518b1799b 8 Timer tuneTimings;
garphil 0:999518b1799b 9 int sum = 0;
garphil 0:999518b1799b 10 int nopRun;
garphil 0:999518b1799b 11
garphil 0:999518b1799b 12 for(int kavg = 0; kavg<20;kavg++) {
garphil 0:999518b1799b 13 tuneTimings.reset();
garphil 0:999518b1799b 14 tuneTimings.start();
garphil 0:999518b1799b 15 for(int nopCount=0; nopCount < 10000; nopCount ++) {
garphil 0:999518b1799b 16 __nop();
garphil 0:999518b1799b 17 }
garphil 0:999518b1799b 18 tuneTimings.stop();
garphil 0:999518b1799b 19 nopRun = tuneTimings.read_us();
garphil 0:999518b1799b 20 sum=nopRun+sum;
garphil 0:999518b1799b 21 }
garphil 0:999518b1799b 22 period_ns = sum/200; /* *1000 for nanoseconds /20 average /10000 count */
garphil 0:999518b1799b 23
garphil 0:999518b1799b 24 int zero_high = ZERO_HIGH/period_ns;
garphil 0:999518b1799b 25 int zero_low = ZERO_LOW/period_ns;
garphil 0:999518b1799b 26 int one_high = ONE_HIGH/period_ns;
garphil 0:999518b1799b 27 int one_low = ONE_LOW/period_ns;
garphil 0:999518b1799b 28
garphil 0:999518b1799b 29 ws = new WS2812(_PinOut, nbLeds, zero_high, zero_low, one_high, one_low);
garphil 0:999518b1799b 30 ws->useII(WS2812::PER_PIXEL);
garphil 0:999518b1799b 31
garphil 0:999518b1799b 32 pxArray = new PixelArray(nbLeds);
garphil 0:999518b1799b 33 pxInsert = new PixelArray(nbLeds);
garphil 0:999518b1799b 34 ResetColor();
garphil 0:999518b1799b 35 rotationState = false;
garphil 0:999518b1799b 36 rotationPosition = nbLeds-1;
garphil 4:15b992a39c77 37 blinkState = false;
garphil 4:15b992a39c77 38 blinkONOFF = false;
garphil 0:999518b1799b 39 intensity = 0xff;
garphil 0:999518b1799b 40 };
garphil 0:999518b1799b 41
garphil 0:999518b1799b 42 LED_WS2812::~LED_WS2812() {
garphil 0:999518b1799b 43 delete(ws);
garphil 0:999518b1799b 44 delete(pxArray);
garphil 0:999518b1799b 45 }
garphil 0:999518b1799b 46
garphil 0:999518b1799b 47
garphil 0:999518b1799b 48
garphil 3:ba8dc8811164 49 void LED_WS2812::SetColor(LED_COLORS _color, int position) {
garphil 3:ba8dc8811164 50 SetColor((unsigned int) _color, position);
garphil 3:ba8dc8811164 51 };
garphil 3:ba8dc8811164 52
garphil 3:ba8dc8811164 53 void LED_WS2812::SetColor(unsigned int _color, int position) {
garphil 3:ba8dc8811164 54 if(position < nbLeds && position >=0 ) {
garphil 3:ba8dc8811164 55 pxArray->Set(position, _color);
garphil 3:ba8dc8811164 56 pxArray->SetI(position,intensity);
garphil 3:ba8dc8811164 57
garphil 3:ba8dc8811164 58 }
garphil 3:ba8dc8811164 59 __writeBuf(0);
garphil 3:ba8dc8811164 60 nbInsert = 0;
garphil 3:ba8dc8811164 61 if(rotationState) StopRotation();
garphil 3:ba8dc8811164 62 rotationPosition = nbLeds;
garphil 3:ba8dc8811164 63 };
garphil 3:ba8dc8811164 64
garphil 0:999518b1799b 65 void LED_WS2812::SetColor(LED_COLORS _color) {
garphil 0:999518b1799b 66 SetColor((unsigned int) _color);
garphil 0:999518b1799b 67 };
garphil 0:999518b1799b 68
garphil 0:999518b1799b 69 void LED_WS2812::SetColor(unsigned int _color) {
garphil 0:999518b1799b 70 for(int i=0;i<nbLeds;i++) {
garphil 0:999518b1799b 71 pxArray->Set(i, _color);
garphil 0:999518b1799b 72 pxArray->SetI(i,intensity);
garphil 0:999518b1799b 73 }
garphil 0:999518b1799b 74 __writeBuf(0);
garphil 0:999518b1799b 75 nbInsert = 0;
garphil 0:999518b1799b 76 if(rotationState) StopRotation();
garphil 0:999518b1799b 77 rotationPosition = nbLeds;
garphil 0:999518b1799b 78 };
garphil 0:999518b1799b 79
garphil 0:999518b1799b 80
garphil 0:999518b1799b 81 void LED_WS2812::ResetColor() {
garphil 0:999518b1799b 82 SetColor(BLACK);
garphil 0:999518b1799b 83
garphil 0:999518b1799b 84 }
garphil 0:999518b1799b 85
garphil 0:999518b1799b 86 void LED_WS2812::__writeBuf(int z) {
garphil 0:999518b1799b 87 ws->write_offsets(pxArray->getBuf(),z,z,z);
garphil 3:ba8dc8811164 88 wait(0.01);
garphil 0:999518b1799b 89 }
garphil 0:999518b1799b 90
garphil 0:999518b1799b 91 void LED_WS2812::__insert2buf() {
garphil 0:999518b1799b 92 for(int i=0;i<nbLeds;i++) {
garphil 0:999518b1799b 93 pxArray->Set(i, pxInsert->Get(i%nbInsert));
garphil 0:999518b1799b 94 }
garphil 0:999518b1799b 95 __writeBuf(0);
garphil 0:999518b1799b 96 rotationPosition = nbLeds;
garphil 0:999518b1799b 97 }
garphil 0:999518b1799b 98
garphil 0:999518b1799b 99 void LED_WS2812::__insertColor(unsigned int _color, int _intensity) {
garphil 0:999518b1799b 100 pxInsert->Set(nbInsert%nbLeds,_color);
garphil 0:999518b1799b 101 pxInsert->SetI(nbInsert%nbLeds,_intensity);
garphil 0:999518b1799b 102 nbInsert++;
garphil 0:999518b1799b 103
garphil 0:999518b1799b 104 };
garphil 0:999518b1799b 105
garphil 0:999518b1799b 106 void LED_WS2812::InsertColor(unsigned int _color) {
garphil 0:999518b1799b 107 InsertColor(_color,intensity*100/0xFF);
garphil 0:999518b1799b 108 };
garphil 0:999518b1799b 109
garphil 0:999518b1799b 110
garphil 0:999518b1799b 111 void LED_WS2812::InsertColor(unsigned int _color, float brightness) {
garphil 0:999518b1799b 112 int pixelIntensity = brightness*0xFF/100;
garphil 0:999518b1799b 113 __insertColor(_color, pixelIntensity);
garphil 0:999518b1799b 114 __insert2buf();
garphil 0:999518b1799b 115 };
garphil 0:999518b1799b 116
garphil 0:999518b1799b 117
garphil 0:999518b1799b 118 void LED_WS2812::InsertColorNtimes(int N, unsigned int _color, float brightness) {
garphil 0:999518b1799b 119 for(int i=0;i<N;i++) {
garphil 0:999518b1799b 120 InsertColor(_color, brightness);
garphil 0:999518b1799b 121 }
garphil 0:999518b1799b 122 };
garphil 0:999518b1799b 123
garphil 0:999518b1799b 124 void LED_WS2812::InsertColorNtimes(int N, unsigned int _color) {
garphil 0:999518b1799b 125 InsertColorNtimes(N, _color, intensity*100/0xFF);
garphil 0:999518b1799b 126 };
garphil 0:999518b1799b 127
garphil 0:999518b1799b 128
garphil 0:999518b1799b 129 void LED_WS2812::InsertColor(LED_COLORS _color) {
garphil 0:999518b1799b 130 InsertColor((unsigned int)_color);
garphil 0:999518b1799b 131 };
garphil 0:999518b1799b 132
garphil 0:999518b1799b 133
garphil 0:999518b1799b 134 void LED_WS2812::InsertColor(LED_COLORS _color, float brightness) {
garphil 0:999518b1799b 135 InsertColor((unsigned int)_color, brightness);
garphil 0:999518b1799b 136 };
garphil 0:999518b1799b 137
garphil 0:999518b1799b 138
garphil 0:999518b1799b 139 void LED_WS2812::InsertColorNtimes(int N, LED_COLORS _color, float brightness) {
garphil 0:999518b1799b 140 InsertColorNtimes(N, (unsigned int)_color, brightness);
garphil 0:999518b1799b 141 };
garphil 0:999518b1799b 142
garphil 0:999518b1799b 143 void LED_WS2812::InsertColorNtimes(int N, LED_COLORS _color) {
garphil 0:999518b1799b 144 InsertColorNtimes(N, _color, intensity*100/0xFF);
garphil 0:999518b1799b 145 };
garphil 0:999518b1799b 146
garphil 0:999518b1799b 147
garphil 0:999518b1799b 148 void LED_WS2812::SetIntensity(float perCent) {
garphil 0:999518b1799b 149 intensity = perCent*0xFF/100;
garphil 0:999518b1799b 150 ws->setII(intensity);
garphil 0:999518b1799b 151 for(int i=0;i<nbLeds;i++) {
garphil 0:999518b1799b 152 pxArray->SetI(i,intensity);
garphil 0:999518b1799b 153 }
garphil 0:999518b1799b 154 }
garphil 0:999518b1799b 155
garphil 0:999518b1799b 156 void LED_WS2812::StartRotation(float interval) {
garphil 0:999518b1799b 157 if(rotationState==false) {
garphil 0:999518b1799b 158 rotationState = true;
garphil 6:c9955bf67a3e 159 LEDSystemTick.attach_us(callback(this, &LED_WS2812::Rotate), interval*1000000);
garphil 0:999518b1799b 160 }
garphil 0:999518b1799b 161 }
garphil 0:999518b1799b 162
garphil 0:999518b1799b 163
garphil 0:999518b1799b 164 void LED_WS2812::StopRotation() {
garphil 0:999518b1799b 165 if(rotationState==true) {
garphil 0:999518b1799b 166 rotationState = false;
garphil 4:15b992a39c77 167 rotationPosition = 0;
garphil 0:999518b1799b 168 LEDSystemTick.detach();
garphil 0:999518b1799b 169 }
garphil 0:999518b1799b 170 }
garphil 0:999518b1799b 171
garphil 0:999518b1799b 172 void LED_WS2812::Rotate() {
garphil 0:999518b1799b 173 rotationPosition--;
garphil 0:999518b1799b 174 if (rotationPosition == -1)
garphil 0:999518b1799b 175 rotationPosition = nbLeds-1;
garphil 4:15b992a39c77 176 if(!blinkState) __writeBuf(rotationPosition);
garphil 4:15b992a39c77 177 }
garphil 4:15b992a39c77 178
garphil 4:15b992a39c77 179
garphil 4:15b992a39c77 180 void LED_WS2812::StartBlink(float interval) {
garphil 4:15b992a39c77 181 if(blinkState==false) {
garphil 4:15b992a39c77 182 blinkState = true;
garphil 6:c9955bf67a3e 183 LEDBlinkSystemTick.attach_us(callback(this, &LED_WS2812::Blink), interval*1000000);
garphil 4:15b992a39c77 184 }
garphil 4:15b992a39c77 185 }
garphil 4:15b992a39c77 186
garphil 4:15b992a39c77 187
garphil 4:15b992a39c77 188 void LED_WS2812::StopBlink() {
garphil 4:15b992a39c77 189 if(blinkState==true) {
garphil 4:15b992a39c77 190 blinkState = false;
garphil 4:15b992a39c77 191 LEDBlinkSystemTick.detach();
garphil 4:15b992a39c77 192 }
garphil 4:15b992a39c77 193 }
garphil 4:15b992a39c77 194
garphil 4:15b992a39c77 195 void LED_WS2812::Blink() {
garphil 4:15b992a39c77 196 blinkONOFF = !blinkONOFF;
garphil 4:15b992a39c77 197 if (blinkONOFF)
garphil 4:15b992a39c77 198 __writeBuf(rotationPosition);
garphil 4:15b992a39c77 199 else {
garphil 4:15b992a39c77 200 ws->useII(WS2812::GLOBAL);
garphil 4:15b992a39c77 201 ws->setII(0);
garphil 4:15b992a39c77 202 __writeBuf(rotationPosition);
garphil 4:15b992a39c77 203 ws->useII(WS2812::PER_PIXEL);
garphil 4:15b992a39c77 204 ws->setII(intensity);
garphil 4:15b992a39c77 205
garphil 4:15b992a39c77 206 }
garphil 4:15b992a39c77 207
garphil 0:999518b1799b 208 }