-
Notifications
You must be signed in to change notification settings - Fork 0
/
apa102.h
73 lines (60 loc) · 2.09 KB
/
apa102.h
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
/* File: apa102.h */
/* Date: Wed 25 Mar 2015 10:15:11 PM CET */
/* Author: Fabian Wermelinger */
/* Tag: APA102 LED */
/* Copyright 2015 Fabian Wermelinger. All Rights Reserved. */
#include <cstdint>
#include <vector>
class APA102
{
public:
typedef uint8_t RAWTYPE;
// representation of one LED
struct LED
{
// order matters here, defined by APA102 datasheet
RAWTYPE brightness, B, G, R;
// default
LED();
// color 3-tuples
LED(const unsigned int bright, const RAWTYPE red, const RAWTYPE green, const RAWTYPE blue);
LED(const RAWTYPE red, const RAWTYPE green, const RAWTYPE blue);
// hex color
LED(const unsigned int bright, const unsigned int color);
LED(const unsigned int color);
inline void set_brightness(const unsigned int bright)
{
brightness = 0xe0 + static_cast<RAWTYPE>(bright/100.0f * 31.0f);
}
inline void set_brightness31(const unsigned int bright31)
{
brightness = 0xe0 + bright31;
}
inline void set_color(const RAWTYPE red, const RAWTYPE green, const RAWTYPE blue)
{
B = blue; G = green; R = red;
}
inline void set_color(const unsigned int hex_color)
{
B = hex_color; G = hex_color >> 8; R = hex_color >> 16;
}
void increase_brightness();
void decrease_brightness();
void state() const;
};
private:
const unsigned int n_led;
const unsigned int nbits_start, nbits_end;
std::vector<RAWTYPE> rawdata;
LED * const leds;
public:
APA102(const unsigned int n = 1);
~APA102();
inline unsigned int size() const { return n_led; }
inline LED * begin() const { return leds; }
inline LED * end() const { return leds + n_led; }
inline LED& operator[](const unsigned int i) { return *(leds + i); }
inline const LED& operator[](const unsigned int i) const { return *(leds + i); }
inline unsigned int datasize() const { return rawdata.size(); }
inline RAWTYPE* data() { return &rawdata[0]; }
};