/
PhysicalLedStrip.cpp
93 lines (79 loc) · 2.41 KB
/
PhysicalLedStrip.cpp
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "PhysicalLedStrip.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "ws2811.h"
#define ARRAY_SIZE(stuff) (sizeof(stuff) / sizeof(stuff[0]))
// defaults for cmdline options
#define TARGET_FREQ WS2811_TARGET_FREQ
#define GPIO_PIN 18
#define DMA 10
#define STRIP_TYPE SK6812_STRIP_GRBW
#define WIDTH 8
#define HEIGHT 8
#define LED_COUNT 132
int width = WIDTH;
int height = HEIGHT;
int led_count = LED_COUNT;
ws2811_t ledstring;
PhysicalLedStrip::PhysicalLedStrip()
{
}
PhysicalLedStrip::~PhysicalLedStrip()
{
}
void PhysicalLedStrip::init()
{
ledstring.freq = TARGET_FREQ;
ledstring.dmanum = DMA;
ledstring.channel[0].gpionum = GPIO_PIN;
ledstring.channel[0].invert = 0;
ledstring.channel[0].count = LED_COUNT;
ledstring.channel[0].strip_type = STRIP_TYPE;
ledstring.channel[0].brightness = 255;
ledstring.channel[0].wshift = 0;
ledstring.channel[0].rshift = 0;
ledstring.channel[0].gshift = 0;
ledstring.channel[0].bshift = 0;
ledstring.channel[0].gamma = 0;
ledstring.channel[1].gpionum = 0;
ledstring.channel[1].invert = 0;
ledstring.channel[1].count = 0;
ledstring.channel[1].strip_type = 0;
ledstring.channel[1].brightness = 0;
ledstring.channel[1].wshift = 0;
ledstring.channel[1].rshift = 0;
ledstring.channel[1].gshift = 0;
ledstring.channel[1].bshift = 0;
ledstring.channel[1].gamma = 0;
ws2811_return_t ret;
if ((ret = ws2811_init(&ledstring)) != WS2811_SUCCESS)
{
fprintf(stderr, "ws2811_init failed: %s\n", ws2811_get_return_t_str(ret));
return;
}
}
void PhysicalLedStrip::term()
{
reset();
usleep(100000); // Wait 1/10th second before terminating
ws2811_fini(&ledstring);
}
void PhysicalLedStrip::reset()
{
for (std::size_t i=0; i<LED_COUNT; ++i) {
ledstring.channel[0].leds[i] = 0;
}
ws2811_render(&ledstring);
}
void PhysicalLedStrip::refresh(const std::vector<NativeColor>& pixels)
{
for (unsigned i=0; i<pixels.size(); ++i) {
ledstring.channel[0].leds[i] = pixels[i];
}
ws2811_return_t ret;
if ((ret = ws2811_render(&ledstring)) != WS2811_SUCCESS) {
fprintf(stderr, "ws2811_render failed: %s\n", ws2811_get_return_t_str(ret));
}
}