Skip to content

Commit

Permalink
splitted timezone from time to allow changing the timezone without ch…
Browse files Browse the repository at this point in the history
…anging the time
  • Loading branch information
itzandroidtab committed May 27, 2024
1 parent a213f13 commit 18a9306
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 21 deletions.
3 changes: 3 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "ui/numeric_popup.hpp"
#include "ui/popup.hpp"
#include "ui/time.hpp"
#include "ui/timezone.hpp"
#include "ui/calibration.hpp"
#include "ui/config.hpp"
#include "ui/mouse.hpp"
Expand Down Expand Up @@ -147,6 +148,7 @@ int main() {
menu::totp<fb_t, storage, rtc, rtc_periph, usb_keyboard> totp = {};
menu::settings<fb_t> settings = {};
menu::time<fb_t, rtc_periph, rtc> time(numeric_popup);
menu::timezone<fb_t, rtc_periph> timezone(numeric_popup);
menu::calibration<fb_t, rtc_periph> calibration(numeric_popup, string_popup);
menu::config<
fb_t, storage, fat_helper,
Expand All @@ -160,6 +162,7 @@ int main() {
&totp,
&settings,
&time,
&timezone,
&calibration,
&config,
&mouse,
Expand Down
1 change: 1 addition & 0 deletions ui/screen_id.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace menu {
totp,
settings,
time,
timezone,
calibration,
config,
mouse,
Expand Down
5 changes: 5 additions & 0 deletions ui/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace menu {
enum class item: uint8_t {
config = 0,
time,
timezone,
calibration,
mouse,
version,
Expand All @@ -33,6 +34,7 @@ namespace menu {
constexpr static char labels[label_count][16] = {
"usb mode",
"time",
"timezone",
"rtc cal",
"usb jiggler",
"version",
Expand All @@ -54,6 +56,9 @@ namespace menu {
case item::time:
screen_base::buffer.change(screen_id::time);
break;
case item::timezone:
screen_base::buffer.change(screen_id::timezone);
break;
case item::calibration:
screen_base::buffer.change(screen_id::calibration);
break;
Expand Down
28 changes: 7 additions & 21 deletions ui/time.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ namespace menu {
using screen_base = screen<FrameBuffer>;

enum class steps: uint8_t {
timezone = 0,
year,
year = 0,
month,
day,
hour,
Expand Down Expand Up @@ -46,9 +45,6 @@ namespace menu {
void next(int32_t value) {
// store the value
switch (current) {
case steps::timezone:
date.timezone = static_cast<int8_t>(value);
break;
case steps::year:
date.year = static_cast<uint16_t>(value);
break;
Expand All @@ -67,12 +63,6 @@ namespace menu {
case steps::second:
date.second = static_cast<uint8_t>(value);

// store the timezone in the rtc registers
RtcPeriph::port->GPREG4 = (
(RtcPeriph::port->GPREG4 & (~0x1f)) |
static_cast<uint8_t>(date.timezone + 12)
);

// we are done. Update the RTC time
Rtc::set(klib::io::rtc::datetime_to_epoch(
date.year, date.month,
Expand All @@ -96,7 +86,7 @@ namespace menu {
}

void cancel() {
if (current == steps::timezone) {
if (current == steps::year) {
// go back to the menu
screen_base::buffer.back();

Expand All @@ -118,13 +108,6 @@ namespace menu {

// get what state we are in
switch (current) {
case steps::timezone:
popup.configure(
"GMT", (RtcPeriph::port->GPREG4 & 0x1f) - 12,
-12, 14, [&](int32_t value){next(value);},
[&](){cancel();}
);
break;
case steps::year:
popup.configure(
"Year", t.year,
Expand Down Expand Up @@ -180,15 +163,18 @@ namespace menu {

public:
time(numeric_popup<FrameBuffer>& popup):
current(steps::timezone), date{}, popup(popup)
current(steps::year), date{}, popup(popup)
{}

virtual void main(const klib::time::us delta, const input::buttons& buttons) override {
// set the timezone we have in the register
date.timezone = (RtcPeriph::port->GPREG4 & 0x1f) - 12;

// change to the first step when we are called. We
// are only called from the settings menu. The
// popup callbacks will skip this by changing
// directly to the new popup
current = steps::timezone;
current = steps::year;

// show the first screen
change_screen(current);
Expand Down
56 changes: 56 additions & 0 deletions ui/timezone.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#pragma once

#include <klib/io/rtc.hpp>

#include "screen.hpp"
#include "numeric_popup.hpp"

namespace menu {
template <typename FrameBuffer, typename RtcPeriph>
class timezone: public screen<FrameBuffer> {
protected:
using screen_base = screen<FrameBuffer>;

// popup to show items
numeric_popup<FrameBuffer>& popup;

void next(int32_t value) {
// store the timezone in the rtc registers
RtcPeriph::port->GPREG4 = (
(RtcPeriph::port->GPREG4 & (~0x1f)) |
static_cast<uint8_t>(value + 12)
);

// go back to the menu
screen_base::buffer.back();
}

void cancel() {
// go back to the menu
screen_base::buffer.back();
}

public:
timezone(numeric_popup<FrameBuffer>& popup):
popup(popup)
{}

virtual void main(const klib::time::us delta, const input::buttons& buttons) override {
// show the first screen
popup.configure(
"GMT", (RtcPeriph::port->GPREG4 & 0x1f) - 12,
-12, 14, [&](int32_t value){next(value);},
[&](){cancel();}
);

screen_base::buffer.change(screen_id::numeric_popup);
}

virtual void draw(FrameBuffer& frame_buffer, const klib::vector2u& offset) override {
// clear the background black
frame_buffer.clear(klib::graphics::black);

// do nothing on a draw call
}
};
}

0 comments on commit 18a9306

Please sign in to comment.