Detected people approaching if the distance is < 6 feet then LEDs blinking RED otherwise LEDs light green.

Dependencies:   mbed X_NUCLEO_53L1A1_mbed WS2812

Committer:
elab
Date:
Tue Sep 29 09:30:43 2020 +0000
Revision:
0:ea8694e55809
first version

Who changed what in which revision?

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