A conversion of the excellent Adafruit WS2801 library for Arduino to work on mbed

Dependents:   WiFiDipCortex_Cheerlights

Revision:
1:6ff477690983
Parent:
0:582e1b9c1cc1
Child:
2:2fdaa13896a4
--- a/Adafruit_WS2801.cpp	Fri Mar 08 08:49:04 2013 +0000
+++ b/Adafruit_WS2801.cpp	Fri Mar 08 13:25:50 2013 +0000
@@ -1,5 +1,4 @@
 #include "mbed.h"
-
 #include "Adafruit_WS2801.h"
 
 // Example to control WS2801-based RGB LED Modules in a strand or strip
@@ -7,12 +6,12 @@
 /*****************************************************************************/
 
 // Constructor for use with hardware SPI (specific clock/data pins):
-Adafruit_WS2801::Adafruit_WS2801(uint16_t n, uint8_t order): clkpin(PTD4), datapin(PTA12)
-{
-    rgb_order = order;
-    alloc(n);
-    updatePins();
-}
+//Adafruit_WS2801::Adafruit_WS2801(uint16_t n, uint8_t order): clkpin(PTD4), datapin(PTA12)
+//{
+//    rgb_order = order;
+//    alloc(n);
+//    updatePins();
+//}
 
 // Constructor for use with arbitrary clock/data pins:
 Adafruit_WS2801::Adafruit_WS2801(uint16_t n, PinName dpin, PinName cpin, uint8_t order)  : clkpin(cpin), datapin(dpin)
@@ -56,14 +55,14 @@
 // command.  If using this constructor, MUST follow up with updateLength()
 // and updatePins() to establish the strand length and output pins!
 // Also, updateOrder() to change RGB vs GRB order (RGB is default).
-Adafruit_WS2801::Adafruit_WS2801(void) : clkpin(PTD4), datapin(PTA12)
-{
-    begun     = false;
-    numLEDs   = 0;
-    pixels    = NULL;
-    rgb_order = WS2801_RGB;
-    updatePins(); // Must assume hardware SPI until pins are set
-}
+//Adafruit_WS2801::Adafruit_WS2801(void) : clkpin(PTD4), datapin(PTA12)
+//{
+//    begun     = false;
+//    numLEDs   = 0;
+//    pixels    = NULL;
+//    rgb_order = WS2801_RGB;
+//    updatePins(); // Must assume hardware SPI until pins are set
+//}
 
 // Release memory (as needed):
 Adafruit_WS2801::~Adafruit_WS2801(void)
@@ -77,62 +76,30 @@
 void Adafruit_WS2801::begin(void)
 {
 
-    if(hardwareSPI == true) {
-        startSPI();
-    } else {
-//    pinMode(datapin, OUTPUT);
-//    pinMode(clkpin , OUTPUT);
-    }
+    datapin = 0;
+    clkpin = 0;
     begun = true;
 }
 
 // Change pin assignments post-constructor, switching to hardware SPI:
+/*
 void Adafruit_WS2801::updatePins(void)
 {
     hardwareSPI = true;
     datapin = 0;
     clkpin = 0;
-
-    // If begin() was previously invoked, init the SPI hardware now:
-    if(begun == true) startSPI();
-    // Otherwise, SPI is NOT initted until begin() is explicitly called.
-
-    // Note: any prior clock/data pin directions are left as-is and are
-    // NOT restored as inputs!
 }
+*/
 
 // Change pin assignments post-constructor, using arbitrary pins:
-void Adafruit_WS2801::updatePins(PinName dpin, PinName cpin)
-{
-
-    if(begun == true) { // If begin() was previously invoked...
-        // If previously using hardware SPI, turn that off:
-//    if(hardwareSPI == true) SPI.end();
-        // Regardless, now enable output on 'soft' SPI pins:
-//    pinMode(dpin, OUTPUT);
-//    pinMode(cpin, OUTPUT);
-    } // Otherwise, pins are not set to outputs until begin() is called.
-
+//void Adafruit_WS2801::updatePins(PinName dpin, PinName cpin)
+//{
     // Note: any prior clock/data pin directions are left as-is and are
     // NOT restored as inputs!
 
-    hardwareSPI = false;
-    datapin     = dpin;
-    clkpin      = cpin;
-//  clkport     = portOutputRegister(digitalPinToPort(cpin));
-//  clkpinmask  = digitalPinToBitMask(cpin);
-//  dataport    = portOutputRegister(digitalPinToPort(dpin));
-//  datapinmask = digitalPinToBitMask(dpin);
-}
-
-// Enable SPI hardware and set up protocol details:
-void Adafruit_WS2801::startSPI(void)
-{
-//    SPI.begin();
-//    SPI.setBitOrder(MSBFIRST);
-//    SPI.setDataMode(SPI_MODE0);
-//    SPI.setClockDivider(SPI_CLOCK_DIV16); // 1 MHz max, else flicker
-}
+//    datapin     = DigitalOut(dpin);
+//    clkpin      = DigitalOut(cpin);
+//}
 
 uint16_t Adafruit_WS2801::numPixels(void)
 {
@@ -162,33 +129,6 @@
     uint8_t  bit;
 
     // Write 24 bits per pixel:
-//  if(hardwareSPI) {
-//    for(i=0; i<nl3; i++) {
-//      SPDR = pixels[i];
-//      while(!(SPSR & (1<<SPIF)));
-//    }
-//  } else {
-
-/*
-    for(int LED_number = 0 ; LED_number < _STRIP_LENGTH ; LED_number++) {
-        if(_level != 100) {
-            int R = ((strip_colors[LED_number] & 0x00ff0000) >> 16)*_level/100;
-            int G = ((strip_colors[LED_number] & 0x0000ff00) >> 8)*_level/100;
-            int B = (strip_colors[LED_number] & 0x000000ff)*_level/100;
-            strip_colors_leveled[LED_number] = (R << 16)|(G << 8)|B;
-        } else strip_colors_leveled[LED_number] = strip_colors[LED_number];
-        int this_led_color = strip_colors_leveled[LED_number]; //24 bits of color data
-        for(char color_bit = 23 ; color_bit != 255 ; color_bit--) {
-            _CKI = 0;
-            mask = 1 << color_bit;
-            if(this_led_color & mask)_SDI = 1;
-            else _SDI = 0;
-            _CKI = 1; //Data is latched when clock goes high
-        }
-    }
-    _CKI = 0;
-    wait_us(_reset_delay); //Wait for 1ms to go into reset
-*/
     for(i=0; i<nl3; i++ ) {
         for(bit=0x80; bit; bit >>= 1) {
             clkpin = 0;
@@ -197,9 +137,10 @@
             clkpin = 1;
         }
     }
-//  }
 
+    clkpin = 0;
     wait_ms(1); // Data is latched by holding clock pin low for 1 millisecond
+    clkpin = 1;
 }
 
 // Set pixel color from separate 8-bit R, G, B components: