// ws2812.writergb(4, string.char(255, 0, 0)) uses GPIO2 and sets the first LED red. // ws2812.writergb(3, string.char(0, 0, 255):rep(10)) uses GPIO0 and sets ten LEDs blue. // ws2812.writergb(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; const char *rgb = luaL_checklstring(L, 2, &length); // dont modify lua-internal lstring - make a copy instead char *buffer = (char *)c_malloc(length); c_memcpy(buffer, rgb, length); // 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; } // Initialize the output pin platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT); platform_gpio_write(pin, 0); // Send the buffer os_intr_lock(); ws2812_write(pin, (uint8_t*) buffer, length); os_intr_unlock(); c_free(buffer); return 0; }
int main(void) { littleWire *lw = NULL; lw = littleWire_connect(); if(lw == NULL){ printf("> Little Wire could not be found!\n"); exit(EXIT_FAILURE); } version = readFirmwareVersion(lw); printf("> Little Wire firmware version: %d.%d\n",((version & 0xF0)>>4),(version&0x0F)); if(version<0x12) { printf("> This example requires the new 1.2 version firmware. Please update soon.\n"); return 0; } pinMode(lw, LED, OUTPUT); for(;;){ printf("Red\n"); ws2812_write(lw, LED, 255,0,0); delay(DELAY); printf("Green\n"); ws2812_write(lw, LED, 0,255,0); delay(DELAY); printf("Blue\n"); ws2812_write(lw, LED, 0,0,255); delay(DELAY); printf("Off..\n"); ws2812_write(lw, LED, 0,0,0); delay(DELAY); if(lwStatus<0) { printf("> lwStatus: %d\n",lwStatus); printf("> Connection error!\n"); return 0; } } }
// Lua: ws2812.write("string") // Byte triples in the string are interpreted as G R B values. // // ws2812.init() should be called first // // ws2812.write(string.char(0, 255, 0)) sets the first LED red. // ws2812.write(string.char(0, 0, 255):rep(10)) sets ten LEDs blue. // ws2812.write(string.char(255, 0, 0, 255, 255, 255)) first LED green, second LED white. static int ws2812_writegrb(lua_State* L) { size_t length; const char *values = luaL_checklstring(L, 1, &length); // Send the buffer ws2812_write((uint8_t*) values, length); return 0; }
static int ws2812_buffer_write(lua_State* L) { ws2812_buffer * buffer = (ws2812_buffer*)lua_touserdata(L, 1); luaL_argcheck(L, buffer && buffer->canary == CANARY_VALUE, 1, "ws2812.buffer expected"); // Send the buffer ws2812_write(buffer->values, buffer->colorsPerLed*buffer->size); return 0; }
// Lua: ws2812.write(pin, "string") // Byte triples in the string are interpreted as G R B values. // This function does not corrupt your buffer. // // ws2812.write(4, string.char(0, 255, 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(255, 0, 0, 255, 255, 255)) first LED green, second LED white. static int ICACHE_FLASH_ATTR ws2812_writegrb(lua_State* L) { const uint8_t pin = luaL_checkinteger(L, 1); size_t length; const char *buffer = luaL_checklstring(L, 2, &length); // Initialize the output pin platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT); platform_gpio_write(pin, 0); // Send the buffer os_intr_lock(); ws2812_write(pin, (uint8_t*) buffer, length); os_intr_unlock(); return 0; }
static void light_set_lightness(u8_t percentage) { #if (!MYNEWT_VAL(USE_NEOPIXEL)) int rc; uint16_t pwm_val = (uint16_t) (percentage * top_val / 100); #if MYNEWT_VAL(PWM_0) rc = pwm_set_duty_cycle(pwm0, 0, pwm_val); assert(rc == 0); #endif #if MYNEWT_VAL(PWM_1) rc = pwm_set_duty_cycle(pwm1, 0, pwm_val); assert(rc == 0); #endif #if MYNEWT_VAL(PWM_2) rc = pwm_set_duty_cycle(pwm2, 0, pwm_val); assert(rc == 0); #endif #if MYNEWT_VAL(PWM_3) rc = pwm_set_duty_cycle(pwm3, 0, pwm_val); assert(rc == 0); #endif #else int i; u32_t lightness; u8_t max_lightness = 0x1f; lightness = (u8_t) (percentage * max_lightness / 100); for (i = 0; i < WS2812_NUM_LED; i++) { neopixel[i] = (lightness | lightness << 8 | lightness << 16); } ws2812_write(neopixel); #endif }
int ws2812_write_rgb(ws2812_t *dev, ws2812_rgb_t *leds, unsigned len, char* buffer){ ws2812_fill_rgb(leds, len, buffer); return ws2812_write(dev, buffer, len*sizeof(ws2812_rgb_t)*8/2); }