SHARE
TWEET

ws2812b.c

a guest Feb 24th, 2020 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "ws2812b.h"
  2.  
  3. ws2812b_config ws2812b_init(SPI_HandleTypeDef * spi_handler, uint16_t diodes_count) {
  4.     ws2812b_config config;
  5.     config.spi_handler = spi_handler;
  6.     config.diodes_count = diodes_count;
  7.     config.colors_array = calloc(diodes_count, sizeof(ws2812b_color));
  8.     return config;
  9. }
  10.  
  11. void w2812b_set_diode_color(ws2812b_config * config, uint16_t diode_id, ws2812b_color color) {
  12.     config->colors_array[diode_id] = color;
  13. }
  14.  
  15. void ws2812b_refresh(ws2812b_config * config) {
  16.     const uint8_t zero = 0b00011111;
  17.     const uint8_t one = 0b00000111;
  18.  
  19.     uint8_t buffer[30 * 24];
  20.  
  21.     for(uint16_t i = 0, j = 0; i < config->diodes_count; i++) {
  22.         //Zielony
  23.         for(int16_t k = 7; k >= 0; k--) {
  24.             if((config->colors_array[i].green & (1<<k)) == 0) buffer[j] = one;
  25.             else buffer[j] = zero;
  26.             j++;
  27.         }
  28.  
  29.         //Czerwony
  30.         for(int16_t k = 7; k >= 0; k--) {
  31.             if((config->colors_array[i].red & (1<<k)) == 0) buffer[j] = one;
  32.             else buffer[j] = zero;
  33.             j++;
  34.         }
  35.  
  36.         //Niebieski
  37.         for(int16_t k = 7; k >= 0; k--) {
  38.             if((config->colors_array[i].blue & (1<<k)) == 0) buffer[j] = one;
  39.             else buffer[j] = zero;
  40.             j++;
  41.         }
  42.     }
  43.  
  44.     HAL_SPI_Transmit(config->spi_handler, &buffer, config->diodes_count * 24, 1000);
  45.     HAL_Delay(1);
  46. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top