aboutsummaryrefslogtreecommitdiff
path: root/src/drv/ws2812B/ws2812b.c
blob: 2a1ccfba5541d9d3d49fa506a510f2b9e422b68c (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
56
57
58
59
60
61
62
#include "drv/ws2812B/ws2812b.h"

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

uint8_t sintable[256] = {
    128, 148, 156, 162, 168, 172, 177, 181, 184, 187, 191, 194, 196, 199, 202,
    204, 207, 209, 211, 213, 215, 217, 219, 221, 223, 224, 226, 228, 229, 231,
    232, 233, 235, 236, 237, 239, 240, 241, 242, 243, 244, 245, 246, 246, 247,
    248, 249, 249, 250, 251, 251, 252, 252, 253, 253, 253, 254, 254, 254, 255,
    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254, 254, 254, 253, 253,
    253, 252, 252, 251, 251, 250, 249, 249, 248, 247, 246, 246, 245, 244, 243,
    242, 241, 240, 239, 237, 236, 235, 233, 232, 231, 229, 228, 226, 224, 223,
    221, 219, 217, 215, 213, 211, 209, 207, 204, 202, 199, 196, 194, 191, 187,
    184, 181, 177, 172, 168, 162, 156, 148, 128, 108, 100, 94,  88,  84,  79,
    75,  72,  69,  65,  62,  60,  57,  54,  52,  49,  47,  45,  43,  41,  39,
    37,  35,  33,  32,  30,  28,  27,  25,  24,  23,  21,  20,  19,  17,  16,
    15,  14,  13,  12,  11,  10,  10,  9,   8,   7,   7,   6,   5,   5,   4,
    4,   3,   3,   3,   2,   2,   2,   1,   1,   1,   1,   1,   1,   1,   1,
    1,   1,   1,   2,   2,   2,   3,   3,   3,   4,   4,   5,   5,   6,   7,
    7,   8,   9,   10,  10,  11,  12,  13,  14,  15,  16,  17,  19,  20,  21,
    23,  24,  25,  27,  28,  30,  32,  33,  35,  37,  39,  41,  43,  45,  47,
    49,  52,  54,  57,  60,  62,  65,  69,  72,  75,  79,  84,  88,  94,  100,
    108};

uint8_t byte_sin(uint8_t n)
{
  return sintable[n];
}

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;
}