Skip to content

Commit 18a9306

Browse files
committed
splitted timezone from time to allow changing the timezone without changing the time
1 parent a213f13 commit 18a9306

File tree

5 files changed

+72
-21
lines changed

5 files changed

+72
-21
lines changed

main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "ui/numeric_popup.hpp"
99
#include "ui/popup.hpp"
1010
#include "ui/time.hpp"
11+
#include "ui/timezone.hpp"
1112
#include "ui/calibration.hpp"
1213
#include "ui/config.hpp"
1314
#include "ui/mouse.hpp"
@@ -147,6 +148,7 @@ int main() {
147148
menu::totp<fb_t, storage, rtc, rtc_periph, usb_keyboard> totp = {};
148149
menu::settings<fb_t> settings = {};
149150
menu::time<fb_t, rtc_periph, rtc> time(numeric_popup);
151+
menu::timezone<fb_t, rtc_periph> timezone(numeric_popup);
150152
menu::calibration<fb_t, rtc_periph> calibration(numeric_popup, string_popup);
151153
menu::config<
152154
fb_t, storage, fat_helper,
@@ -160,6 +162,7 @@ int main() {
160162
&totp,
161163
&settings,
162164
&time,
165+
&timezone,
163166
&calibration,
164167
&config,
165168
&mouse,

ui/screen_id.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace menu {
66
totp,
77
settings,
88
time,
9+
timezone,
910
calibration,
1011
config,
1112
mouse,

ui/settings.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace menu {
1616
enum class item: uint8_t {
1717
config = 0,
1818
time,
19+
timezone,
1920
calibration,
2021
mouse,
2122
version,
@@ -33,6 +34,7 @@ namespace menu {
3334
constexpr static char labels[label_count][16] = {
3435
"usb mode",
3536
"time",
37+
"timezone",
3638
"rtc cal",
3739
"usb jiggler",
3840
"version",
@@ -54,6 +56,9 @@ namespace menu {
5456
case item::time:
5557
screen_base::buffer.change(screen_id::time);
5658
break;
59+
case item::timezone:
60+
screen_base::buffer.change(screen_id::timezone);
61+
break;
5762
case item::calibration:
5863
screen_base::buffer.change(screen_id::calibration);
5964
break;

ui/time.hpp

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ namespace menu {
1212
using screen_base = screen<FrameBuffer>;
1313

1414
enum class steps: uint8_t {
15-
timezone = 0,
16-
year,
15+
year = 0,
1716
month,
1817
day,
1918
hour,
@@ -46,9 +45,6 @@ namespace menu {
4645
void next(int32_t value) {
4746
// store the value
4847
switch (current) {
49-
case steps::timezone:
50-
date.timezone = static_cast<int8_t>(value);
51-
break;
5248
case steps::year:
5349
date.year = static_cast<uint16_t>(value);
5450
break;
@@ -67,12 +63,6 @@ namespace menu {
6763
case steps::second:
6864
date.second = static_cast<uint8_t>(value);
6965

70-
// store the timezone in the rtc registers
71-
RtcPeriph::port->GPREG4 = (
72-
(RtcPeriph::port->GPREG4 & (~0x1f)) |
73-
static_cast<uint8_t>(date.timezone + 12)
74-
);
75-
7666
// we are done. Update the RTC time
7767
Rtc::set(klib::io::rtc::datetime_to_epoch(
7868
date.year, date.month,
@@ -96,7 +86,7 @@ namespace menu {
9686
}
9787

9888
void cancel() {
99-
if (current == steps::timezone) {
89+
if (current == steps::year) {
10090
// go back to the menu
10191
screen_base::buffer.back();
10292

@@ -118,13 +108,6 @@ namespace menu {
118108

119109
// get what state we are in
120110
switch (current) {
121-
case steps::timezone:
122-
popup.configure(
123-
"GMT", (RtcPeriph::port->GPREG4 & 0x1f) - 12,
124-
-12, 14, [&](int32_t value){next(value);},
125-
[&](){cancel();}
126-
);
127-
break;
128111
case steps::year:
129112
popup.configure(
130113
"Year", t.year,
@@ -180,15 +163,18 @@ namespace menu {
180163

181164
public:
182165
time(numeric_popup<FrameBuffer>& popup):
183-
current(steps::timezone), date{}, popup(popup)
166+
current(steps::year), date{}, popup(popup)
184167
{}
185168

186169
virtual void main(const klib::time::us delta, const input::buttons& buttons) override {
170+
// set the timezone we have in the register
171+
date.timezone = (RtcPeriph::port->GPREG4 & 0x1f) - 12;
172+
187173
// change to the first step when we are called. We
188174
// are only called from the settings menu. The
189175
// popup callbacks will skip this by changing
190176
// directly to the new popup
191-
current = steps::timezone;
177+
current = steps::year;
192178

193179
// show the first screen
194180
change_screen(current);

ui/timezone.hpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#pragma once
2+
3+
#include <klib/io/rtc.hpp>
4+
5+
#include "screen.hpp"
6+
#include "numeric_popup.hpp"
7+
8+
namespace menu {
9+
template <typename FrameBuffer, typename RtcPeriph>
10+
class timezone: public screen<FrameBuffer> {
11+
protected:
12+
using screen_base = screen<FrameBuffer>;
13+
14+
// popup to show items
15+
numeric_popup<FrameBuffer>& popup;
16+
17+
void next(int32_t value) {
18+
// store the timezone in the rtc registers
19+
RtcPeriph::port->GPREG4 = (
20+
(RtcPeriph::port->GPREG4 & (~0x1f)) |
21+
static_cast<uint8_t>(value + 12)
22+
);
23+
24+
// go back to the menu
25+
screen_base::buffer.back();
26+
}
27+
28+
void cancel() {
29+
// go back to the menu
30+
screen_base::buffer.back();
31+
}
32+
33+
public:
34+
timezone(numeric_popup<FrameBuffer>& popup):
35+
popup(popup)
36+
{}
37+
38+
virtual void main(const klib::time::us delta, const input::buttons& buttons) override {
39+
// show the first screen
40+
popup.configure(
41+
"GMT", (RtcPeriph::port->GPREG4 & 0x1f) - 12,
42+
-12, 14, [&](int32_t value){next(value);},
43+
[&](){cancel();}
44+
);
45+
46+
screen_base::buffer.change(screen_id::numeric_popup);
47+
}
48+
49+
virtual void draw(FrameBuffer& frame_buffer, const klib::vector2u& offset) override {
50+
// clear the background black
51+
frame_buffer.clear(klib::graphics::black);
52+
53+
// do nothing on a draw call
54+
}
55+
};
56+
}

0 commit comments

Comments
 (0)