Working version without LEDs

Dependencies:   mbed WS2812

Voici le dernier schéma de cablage (version du 08/02/2020)

https://os.mbed.com/media/uploads/max_ence/schemarobot_fev2020.pdf

Committer:
elab
Date:
Sat Feb 08 09:48:46 2020 +0000
Revision:
0:0e577ce96b2f
working version wihout LEDs

Who changed what in which revision?

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