forked from oseiler2/CO2Monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeatherMatrix.cpp
More file actions
100 lines (85 loc) · 3.16 KB
/
Copy pathfeatherMatrix.cpp
File metadata and controls
100 lines (85 loc) · 3.16 KB
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
#include <featherMatrix.h>
#include <config.h>
#include <configManager.h>
#include <Fonts/TomThumb.h>
#define BRIGHTNESS 20
// MATRIX DECLARATION:
// Parameter 1 = width of DotStar matrix
// Parameter 2 = height of matrix
// Parameter 3 = pin number (most are valid)
// Parameter 4 = matrix layout flags, add together as needed:
// DS_MATRIX_TOP, DS_MATRIX_BOTTOM, DS_MATRIX_LEFT, DS_MATRIX_RIGHT:
// Position of the FIRST LED in the matrix; pick two, e.g.
// DS_MATRIX_TOP + DS_MATRIX_LEFT for the top-left corner.
// DS_MATRIX_ROWS, DS_MATRIX_COLUMNS: LEDs are arranged in horizontal
// rows or in vertical columns, respectively; pick one or the other.
// DS_MATRIX_PROGRESSIVE, DS_MATRIX_ZIGZAG: all rows/columns proceed
// in the same order, or alternate lines reverse direction; pick one.
// See example below for these values in action.
// Parameter 5 = pixel type:
// DOTSTAR_BRG Pixels are wired for BRG bitstream (most DotStar items)
// DOTSTAR_GBR Pixels are wired for GBR bitstream (some older DotStars)
// DOTSTAR_BGR Pixels are wired for BGR bitstream (APA102-2020 DotStars)
FeatherMatrix::FeatherMatrix(Model* _model, uint8_t dataPin, uint8_t clockPin) {
this->model = _model;
this->matrix = new Adafruit_DotStarMatrix(
12, 6, dataPin, clockPin,
DS_MATRIX_BOTTOM + DS_MATRIX_LEFT +
DS_MATRIX_ROWS + DS_MATRIX_PROGRESSIVE,
DOTSTAR_BGR);
matrix->begin();
matrix->setFont(&TomThumb);
matrix->setTextWrap(false);
matrix->setBrightness(BRIGHTNESS);
for (byte i = 0; i < 3; i++) {
matrix->fillScreen(matrix->Color(255 * (i == 0 ? 1 : 0), 255 * (i == 1 ? 1 : 0), 255 * (i == 2 ? 1 : 0)));
matrix->show();
vTaskDelay(pdMS_TO_TICKS(500));
}
matrix->fillScreen(0);
matrix->show();
cyclicTimer = new Ticker();
cyclicTimer->attach(0.5, +[](FeatherMatrix* instance) { instance->timer(); }, this);
}
FeatherMatrix::~FeatherMatrix() {
if (this->matrix) delete matrix;
if (cyclicTimer) delete cyclicTimer;
};
void FeatherMatrix::update(uint16_t mask, TrafficLightStatus oldStatus, TrafficLightStatus newStatus) {
if (newStatus == GREEN) {
matrix->setTextColor(matrix->Color(0, 255, 0));
} else if (newStatus == YELLOW) {
matrix->setTextColor(matrix->Color(200, 255, 0));
} else if (newStatus == RED) {
matrix->setTextColor(matrix->Color(255, 0, 0));
} else if (newStatus == DARK_RED) {
matrix->setTextColor(matrix->Color(255, 0, 128));
}
matrix->fillScreen(0);
matrix->setCursor(0, 5);
scrollPosition = 0;
if (model->getCo2() == 0) {
strcpy(txt, "---");
} else {
sprintf(txt, "%u", model->getCo2());
}
int16_t x1, y1 = 0;
uint16_t h = 0;
matrix->getTextBounds(txt, 0, 0, &x1, &y1, &textWidth, &h);
scrollWidth = textWidth - matrix->width();
if (scrollWidth > 0) {
scrollWidth += 2;
}
matrix->print(txt);
matrix->show();
}
void FeatherMatrix::timer() {
matrix->fillScreen(0);
if (scrollWidth <= 0) return;
if (scrollPosition == 0) scrollDirection = -1;
else if (scrollPosition == -scrollWidth) scrollDirection = 1;
scrollPosition += scrollDirection;
matrix->setCursor(scrollPosition + (scrollWidth > 0 ? 1 : 0), 5);
matrix->print(txt);
matrix->show();
}