tweaked library for debug but have to commit to publish...

Dependents:   ChrisRGB-Ring

Fork of WS2812 by Chris Styles

Committer:
chris
Date:
Wed Aug 06 08:34:32 2014 +0000
Revision:
4:b230e85fc5e7
Parent:
3:d56ffdf7beeb
Child:
5:a07522fe36d4
Cleaned up code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chris 0:be22a9d4df5f 1 #include "WS2812.h"
chris 0:be22a9d4df5f 2
chris 0:be22a9d4df5f 3 WS2812::WS2812(PinName d, int size) : __spi(d, NC, NC)
chris 0:be22a9d4df5f 4 {
chris 0:be22a9d4df5f 5 __size = size;
chris 0:be22a9d4df5f 6 __spi.format(SPIBPF,0);
chris 0:be22a9d4df5f 7 __spi.frequency(SPICLK);
chris 0:be22a9d4df5f 8 __use_II = 0; // 0=off,1=use global,2=per pixel
chris 0:be22a9d4df5f 9 __II = 0xFF; // set global intensity to full
chris 0:be22a9d4df5f 10 }
chris 0:be22a9d4df5f 11
chris 0:be22a9d4df5f 12
chris 0:be22a9d4df5f 13 WS2812::~WS2812()
chris 0:be22a9d4df5f 14 {
chris 0:be22a9d4df5f 15
chris 0:be22a9d4df5f 16 }
chris 0:be22a9d4df5f 17
chris 4:b230e85fc5e7 18
chris 4:b230e85fc5e7 19 void WS2812::write(int buf[])
chris 0:be22a9d4df5f 20 {
chris 0:be22a9d4df5f 21 // for each of the data points in the buffer
chris 0:be22a9d4df5f 22 for (int i = 0; i < __size ; i++) {
chris 4:b230e85fc5e7 23 __write(buf[i]);
chris 0:be22a9d4df5f 24 }
chris 0:be22a9d4df5f 25 }
chris 0:be22a9d4df5f 26
chris 0:be22a9d4df5f 27
chris 0:be22a9d4df5f 28 void WS2812::setAll(int colour)
chris 0:be22a9d4df5f 29 {
chris 0:be22a9d4df5f 30 // for each of the data points in the buffer
chris 0:be22a9d4df5f 31 for (int i = 0; i < __size ; i++) {
chris 0:be22a9d4df5f 32 __write(colour);
chris 0:be22a9d4df5f 33 }
chris 0:be22a9d4df5f 34 }
chris 0:be22a9d4df5f 35
chris 0:be22a9d4df5f 36
chris 0:be22a9d4df5f 37 void WS2812::useII(int d)
chris 0:be22a9d4df5f 38 {
chris 0:be22a9d4df5f 39 if (d > 0) {
chris 0:be22a9d4df5f 40 __use_II = d;
chris 0:be22a9d4df5f 41 } else {
chris 0:be22a9d4df5f 42 __use_II = 0;
chris 0:be22a9d4df5f 43 }
chris 0:be22a9d4df5f 44 }
chris 0:be22a9d4df5f 45
chris 0:be22a9d4df5f 46
chris 0:be22a9d4df5f 47 void WS2812::setII(unsigned char II)
chris 0:be22a9d4df5f 48 {
chris 0:be22a9d4df5f 49 __II = II;
chris 0:be22a9d4df5f 50 }
chris 0:be22a9d4df5f 51
chris 0:be22a9d4df5f 52
chris 0:be22a9d4df5f 53
chris 0:be22a9d4df5f 54 void WS2812::__write(int color)
chris 0:be22a9d4df5f 55 {
chris 0:be22a9d4df5f 56
chris 0:be22a9d4df5f 57 // Outut format : GGRRBB
chris 0:be22a9d4df5f 58 // Inout format : IIRRGGBB
chris 0:be22a9d4df5f 59 unsigned char agrb[4] = {0x0, 0x0, 0x0, 0x0};
chris 0:be22a9d4df5f 60
chris 4:b230e85fc5e7 61 unsigned char sf; // scaling factor for II
chris 4:b230e85fc5e7 62
chris 0:be22a9d4df5f 63 // extract colour fields from incoming
chris 0:be22a9d4df5f 64 // 0 = blue, 1 = red, 2 = green, 3 = brightness
chris 0:be22a9d4df5f 65 agrb[0] = color & 0x000000FF;
chris 0:be22a9d4df5f 66 agrb[1] = (color & 0x00FF0000) >> 16;
chris 0:be22a9d4df5f 67 agrb[2] = (color & 0x0000FF00) >> 8;
chris 0:be22a9d4df5f 68 agrb[3] = (color & 0xFF000000) >> 24;
chris 0:be22a9d4df5f 69
chris 4:b230e85fc5e7 70 // set and intensity scaling factor (global, per pixel, none)
chris 0:be22a9d4df5f 71 if (__use_II == 1) {
chris 4:b230e85fc5e7 72 sf = __II;
chris 0:be22a9d4df5f 73 } else if (__use_II == 2) {
chris 4:b230e85fc5e7 74 sf = agrb[3];
chris 4:b230e85fc5e7 75 } else {
chris 4:b230e85fc5e7 76 sf = 0xFF;
chris 0:be22a9d4df5f 77 }
chris 0:be22a9d4df5f 78
chris 4:b230e85fc5e7 79 // Apply the scaling factor to each othe colour components
chris 4:b230e85fc5e7 80 for (int clr = 2; clr >= 0; clr--) {
chris 4:b230e85fc5e7 81 agrb[clr] = ((agrb[clr] * sf) >> 8);
chris 4:b230e85fc5e7 82 }
chris 0:be22a9d4df5f 83
chris 4:b230e85fc5e7 84 // For each colour component G,R,B
chris 4:b230e85fc5e7 85 // shift out the data 7..0, writing a SPI frame per bit
chris 0:be22a9d4df5f 86 // green=2,red=1,blue=0,
chris 0:be22a9d4df5f 87 for (int clr = 2; clr >= 0; clr--) {
chris 0:be22a9d4df5f 88 for (int bit = 7 ; bit >= 0 ; bit--) {
chris 0:be22a9d4df5f 89 if (agrb[clr] & (0x1 << bit)) {
chris 0:be22a9d4df5f 90 __spi.write(WS1);
chris 0:be22a9d4df5f 91 } else {
chris 0:be22a9d4df5f 92 __spi.write(WS0);
chris 0:be22a9d4df5f 93 }
chris 0:be22a9d4df5f 94 }
chris 0:be22a9d4df5f 95 }
chris 0:be22a9d4df5f 96 }
chris 0:be22a9d4df5f 97
chris 0:be22a9d4df5f 98
chris 0:be22a9d4df5f 99
chris 0:be22a9d4df5f 100
chris 0:be22a9d4df5f 101
chris 0:be22a9d4df5f 102
chris 0:be22a9d4df5f 103
chris 0:be22a9d4df5f 104