-
Notifications
You must be signed in to change notification settings - Fork 0
/
inkplate_memento.ino
225 lines (204 loc) · 4.51 KB
/
inkplate_memento.ino
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
#include "Inkplate.h"
Inkplate display(INKPLATE_1BIT);
#include "pics/dinosaur.h"
#include "pics/conjuror.h"
#include "pics/skeleton.h"
#include "pics/birb.h"
#include "pics/anatomy.h"
#include "pics/cut_apple.h"
#include "pics/horse_bones.h"
#include "pics/macintosh.h"
struct {
const uint8_t *bits;
int w;
int h;
} pics[] = {
{ skeleton, skeleton_w, skeleton_h },
{ conjuror, conjuror_w, conjuror_h },
{ dinosaur, dinosaur_w, dinosaur_h },
{ birb, birb_w, birb_h },
{ anatomy, anatomy_w, anatomy_h },
{ cut_apple, cut_apple_w, cut_apple_h },
{ horse_bones, horse_bones_w, horse_bones_h },
{ macintosh, macintosh_w, macintosh_h },
{ NULL, 0, 0 }
};
#define LINES 9
#define LINE_W 64
char msg[LINES][LINE_W] = {
"Have a great day",
"",
"",
"",
"",
"",
"",
"",
""
};
#define ELEMENTS 10
enum el_type {
EL_NONE,
EL_TEXT,
EL_PIC,
EL_PAD
};
struct {
enum el_type type;
int n;
} elements[ELEMENTS] = {
{ EL_PIC, 0 },
{ EL_TEXT, 0 },
{ EL_TEXT, 1 },
{ EL_TEXT, 2 },
{ EL_TEXT, 3 },
{ EL_NONE, 0 },
{ EL_NONE, 0 },
{ EL_NONE, 0 },
{ EL_NONE, 0 },
{ EL_NONE, 0 }
};
#define DFL_PIC_TOP 80
#define DFL_TEXT_MARGIN 45
#define DFL_LINE_PADDING 10
int pic_top = DFL_PIC_TOP;
int text_margin = DFL_TEXT_MARGIN;
int line_padding = DFL_LINE_PADDING;
#include "font.h"
#define FONT Century_Schoolbook_L_Italic_32
#define PIC(n) pics[n].bits
#define PIC_W(n) pics[n].w
#define PIC_H(n) pics[n].h
#define DELAY 5000
int needsDraw = 1;
int width = 0;
int height = 0;
int16_t tx0 = 0;
int16_t tx1 = 0;
int16_t ty0 = 0;
int16_t ty1 = 0;
uint16_t tw = 0;
uint16_t th = 0;
void setup()
{
Serial.begin(115200);
display.begin();
display.setRotation(3);
display.clearDisplay();
display.display();
display.setFont(&FONT);
display.setTextColor(BLACK, WHITE);
width = display.width();
height = display.height();
}
void loop()
{
while (Serial.available()) {
int cmd = Serial.read();
switch (cmd) {
case '$':
{
int line = Serial.read();
if (line >= LINES) {
Serial.print("Bad line: ");
Serial.println(line);
}
int len = Serial.read();
int read = Serial.readBytesUntil('\0', msg[line], len);
msg[line][read] = '\0';
needsDraw = 1;
}
break;
case '=':
pic_top = DFL_PIC_TOP;
text_margin = DFL_TEXT_MARGIN;
line_padding = DFL_LINE_PADDING;
elements[0] = { EL_PIC, 0 };
for (int l = 0; l < LINES; l++) {
msg[l][0] = '\0';
elements[l+1] = { EL_TEXT, l };
}
needsDraw = 1;
break;
case '@':
elements[0] = { EL_PIC, Serial.read() };
needsDraw = 1;
break;
case '*':
{
enum el_type typ = EL_NONE;
int el_n = Serial.read();
int typ_c = Serial.read();
int n = Serial.read();
switch(typ_c) {
case '@':
typ = EL_PIC;
break;
case '$':
typ = EL_TEXT;
break;
case '_':
typ = EL_PAD;
break;
default:
typ = EL_NONE;
break;
}
elements[el_n] = { typ, n };
}
needsDraw = 1;
break;
case '<':
pic_top = Serial.read();
needsDraw = 1;
break;
case '>':
text_margin = Serial.read();
needsDraw = 1;
break;
case '_':
line_padding = Serial.read();
needsDraw = 1;
break;
default:
Serial.print("Unknown cmd ");
Serial.println(cmd);
}
}
if (needsDraw) {
display.clearDisplay();
ty0 = pic_top;
tx0 = 0;
for (int el = 0; el < ELEMENTS; el++) {
int el_n = elements[el].n;
switch (elements[el].type) {
case EL_PIC:
display.drawImage(
PIC(el_n),
(width / 2) - (PIC_W(el_n) / 2),
ty0,
PIC_W(el_n),
PIC_H(el_n),
BLACK
);
ty0 += PIC_H(el_n) + text_margin;
break;
case EL_TEXT:
display.getTextBounds(msg[el_n], tx0, ty0, &tx1, &ty1, &tw, &th);
display.setCursor((width / 2) - (tw / 2), ty0);
display.print(msg[el_n]);
ty0 += th + line_padding;
break;
case EL_PAD:
ty0 += el_n;
break;
case EL_NONE:
// pass
break;
}
}
display.display();
needsDraw = 0;
}
delay(DELAY);
}