A conversion of the excellent Adafruit WS2801 library for Arduino to work on mbed
Diff: Adafruit_WS2801.cpp
- Revision:
- 2:2fdaa13896a4
- Parent:
- 1:6ff477690983
- Child:
- 3:dfffbd9f8ac6
--- a/Adafruit_WS2801.cpp Fri Mar 08 13:25:50 2013 +0000 +++ b/Adafruit_WS2801.cpp Wed Feb 12 22:05:44 2014 +0000 @@ -5,6 +5,8 @@ // Written by Adafruit - MIT license /*****************************************************************************/ +SPI spi(p16, p15, p13); // mosi, miso, sclk + // Constructor for use with hardware SPI (specific clock/data pins): //Adafruit_WS2801::Adafruit_WS2801(uint16_t n, uint8_t order): clkpin(PTD4), datapin(PTA12) //{ @@ -18,7 +20,7 @@ { rgb_order = order; alloc(n); - + // updatePins(dpin, cpin); } @@ -41,12 +43,14 @@ begun = false; numLEDs = ((pixels = (uint8_t *)calloc(n, 3)) != NULL) ? n : 0; - for(int bits = 0; bits <= numLEDs*24; bits++) { - clkpin = 0; - datapin = 0; - clkpin = 1; - } - clkpin = 0; + +// for(int bits = 0; bits <= numLEDs*24; bits++) { +// spi.write(0x00); +// clkpin = 0; +// datapin = 0; +// clkpin = 1; +// } +// clkpin = 0; } // via Michael Vogt/neophob: empty constructor is used when strand length @@ -75,21 +79,26 @@ // Activate hard/soft SPI as appropriate: void Adafruit_WS2801::begin(void) { - - datapin = 0; - clkpin = 0; + if( hardwareSPI ) { + // Setup the spi for 8 bit data, high steady state clock, + // second edge capture, with a 1MHz clock rate + spi.format(8,0); + spi.frequency(1000000); + } else { + 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; } -*/ + // Change pin assignments post-constructor, using arbitrary pins: //void Adafruit_WS2801::updatePins(PinName dpin, PinName cpin) @@ -126,21 +135,30 @@ void Adafruit_WS2801::show(void) { uint16_t i, nl3 = numLEDs * 3; // 3 bytes per LED - uint8_t bit; + + if( hardwareSPI ) { + for(i=0; i<nl3; i++ ) { + spi.write( pixels[i]); + } + wait_ms(1); // Data is latched by holding clock pin low for 1 millisecond + } else { + uint8_t bit; - // Write 24 bits per pixel: - for(i=0; i<nl3; i++ ) { - for(bit=0x80; bit; bit >>= 1) { - clkpin = 0; - if(pixels[i] & bit) datapin = 1; - else datapin = 0; - clkpin = 1; + // Write 24 bits per pixel: + for(i=0; i<nl3; i++ ) { + for(bit=0x80; bit; bit >>= 1) { + clkpin = 0; + datapin = (pixels[i] & bit) ? 1 : 0; + clkpin = 1; + wait_us(100); + } } - } + datapin = 0; - clkpin = 0; - wait_ms(1); // Data is latched by holding clock pin low for 1 millisecond - 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: