Heroic Robotics / SD600A

Fork of SD600A by Heroic Robotics

Revision:
16:910bf46f2ce4
Parent:
15:2733cd5f34e4
Child:
17:a625ffe26993
--- a/SD600A.cpp	Wed Oct 10 06:36:39 2012 +0000
+++ b/SD600A.cpp	Thu Oct 11 04:05:02 2012 +0000
@@ -9,9 +9,21 @@
 #include "LedStrip.h"
 #include "SD600A.h"
 
+/* uint8_t current_byte;     // used by SPI ISR
+  uint32_t bit_index;       // used by SPI ISR
+  uint32_t byte_index; */
+
 void SD600A::idle_function(void) {
-    dat = 0;
-    clk = !clk;
+    clk = 1;
+    dat = pixels[byte_index] & (0x80 >> bit_index);
+    clk = 0;
+    bit_index++;
+    if (bit_index == 8) {
+        byte_index++;
+        bit_index = 0;
+    }
+    if (byte_index == data_length)
+        byte_index = 0;
 }
 
 SD600A::SD600A(PinName dataPin, PinName clockPin, int n) :
@@ -19,89 +31,34 @@
     clk(clockPin)  {
     // Allocate 3 bytes per pixel:
     numLEDs = n;
-    if ((pixels = (uint8_t *)malloc(numLEDs * 3))) {
+    if ((pixels = (uint8_t *)malloc(4+ numLEDs * 3))) {
         memset(pixels, 0, numLEDs * 3); // Init to RGB 'off' state
-    }
+        pixels[numLEDs*3] = 0x7f;
+        pixels[numLEDs*3+1] = 0xff;
+        pixels[numLEDs*3+2] = 0xff;
+        pixels[numLEDs*3+3] = 0x80;
+        data_length = numLEDs*3 +4;
+        byte_index = 0;
+        bit_index = 0;
+    } else error("SD600A could not allocate memory!\r\n");
+    NVIC_SetPriority(TIMER3_IRQn, 0);
     idletoggle.attach_us(this, &SD600A::idle_function, IDLE_INTERVAL);
 }
 
-/*
- *  Soft SPI clock-out implementation (CPOL = 1, CPHA = 0).
- *  Certainly not the fastest in the world but it'll do.
- *  Gets about 3.6 MHz;  could get several times as much
- *  using the bitbands directly  - jas.
- */
- 
-void SD600A::write(uint8_t byte) {
-    clk=1;
-    for (int i=0; i<8; i++) {
-        dat = !!(byte & (1 << (7 - i)));
-    
-        clk = 0;
-       // dat = (byte & 0x80);
-        #ifdef DELAY_PERIOD
-        wait_us(DELAY_PERIOD);
-        #endif
-        clk = 1;
-        #ifdef DELAY_PERIOD
-        wait_us(DELAY_PERIOD);
-        #endif
-        //byte <<= 1;
-    }
-}
-
 void SD600A::begin(void) {
-    // Issue initial latch to 'wake up' strip (latch length varies w/numLEDs)
-    idletoggle.detach();
-    // set the Ticker interrupt to the highest possible priority
-    NVIC_SetPriority(TIMER3_IRQn, 0);
-    for (int i=0; i<numLEDs; i++) {
-        write(0);
-        write(0);
-        write(0);
-    }
-    writeguard();
-    idletoggle.attach_us(this, &SD600A::idle_function, IDLE_INTERVAL);
+ // Null stub.
 }
 
 uint16_t SD600A::numPixels(void) {
     return numLEDs;
 }
 
-void SD600A::writeguard(void) {
-    // generate a 25-bit word of ones
-    clk = 1;
-    #ifdef DELAY_PERIOD
-    wait_us(DELAY_PERIOD);
-    #endif
-    dat = 1;
-    #ifdef DELAY_PERIOD
-    wait_us(DELAY_PERIOD);
-    #endif
-    clk = 0;
-    #ifdef DELAY_PERIOD
-    wait_us(DELAY_PERIOD);
-    #endif
-    write(0xff);
-    write(0xff);
-    write(0xff);
-}
-
 void SD600A::blank(void) {
     memset(pixels, 0x00, numLEDs * 3);
 }
 
 void SD600A::show(void) {
-    uint16_t i, nl3 = numLEDs * 3; // 3 bytes per LED
-    idletoggle.detach();
-
-    for (i=nl3; i; i-- ) {
-        write(pixels[i]);
-    }
-
-    // Write guard word
-    writeguard();
-    idletoggle.attach_us(this, &SD600A::idle_function, IDLE_INTERVAL);
+   // Null stub, since shows continuously.
 }
 
 // Convert R,G,B to combined 32-bit color