Xmas tree serial LED blinking example code

Dependencies:   mbed

Fork of JBB_SerialLED_test by Junichi Katsu

Revision:
0:57df396a2de8
Child:
1:832ea8c566a8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WS2812B/LEDStrip_WS2812.cpp	Mon May 12 11:01:44 2014 +0000
@@ -0,0 +1,128 @@
+/*
+ * WS2812 tape led IC
+ *
+ *          0.35us   0.8us    (+-150ns)
+ *  0:     |^^^^^|__________|
+ *
+ *             0.7us   0.6us  (+-150ns)
+ *  1:     |^^^^^^^^^^|_____|
+ *
+ *               >50us
+ *  RESET: |________________|
+ */
+#include "mbed.h"
+#include "LEDStrip.h"
+
+SPI tape(p11, NC, NC);
+
+#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
+    LPC_SSP_TypeDef *_ssp = LPC_SSP0;
+#elif defined(TARGET_LPC11U24)
+    LPC_SSPx_Type *_ssp = LPC_SSP1;
+#endif
+
+int num = 100;
+int *data;
+volatile int busy = 0, wakeup = 0;
+
+
+extern "C"
+#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
+void SSP0_IRQHandler() {
+#elif defined(TARGET_LPC11U24)
+void SSP1_IRQHandler() {
+#endif
+    static int addr = 0, bit = 0x800000;
+repeat:
+    if (busy) {
+        // led data
+      while (_ssp->SR & (1<<1)) { // TNF
+        if (data[addr] & bit) {
+            // 1
+            //_ssp->DR = 0x01f;
+			_ssp->DR = 0x007;
+        } else {
+            // 0
+            _ssp->DR = 0x07f;
+        }
+        bit = bit >> 1;
+        if (bit == 0) {
+            bit = 0x800000;
+            addr ++;
+            if (addr >= num) {
+                addr = 0;
+                busy = 0;
+                goto repeat;
+            }
+        }
+      }
+    } else {
+        // blank
+        while (_ssp->SR & (1<<1)) { // TNF
+            _ssp->DR = 0xfff;
+            if (addr < 50) {
+                addr ++;
+            } else {
+                addr = 0;
+                if (wakeup) {
+                    busy = 1;
+                    wakeup = 0;
+                    goto repeat;
+                }
+            }
+        }
+    }
+}
+
+void tapeInit (int freq, int n) {
+
+    num = n;
+//    data = new int(num);
+    data = (int*)malloc(sizeof(int) * num);
+    for (int i = 0; i < num; i ++) {
+        data[i] = 0;
+    }
+
+    tape.format(10, 1);
+    if (freq) {
+        tape.frequency(freq * 1000);
+    } else {
+        tape.frequency(8000000);
+    }
+#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
+    NVIC_SetVector(SSP0_IRQn, (uint32_t)SSP0_IRQHandler);
+    NVIC_SetPriority(SSP0_IRQn, 0);
+    NVIC_EnableIRQ(SSP0_IRQn);
+#elif defined(TARGET_LPC11U24)
+    NVIC_SetVector(SSP1_IRQn, (uint32_t)SSP1_IRQHandler);
+    NVIC_SetPriority(SSP1_IRQn, 0);
+    NVIC_EnableIRQ(SSP1_IRQn);
+#endif
+    _ssp->IMSC |= (1<<3); // TXIM
+}
+
+void tapeSet (int n, int dat) {
+    if (n >= 0 && n < num) {
+        // RGB -> GRB
+        data[n] = ((dat & 0xff0000) >> 8) | ((dat & 0xff00) << 8) | (dat & 0xff);
+    }
+}
+void tapeSet(int n, int red , int green , int blue) {
+    if (n >= 0 && n < num) {
+        // RGB -> GRB
+        data[n] = ((red & 0xff) << 8) | ((green & 0xff) << 16) | (blue & 0xff);
+    }
+}
+
+void tapeSend () {
+    if (busy) {
+        while (busy);
+        wait_us(50);
+    }
+    wakeup = 1;
+    while (wakeup);
+}
+
+int tapeGet (int n) {
+    return ((data[n] & 0xff0000) >> 8) | ((data[n] & 0xff00) << 8) | (data[n] & 0xff);
+}