-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.cpp
More file actions
205 lines (194 loc) · 5.31 KB
/
Config.cpp
File metadata and controls
205 lines (194 loc) · 5.31 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
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
#include <Arduino.h>
#include "BTN.h"
#include "RTC.h"
#include "SparseXL.h"
#include "Game.h"
#include "Data.h"
#include "Config.h"
namespace Config {
void Button(int x, int y, const uint8_t* pData, const word* palette)
{
// draw the given button with top-left at (x, y), using palette
LCD_BEGIN_FILL(x, y, Data::ButtonSize, Data::ButtonSize);
while (true)
{
uint8_t data = pgm_read_byte_near(pData++);
if (data)
LCD_FILL_COLOUR(data >> 2, pgm_read_word_near(palette + (data & 0x03)));
else
break;
}
}
void Text(int16_t x, int16_t y, uint8_t idx)
{
// just 3 chars from above array, starting at idx
for (uint8_t ch = 0; ch < 3; ch++, idx++)
{
LCD_BEGIN_FILL(x, y, 8, 8);
for (uint8_t row = 0; row < 8; row++)
{
uint8_t data = pgm_read_byte_near(Data::Font() + 8*idx + row);
uint8_t mask = 0x80;
for (uint8_t col = 0; col < 8; col++, mask >>= 1)
{
LCD_FILL_BYTE(1, (data & mask) ? 0xFF : 0x00);
}
}
x += 8;
}
}
constexpr int ButtonTop = 5;
constexpr int ButtonSetLeft = 200;
constexpr int ButtonAdjLeft = 250;
byte BottomY = ButtonTop + Data::ButtonSize;
enum Btn { NoBtn, SetBtn, AdjBtn, OutsideBtn };
Btn GetButtonPressed(bool touch)
{
// returns button pressed, or NoBtn/OutsideBtn
// a touch on the graphical button, or a hard-key press
if (touch)
{
int x, y;
if (LCD_GET_TOUCH(x, y))
{
if (ButtonSetLeft <= x && x < ButtonSetLeft + Data::ButtonSize && y < BottomY)
return SetBtn;
else if (ButtonAdjLeft <= x && x < ButtonAdjLeft + Data::ButtonSize && y < BottomY)
return AdjBtn;
else if (y >= BottomY)
// clicked outside config area
return OutsideBtn;
}
}
else
{
if (btn1Set.CheckButtonPress())
return SetBtn;
else if (btn2Adj.CheckButtonPress())
return AdjBtn;
}
return NoBtn;
}
bool GetButtonUp(Btn btn, bool touch)
{
// returns true if the button is still pressed
// touching anywhere, or the hard-key released
if (touch)
{
int x, y;
return !LCD_GET_TOUCH(x, y);
}
else
{
if (btn == SetBtn)
return !btn1Set.IsDown();
else if (btn == AdjBtn)
return !btn2Adj.IsDown();
}
return false;
}
void SetTime(bool touch)
{
// set the RTC's time
#ifdef ENABLE_TIME_SCORE
rtc.ReadTime();
uint8_t hour24 = rtc.m_Hour24 % 24, minute = rtc.m_Minute % 60;
int16_t TimeY, LivesY, LabelY;
Game::GetElementPixelPositions(TimeY, LivesY);
LabelY = max(TimeY + 1, ButtonTop + Data::ButtonSize + 2);
BottomY = max(LabelY + 8 + 4, LivesY);
// Clear to line
LCD_FILL_BYTE(LCD_BEGIN_FILL(0, 0, LCD_WIDTH, BottomY), 0x00);
// Hz line above asteroids area
LCD_FILL_BYTE(LCD_BEGIN_FILL(0, BottomY, LCD_WIDTH, 1), 0xFF);
// Fixed labels
if (touch)
{
Text(ButtonSetLeft + (Data::ButtonSize - 3 * 8) / 2, LabelY, 0); // "SET"
Text(ButtonAdjLeft + (Data::ButtonSize - 3 * 8) / 2, LabelY, 3); // "ADJ"
}
bool update = true;
bool blink = false;
bool save = false;
Btn btnDown = NoBtn;
uint8_t mask = 0x01;
unsigned long idleMS, blinkMS;
idleMS = blinkMS = millis();
while (true)
{
if (btnDown == NoBtn)
{
btnDown = GetButtonPressed(touch);
if (btnDown == SetBtn)
{
if (mask == 0x01) // blinking hour->blinking minute
mask = 0x02;
else
mask = 0x03; // none blinking 2nd press of Set is being held
}
else if (btnDown == AdjBtn)
{
if (mask == 0x01) // blinkng hour
{
hour24++;
hour24 %= 24;
}
else
{
minute++;
minute %= 60;
}
}
else if (btnDown == OutsideBtn)
// clicked outside config area, bail
break;
if (btnDown != NoBtn)
{
update = true;
blink = false;
idleMS = blinkMS = millis();
}
}
else
{
// button is still down?
if (GetButtonUp(btnDown, touch))
{
btnDown = NoBtn;
if (mask == 0x03) // done
{
save = true;
break;
}
}
update = (btnDown == NoBtn);
}
unsigned long nowMS = millis();
if ((nowMS - blinkMS) > 500UL)
{
blink = !blink;
update = true;
blinkMS = millis();
}
if ((nowMS - idleMS) > 30000L)
break;
if (update)
{
Game::PaintConfigTime(hour24, minute, blink ? mask : 0x03);
if (touch)
{
Button(ButtonSetLeft, ButtonTop, Data::ButtonImage(btnDown == SetBtn), Data::ButtonPalette(true));
Button(ButtonAdjLeft, ButtonTop, Data::ButtonImage(btnDown == AdjBtn), Data::ButtonPalette(false));
}
update = false;
}
}
if (save)
{
rtc.m_Hour24 = hour24;
rtc.m_Minute = minute;
rtc.WriteTime();
}
#endif
}
}