for thww WS2811 RGB

Dependencies:   mbed

Fork of WS2811 by Bulme Projekt

Committer:
Drohne
Date:
Fri Apr 29 07:46:25 2016 +0000
Revision:
2:ccf3f0c36dae
Parent:
1:165513b4e20a
Child:
3:2fbb2f0e79fb
diable interrrupt => speed up;

Who changed what in which revision?

UserRevisionLine numberNew 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 2:ccf3f0c36dae 10 uint8_t ledmatrix[LED_MAX][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 2:ccf3f0c36dae 38 __disable_irq; // disable interrupt to speed up
Drohne 0:c6a6fa47dadc 39 writeled(pos, g, r, b); // call the underprogramm
Drohne 0:c6a6fa47dadc 40
Drohne 0:c6a6fa47dadc 41 for(int led = 0; led < LED_MAX; led++){ // write the matrix on the RGBs
Drohne 0:c6a6fa47dadc 42 for(int i = 0; i < 3; i++){ // write the position
Drohne 0:c6a6fa47dadc 43 for(int b = 0; b < 8; b++){ // write the bit
Drohne 0:c6a6fa47dadc 44 writeledbit(ledmatrix[led][i] & 1<<b); // write the byte on the RGBs
Drohne 0:c6a6fa47dadc 45 }
Drohne 0:c6a6fa47dadc 46 }
Drohne 0:c6a6fa47dadc 47 }
Drohne 0:c6a6fa47dadc 48 wait(DELAY); // wait for the supply voltage
Drohne 2:ccf3f0c36dae 49 __enable_irq; // enable interrupt pin for the basic uses
Drohne 0:c6a6fa47dadc 50 }
Drohne 0:c6a6fa47dadc 51
Drohne 1:165513b4e20a 52 void clear(){ // set all RGBs off
Drohne 0:c6a6fa47dadc 53 sendColours(0, 0, 0, 0);
Drohne 0:c6a6fa47dadc 54 sendColours(1, 0, 0, 0);
Drohne 0:c6a6fa47dadc 55 sendColours(2, 0, 0, 0);
Drohne 1:165513b4e20a 56 wait_ms(OFF_DELAY);
Drohne 1:165513b4e20a 57 }
Drohne 1:165513b4e20a 58
Drohne 1:165513b4e20a 59 void runlight(float waittime){
Drohne 1:165513b4e20a 60 sendColours(0, 100, 0, 0);
Drohne 1:165513b4e20a 61 wait(waittime);
Drohne 1:165513b4e20a 62 sendColours(1, 0, 100, 0);
Drohne 1:165513b4e20a 63 wait(waittime);
Drohne 1:165513b4e20a 64 sendColours(2, 0, 0, 100);
Drohne 1:165513b4e20a 65 wait(waittime);
Drohne 2:ccf3f0c36dae 66 //clear();
Drohne 1:165513b4e20a 67 sendColours(0, 0, 0, 100);
Drohne 1:165513b4e20a 68 wait(waittime);
Drohne 1:165513b4e20a 69 sendColours(1, 100, 0, 0);
Drohne 1:165513b4e20a 70 wait(waittime);
Drohne 1:165513b4e20a 71 sendColours(2, 0, 100, 0);
Drohne 1:165513b4e20a 72 wait(waittime);
Drohne 2:ccf3f0c36dae 73 //clear();
Drohne 1:165513b4e20a 74 sendColours(0, 0, 100, 0);
Drohne 1:165513b4e20a 75 wait(waittime);
Drohne 1:165513b4e20a 76 sendColours(1, 0, 0, 100);
Drohne 1:165513b4e20a 77 wait(waittime);
Drohne 1:165513b4e20a 78 sendColours(2, 100, 0, 0);
Drohne 1:165513b4e20a 79 wait(waittime);
Drohne 2:ccf3f0c36dae 80 //clear();
Drohne 0:c6a6fa47dadc 81 }