Library for controlling LED strips or arrays based on the WS2801 3-Channel Constant Current LED Driver, like the SF 32LED/m addressable LED strip. Any two digital out capable pins can be used for clock & data. also includes a header that defines the standart HTML color names

Dependents:   ws2801_controller

Committer:
wertyfrog
Date:
Sat Jun 18 00:33:14 2011 +0000
Revision:
3:2b362d164405
Parent:
0:b964d673c7db

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wertyfrog 0:b964d673c7db 1 #include "ws2801.h"
wertyfrog 0:b964d673c7db 2 #include "mbed.h"
wertyfrog 0:b964d673c7db 3
wertyfrog 0:b964d673c7db 4
wertyfrog 0:b964d673c7db 5 //------------------------------------------------------------------------------------------------------------
wertyfrog 0:b964d673c7db 6 ws2801::ws2801(PinName CKI, PinName SDI, int STRIP_LENGTH, int reset_delay) : _CKI(CKI), _SDI(SDI), _STRIP_LENGTH(STRIP_LENGTH), _reset_delay(reset_delay)
wertyfrog 0:b964d673c7db 7 {
wertyfrog 0:b964d673c7db 8 _level=100;
wertyfrog 0:b964d673c7db 9 //ws2801.clear();
wertyfrog 0:b964d673c7db 10 for(int bits = 0; bits <= _STRIP_LENGTH*24; bits++)
wertyfrog 0:b964d673c7db 11 {
wertyfrog 0:b964d673c7db 12 _CKI = 0;
wertyfrog 0:b964d673c7db 13 _SDI = 0;
wertyfrog 0:b964d673c7db 14 _CKI = 1;
wertyfrog 0:b964d673c7db 15 }
wertyfrog 0:b964d673c7db 16 _CKI = 0;
wertyfrog 0:b964d673c7db 17 wait_us(_reset_delay);
wertyfrog 0:b964d673c7db 18 }
wertyfrog 0:b964d673c7db 19 //------------------------------------------------------------------------------------------------------------
wertyfrog 0:b964d673c7db 20 void ws2801::post(int *strip_colors)
wertyfrog 0:b964d673c7db 21 {
wertyfrog 0:b964d673c7db 22
wertyfrog 0:b964d673c7db 23 int strip_colors_leveled[_STRIP_LENGTH];
wertyfrog 0:b964d673c7db 24 uint32_t mask;
wertyfrog 0:b964d673c7db 25
wertyfrog 0:b964d673c7db 26 for(int LED_number = 0 ; LED_number < _STRIP_LENGTH ; LED_number++)
wertyfrog 0:b964d673c7db 27 {
wertyfrog 0:b964d673c7db 28 if(_level != 100)
wertyfrog 0:b964d673c7db 29 {
wertyfrog 0:b964d673c7db 30 int R = ((strip_colors[LED_number] & 0x00ff0000) >> 16)*_level/100;
wertyfrog 0:b964d673c7db 31 int G = ((strip_colors[LED_number] & 0x0000ff00) >> 8)*_level/100;
wertyfrog 0:b964d673c7db 32 int B = (strip_colors[LED_number] & 0x000000ff)*_level/100;
wertyfrog 0:b964d673c7db 33 strip_colors_leveled[LED_number] = (R << 16)|(G << 8)|B;
wertyfrog 0:b964d673c7db 34 }
wertyfrog 0:b964d673c7db 35 else strip_colors_leveled[LED_number] = strip_colors[LED_number];
wertyfrog 0:b964d673c7db 36 int this_led_color = strip_colors_leveled[LED_number]; //24 bits of color data
wertyfrog 0:b964d673c7db 37 for(char color_bit = 23 ; color_bit != 255 ; color_bit--)
wertyfrog 0:b964d673c7db 38 {
wertyfrog 0:b964d673c7db 39 _CKI = 0;
wertyfrog 0:b964d673c7db 40 mask = 1 << color_bit;
wertyfrog 0:b964d673c7db 41 if(this_led_color & mask)_SDI = 1;
wertyfrog 0:b964d673c7db 42 else _SDI = 0;
wertyfrog 0:b964d673c7db 43 _CKI = 1; //Data is latched when clock goes high
wertyfrog 0:b964d673c7db 44 }
wertyfrog 0:b964d673c7db 45 }
wertyfrog 0:b964d673c7db 46 _CKI = 0;
wertyfrog 0:b964d673c7db 47 wait_us(_reset_delay); //Wait for 1ms to go into reset
wertyfrog 0:b964d673c7db 48 }
wertyfrog 0:b964d673c7db 49 //------------------------------------------------------------------------------------------------------------
wertyfrog 0:b964d673c7db 50 void ws2801::clear(void)
wertyfrog 0:b964d673c7db 51 {
wertyfrog 0:b964d673c7db 52 for(int bits = 0; bits <= _STRIP_LENGTH*24; bits++)
wertyfrog 0:b964d673c7db 53 {
wertyfrog 0:b964d673c7db 54 _CKI = 0;
wertyfrog 0:b964d673c7db 55 _SDI = 0;
wertyfrog 0:b964d673c7db 56 _CKI = 1;
wertyfrog 0:b964d673c7db 57 }
wertyfrog 0:b964d673c7db 58 _CKI = 0;
wertyfrog 0:b964d673c7db 59 wait_us(_reset_delay);
wertyfrog 0:b964d673c7db 60 }
wertyfrog 0:b964d673c7db 61 //------------------------------------------------------------------------------------------------------------
wertyfrog 3:2b362d164405 62 int ws2801::level(int level)
wertyfrog 0:b964d673c7db 63 {
wertyfrog 0:b964d673c7db 64 if((level <= 100) && level)
wertyfrog 0:b964d673c7db 65 {
wertyfrog 0:b964d673c7db 66 _level = level;
wertyfrog 0:b964d673c7db 67 return _level;
wertyfrog 0:b964d673c7db 68 }
wertyfrog 0:b964d673c7db 69 return 0;
wertyfrog 0:b964d673c7db 70 }
wertyfrog 0:b964d673c7db 71
wertyfrog 0:b964d673c7db 72 //------------------------------------------------------------------------------------------------------------
wertyfrog 3:2b362d164405 73 int ws2801::delay(uint32_t reset_delay)
wertyfrog 0:b964d673c7db 74 {
wertyfrog 3:2b362d164405 75 if(reset_delay <= 0xffffffff)_reset_delay = reset_delay;
wertyfrog 0:b964d673c7db 76 return _reset_delay;
wertyfrog 0:b964d673c7db 77 }
wertyfrog 0:b964d673c7db 78 //---------EOF---------------EOF------------------------------------------------------------------------------