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