8000 Wed Feb 15 22:14:34 CST 2017 · mikepang/esp32-snippets@7c00a98 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7c00a98

Browse files
author
kolban
committed
Wed Feb 15 22:14:34 CST 2017
1 parent a7ea154 commit 7c00a98

File tree

4 files changed

+224
-38
lines changed

4 files changed

+224
-38
lines changed

hardware/neopixels/WS2812.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,26 @@ static void setItem1(rmt_item32_t *pItem) {
3636
} // setItem1
3737

3838

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+
3959
/**
4060
* Set two levels of RMT output to the Neopixel value for a "0".
4161
*/
@@ -52,6 +72,7 @@ WS2812::WS2812(rmt_channel_t channel, gpio_num_t gpioNum, uint16_t pixelCount) {
5272
this->channel = channel;
5373
this->items = (rmt_item32_t *)calloc(sizeof(rmt_item32_t), pixelCount * 24);
5474
this->pixels = (pixel_t *)calloc(sizeof(pixel_t),pixelCount);
75+
this->colorOrder = "RGB";
5576

5677
rmt_config_t config;
5778
config.rmt_mode = RMT_MODE_TX;
@@ -84,7 +105,9 @@ void WS2812::show() {
84105
uint32_t i,j;
85106
rmt_item32_t *pCurrentItem = this->items;
86107
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);
88111
for (j=0; j<24; j++) {
89112
if (currentPixel & 1<<j) {
90113
setItem1(pCurrentItem);
@@ -100,6 +123,18 @@ void WS2812::show() {
100123
} // show
101124

102125

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+
103138
void WS2812::setPixel(uint16_t index, uint8_t red, uint8_t green,
104139
uint8_t blue) {
105140
if (index >= this->pixelCount) {

hardware/neopixels/WS2812.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ class WS2812 {
1919
public:
2020
WS2812(rmt_channel_t channel, gpio_num_t gpioNum, uint16_t pixelCount);
2121
void show();
22+
void setColorOrder(char *order);
2223
void setPixel(uint16_t index, uint8_t red, uint8_t green, uint8_t blue);
2324
void setPixel(uint16_t index, pixel_t pixel);
2425
void setPixel(uint16_t index, uint32_t pixel);
2526
void clear();
2627
virtual ~WS2812();
2728
private:
29+
char *colorOrder;
2830
uint16_t pixelCount;
2931
rmt_channel_t channel;
3032
rmt_item32_t *items;

0 commit comments

Comments
 (0)
0