aboutsummaryrefslogtreecommitdiff
path: root/src/drv/ws2812B/ws2812b.c
blob: e1e9309ad7e8eb635987d910f4ace5403f580170 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "drv/ws2812B/ws2812b.h"

#include "kern/mem.h"
#include "kern/panic.h"
#include "kern/dma/dma_manager.h"
#include "arch/stm32l4xxx/peripherals/spi.h"

uint8_t* ws2812b_compile_rgb(rgb_t* out_, size_t arr_len)
{
  uint8_t* out = (uint8_t*) out_;
  uint8_t* spi_out = kalloc(arr_len * 9);

  if (!spi_out) {
    panic("Unable to allocate spi_out\n");
  }

  size_t i;
  size_t j;

  for (i = 0, j = 0; i < arr_len * 3; ++i, j += 3) {
    // stuff
    uint8_t c = out[i];
    spi_out[j] = 0
      | (1 << 7)
      | ((c & (1 << 7)) << 6)
      | (0 << 5)
      | (1 << 4)
      | ((c & (1 << 6)) << 3)
      | (0 << 2)
      | (1 << 1)
      | ((c & (1 << 5)) << 0);

    spi_out[j + 1] = 0
      | (0 << 7)
      | (1 << 6)
      | ((c & (1 << 4)) << 5)
      | (0 << 4)
      | (1 << 3)
      | ((c & (1 << 3)) << 2)
      | (0 << 1)
      | (1 << 0);

    spi_out[j + 2] = 0
      | ((c & (1 << 2)) << 7)
      | (0 << 6)
      | (1 << 5)
      | ((c & (1 << 1)) << 4)
      | (0 << 3)
      | (1 << 2)
      | ((c & (1 << 0)) << 1)
      | (0 << 0);
  }

  return spi_out;
}