![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
This is a simple controller for the ws2801 that I got working on my stm32f401re board, but it should generalize. It's meant to take a line of digits over serial to represent the state of the strip.
Dependencies: mbed-rtos mbed ws2801
Diff: main.cpp
- Revision:
- 1:0634ad3920b7
- Parent:
- 0:114c35218e65
- Child:
- 2:032f1776f8bd
--- a/main.cpp Mon Jan 26 01:47:13 2015 +0000 +++ b/main.cpp Mon Jan 26 03:44:39 2015 +0000 @@ -1,81 +1,57 @@ #include "mbed.h" #include "ws2801.h" - -//------------------------------------ -// Hyperterminal configuration -// 9600 bauds, 8-bit data, no parity -//------------------------------------ +#include "rtos.h" #define STRIP_LEN 32 -#define SERIAL_RATE 57600 +#define SERIAL_RATE 2000000 RawSerial pc(SERIAL_TX, SERIAL_RX); -RawSerial tty(PA_11, PA_12); ws2801 led(PA_5, PA_7, STRIP_LEN); -//input buffer -char c[STRIP_LEN * 6 + 4] = ""; //input cursor int c_len = 0; - +//led buffer int led_buffer[STRIP_LEN]; - -DigitalOut myled(LED1); +bool reading = false; +bool buffer_ready = false; void repeat() { char in = pc.getc(); - c[c_len++] = in; - c[c_len] = 0; - if (in == '\n') { - myled = 1;//!myled; + + if (in != '\n' && in != '\r') { + reading = true; + if ( (c_len % 3) == 0 ) { + led_buffer[ c_len / 3 ] = (in - 48) * 0x220000; + } + else { + led_buffer[ c_len / 3 ] += (in - 48) * (0x220000 >> (8 * (c_len % 3))); + } + + c_len++; } -} - -void set_strip_color() { - int len, k; - - for ( len = 0 ; c[len * 3] != '\r' && c[len * 3] != '\n'; len++) { - led_buffer[ len ] = - ( c[ len * 3 + 0 ] - 48 ) * 0x220000 - + ( c[ len * 3 + 1 ] - 48 ) * 0x002200 - + ( c[ len * 3 + 2 ] - 48 ) * 0x000022 - ; - - tty.printf("Color to be sent to #%d is 0x%06x\r\n", len, led_buffer[len]); + else { + buffer_ready = 1; } - - for ( k = len; k < STRIP_LEN; k++ ) { - led_buffer[ k ] = led_buffer[ k % len ]; - - tty.printf("Color to be sent to #%d is 0x%06x\r\n", len, led_buffer[k]); - } - - tty.printf("Pattern len is %d\r\n", len); } int main() { - pc.baud(SERIAL_RATE); + pc.baud(SERIAL_RATE); - pc.attach( - repeat, - Serial::RxIrq - ); - - myled = 0; + pc.attach( + repeat, + Serial::RxIrq + ); - while(1) { - if (myled) { - led.clear(); - set_strip_color(); - led.post(led_buffer); - - c_len = 0; - c[0] = 0; - - myled = 0; + buffer_ready = 0; + + while (1) { + if (buffer_ready) { + led.post(led_buffer); + reading = false; + c_len = 0; + buffer_ready = 0; + } + Thread::wait(1); } - wait_us(100); - } } - \ No newline at end of file