My Version of The LED_WS2812 Library

Dependencies:   WS2812 PixelArray

Fork of LED_WS2812 by CreaLab

Committer:
garphil
Date:
Wed Feb 15 01:44:53 2017 +0000
Revision:
0:999518b1799b
Child:
3:ba8dc8811164
First publish

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