@@ -36,6 +36,26 @@ static void setItem1(rmt_item32_t *pItem) {
36
36
} // setItem1
37
37
38
38
39
+ /*
40
+ * Internal function not exposed. Get the pixel channel color from the channel
41
+ * type which should be one of 'R', 'G' or 'B'.
42
+ */
43
+ static uint8_t getChannelValueByType (char type, pixel_t pixel) {
44
+ switch (type) {
45
+ case ' r' :
46
+ case ' R' :
47
+ return pixel.red ;
48
+ case ' b' :
49
+ case ' B' :
50
+ return pixel.blue ;
51
+ case ' g' :
52
+ case ' G' :
53
+ return pixel.green ;
54
+ }
55
+ return 0 ;
56
+ } // getChannelValueByType
57
+
58
+
39
59
/* *
40
60
* Set two levels of RMT output to the Neopixel value for a "0".
41
61
*/
@@ -52,6 +72,7 @@ WS2812::WS2812(rmt_channel_t channel, gpio_num_t gpioNum, uint16_t pixelCount) {
52
72
this ->channel = channel;
53
73
this ->items = (rmt_item32_t *)calloc (sizeof (rmt_item32_t ), pixelCount * 24 );
54
74
this ->pixels = (pixel_t *)calloc (sizeof (pixel_t ),pixelCount);
75
+ this ->colorOrder = " RGB" ;
55
76
56
77
rmt_config_t config;
57
78
config.rmt_mode = RMT_MODE_TX;
@@ -84,7 +105,9 @@ void WS2812::show() {
84
105
uint32_t i,j;
85
106
rmt_item32_t *pCurrentItem = this ->items ;
86
107
for (i=0 ; i<this ->pixelCount ; i++) {
87
- uint32_t currentPixel = this ->pixels [i].red | (this ->pixels [i].green << 8 ) | (this ->pixels [i].blue << 16 );
108
+ uint32_t currentPixel = getChannelValueByType (this ->colorOrder [0 ], this ->pixels [i]) |
109
+ (getChannelValueByType (this ->colorOrder [1 ], this ->pixels [i]) << 8 ) |
110
+ (getChannelValueByType (this ->colorOrder [2 ], this ->pixels [i]) << 16 );
88
111
for (j=0 ; j<24 ; j++) {
89
112
if (currentPixel & 1 <<j) {
90
113
setItem1 (pCurrentItem);
@@ -100,6 +123,18 @@ void WS2812::show() {
100
123
} // show
101
124
102
125
126
+ /*
127
+ * Set the color order of data sent to the LEDs. The default is "RGB" but we can specify
128
+ * an alternate order by supply an alternate three character string made up of 'R', 'G' and 'B'
129
+ * for example "GRB".
130
+ */
131
+ void WS2812::setColorOrder (char *colorOrder) {
132
+ if (colorOrder != NULL && strlen (colorOrder) == 3 ) {
133
+ this ->colorOrder = colorOrder;
134
+ }
135
+ } // setColorOrder
136
+
137
+
103
138
void WS2812::setPixel (uint16_t index, uint8_t red, uint8_t green,
104
139
uint8_t blue) {
105
140
if (index >= this ->pixelCount ) {
0 commit comments