Advertisement
Guest User

ws2812b.c

a guest
Feb 24th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.22 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement