forked from Electry/Arduino_SK6812
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SK6812.cpp
54 lines (44 loc) · 1.01 KB
/
SK6812.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
#include "SK6812.h"
#include <stdlib.h>
SK6812::SK6812(uint16_t num_leds)
{
_count_led = num_leds;
_pixels = (RGBW *)malloc(_count_led * sizeof(RGBW));
}
SK6812::~SK6812()
{
free(_pixels);
}
void SK6812::set_output(uint8_t pin)
{
_pin_mask = digitalPinToBitMask(pin);
_port = portOutputRegister(digitalPinToPort(pin));
_port_reg = portModeRegister(digitalPinToPort(pin));
}
RGBW SK6812::get_rgbw(uint16_t index)
{
RGBW px_value;
if (index < _count_led) {
px_value.r = _pixels[index].r;
px_value.g = _pixels[index].g;
px_value.b = _pixels[index].b;
px_value.w = _pixels[index].w;
}
return px_value;
}
uint8_t SK6812::set_rgbw(uint16_t index, RGBW px_value)
{
if (index < _count_led) {
_pixels[index].r = px_value.r;
_pixels[index].g = px_value.g;
_pixels[index].b = px_value.b;
_pixels[index].w = px_value.w;
return 0;
}
return 1;
}
void SK6812::sync()
{
*_port_reg |= _pin_mask;
sendarray_mask((uint8_t *)_pixels, _count_led * sizeof(RGBW), _pin_mask, (uint8_t *)_port, (uint8_t *)_port_reg);
}