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