My Version of The LED_WS2812 Library
Dependencies: WS2812 PixelArray
Fork of LED_WS2812 by
Revision 0:999518b1799b, committed 2017-02-15
- Comitter:
- garphil
- Date:
- Wed Feb 15 01:44:53 2017 +0000
- Child:
- 1:64e72a25801f
- Commit message:
- First publish
Changed in this revision
| LED_WS2812.cpp | Show annotated file Show diff for this revision Revisions of this file |
| LED_WS2812.h | Show annotated file Show diff for this revision Revisions of this file |
--- /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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LED_WS2812.h Wed Feb 15 01:44:53 2017 +0000
@@ -0,0 +1,69 @@
+
+#ifndef LED_WS2812_H
+#define LED_WS2812_H
+
+#include "mbed.h"
+#include "WS2812.h"
+#include "PixelArray.h"
+
+
+#define ZERO_HIGH 250.0
+#define ZERO_LOW 1000.0
+#define ONE_HIGH 1000.0
+#define ONE_LOW 250.0
+
+ typedef enum _LED_COLORS {
+ BLUE = 0x0000FF,
+ LIGHTBLUE = 0x00FFF6,
+ RED = 0xFF0000,
+ GREEN = 0X00FF00,
+ BLACK = 0X000000,
+ WHITE = 0XFFFFFF,
+ PURPLE = 0XFF00FF,
+ PINK = 0XFF84A3,
+ YELLOW = 0XFFFF00,
+ DARK_YELLOW = 0X555500,
+ DEFAULT = 0x000000
+ } LED_COLORS;
+
+class LED_WS2812
+{
+public:
+ LED_WS2812(PinName _PinOut, int _nbLeds);
+ ~LED_WS2812();
+ void SetColor(unsigned int _color);
+ void SetColor(LED_COLORS _color);
+ void ResetColor();
+ void SetIntensity(float perCent);
+
+ void InsertColor(unsigned int _color);
+ void InsertColor(unsigned int _color, float brightness);
+ void InsertColorNtimes(int N, unsigned int _color, float brightness);
+ void InsertColorNtimes(int N, unsigned int _color);
+
+ void InsertColor(LED_COLORS _color);
+ void InsertColor(LED_COLORS _color, float brightness);
+ void InsertColorNtimes(int N, LED_COLORS _color, float brightness);
+ void InsertColorNtimes(int N, LED_COLORS _color);
+
+ void StartRotation(float interval); // itnerval in ms
+ void StopRotation(); // interval in ms
+ void Rotate(); // One Rotation
+
+private:
+ void __writeBuf(int z);
+ void __insert2buf();
+ void __insertColor(unsigned int _color, int _intensity);
+
+ WS2812 *ws;
+ int nbLeds;
+ PixelArray *pxArray;
+ int nbInsert;
+ PixelArray *pxInsert;
+ Ticker LEDSystemTick; // System Callback for Rotation
+ bool rotationState;
+ int rotationPosition;
+ int intensity;
+};
+
+#endif
\ No newline at end of file
