Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: WS2812 PixelArray
Fork of LED_WS2812 by
Diff: LED_WS2812.cpp
- Revision:
- 0:999518b1799b
- Child:
- 3:ba8dc8811164
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LED_WS2812.cpp Wed Feb 15 01:44:53 2017 +0000
@@ -0,0 +1,157 @@
+#include "LED_WS2812.h"
+
+
+
+LED_WS2812::LED_WS2812(PinName _PinOut, int _nbLeds) {
+ nbLeds = _nbLeds;
+ double period_ns;
+ Timer tuneTimings;
+ int sum = 0;
+ int nopRun;
+
+ for(int kavg = 0; kavg<20;kavg++) {
+ tuneTimings.reset();
+ tuneTimings.start();
+ for(int nopCount=0; nopCount < 10000; nopCount ++) {
+ __nop();
+ }
+ tuneTimings.stop();
+ nopRun = tuneTimings.read_us();
+ sum=nopRun+sum;
+ }
+ period_ns = sum/200; /* *1000 for nanoseconds /20 average /10000 count */
+
+ int zero_high = ZERO_HIGH/period_ns;
+ int zero_low = ZERO_LOW/period_ns;
+ int one_high = ONE_HIGH/period_ns;
+ int one_low = ONE_LOW/period_ns;
+
+ ws = new WS2812(_PinOut, nbLeds, zero_high, zero_low, one_high, one_low);
+ ws->useII(WS2812::PER_PIXEL);
+
+ pxArray = new PixelArray(nbLeds);
+ pxInsert = new PixelArray(nbLeds);
+ ResetColor();
+ rotationState = false;
+ rotationPosition = nbLeds-1;
+ intensity = 0xff;
+};
+
+LED_WS2812::~LED_WS2812() {
+ delete(ws);
+ delete(pxArray);
+}
+
+
+
+void LED_WS2812::SetColor(LED_COLORS _color) {
+ SetColor((unsigned int) _color);
+};
+
+void LED_WS2812::SetColor(unsigned int _color) {
+ for(int i=0;i<nbLeds;i++) {
+ pxArray->Set(i, _color);
+ pxArray->SetI(i,intensity);
+ }
+ __writeBuf(0);
+ nbInsert = 0;
+ if(rotationState) StopRotation();
+ rotationPosition = nbLeds;
+};
+
+
+void LED_WS2812::ResetColor() {
+ SetColor(BLACK);
+
+ }
+
+void LED_WS2812::__writeBuf(int z) {
+ ws->write_offsets(pxArray->getBuf(),z,z,z);
+ }
+
+void LED_WS2812::__insert2buf() {
+ for(int i=0;i<nbLeds;i++) {
+ pxArray->Set(i, pxInsert->Get(i%nbInsert));
+ }
+ __writeBuf(0);
+ rotationPosition = nbLeds;
+}
+
+void LED_WS2812::__insertColor(unsigned int _color, int _intensity) {
+ pxInsert->Set(nbInsert%nbLeds,_color);
+ pxInsert->SetI(nbInsert%nbLeds,_intensity);
+ nbInsert++;
+
+};
+
+void LED_WS2812::InsertColor(unsigned int _color) {
+ InsertColor(_color,intensity*100/0xFF);
+};
+
+
+void LED_WS2812::InsertColor(unsigned int _color, float brightness) {
+ int pixelIntensity = brightness*0xFF/100;
+ __insertColor(_color, pixelIntensity);
+ __insert2buf();
+};
+
+
+void LED_WS2812::InsertColorNtimes(int N, unsigned int _color, float brightness) {
+ for(int i=0;i<N;i++) {
+ InsertColor(_color, brightness);
+ }
+};
+
+void LED_WS2812::InsertColorNtimes(int N, unsigned int _color) {
+ InsertColorNtimes(N, _color, intensity*100/0xFF);
+};
+
+
+void LED_WS2812::InsertColor(LED_COLORS _color) {
+ InsertColor((unsigned int)_color);
+};
+
+
+void LED_WS2812::InsertColor(LED_COLORS _color, float brightness) {
+ InsertColor((unsigned int)_color, brightness);
+};
+
+
+void LED_WS2812::InsertColorNtimes(int N, LED_COLORS _color, float brightness) {
+ InsertColorNtimes(N, (unsigned int)_color, brightness);
+};
+
+void LED_WS2812::InsertColorNtimes(int N, LED_COLORS _color) {
+ InsertColorNtimes(N, _color, intensity*100/0xFF);
+};
+
+
+void LED_WS2812::SetIntensity(float perCent) {
+ intensity = perCent*0xFF/100;
+ ws->setII(intensity);
+ for(int i=0;i<nbLeds;i++) {
+ pxArray->SetI(i,intensity);
+ }
+}
+
+void LED_WS2812::StartRotation(float interval) {
+ if(rotationState==false) {
+ rotationState = true;
+ LEDSystemTick.attach_us(this, &LED_WS2812::Rotate, interval*1000000);
+ }
+}
+
+
+void LED_WS2812::StopRotation() {
+ if(rotationState==true) {
+ rotationState = false;
+ LEDSystemTick.detach();
+ }
+}
+
+void LED_WS2812::Rotate() {
+ rotationPosition--;
+ if (rotationPosition == -1)
+ rotationPosition = nbLeds-1;
+ __writeBuf(rotationPosition);
+}
\ No newline at end of file
