forked from harbaum/make-block-reloaded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitials.ino
190 lines (156 loc) · 5.29 KB
/
initials.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
/*
initials.ino
*/
#include <FastLED.h>
#include <EEPROM.h>
#include "tetris.h"
#define INITIALS_BACK 1
#define INITIALS_DONE 2
#define INITIALS_SCORE_Y 2
#define INITIALS_Y 12
static char initials_str[16]; // 15 chars
static char initials_score_str[] = "NEW HIGH SCORE: xxxxxxx";
static int16_t initials_score_len, initials_score_scroll;
static int16_t initials_x;
static uint8_t initials_cur_chr;
static uint8_t initials_when_entered;
void initials_colorbar(uint8_t y, uint8_t colorIndex) {
CRGBPalette16 currentPalette = RainbowColors_p;
for( uint8_t x = 0; x < W; x++) {
LED(x,y) =
ColorFromPalette( currentPalette, colorIndex, 255, LINEARBLEND);
colorIndex += 3;
}
}
void initials_entry_draw() {
// erase area
for(uint8_t y=0;y<7;y++)
for(uint8_t x=0;x<W;x++)
LED(x, INITIALS_Y-1+y) = CRGB::Black;
// determine x offset
int16_t off = -1-(W/2); // char to edit is in the middle of the screen
// calculate offset of char in string
for(uint8_t i=0;i<initials_cur_chr;i++)
off += text_char_width(initials_str[i]);
// current char is centered
uint8_t cwid = text_char_width(initials_str[initials_cur_chr]);
off += cwid/2;
// draw highlight
for(uint8_t y=0;y<7;y++)
for(uint8_t x=0;x<cwid+1;x++)
LED(W/2-cwid/2+x, INITIALS_Y-1+y) = 0x800000; // 50% red
// draw text
text_scroll(initials_str, off, 0, W, INITIALS_Y, CRGB::White);
}
void initials_init(uint32_t score) {
LEDS.clear();
LEDS.setBrightness(config_brightness);
// load previous name from eeprom
// this is stored from byte 20 to 36
memset(initials_str, 0, 16);
if(EEPROM.read(20) == 0x42) {
for(uint8_t i=0;i<15;i++)
EEPROM.get(21+i, initials_str[i]);
} else
strcpy(initials_str, "A");
ltoa(score, initials_score_str+16, 10);
initials_score_len = text_str_len(initials_score_str);
initials_score_scroll = -2*W;
// editing starts with first char
initials_cur_chr = 0;
initials_entry_draw();
}
uint8_t initials_process(uint8_t keys, uint8_t score_position) {
// some colorful animation ...
static uint8_t coff = 0;
initials_colorbar(INITIALS_SCORE_Y-2, coff);
initials_colorbar(INITIALS_SCORE_Y+6, coff);
++coff;
// process name entry field
if((keys & KEY_LEFT) && initials_cur_chr > 0) {
// going left from BACK/OK? Erase that
if((initials_str[initials_cur_chr] == '\x01') ||
(initials_str[initials_cur_chr] == '\x02'))
initials_str[initials_cur_chr] = initials_when_entered;
initials_cur_chr--;
initials_entry_draw();
}
if(((keys & KEY_ROTATE) || (keys & KEY_RIGHT))
&& (initials_cur_chr < 14)) {
// can't go right from BACK or OK
if((initials_str[initials_cur_chr] != '\x01') &&
(initials_str[initials_cur_chr] != '\x02')) {
initials_cur_chr++;
initials_when_entered = initials_str[initials_cur_chr];
// check if there isn't already a character
if(!initials_str[initials_cur_chr])
initials_str[initials_cur_chr] = '\x02';
initials_entry_draw();
keys = 0; // prevent key from fire again
}
}
if(keys & KEY_ROTATE) {
if(initials_str[initials_cur_chr] == INITIALS_BACK) {
initials_cur_chr--;
for(uint8_t i=initials_cur_chr;i<15;i++)
initials_str[i] = initials_str[i+1];
// deleted second char? Then make first char a 'A' instead
// of the del char
if(!initials_cur_chr)
initials_str[initials_cur_chr] = 'A';
initials_entry_draw();
}
if(initials_str[initials_cur_chr] == INITIALS_DONE) {
// fill current string up with zeros
for(uint8_t i=initials_cur_chr;i<16;i++)
initials_str[i] = 0;
save_name_at(initials_str, score_position);
EEPROM.write(20, 0x42); // write magic marker
for(uint8_t i=0;i<15;i++) {
EEPROM.put(21+i, initials_str[i]);
}
return 1;
}
}
// "drop" is actually up
if((keys & KEY_DROP) || (keys & KEY_DOWN)) {
// keys are A-Z/0-9/-/SPC/BACK/OK
char c = initials_str[initials_cur_chr];
if(keys & KEY_DROP) {
if((c >= 'A') && (c < 'Z')) c++;
else if(c == 'Z') c = '0';
else if((c >= '0') && (c < '9')) c++;
else if(c == '9') c = '-';
else if(c == '-') c = ' ';
// first char cannot be removed and also not completed
else if((c == ' ') && (initials_cur_chr == 0)) c = 'A';
else if(c == ' ') c = INITIALS_BACK;
else if(c == INITIALS_BACK) c = INITIALS_DONE;
else if(c == INITIALS_DONE) c = 'A';
} else {
if((c == 'A') && (initials_cur_chr == 0)) c=' ';
else if(c == 'A') c = INITIALS_DONE;
else if((c > 'A') && (c <= 'Z')) c--;
else if(c == '0') c = 'Z';
else if((c > '0') && (c <= '9')) c--;
else if(c == '-') c = '9';
else if(c == ' ') c = '-';
else if(c == INITIALS_BACK) c = ' ';
else if(c == INITIALS_DONE) c = INITIALS_BACK;
}
initials_str[initials_cur_chr] = c;
initials_entry_draw();
}
// draw scrolling score, scroll at 30Hz
if(initials_score_scroll & 1) {
// clear text area
for(uint8_t y=0;y<5;y++)
for(uint8_t x=0;x<W;x++)
LED(x,INITIALS_SCORE_Y+y) = CRGB::Black;
text_scroll(initials_score_str, initials_score_scroll>>1, 0, W,
INITIALS_SCORE_Y, CRGB::White);
}
if(++initials_score_scroll > 2*(initials_score_len+W))
initials_score_scroll = -2*W;
return 0;
}