/
TM1637_DotMatrixLEDController.h
347 lines (276 loc) · 15.4 KB
/
TM1637_DotMatrixLEDController.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//
// Copyright (c) 2019 smdn (https://smdn.jp/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#ifndef TM1637_DotMatrixLEDController_h
#define TM1637_DotMatrixLEDController_h
#include "TM1637Controller.h"
template <
size_t NUM_OF_ANODE_LINES, // = number of anode pins
size_t NUM_OF_CATHODE_LINES // = number of cathode pins
>
class TM1637_DotMatrixLEDController
: public TM1637Controller
{
static_assert(0 < NUM_OF_ANODE_LINES && NUM_OF_ANODE_LINES <= TM1637Controller::NUM_OF_GRIDS, "NUM_OF_ANODE_LINES must be in range of 1~6");
static_assert(0 < NUM_OF_CATHODE_LINES && NUM_OF_CATHODE_LINES <= TM1637Controller::NUM_BITS_PER_GRID, "NUM_OF_CATHODE_LINES must be in range of 1~8");
public:
static constexpr inline _ATTR_ALWAYS_INLINE_ size_t numberOfAnodeLines() { return NUM_OF_ANODE_LINES; }
static constexpr inline _ATTR_ALWAYS_INLINE_ size_t numberOfCathodeLines() { return NUM_OF_CATHODE_LINES; }
void fill(const bool flush = true);
void clear(const bool flush = true);
void invert(const bool flush = true);
inline _ATTR_ALWAYS_INLINE_ void flush() const { flushAllAnodeLines(); }
virtual void fillRow(const unsigned int row, const bool flush = true) = 0;
virtual void clearRow(const unsigned int row, const bool flush = true) = 0;
virtual void fillColumn(const unsigned int column, const bool flush = true) = 0;
virtual void clearColumn(const unsigned int column, const bool flush = true) = 0;
inline _ATTR_ALWAYS_INLINE_ void setOn(const unsigned int column, const unsigned int row, const bool flush = true) { set(column, row, true, flush); }
inline _ATTR_ALWAYS_INLINE_ void setOff(const unsigned int column, const unsigned int row, const bool flush = true) { set(column, row, false, flush); }
virtual void set(const unsigned int column, const unsigned int row, const bool trueForOnOtherwiseOff, const bool flush = true) = 0;
protected:
TM1637_DotMatrixLEDController(
uint8_t pinDIO,
uint8_t pinCLK
) : TM1637Controller(pinDIO, pinCLK) {}
void fillAnodeLine(const unsigned int line, const bool flush = true);
void clearAnodeLine(const unsigned int line, const bool flush = true);
void fillCathodeLine(const unsigned int line, const bool flush = true);
void clearCathodeLine(const unsigned int line, const bool flush = true);
void setAt(const unsigned int cathodeLine, const unsigned int anodeLine, const bool trueForOnOtherwiseOff, const bool flush = true);
void setWithBitmap(const uint8_t (&bitmap)[NUM_OF_ANODE_LINES], const bool flush = true);
void setWithBitmapFlipVertical(const uint8_t (&bitmap)[NUM_OF_ANODE_LINES], const bool flush = true);
void setWithBitmapRotate90(const uint8_t (&bitmap)[NUM_OF_CATHODE_LINES], const bool flush = true);
void setWithBitmapRotate270(const uint8_t (&bitmap)[NUM_OF_CATHODE_LINES], const bool flush = true);
private:
static constexpr size_t BITS_OF_CATHODE_LINES = TM1637Controller::NUM_BITS_PER_GRID;
static constexpr uint8_t AVAILABLE_CATHODE_LINE_MASK = (uint8_t)(0xffu << (BITS_OF_CATHODE_LINES - NUM_OF_CATHODE_LINES));
static constexpr uint8_t BIT_CATHODE_LINE0 = 1u << (BITS_OF_CATHODE_LINES - 1);
inline _ATTR_ALWAYS_INLINE_ void flushAllAnodeLines() const;
inline _ATTR_ALWAYS_INLINE_ void flushAnodeLine(const unsigned int line) const;
uint8_t m_anodeLineBits[NUM_OF_ANODE_LINES] = {0}; // in MSBFIRST order
};
template <
size_t NUM_OF_COLS, // must be in range of 1~8
size_t NUM_OF_ROWS // must be in range of 1~NUMBER_OF_GRIDS(6)
>
class TM1637_CathodeColumnsAnodeRowsDotMatrixLEDController
: public TM1637_DotMatrixLEDController<NUM_OF_ROWS, NUM_OF_COLS>
{
static_assert(0 < NUM_OF_ROWS && NUM_OF_ROWS <= TM1637Controller::NUM_OF_GRIDS, "NUM_OF_ROWS must be in range of 1~6");
static_assert(0 < NUM_OF_COLS && NUM_OF_COLS <= TM1637Controller::NUM_BITS_PER_GRID, "NUM_OF_COLS must be in range of 1~8");
public:
static constexpr inline _ATTR_ALWAYS_INLINE_ size_t numberOfRows() { return NUM_OF_ROWS; }
static constexpr inline _ATTR_ALWAYS_INLINE_ size_t numberOfColumns() { return NUM_OF_COLS; }
TM1637_CathodeColumnsAnodeRowsDotMatrixLEDController(
uint8_t pinDIO,
uint8_t pinCLK
) : TM1637_DotMatrixLEDController<NUM_OF_ROWS, NUM_OF_COLS>(pinDIO, pinCLK) {}
inline _ATTR_ALWAYS_INLINE_ void fillRow(const unsigned int row, const bool flush = true) override { this->fillAnodeLine(row, flush); }
inline _ATTR_ALWAYS_INLINE_ void clearRow(const unsigned int row, const bool flush = true) override { this->clearAnodeLine(row, flush); }
inline _ATTR_ALWAYS_INLINE_ void fillColumn(const unsigned int column, const bool flush = true) override { this->fillCathodeLine(column, flush); }
inline _ATTR_ALWAYS_INLINE_ void clearColumn(const unsigned int column, const bool flush = true) override { this->clearCathodeLine(column, flush); }
void set(const unsigned int column, const unsigned int row, const bool trueForOnOtherwiseOff, const bool flush = true) override { this->setAt(column, row, trueForOnOtherwiseOff, flush); }
inline _ATTR_ALWAYS_INLINE_ void draw(const uint8_t (&bitmapTopLeftOrigin)[NUM_OF_ROWS], const bool flush = true) { this->setWithBitmap(bitmapTopLeftOrigin, flush); }
inline _ATTR_ALWAYS_INLINE_ void drawRotated(const uint8_t (&bitmapBottomLeftOrigin)[NUM_OF_COLS], const bool flush = true) { this->setWithBitmapRotate90(bitmapBottomLeftOrigin, flush); }
};
using TM1637_8CathodeColumns6AnodeRowsDotMatrixLEDController = TM1637_CathodeColumnsAnodeRowsDotMatrixLEDController<8, 6>;
using TM1637_7CathodeColumns5AnodeRowsDotMatrixLEDController = TM1637_CathodeColumnsAnodeRowsDotMatrixLEDController<7, 5>;
using TM1637_4CathodeColumns4AnodeRowsDotMatrixLEDController = TM1637_CathodeColumnsAnodeRowsDotMatrixLEDController<4, 4>;
using TM1637_3CathodeColumns3AnodeRowsDotMatrixLEDController = TM1637_CathodeColumnsAnodeRowsDotMatrixLEDController<3, 3>;
using TM1637_2CathodeColumns2AnodeRowsDotMatrixLEDController = TM1637_CathodeColumnsAnodeRowsDotMatrixLEDController<2, 2>;
template <
size_t NUM_OF_COLS, // must be in range of 1~NUMBER_OF_GRIDS(6)
size_t NUM_OF_ROWS // must be in range of 1~8
>
class TM1637_AnodeColumnsCathodeRowsDotMatrixLEDController
: public TM1637_DotMatrixLEDController<NUM_OF_COLS, NUM_OF_ROWS>
{
static_assert(0 < NUM_OF_ROWS && NUM_OF_ROWS <= TM1637Controller::NUM_BITS_PER_GRID, "NUM_OF_ROWS must be in range of 1~8");
static_assert(0 < NUM_OF_COLS && NUM_OF_COLS <= TM1637Controller::NUM_OF_GRIDS, "NUM_OF_COLS must be in range of 1~6");
public:
static constexpr inline _ATTR_ALWAYS_INLINE_ size_t numberOfRows() { return NUM_OF_ROWS; }
static constexpr inline _ATTR_ALWAYS_INLINE_ size_t numberOfColumns() { return NUM_OF_COLS; }
TM1637_AnodeColumnsCathodeRowsDotMatrixLEDController(
uint8_t pinDIO,
uint8_t pinCLK
) : TM1637_DotMatrixLEDController<NUM_OF_COLS, NUM_OF_ROWS>(pinDIO, pinCLK) {}
inline _ATTR_ALWAYS_INLINE_ void fillRow(const unsigned int row, const bool flush = true) override { this->fillCathodeLine(row, flush); }
inline _ATTR_ALWAYS_INLINE_ void clearRow(const unsigned int row, const bool flush = true) override { this->clearCathodeLine(row, flush); }
inline _ATTR_ALWAYS_INLINE_ void fillColumn(const unsigned int column, const bool flush = true) override { this->fillAnodeLine(column, flush); }
inline _ATTR_ALWAYS_INLINE_ void clearColumn(const unsigned int column, const bool flush = true) override { this->clearAnodeLine(column, flush); }
void set(const unsigned int column, const unsigned int row, const bool trueForOnOtherwiseOff, const bool flush = true) override { this->setAt(row, column, trueForOnOtherwiseOff, flush); }
inline _ATTR_ALWAYS_INLINE_ void draw(const uint8_t (&bitmapTopLeftOrigin)[NUM_OF_ROWS], const bool flush = true) { this->setWithBitmapRotate270(bitmapTopLeftOrigin, flush); }
inline _ATTR_ALWAYS_INLINE_ void drawRotated(const uint8_t (&bitmapBottomLeftOrigin)[NUM_OF_COLS], const bool flush = true) { this->setWithBitmapFlipVertical(bitmapBottomLeftOrigin, flush); }
};
using TM1637_6AnodeColumns8CathodeRowsDotMatrixLEDController = TM1637_AnodeColumnsCathodeRowsDotMatrixLEDController<6, 8>;
using TM1637_5AnodeColumns7CathodeRowsDotMatrixLEDController = TM1637_AnodeColumnsCathodeRowsDotMatrixLEDController<5, 7>;
using TM1637_4AnodeColumns4CathodeRowsDotMatrixLEDController = TM1637_AnodeColumnsCathodeRowsDotMatrixLEDController<4, 4>;
using TM1637_3AnodeColumns3CathodeRowsDotMatrixLEDController = TM1637_AnodeColumnsCathodeRowsDotMatrixLEDController<3, 3>;
using TM1637_2AnodeColumns2CathodeRowsDotMatrixLEDController = TM1637_AnodeColumnsCathodeRowsDotMatrixLEDController<2, 2>;
/*
* TM1637_DotMatrixLEDController
*/
template <size_t NUM_OF_ANODE_LINES, size_t NUM_OF_CATHODE_LINES>
void TM1637_DotMatrixLEDController<NUM_OF_ANODE_LINES, NUM_OF_CATHODE_LINES>::flushAllAnodeLines() const
{
this->transferDataToAddressAutomatic(m_anodeLineBits, NUM_OF_ANODE_LINES, MSBFIRST, 0);
}
template <size_t NUM_OF_ANODE_LINES, size_t NUM_OF_CATHODE_LINES>
void TM1637_DotMatrixLEDController<NUM_OF_ANODE_LINES, NUM_OF_CATHODE_LINES>::flushAnodeLine(const unsigned int line) const
{
this->transferDataToAddressFixed(m_anodeLineBits[line], MSBFIRST, line);
}
template <size_t NUM_OF_ANODE_LINES, size_t NUM_OF_CATHODE_LINES>
void TM1637_DotMatrixLEDController<NUM_OF_ANODE_LINES, NUM_OF_CATHODE_LINES>::fill(const bool flush)
{
for (auto al = 0u; al < NUM_OF_ANODE_LINES; al++)
m_anodeLineBits[al] = 0b11111111u & AVAILABLE_CATHODE_LINE_MASK;
if (flush)
flushAllAnodeLines();
}
template <size_t NUM_OF_ANODE_LINES, size_t NUM_OF_CATHODE_LINES>
void TM1637_DotMatrixLEDController<NUM_OF_ANODE_LINES, NUM_OF_CATHODE_LINES>::clear(const bool flush)
{
for (auto al = 0u; al < NUM_OF_ANODE_LINES; al++)
m_anodeLineBits[al] = 0b00000000u;
if (flush)
flushAllAnodeLines();
}
template <size_t NUM_OF_ANODE_LINES, size_t NUM_OF_CATHODE_LINES>
void TM1637_DotMatrixLEDController<NUM_OF_ANODE_LINES, NUM_OF_CATHODE_LINES>::invert(const bool flush)
{
for (auto al = 0u; al < NUM_OF_ANODE_LINES; al++)
m_anodeLineBits[al] = ~m_anodeLineBits[al] & AVAILABLE_CATHODE_LINE_MASK;
if (flush)
flushAllAnodeLines();
}
template <size_t NUM_OF_ANODE_LINES, size_t NUM_OF_CATHODE_LINES>
void TM1637_DotMatrixLEDController<NUM_OF_ANODE_LINES, NUM_OF_CATHODE_LINES>::fillAnodeLine(const unsigned int line, const bool flush)
{
if (NUM_OF_ANODE_LINES <= line)
return; // out of range
m_anodeLineBits[line] = 0b11111111u & AVAILABLE_CATHODE_LINE_MASK;
if (flush)
flushAnodeLine(line);
}
template <size_t NUM_OF_ANODE_LINES, size_t NUM_OF_CATHODE_LINES>
void TM1637_DotMatrixLEDController<NUM_OF_ANODE_LINES, NUM_OF_CATHODE_LINES>::clearAnodeLine(const unsigned int line, const bool flush)
{
if (NUM_OF_ANODE_LINES <= line)
return; // out of range
m_anodeLineBits[line] = 0b00000000u;
if (flush)
flushAnodeLine(line);
}
template <size_t NUM_OF_ANODE_LINES, size_t NUM_OF_CATHODE_LINES>
void TM1637_DotMatrixLEDController<NUM_OF_ANODE_LINES, NUM_OF_CATHODE_LINES>::fillCathodeLine(const unsigned int line, const bool flush)
{
if (NUM_OF_CATHODE_LINES <= line)
return; // out of range
uint8_t mask = BIT_CATHODE_LINE0 >> line;
for (auto al = 0u; al < NUM_OF_ANODE_LINES; al++)
m_anodeLineBits[al] |= mask;
if (flush)
flushAllAnodeLines();
}
template <size_t NUM_OF_ANODE_LINES, size_t NUM_OF_CATHODE_LINES>
void TM1637_DotMatrixLEDController<NUM_OF_ANODE_LINES, NUM_OF_CATHODE_LINES>::clearCathodeLine(const unsigned int line, const bool flush)
{
if (NUM_OF_CATHODE_LINES <= line)
return; // out of range
uint8_t mask = ~(BIT_CATHODE_LINE0 >> line);
for (auto al = 0u; al < NUM_OF_ANODE_LINES; al++)
m_anodeLineBits[al] &= mask;
if (flush)
flushAllAnodeLines();
}
template <size_t NUM_OF_ANODE_LINES, size_t NUM_OF_CATHODE_LINES>
void TM1637_DotMatrixLEDController<NUM_OF_ANODE_LINES, NUM_OF_CATHODE_LINES>::setAt(
const unsigned int cathodeLine,
const unsigned int anodeLine,
const bool trueForOnOtherwiseOff,
const bool flush
)
{
if (NUM_OF_CATHODE_LINES <= cathodeLine)
return; // out of range
if (NUM_OF_ANODE_LINES <= anodeLine)
return; // out of range
uint8_t mask = BIT_CATHODE_LINE0 >> cathodeLine;
if (trueForOnOtherwiseOff)
m_anodeLineBits[anodeLine] |= mask;
else
m_anodeLineBits[anodeLine] &= ~mask;
if (flush)
flushAnodeLine(anodeLine);
}
template <size_t NUM_OF_ANODE_LINES, size_t NUM_OF_CATHODE_LINES>
void TM1637_DotMatrixLEDController<NUM_OF_ANODE_LINES, NUM_OF_CATHODE_LINES>::setWithBitmap(
const uint8_t (&bitmap)[NUM_OF_ANODE_LINES],
const bool flush
)
{
for (auto al = 0u; al < NUM_OF_ANODE_LINES; al++)
m_anodeLineBits[al] = bitmap[al] << (BITS_OF_CATHODE_LINES - NUM_OF_CATHODE_LINES);
if (flush)
flushAllAnodeLines();
}
template <size_t NUM_OF_ANODE_LINES, size_t NUM_OF_CATHODE_LINES>
void TM1637_DotMatrixLEDController<NUM_OF_ANODE_LINES, NUM_OF_CATHODE_LINES>::setWithBitmapRotate90(
const uint8_t (&bitmap)[NUM_OF_CATHODE_LINES],
const bool flush
)
{
for (auto al = 0u; al < NUM_OF_ANODE_LINES; al++) {
uint8_t lineBits = 0u;
for (auto cl = 0u; cl < NUM_OF_CATHODE_LINES; cl++)
lineBits |= ((bitmap[cl] >> (NUM_OF_ANODE_LINES - 1 - al)) & 0b1) << (cl + BITS_OF_CATHODE_LINES - NUM_OF_CATHODE_LINES);
m_anodeLineBits[al] = lineBits & AVAILABLE_CATHODE_LINE_MASK;
}
if (flush)
flushAllAnodeLines();
}
template <size_t NUM_OF_ANODE_LINES, size_t NUM_OF_CATHODE_LINES>
void TM1637_DotMatrixLEDController<NUM_OF_ANODE_LINES, NUM_OF_CATHODE_LINES>::setWithBitmapFlipVertical(
const uint8_t (&bitmap)[NUM_OF_ANODE_LINES],
const bool flush
)
{
for (auto al = 0u; al < NUM_OF_ANODE_LINES; al++)
m_anodeLineBits[al] = (bitmap[NUM_OF_ANODE_LINES - 1 - al] << (BITS_OF_CATHODE_LINES - NUM_OF_CATHODE_LINES)) & AVAILABLE_CATHODE_LINE_MASK;
if (flush)
flushAllAnodeLines();
}
template <size_t NUM_OF_ANODE_LINES, size_t NUM_OF_CATHODE_LINES>
void TM1637_DotMatrixLEDController<NUM_OF_ANODE_LINES, NUM_OF_CATHODE_LINES>::setWithBitmapRotate270(
const uint8_t (&bitmap)[NUM_OF_CATHODE_LINES],
const bool flush
)
{
for (auto al = 0u; al < NUM_OF_ANODE_LINES; al++) {
uint8_t lineBits = 0u;
for (auto cl = 0u; cl < NUM_OF_CATHODE_LINES; cl++)
lineBits |= ((bitmap[NUM_OF_CATHODE_LINES - 1 - cl] >> (NUM_OF_ANODE_LINES - 1 - al)) & 0b1) << (cl + BITS_OF_CATHODE_LINES - NUM_OF_CATHODE_LINES);
m_anodeLineBits[al] = lineBits & AVAILABLE_CATHODE_LINE_MASK;
}
if (flush)
flushAllAnodeLines();
}
#endif // TM1637_DotMatrixLEDController_h