My Version of The LED_WS2812 Library

Dependencies:   WS2812 PixelArray

Fork of LED_WS2812 by CreaLab

Committer:
garphil
Date:
Thu Nov 02 09:56:59 2017 +0000
Revision:
3:ba8dc8811164
Parent:
0:999518b1799b
Child:
4:15b992a39c77
Added SetColor at position + timing to wait after write.

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 0:999518b1799b 37 intensity = 0xff;
garphil 0:999518b1799b 38 };
garphil 0:999518b1799b 39
garphil 0:999518b1799b 40 LED_WS2812::~LED_WS2812() {
garphil 0:999518b1799b 41 delete(ws);
garphil 0:999518b1799b 42 delete(pxArray);
garphil 0:999518b1799b 43 }
garphil 0:999518b1799b 44
garphil 0:999518b1799b 45
garphil 0:999518b1799b 46
garphil 3:ba8dc8811164 47 void LED_WS2812::SetColor(LED_COLORS _color, int position) {
garphil 3:ba8dc8811164 48 SetColor((unsigned int) _color, position);
garphil 3:ba8dc8811164 49 };
garphil 3:ba8dc8811164 50
garphil 3:ba8dc8811164 51 void LED_WS2812::SetColor(unsigned int _color, int position) {
garphil 3:ba8dc8811164 52 if(position < nbLeds && position >=0 ) {
garphil 3:ba8dc8811164 53 pxArray->Set(position, _color);
garphil 3:ba8dc8811164 54 pxArray->SetI(position,intensity);
garphil 3:ba8dc8811164 55
garphil 3:ba8dc8811164 56 }
garphil 3:ba8dc8811164 57 __writeBuf(0);
garphil 3:ba8dc8811164 58 nbInsert = 0;
garphil 3:ba8dc8811164 59 if(rotationState) StopRotation();
garphil 3:ba8dc8811164 60 rotationPosition = nbLeds;
garphil 3:ba8dc8811164 61 };
garphil 3:ba8dc8811164 62
garphil 0:999518b1799b 63 void LED_WS2812::SetColor(LED_COLORS _color) {
garphil 0:999518b1799b 64 SetColor((unsigned int) _color);
garphil 0:999518b1799b 65 };
garphil 0:999518b1799b 66
garphil 0:999518b1799b 67 void LED_WS2812::SetColor(unsigned int _color) {
garphil 0:999518b1799b 68 for(int i=0;i<nbLeds;i++) {
garphil 0:999518b1799b 69 pxArray->Set(i, _color);
garphil 0:999518b1799b 70 pxArray->SetI(i,intensity);
garphil 0:999518b1799b 71 }
garphil 0:999518b1799b 72 __writeBuf(0);
garphil 0:999518b1799b 73 nbInsert = 0;
garphil 0:999518b1799b 74 if(rotationState) StopRotation();
garphil 0:999518b1799b 75 rotationPosition = nbLeds;
garphil 0:999518b1799b 76 };
garphil 0:999518b1799b 77
garphil 0:999518b1799b 78
garphil 0:999518b1799b 79 void LED_WS2812::ResetColor() {
garphil 0:999518b1799b 80 SetColor(BLACK);
garphil 0:999518b1799b 81
garphil 0:999518b1799b 82 }
garphil 0:999518b1799b 83
garphil 0:999518b1799b 84 void LED_WS2812::__writeBuf(int z) {
garphil 0:999518b1799b 85 ws->write_offsets(pxArray->getBuf(),z,z,z);
garphil 3:ba8dc8811164 86 wait(0.01);
garphil 0:999518b1799b 87 }
garphil 0:999518b1799b 88
garphil 0:999518b1799b 89 void LED_WS2812::__insert2buf() {
garphil 0:999518b1799b 90 for(int i=0;i<nbLeds;i++) {
garphil 0:999518b1799b 91 pxArray->Set(i, pxInsert->Get(i%nbInsert));
garphil 0:999518b1799b 92 }
garphil 0:999518b1799b 93 __writeBuf(0);
garphil 0:999518b1799b 94 rotationPosition = nbLeds;
garphil 0:999518b1799b 95 }
garphil 0:999518b1799b 96
garphil 0:999518b1799b 97 void LED_WS2812::__insertColor(unsigned int _color, int _intensity) {
garphil 0:999518b1799b 98 pxInsert->Set(nbInsert%nbLeds,_color);
garphil 0:999518b1799b 99 pxInsert->SetI(nbInsert%nbLeds,_intensity);
garphil 0:999518b1799b 100 nbInsert++;
garphil 0:999518b1799b 101
garphil 0:999518b1799b 102 };
garphil 0:999518b1799b 103
garphil 0:999518b1799b 104 void LED_WS2812::InsertColor(unsigned int _color) {
garphil 0:999518b1799b 105 InsertColor(_color,intensity*100/0xFF);
garphil 0:999518b1799b 106 };
garphil 0:999518b1799b 107
garphil 0:999518b1799b 108
garphil 0:999518b1799b 109 void LED_WS2812::InsertColor(unsigned int _color, float brightness) {
garphil 0:999518b1799b 110 int pixelIntensity = brightness*0xFF/100;
garphil 0:999518b1799b 111 __insertColor(_color, pixelIntensity);
garphil 0:999518b1799b 112 __insert2buf();
garphil 0:999518b1799b 113 };
garphil 0:999518b1799b 114
garphil 0:999518b1799b 115
garphil 0:999518b1799b 116 void LED_WS2812::InsertColorNtimes(int N, unsigned int _color, float brightness) {
garphil 0:999518b1799b 117 for(int i=0;i<N;i++) {
garphil 0:999518b1799b 118 InsertColor(_color, brightness);
garphil 0:999518b1799b 119 }
garphil 0:999518b1799b 120 };
garphil 0:999518b1799b 121
garphil 0:999518b1799b 122 void LED_WS2812::InsertColorNtimes(int N, unsigned int _color) {
garphil 0:999518b1799b 123 InsertColorNtimes(N, _color, intensity*100/0xFF);
garphil 0:999518b1799b 124 };
garphil 0:999518b1799b 125
garphil 0:999518b1799b 126
garphil 0:999518b1799b 127 void LED_WS2812::InsertColor(LED_COLORS _color) {
garphil 0:999518b1799b 128 InsertColor((unsigned int)_color);
garphil 0:999518b1799b 129 };
garphil 0:999518b1799b 130
garphil 0:999518b1799b 131
garphil 0:999518b1799b 132 void LED_WS2812::InsertColor(LED_COLORS _color, float brightness) {
garphil 0:999518b1799b 133 InsertColor((unsigned int)_color, brightness);
garphil 0:999518b1799b 134 };
garphil 0:999518b1799b 135
garphil 0:999518b1799b 136
garphil 0:999518b1799b 137 void LED_WS2812::InsertColorNtimes(int N, LED_COLORS _color, float brightness) {
garphil 0:999518b1799b 138 InsertColorNtimes(N, (unsigned int)_color, brightness);
garphil 0:999518b1799b 139 };
garphil 0:999518b1799b 140
garphil 0:999518b1799b 141 void LED_WS2812::InsertColorNtimes(int N, LED_COLORS _color) {
garphil 0:999518b1799b 142 InsertColorNtimes(N, _color, intensity*100/0xFF);
garphil 0:999518b1799b 143 };
garphil 0:999518b1799b 144
garphil 0:999518b1799b 145
garphil 0:999518b1799b 146 void LED_WS2812::SetIntensity(float perCent) {
garphil 0:999518b1799b 147 intensity = perCent*0xFF/100;
garphil 0:999518b1799b 148 ws->setII(intensity);
garphil 0:999518b1799b 149 for(int i=0;i<nbLeds;i++) {
garphil 0:999518b1799b 150 pxArray->SetI(i,intensity);
garphil 0:999518b1799b 151 }
garphil 0:999518b1799b 152 }
garphil 0:999518b1799b 153
garphil 0:999518b1799b 154 void LED_WS2812::StartRotation(float interval) {
garphil 0:999518b1799b 155 if(rotationState==false) {
garphil 0:999518b1799b 156 rotationState = true;
garphil 0:999518b1799b 157 LEDSystemTick.attach_us(this, &LED_WS2812::Rotate, interval*1000000);
garphil 0:999518b1799b 158 }
garphil 0:999518b1799b 159 }
garphil 0:999518b1799b 160
garphil 0:999518b1799b 161
garphil 0:999518b1799b 162 void LED_WS2812::StopRotation() {
garphil 0:999518b1799b 163 if(rotationState==true) {
garphil 0:999518b1799b 164 rotationState = false;
garphil 0:999518b1799b 165 LEDSystemTick.detach();
garphil 0:999518b1799b 166 }
garphil 0:999518b1799b 167 }
garphil 0:999518b1799b 168
garphil 0:999518b1799b 169 void LED_WS2812::Rotate() {
garphil 0:999518b1799b 170 rotationPosition--;
garphil 0:999518b1799b 171 if (rotationPosition == -1)
garphil 0:999518b1799b 172 rotationPosition = nbLeds-1;
garphil 0:999518b1799b 173 __writeBuf(rotationPosition);
garphil 0:999518b1799b 174 }