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

Dependents:   ChrisRGB-Ring

Fork of WS2812 by Chris Styles

Committer:
chris
Date:
Sat Jul 26 23:08:04 2014 +0000
Revision:
0:be22a9d4df5f
Child:
1:c278c3d136bb
first commit

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
chris 0:be22a9d4df5f 14 WS2812::~WS2812()
chris 0:be22a9d4df5f 15 {
chris 0:be22a9d4df5f 16
chris 0:be22a9d4df5f 17 }
chris 0:be22a9d4df5f 18
chris 0:be22a9d4df5f 19 void WS2812::update(int buff[])
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 0:be22a9d4df5f 23 __write(buff[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 0:be22a9d4df5f 61 // extract colour fields from incoming
chris 0:be22a9d4df5f 62 // 0 = blue, 1 = red, 2 = green, 3 = brightness
chris 0:be22a9d4df5f 63 agrb[0] = color & 0x000000FF;
chris 0:be22a9d4df5f 64 agrb[1] = (color & 0x00FF0000) >> 16;
chris 0:be22a9d4df5f 65 agrb[2] = (color & 0x0000FF00) >> 8;
chris 0:be22a9d4df5f 66 agrb[3] = (color & 0xFF000000) >> 24;
chris 0:be22a9d4df5f 67
chris 0:be22a9d4df5f 68 // apply intensity
chris 0:be22a9d4df5f 69 // 1 use global, 2=per pixel
chris 0:be22a9d4df5f 70 if (__use_II == 1) {
chris 0:be22a9d4df5f 71 for (int clr = 2; clr >= 0; clr--) {
chris 0:be22a9d4df5f 72 agrb[clr] = ((agrb[clr] * __II) >> 8);
chris 0:be22a9d4df5f 73 }
chris 0:be22a9d4df5f 74 } else if (__use_II == 2) {
chris 0:be22a9d4df5f 75 for (int clr = 2; clr >= 0; clr--) {
chris 0:be22a9d4df5f 76 agrb[clr] = ((agrb[clr] * agrb[3]) >> 8);
chris 0:be22a9d4df5f 77 }
chris 0:be22a9d4df5f 78 }
chris 0:be22a9d4df5f 79
chris 0:be22a9d4df5f 80
chris 0:be22a9d4df5f 81 // green=2,red=1,blue=0,
chris 0:be22a9d4df5f 82 for (int clr = 2; clr >= 0; clr--) {
chris 0:be22a9d4df5f 83 for (int bit = 7 ; bit >= 0 ; bit--) {
chris 0:be22a9d4df5f 84 if (agrb[clr] & (0x1 << bit)) {
chris 0:be22a9d4df5f 85 __spi.write(WS1);
chris 0:be22a9d4df5f 86 } else {
chris 0:be22a9d4df5f 87 __spi.write(WS0);
chris 0:be22a9d4df5f 88 }
chris 0:be22a9d4df5f 89 }
chris 0:be22a9d4df5f 90 }
chris 0:be22a9d4df5f 91
chris 0:be22a9d4df5f 92 }
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