// Byte triples in the buffer are interpreted as G R B values and sent to the hardware as G R B. int ICACHE_FLASH_ATTR ws2812_writegrb(uint8_t gpio, char *buffer, size_t length) { // Initialize the output pin: pinMode(gpio, OUTPUT); digitalWrite(gpio, 0); // Ignore incomplete Byte triples at the end of buffer: length -= length % 3; // Do not remove these: os_delay_us(1); os_delay_us(1); // Send the buffer: ets_intr_lock(); const char * const end = buffer + length; while (buffer != end) { uint8_t mask = 0x80; while (mask) { (*buffer & mask) ? send_ws_1(gpio) : send_ws_0(gpio); mask >>= 1; } ++buffer; } ets_intr_unlock(); }
// Byte triples in the buffer are interpreted as R G B values and sent to the hardware as G R B. int ICACHE_FLASH_ATTR ws2812_writergb(uint8_t gpio, char *buffer, size_t length) { // Initialize the output pin: pinMode(gpio, OUTPUT); digitalWrite(gpio, 0); // Ignore incomplete Byte triples at the end of buffer: length -= length % 3; // Rearrange R G B values to G R B order needed by WS2812 LEDs: size_t i; for (i = 0; i < length; i += 3) { const char r = buffer[i]; const char g = buffer[i + 1]; buffer[i] = g; buffer[i + 1] = r; } // Do not remove these: os_delay_us(1); os_delay_us(1); // Send the buffer: ets_intr_lock(); const char * const end = buffer + length; while (buffer != end) { uint8_t mask = 0x80; while (mask) { (*buffer & mask) ? send_ws_1(gpio) : send_ws_0(gpio); mask >>= 1; } ++buffer; } ets_intr_unlock(); }
void WS2812OutBuffer( uint8_t * buffer, uint16_t length, int dim) { uint16_t i; GPIO_OUTPUT_SET(GPIO_ID_PIN(WSGPIO), 0); for( i = 0; i < length; i++ ) { int tot = i / 3; //tot = tot; if(REALROWS == 7) { if(tot == 0 || tot == 15 || tot == 31 || tot == 47 || tot == 63 || tot == 79 || tot == 95 ) { if(tot == 0) { i = i + 3; } else { i = i + 6; } } } taskYIELD(); uint8_t byte = lTmlBuf[i]; //printf("%d", byte); if( byte & 0x80 ) send_ws_1(WSGPIO); else send_ws_0(WSGPIO); if( byte & 0x40 ) send_ws_1(WSGPIO); else send_ws_0(WSGPIO); if( byte & 0x20 ) send_ws_1(WSGPIO); else send_ws_0(WSGPIO); if( byte & 0x10 ) send_ws_1(WSGPIO); else send_ws_0(WSGPIO); if( byte & 0x08 ) send_ws_1(WSGPIO); else send_ws_0(WSGPIO); if( byte & 0x04 ) send_ws_1(WSGPIO); else send_ws_0(WSGPIO); if( byte & 0x02 ) send_ws_1(WSGPIO); else send_ws_0(WSGPIO); if( byte & 0x01 ) send_ws_1(WSGPIO); else send_ws_0(WSGPIO); } //os_printf("\r\n"); //reset will happen when it's low long enough. //(don't call this function twice within 10us) }
// Lua: ws2812.write(pin, "string") // Byte triples in the string are interpreted as R G B values and sent to the hardware as G R B. // ws2812.write(4, string.char(255, 0, 0)) uses GPIO2 and sets the first LED red. // ws2812.write(3, string.char(0, 0, 255):rep(10)) uses GPIO0 and sets ten LEDs blue. // ws2812.write(4, string.char(0, 255, 0, 255, 255, 255)) first LED green, second LED white. static int ICACHE_FLASH_ATTR ws2812_writergb(lua_State* L) { const uint8_t pin = luaL_checkinteger(L, 1); size_t length; char *buffer = (char *)luaL_checklstring(L, 2, &length); // Cast away the constness. // Initialize the output pin: platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT); platform_gpio_write(pin, 0); // Ignore incomplete Byte triples at the end of buffer: length -= length % 3; // Rearrange R G B values to G R B order needed by WS2812 LEDs: size_t i; for (i = 0; i < length; i += 3) { const char r = buffer[i]; const char g = buffer[i + 1]; buffer[i] = g; buffer[i + 1] = r; } // Do not remove these: os_delay_us(1); os_delay_us(1); // Send the buffer: os_intr_lock(); const char * const end = buffer + length; while (buffer != end) { uint8_t mask = 0x80; while (mask) { (*buffer & mask) ? send_ws_1(pin_num[pin]) : send_ws_0(pin_num[pin]); mask >>= 1; } ++buffer; } os_intr_unlock(); return 0; }