nima aghli
/
WS2811
for thww WS2811 RGB
Fork of WS2811 by
WS2811.cpp@1:165513b4e20a, 2016-04-18 (annotated)
- Committer:
- Drohne
- Date:
- Mon Apr 18 07:38:23 2016 +0000
- Revision:
- 1:165513b4e20a
- Parent:
- 0:c6a6fa47dadc
- Child:
- 2:ccf3f0c36dae
expand clear and runlight
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 | 1:165513b4e20a | 50 | void clear(){ // 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 | 1:165513b4e20a | 54 | wait_ms(OFF_DELAY); |
Drohne | 1:165513b4e20a | 55 | } |
Drohne | 1:165513b4e20a | 56 | |
Drohne | 1:165513b4e20a | 57 | void runlight(float waittime){ |
Drohne | 1:165513b4e20a | 58 | sendColours(0, 100, 0, 0); |
Drohne | 1:165513b4e20a | 59 | wait(waittime); |
Drohne | 1:165513b4e20a | 60 | sendColours(1, 0, 100, 0); |
Drohne | 1:165513b4e20a | 61 | wait(waittime); |
Drohne | 1:165513b4e20a | 62 | sendColours(2, 0, 0, 100); |
Drohne | 1:165513b4e20a | 63 | wait(waittime); |
Drohne | 1:165513b4e20a | 64 | clear(); |
Drohne | 1:165513b4e20a | 65 | sendColours(0, 0, 0, 100); |
Drohne | 1:165513b4e20a | 66 | wait(waittime); |
Drohne | 1:165513b4e20a | 67 | sendColours(1, 100, 0, 0); |
Drohne | 1:165513b4e20a | 68 | wait(waittime); |
Drohne | 1:165513b4e20a | 69 | sendColours(2, 0, 100, 0); |
Drohne | 1:165513b4e20a | 70 | wait(waittime); |
Drohne | 1:165513b4e20a | 71 | clear(); |
Drohne | 1:165513b4e20a | 72 | sendColours(0, 0, 100, 0); |
Drohne | 1:165513b4e20a | 73 | wait(waittime); |
Drohne | 1:165513b4e20a | 74 | sendColours(1, 0, 0, 100); |
Drohne | 1:165513b4e20a | 75 | wait(waittime); |
Drohne | 1:165513b4e20a | 76 | sendColours(2, 100, 0, 0); |
Drohne | 1:165513b4e20a | 77 | wait(waittime); |
Drohne | 1:165513b4e20a | 78 | clear(); |
Drohne | 0:c6a6fa47dadc | 79 | } |