Alan Rager / Mbed 2 deprecated ws2801_controller

Dependencies:   mbed-rtos mbed ws2801

Committer:
AlanRager
Date:
Mon Jan 26 01:47:13 2015 +0000
Revision:
0:114c35218e65
Child:
1:0634ad3920b7
Initial commit - this takes arbitrary strings of numeric triplets and converts them into a pattern to display on a ws2801 strip

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AlanRager 0:114c35218e65 1 #include "mbed.h"
AlanRager 0:114c35218e65 2 #include "ws2801.h"
AlanRager 0:114c35218e65 3
AlanRager 0:114c35218e65 4 //------------------------------------
AlanRager 0:114c35218e65 5 // Hyperterminal configuration
AlanRager 0:114c35218e65 6 // 9600 bauds, 8-bit data, no parity
AlanRager 0:114c35218e65 7 //------------------------------------
AlanRager 0:114c35218e65 8
AlanRager 0:114c35218e65 9 #define STRIP_LEN 32
AlanRager 0:114c35218e65 10 #define SERIAL_RATE 57600
AlanRager 0:114c35218e65 11
AlanRager 0:114c35218e65 12 RawSerial pc(SERIAL_TX, SERIAL_RX);
AlanRager 0:114c35218e65 13 RawSerial tty(PA_11, PA_12);
AlanRager 0:114c35218e65 14
AlanRager 0:114c35218e65 15 ws2801 led(PA_5, PA_7, STRIP_LEN);
AlanRager 0:114c35218e65 16
AlanRager 0:114c35218e65 17 //input buffer
AlanRager 0:114c35218e65 18 char c[STRIP_LEN * 6 + 4] = "";
AlanRager 0:114c35218e65 19 //input cursor
AlanRager 0:114c35218e65 20 int c_len = 0;
AlanRager 0:114c35218e65 21
AlanRager 0:114c35218e65 22 int led_buffer[STRIP_LEN];
AlanRager 0:114c35218e65 23
AlanRager 0:114c35218e65 24 DigitalOut myled(LED1);
AlanRager 0:114c35218e65 25
AlanRager 0:114c35218e65 26 void repeat() {
AlanRager 0:114c35218e65 27 char in = pc.getc();
AlanRager 0:114c35218e65 28 c[c_len++] = in;
AlanRager 0:114c35218e65 29 c[c_len] = 0;
AlanRager 0:114c35218e65 30 if (in == '\n') {
AlanRager 0:114c35218e65 31 myled = 1;//!myled;
AlanRager 0:114c35218e65 32 }
AlanRager 0:114c35218e65 33 }
AlanRager 0:114c35218e65 34
AlanRager 0:114c35218e65 35 void set_strip_color() {
AlanRager 0:114c35218e65 36 int len, k;
AlanRager 0:114c35218e65 37
AlanRager 0:114c35218e65 38 for ( len = 0 ; c[len * 3] != '\r' && c[len * 3] != '\n'; len++) {
AlanRager 0:114c35218e65 39 led_buffer[ len ] =
AlanRager 0:114c35218e65 40 ( c[ len * 3 + 0 ] - 48 ) * 0x220000
AlanRager 0:114c35218e65 41 + ( c[ len * 3 + 1 ] - 48 ) * 0x002200
AlanRager 0:114c35218e65 42 + ( c[ len * 3 + 2 ] - 48 ) * 0x000022
AlanRager 0:114c35218e65 43 ;
AlanRager 0:114c35218e65 44
AlanRager 0:114c35218e65 45 tty.printf("Color to be sent to #%d is 0x%06x\r\n", len, led_buffer[len]);
AlanRager 0:114c35218e65 46 }
AlanRager 0:114c35218e65 47
AlanRager 0:114c35218e65 48 for ( k = len; k < STRIP_LEN; k++ ) {
AlanRager 0:114c35218e65 49 led_buffer[ k ] = led_buffer[ k % len ];
AlanRager 0:114c35218e65 50
AlanRager 0:114c35218e65 51 tty.printf("Color to be sent to #%d is 0x%06x\r\n", len, led_buffer[k]);
AlanRager 0:114c35218e65 52 }
AlanRager 0:114c35218e65 53
AlanRager 0:114c35218e65 54 tty.printf("Pattern len is %d\r\n", len);
AlanRager 0:114c35218e65 55 }
AlanRager 0:114c35218e65 56
AlanRager 0:114c35218e65 57 int main() {
AlanRager 0:114c35218e65 58 pc.baud(SERIAL_RATE);
AlanRager 0:114c35218e65 59
AlanRager 0:114c35218e65 60 pc.attach(
AlanRager 0:114c35218e65 61 repeat,
AlanRager 0:114c35218e65 62 Serial::RxIrq
AlanRager 0:114c35218e65 63 );
AlanRager 0:114c35218e65 64
AlanRager 0:114c35218e65 65 myled = 0;
AlanRager 0:114c35218e65 66
AlanRager 0:114c35218e65 67 while(1) {
AlanRager 0:114c35218e65 68 if (myled) {
AlanRager 0:114c35218e65 69 led.clear();
AlanRager 0:114c35218e65 70 set_strip_color();
AlanRager 0:114c35218e65 71 led.post(led_buffer);
AlanRager 0:114c35218e65 72
AlanRager 0:114c35218e65 73 c_len = 0;
AlanRager 0:114c35218e65 74 c[0] = 0;
AlanRager 0:114c35218e65 75
AlanRager 0:114c35218e65 76 myled = 0;
AlanRager 0:114c35218e65 77 }
AlanRager 0:114c35218e65 78 wait_us(100);
AlanRager 0:114c35218e65 79 }
AlanRager 0:114c35218e65 80 }
AlanRager 0:114c35218e65 81