Drives a strip of serial RGB LEDs (WS2812) through SPI port (mosi: p5).

Dependencies:   BurstSPI

Dependents:   colorsExample

Files at this revision

API Documentation at this revision

Comitter:
xkozima
Date:
Sat May 23 01:31:14 2015 +0000
Commit message:
This library drives WS2812 Serial RGB LED strips. See main.cpp as an example usage.

Changed in this revision

BurstSPI.lib Show annotated file Show diff for this revision Revisions of this file
colors.cpp Show annotated file Show diff for this revision Revisions of this file
colors.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r f7ac8595c47a BurstSPI.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BurstSPI.lib	Sat May 23 01:31:14 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/Sissors/code/BurstSPI/#bc069279eb37
diff -r 000000000000 -r f7ac8595c47a colors.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/colors.cpp	Sat May 23 01:31:14 2015 +0000
@@ -0,0 +1,52 @@
+#include "mbed.h"
+#include "colors.h"
+
+BurstSPI Spi(p5, p6, p7);
+
+void Colors::setup(int n) {
+    num = n;
+    Spi.frequency(2400000);
+    Spi.format(12);
+}
+
+void Colors::sendOne(unsigned char r, unsigned char g, unsigned char b) {
+    int data;
+    //  W2812: g, r, b  (0:100, 1:110)
+    //  g[7:4]  0000abcdxxxx
+    //          1a01b01c01d0
+    data = ((g & 0x80) << 3) | ((g & 0x40) << 1)
+         | ((g & 0x20) >> 1) | ((g & 0x10) >> 3);
+    Spi.fastWrite(data | 0x924);
+    //  g[3:0]  0000xxxxabcd
+    //          1a01b01c01d0
+    data = ((g & 0x08) << 7) | ((g & 0x04) << 5)
+         | ((g & 0x02) << 3) | ((g & 0x01) << 1);
+    Spi.fastWrite(data | 0x924);
+    //  r[7:4]  0000abcdxxxx
+    //          1a01b01c01d0
+    data = ((r & 0x80) << 3) | ((r & 0x40) << 1)
+         | ((r & 0x20) >> 1) | ((r & 0x10) >> 3);
+    Spi.fastWrite(data | 0x924);
+    //  r[3:0]  0000xxxxabcd
+    //          1a01b01c01d0
+    data = ((r & 0x08) << 7) | ((r & 0x04) << 5)
+         | ((r & 0x02) << 3) | ((r & 0x01) << 1);
+    Spi.fastWrite(data | 0x924);
+    //  b[7:4]  0000abcdxxxx
+    //          1a01b01c01d0
+    data = ((b & 0x80) << 3) | ((b & 0x40) << 1)
+         | ((b & 0x20) >> 1) | ((b & 0x10) >> 3);
+    Spi.fastWrite(data | 0x924);
+    //  b[3:0]  0000xxxxabcd
+    //          1a01b01c01d0
+    data = ((b & 0x08) << 7) | ((b & 0x04) << 5)
+         | ((b & 0x02) << 3) | ((b & 0x01) << 1);
+    Spi.fastWrite(data | 0x924);
+}
+
+void Colors::send(unsigned char data[][3]) {
+    for (int i = 0; i < num; i++) {
+        sendOne(data[i][0], data[i][1], data[i][2]);
+    }
+    wait_us(50);
+}
diff -r 000000000000 -r f7ac8595c47a colors.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/colors.h	Sat May 23 01:31:14 2015 +0000
@@ -0,0 +1,11 @@
+#include "mbed.h"
+#include "BurstSPI.h"
+
+class Colors {
+public:
+    void setup(int n);
+    void send(unsigned char data[][3]);
+private:
+    void sendOne(unsigned char r, unsigned char g, unsigned char b);
+    int num;
+};