-
Notifications
You must be signed in to change notification settings - Fork 2
/
BlinkyShield.cpp
174 lines (146 loc) · 3.82 KB
/
BlinkyShield.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "mbed.h"
#include "BlinkyShield.h"
#include "PixelArray.h"
#include "WS2812.h"
#include "string.h"
#include "stdio.h"
BlinkyShield::BlinkyShield(PinName pin)
: Adafruit_GFX(5, 8)
{
_shield = new WS2812((PinName)pin, STRIPLEN,
MBED_CONF_BLINKY_SHIELD_WS2812_ZERO_HIGH,
MBED_CONF_BLINKY_SHIELD_WS2812_ZERO_LOW,
MBED_CONF_BLINKY_SHIELD_WS2812_ONE_HIGH,
MBED_CONF_BLINKY_SHIELD_WS2812_ONE_LOW);
_px = new PixelArray(STRIPLEN);
off();
}
BlinkyShield::~BlinkyShield()
{
delete _shield;
delete _px;
}
void BlinkyShield::setIntensity(uint8_t intensity)
{
_shield->useII(WS2812::GLOBAL);
_shield->setII(intensity);
}
void BlinkyShield::wipeColor(uint16_t color, uint8_t n_pixels)
{
uint32_t c = colorCvtRev(color);
for (uint8_t i = 0; i < STRIPLEN; i++)
{
if (i < n_pixels)
{
_px->Set(i, c & 0xffffff);
}
else
{
_px->Set(i, 0);
}
}
_shield->write_offsets(_px->getBuf(),0,0,0);
}
void BlinkyShield::red(uint8_t n_pixels)
{
wipeColor(RED, n_pixels);
}
void BlinkyShield::green(uint8_t n_pixels)
{
wipeColor(GREEN, n_pixels);
}
void BlinkyShield::blue(uint8_t n_pixels)
{
wipeColor(BLUE, n_pixels);
}
void BlinkyShield::off()
{
wipeColor(0);
}
void BlinkyShield::drawPixel(int16_t x, int16_t y, uint16_t color)
{
if ((x >= this->width()) || (y >= this->height()))
return;
uint8_t n = y + (this->width() - x - 1) * this->height();
_px->Set(n, colorCvtRev(color));
_shield->write_offsets(_px->getBuf(),0,0,0);
}
void BlinkyShield::scroll(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
const uint8_t max_str_len = 100;
char s[max_str_len] = {0};
vsnprintf(s, max_str_len, fmt, args);
uint8_t len = strnlen(s, max_str_len);
int len_in_px = len * 6;
this->setTextWrap(false);
for (int x = this->width(); x > -len_in_px; --x)
{
this->clear();
this->setCursor(x, 1);
this->printf(s);
wait_ms(200);
}
va_end(args);
}
void BlinkyShield::clear()
{
this->off();
this->setCursor(0, 0);
}
uint16_t BlinkyShield::colorCvt(uint32_t color)
{
uint32_t r = (color >> 16 & 0xff) * 0x1f / 0xff;
uint32_t g = (color >> 8 & 0xff) * 0x3f / 0xff;
uint32_t b = (color >> 0 & 0xff) * 0x1f / 0xff;
return r << 11 | g << 5 | b;
}
uint32_t BlinkyShield::colorCvtRev(uint16_t color)
{
uint32_t r = (color >> 11 & 0x1f) * 0xff / 0x1f;
uint32_t g = (color >> 5 & 0x3f) * 0xff / 0x3f;
uint32_t b = (color >> 0 & 0x1f) * 0xff / 0x1f;
return r << 16 | g << 8 | b;
}
uint16_t BlinkyShield::color(uint8_t r, uint8_t g, uint8_t b)
{
uint32_t r_prime = (r & 0xff) << 16;
uint32_t g_prime = (g & 0xff) << 8;
uint32_t b_prime = (b & 0xff) << 0;
return colorCvt(r_prime | g_prime | b_prime);
}
uint32_t BlinkyShield::numPixels()
{
return 40;
}
void BlinkyShield::setPixelColor(uint8_t x, uint8_t y, uint16_t color)
{
if ((x >= this->width()) || (y >= this->height()))
return;
uint8_t n = y + (this->width() - x - 1) * this->height();
this->setPixelColor(n, color);
}
void BlinkyShield::setPixelColor(uint16_t n, uint16_t color)
{
_px->Set(n, colorCvtRev(color));
}
void BlinkyShield::show()
{
_shield->write(_px->getBuf());
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t BlinkyShield::colorWheel(uint8_t wheel_pos)
{
wheel_pos = 255 - wheel_pos;
if (wheel_pos < 85) {
return this->color(255 - wheel_pos * 3, 0, wheel_pos * 3);
} else if (wheel_pos < 170) {
wheel_pos -= 85;
return this->color(0, wheel_pos * 3, 255 - wheel_pos * 3);
} else {
wheel_pos -= 170;
return this->color(wheel_pos * 3, 255 - wheel_pos * 3, 0);
}
}