nima aghli
/
WS2811
for thww WS2811 RGB
Fork of WS2811 by
WS2811.cpp@0:c6a6fa47dadc, 2016-04-15 (annotated)
- Committer:
- Drohne
- Date:
- Fri Apr 15 17:55:30 2016 +0000
- Revision:
- 0:c6a6fa47dadc
- Child:
- 1:165513b4e20a
RGB; self programm able
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Drohne | 0:c6a6fa47dadc | 1 | /* |
Drohne | 0:c6a6fa47dadc | 2 | * Creator: Matthias Hemmer |
Drohne | 0:c6a6fa47dadc | 3 | * created: 15.04.2016 |
Drohne | 0:c6a6fa47dadc | 4 | * |
Drohne | 0:c6a6fa47dadc | 5 | * function: write a register of 8 bits to the WS2811 RGBs |
Drohne | 0:c6a6fa47dadc | 6 | */ |
Drohne | 0:c6a6fa47dadc | 7 | #include "mbed.h" |
Drohne | 0:c6a6fa47dadc | 8 | #include "WS2811.h" |
Drohne | 0:c6a6fa47dadc | 9 | |
Drohne | 0:c6a6fa47dadc | 10 | uint8_t ledmatrix[3][3]; |
Drohne | 0:c6a6fa47dadc | 11 | |
Drohne | 0:c6a6fa47dadc | 12 | DigitalOut myled(p20); |
Drohne | 0:c6a6fa47dadc | 13 | |
Drohne | 0:c6a6fa47dadc | 14 | void writeled(int pos, uint8_t g, uint8_t r, uint8_t b){ // define the register |
Drohne | 0:c6a6fa47dadc | 15 | ledmatrix[pos][0] = g; // green register |
Drohne | 0:c6a6fa47dadc | 16 | ledmatrix[pos][1] = r; // red register |
Drohne | 0:c6a6fa47dadc | 17 | ledmatrix[pos][2] = b; // blue register |
Drohne | 0:c6a6fa47dadc | 18 | } |
Drohne | 0:c6a6fa47dadc | 19 | |
Drohne | 0:c6a6fa47dadc | 20 | void writeledbit(char wert){ |
Drohne | 0:c6a6fa47dadc | 21 | |
Drohne | 0:c6a6fa47dadc | 22 | if(wert){ // example: if(1) than make what is declatrated |
Drohne | 0:c6a6fa47dadc | 23 | myled = 1; |
Drohne | 0:c6a6fa47dadc | 24 | for(int i=0;i<HIGH_SIGNAL;i++) __nop(); // wait is to slow, becouse reset = 50µs |
Drohne | 0:c6a6fa47dadc | 25 | myled = 0; |
Drohne | 0:c6a6fa47dadc | 26 | for(int i=0;i<LOW_SIGNAL;i++) __nop(); |
Drohne | 0:c6a6fa47dadc | 27 | } |
Drohne | 0:c6a6fa47dadc | 28 | |
Drohne | 0:c6a6fa47dadc | 29 | else{ // else make this |
Drohne | 0:c6a6fa47dadc | 30 | myled = 1; |
Drohne | 0:c6a6fa47dadc | 31 | for(int i=0;i<LOW_SIGNAL;i++) __nop(); |
Drohne | 0:c6a6fa47dadc | 32 | myled = 0; |
Drohne | 0:c6a6fa47dadc | 33 | for(int i=0;i<HIGH_SIGNAL;i++) __nop(); |
Drohne | 0:c6a6fa47dadc | 34 | } |
Drohne | 0:c6a6fa47dadc | 35 | } |
Drohne | 0:c6a6fa47dadc | 36 | |
Drohne | 0:c6a6fa47dadc | 37 | void sendColours(int pos, uint8_t r, uint8_t g, uint8_t b){ |
Drohne | 0:c6a6fa47dadc | 38 | writeled(pos, g, r, b); // call the underprogramm |
Drohne | 0:c6a6fa47dadc | 39 | |
Drohne | 0:c6a6fa47dadc | 40 | for(int led = 0; led < LED_MAX; led++){ // write the matrix on the RGBs |
Drohne | 0:c6a6fa47dadc | 41 | for(int i = 0; i < 3; i++){ // write the position |
Drohne | 0:c6a6fa47dadc | 42 | for(int b = 0; b < 8; b++){ // write the bit |
Drohne | 0:c6a6fa47dadc | 43 | writeledbit(ledmatrix[led][i] & 1<<b); // write the byte on the RGBs |
Drohne | 0:c6a6fa47dadc | 44 | } |
Drohne | 0:c6a6fa47dadc | 45 | } |
Drohne | 0:c6a6fa47dadc | 46 | } |
Drohne | 0:c6a6fa47dadc | 47 | wait(DELAY); // wait for the supply voltage |
Drohne | 0:c6a6fa47dadc | 48 | } |
Drohne | 0:c6a6fa47dadc | 49 | |
Drohne | 0:c6a6fa47dadc | 50 | void off(){ // set all RGBs off |
Drohne | 0:c6a6fa47dadc | 51 | sendColours(0, 0, 0, 0); |
Drohne | 0:c6a6fa47dadc | 52 | sendColours(1, 0, 0, 0); |
Drohne | 0:c6a6fa47dadc | 53 | sendColours(2, 0, 0, 0); |
Drohne | 0:c6a6fa47dadc | 54 | wait(0.5); |
Drohne | 0:c6a6fa47dadc | 55 | } |