-
Notifications
You must be signed in to change notification settings - Fork 0
/
pixel_array.h
137 lines (66 loc) · 2.19 KB
/
pixel_array.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
#ifndef __PIXEL_ARRAY_H__
#define __PIXEL_ARRAY_H__
#include "led.h"
#include "pixel_rainbow.h"
extern const color_t pix_colorx[10];
class pixelArray : public ws2812b {
public:
pixelArray(uint8_t led_pin, uint16_t led_width, uint16_t led_height, const uint16_t *led_layout, color_t *led_buffer=NULL)
: ws2812b(led_pin, led_width*led_height),
grid_width( led_width),
grid_height(led_height) {
layout = led_layout;
grid = led_buffer
? led_buffer
: (color_t*) malloc(total() * sizeof(color_t));
}
void show();
int16_t index(int8_t x, int8_t y) const {
if (x < 0 || x >= width() || y < 0 || y >= height()) return -1;
return (y * width()) + x;
}
INLINE void setPixelColor(uint16_t pos, color_t color) {
if (pos < total()) grid[pos] = color;
}
INLINE color_t getPixelColor(uint16_t pos) const {
return (pos < total()) ? grid[pos] : color_t::black();
}
INLINE color_t *getPixels() {
return grid;
}
INLINE void draw(int8_t x, int8_t y, color_t color) {
int16_t pos = index(x, y);
if (pos != -1) setPixelColor(pos, color);
}
void draw(int8_t x, int8_t y);
INLINE color_t read(int8_t x, int8_t y) const {
int16_t pos = index(x, y);
return (pos != -1)
? color_t(getPixelColor(pos))
: color_t::black();
}
color_t swap(int8_t x, int8_t y, color_t color);
INLINE void symbol(uint8_t c, int8_t x, int8_t y, color_t color) {
const char z[2] = {c, 0};
string(z, x, y, color);
}
void string(const char *text, int16_t x_offset, int16_t y_offset);
void string(const char *text, int16_t x_offset, int16_t y_offset, color_t color);
static int16_t stringWidth(const char *text);
INLINE void clear() {
memset(grid, 0, this->total() * sizeof(color_t));
}
INLINE uint16_t width() const { return grid_width; }
INLINE uint16_t height() const { return grid_height; }
static void increment();
static void animation(PIXEL_RAINBOW rainbow) { color_anim = rainbow; }
protected:
const uint16_t grid_width;
const uint16_t grid_height;
const uint16_t *layout;
color_t *grid;
static uint8_t color_offset;
static PIXEL_RAINBOW color_anim;
static color_t color_palette[GRID_MAX];
};
#endif //__PIXEL_ARRAY_H__