ws2812_custom.c (1137B)
1 #include "ws2812.h" 2 #include "i2c_master.h" 3 4 #ifdef WS2812_RGBW 5 # error "RGBW not supported" 6 #endif 7 8 #ifndef WS2812_I2C_ADDRESS 9 # define WS2812_I2C_ADDRESS 0xB0 10 #endif 11 12 #ifndef WS2812_I2C_ADDRESS_RIGHT 13 # define WS2812_I2C_ADDRESS_RIGHT 0xB8 14 #endif 15 16 #ifndef WS2812_I2C_TIMEOUT 17 # define WS2812_I2C_TIMEOUT 100 18 #endif 19 20 ws2812_led_t ws2812_leds[WS2812_LED_COUNT]; 21 22 void ws2812_init(void) { 23 i2c_init(); 24 } 25 26 void ws2812_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { 27 ws2812_leds[index].r = red; 28 ws2812_leds[index].g = green; 29 ws2812_leds[index].b = blue; 30 } 31 32 void ws2812_set_color_all(uint8_t red, uint8_t green, uint8_t blue) { 33 for (int i = 0; i < WS2812_LED_COUNT; i++) { 34 ws2812_set_color(i, red, green, blue); 35 } 36 } 37 38 void ws2812_flush(void) { 39 i2c_transmit(WS2812_I2C_ADDRESS, (uint8_t *)ws2812_leds, sizeof(ws2812_led_t) * (WS2812_LED_COUNT >> 1), WS2812_I2C_TIMEOUT); 40 i2c_transmit(WS2812_I2C_ADDRESS_RIGHT, (uint8_t *)ws2812_leds + (sizeof(ws2812_led_t) * (WS2812_LED_COUNT >> 1)), sizeof(ws2812_led_t) * (WS2812_LED_COUNT - (WS2812_LED_COUNT >> 1)), WS2812_I2C_TIMEOUT); 41 }