-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay_handler.cpp
More file actions
61 lines (49 loc) · 1.72 KB
/
display_handler.cpp
File metadata and controls
61 lines (49 loc) · 1.72 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
#include "display_handler.h"
DisplayHandler::DisplayHandler(uint8_t pin)
: matrix(MATRIX_WIDTH * MATRIX_HEIGHT, pin, NEO_RGB + NEO_KHZ800),
isPulsing(false),
lastPulseUpdate(0),
pulseValue(MIN_BRIGHTNESS),
pulseIncreasing(true) {
}
void DisplayHandler::begin() {
matrix.begin();
matrix.setBrightness(BRIGHTNESS);
matrix.clear();
for(int i = 0; i < MATRIX_WIDTH * MATRIX_HEIGHT; i++) {
matrix.setPixelColor(i, 0);
}
matrix.show();
delay(50);
}
// Add this helper method to rotate pixel coordinates 180 degrees
uint16_t DisplayHandler::getRotatedPixel(uint16_t pixel) {
// For 8x8 matrix, rotate 180 degrees means:
// (x,y) becomes (7-x, 7-y)
uint8_t x = pixel % MATRIX_WIDTH;
uint8_t y = pixel / MATRIX_WIDTH;
return ((MATRIX_HEIGHT - 1 - y) * MATRIX_WIDTH) + (MATRIX_WIDTH - 1 - x);
}
void DisplayHandler::setPixelColor(uint16_t pixel, uint32_t color) {
matrix.setPixelColor(getRotatedPixel(pixel), color);
}
void DisplayHandler::update() {
if (!isPulsing) return;
unsigned long currentMillis = millis();
if (currentMillis - lastPulseUpdate < 30) return;
lastPulseUpdate = currentMillis;
float t = (float)(currentMillis) / 40000.0;
float sine = sin(2 * PI * t);
float normalized = (sine + 1.0) / 2.0;
uint8_t r = ((currentColor >> 16) & 0xFF) * normalized;
uint8_t g = ((currentColor >> 8) & 0xFF) * normalized;
uint8_t b = (currentColor & 0xFF) * normalized;
uint32_t scaledColor = matrix.Color(r, g, b);
fillScreen(scaledColor);
}
void DisplayHandler::fillScreen(uint32_t color) {
for(int i = 0; i < MATRIX_WIDTH * MATRIX_HEIGHT; i++) {
matrix.setPixelColor(i, color);
}
matrix.show();
}