test led strip

Fork of apa102 by Alan Rager

Committer:
AlanRager
Date:
Tue Jan 27 17:33:01 2015 +0000
Revision:
0:cd4bcb59e36e
Child:
1:c0e649ca77af
First commit - this is heavily based on the ws2801 found here: https://developer.mbed.org/users/wertyfrog/code/ws2801/

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AlanRager 0:cd4bcb59e36e 1 #include "apa102.h"
AlanRager 0:cd4bcb59e36e 2 #include "mbed.h"
AlanRager 0:cd4bcb59e36e 3
AlanRager 0:cd4bcb59e36e 4 #define DO_N(N, F) for( int repetier = 0; repetier < N; repetier++) { F; }
AlanRager 0:cd4bcb59e36e 5
AlanRager 0:cd4bcb59e36e 6 //------------------------------------------------------------------------------------------------------------
AlanRager 0:cd4bcb59e36e 7 apa102::apa102(PinName CKI, PinName SDI, int STRIP_LENGTH, int reset_delay)
AlanRager 0:cd4bcb59e36e 8 : _CKI(CKI), _SDI(SDI), _STRIP_LENGTH(STRIP_LENGTH), _reset_delay(reset_delay) {
AlanRager 0:cd4bcb59e36e 9
AlanRager 0:cd4bcb59e36e 10 _level=100;
AlanRager 0:cd4bcb59e36e 11 this->clear();
AlanRager 0:cd4bcb59e36e 12 wait_us(_reset_delay);
AlanRager 0:cd4bcb59e36e 13 }
AlanRager 0:cd4bcb59e36e 14 //------------------------------------------------------------------------------------------------------------
AlanRager 0:cd4bcb59e36e 15 void apa102::post(int *strip_colors) {
AlanRager 0:cd4bcb59e36e 16 this->preamble();
AlanRager 0:cd4bcb59e36e 17
AlanRager 0:cd4bcb59e36e 18 int scaled_level = 0xE0 | (_level * 31 / 100);
AlanRager 0:cd4bcb59e36e 19
AlanRager 0:cd4bcb59e36e 20 for(int LED_number = 0 ; LED_number < _STRIP_LENGTH ; LED_number++) {
AlanRager 0:cd4bcb59e36e 21 this->write_word(scaled_level, 8);
AlanRager 0:cd4bcb59e36e 22 this->write_word(strip_colors[LED_number], 24);
AlanRager 0:cd4bcb59e36e 23 }
AlanRager 0:cd4bcb59e36e 24
AlanRager 0:cd4bcb59e36e 25 this->afterword();
AlanRager 0:cd4bcb59e36e 26 wait_us(_reset_delay); //Wait for 1ms to go into reset
AlanRager 0:cd4bcb59e36e 27 }
AlanRager 0:cd4bcb59e36e 28 //------------------------------------------------------------------------------------------------------------
AlanRager 0:cd4bcb59e36e 29 void apa102::clear(void) {
AlanRager 0:cd4bcb59e36e 30 this->preamble();
AlanRager 0:cd4bcb59e36e 31 DO_N(_STRIP_LENGTH, this->write_word(0xE0000000, 32));
AlanRager 0:cd4bcb59e36e 32 this->afterword();
AlanRager 0:cd4bcb59e36e 33 }
AlanRager 0:cd4bcb59e36e 34 //------------------------------------------------------------------------------------------------------------
AlanRager 0:cd4bcb59e36e 35 int apa102::level(int level)
AlanRager 0:cd4bcb59e36e 36 {
AlanRager 0:cd4bcb59e36e 37 if((level <= 100) && level)
AlanRager 0:cd4bcb59e36e 38 {
AlanRager 0:cd4bcb59e36e 39 _level = level;
AlanRager 0:cd4bcb59e36e 40 return _level;
AlanRager 0:cd4bcb59e36e 41 }
AlanRager 0:cd4bcb59e36e 42 return 0;
AlanRager 0:cd4bcb59e36e 43 }
AlanRager 0:cd4bcb59e36e 44
AlanRager 0:cd4bcb59e36e 45 //------------------------------------------------------------------------------------------------------------
AlanRager 0:cd4bcb59e36e 46 int apa102::delay(uint32_t reset_delay)
AlanRager 0:cd4bcb59e36e 47 {
AlanRager 0:cd4bcb59e36e 48 if(reset_delay <= 0xffffffff)_reset_delay = reset_delay;
AlanRager 0:cd4bcb59e36e 49 return _reset_delay;
AlanRager 0:cd4bcb59e36e 50 }
AlanRager 0:cd4bcb59e36e 51 //------------------------------------------------------------------------------------------------------------
AlanRager 0:cd4bcb59e36e 52 inline void apa102::write_hi() {
AlanRager 0:cd4bcb59e36e 53 this->write_bit(1);
AlanRager 0:cd4bcb59e36e 54 }
AlanRager 0:cd4bcb59e36e 55 inline void apa102::write_lo() {
AlanRager 0:cd4bcb59e36e 56 this->write_bit(0);
AlanRager 0:cd4bcb59e36e 57 }
AlanRager 0:cd4bcb59e36e 58 inline void apa102::write_bit(bool bit) {
AlanRager 0:cd4bcb59e36e 59 _CKI = 0;
AlanRager 0:cd4bcb59e36e 60 _SDI = bit;
AlanRager 0:cd4bcb59e36e 61 _CKI = 1;
AlanRager 0:cd4bcb59e36e 62 }
AlanRager 0:cd4bcb59e36e 63 inline void apa102::write_word(uint32_t word, int bits) {
AlanRager 0:cd4bcb59e36e 64 for(char color_bit = bits - 1 ; color_bit != 255 ; color_bit--) {
AlanRager 0:cd4bcb59e36e 65 this->write_bit(
AlanRager 0:cd4bcb59e36e 66 (word & (1 << color_bit)) ? 1 : 0
AlanRager 0:cd4bcb59e36e 67 );
AlanRager 0:cd4bcb59e36e 68 }
AlanRager 0:cd4bcb59e36e 69 }
AlanRager 0:cd4bcb59e36e 70 inline void apa102::write_done() {
AlanRager 0:cd4bcb59e36e 71 _CKI = 0;
AlanRager 0:cd4bcb59e36e 72 }
AlanRager 0:cd4bcb59e36e 73 inline void apa102::preamble() {
AlanRager 0:cd4bcb59e36e 74 DO_N(32, this->write_lo());
AlanRager 0:cd4bcb59e36e 75 }
AlanRager 0:cd4bcb59e36e 76 inline void apa102::afterword() {
AlanRager 0:cd4bcb59e36e 77 DO_N(32, this->write_hi());
AlanRager 0:cd4bcb59e36e 78 this->write_done();
AlanRager 0:cd4bcb59e36e 79 }
AlanRager 0:cd4bcb59e36e 80 //---------EOF---------------EOF------------------------------------------------------------------------------