-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.cpp
154 lines (142 loc) · 4.09 KB
/
display.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
#include "string.h"
#include "display.h"
#include "neopixels.h"
uint8_t Display::get_pixel(int8_t x, int8_t y) {
int index = y * width + x;
if (index < 0 || index >= MAX_DISPLAY_PIXELS) {
return 0;
}
return state[index];
}
uint8_t Display::set_pixel(int8_t x, int8_t y, uint8_t val) {
int16_t index = y * width + x;
if (index < 0 || index >= MAX_DISPLAY_PIXELS) {
return false;
}
uint8_t old_value = state[index];
state[index] = val;
return old_value;
}
bool Display::set_rect(int8_t x, int8_t y, int8_t width, int8_t height, uint8_t val) {
bool result = false;
for (int8_t cx = x; cx < x + width; cx++) {
for (int8_t cy = y; cy < y + height; cy++) {
result = set_pixel(cx, cy, val) || result;
}
}
return result;
}
void Display::clear_all() {
memset(&state, 0, MAX_DISPLAY_PIXELS);
}
void Display::refresh(bool multi_display, get_pixel_func_t get_pixel_func) {
uint8_t total_pixels = width * height;
uint8_t r_shift = 0;
uint8_t l_shift = 0;
switch (brightness) {
case 0:
r_shift = 7;
break;
case 1:
r_shift = 2;
break;
case 2:
r_shift = 1;
break;
default:
l_shift = brightness - 3;
break;
};
for (int p = 0; p < total_pixels; p++) {
int i = p;
if (!neopixels) {
i = total_pixels - 1 - i;
}
int8_t x, y;
switch (mode) {
case Mode::rows: {
x = i % width;
y = i / width;
break;
}
case Mode::U: {
if (i < total_pixels / 2) {
x = i % (width / 2);
y = i / (width / 2);
} else {
x = width / 2 + i % (width / 2);
y = (height - 1) - (i - total_pixels / 2) / (width / 2);
}
break;
}
case Mode::N: {
x = i / height;
y = i % height;
if (x % 2 == 1) {
y = height - y;
}
break;
}
}
uint8_t val = get_pixel(x, y);
if (neopixels) {
if (palette) {
if (multi_display) {
// In multi-display mode, the host display's pixel is in the low 4 bits, and
// the peripheral display's pixel is in the high 4 bits.
uint8_t *pixel1 = palette[val & 0xF];
uint8_t *pixel2 = palette[val >> 4];
// WS2812-compatible LEDs expect colors in GRB order. We use the sendByte function
// here instead of sendPixel, to have less time between bits sent; if it's too long
// (5 microseconds), the screen will refresh.
cli();
uint8_t g1 = pixel1[1];
uint8_t g2 = pixel2[1];
Neopixels::sendByte((g1 >> r_shift) << l_shift, (g2 >> r_shift) << l_shift);
uint8_t r1 = pixel1[0];
uint8_t r2 = pixel2[0];
Neopixels::sendByte((r1 >> r_shift) << l_shift, (r2 >> r_shift) << l_shift);
uint8_t b1 = pixel1[2];
uint8_t b2 = pixel2[2];
Neopixels::sendByte((b1 >> r_shift) << l_shift, (b2 >> r_shift) << l_shift);
sei();
} else {
uint8_t *pixel = palette[val];
uint8_t r = pixel[0];
uint8_t g = pixel[1];
uint8_t b = pixel[2];
if (get_pixel_func) {
uint32_t p = get_pixel_func(x, y);
r = (p >> 24) & 0xFF;
g = (p >> 16) & 0xFF;
b = (p >> 8) & 0xFF;
}
Neopixels::sendPixel((r >> r_shift) << l_shift, (g >> r_shift) << l_shift, (b >> r_shift) << l_shift);
}
} else {
uint8_t pixel_brightness = (val ? brightness : 0);
Neopixels::sendPixel(pixel_brightness, pixel_brightness, pixel_brightness);
}
} else {
digitalWrite(SER_PIN, val);
digitalWrite(SRCLK_PIN, HIGH);
digitalWrite(SRCLK_PIN, LOW);
}
}
if (neopixels) {
Neopixels::show();
} else {
digitalWrite(RCLK_PIN, HIGH);
digitalWrite(RCLK_PIN, LOW);
}
}
void Display::set_brightness(uint8_t brightness) {
// Output enable on 595 shift registers is on when grounded
this->brightness = brightness;
if (BRIGHTNESS_ENABLED) {
analogWrite(OE_PIN, 255 - this->brightness);
}
}
uint8_t Display::get_brightness() {
return brightness;
}