Skip to content

Commit

Permalink
changed cal options to have text fields instead of 0 and 1 + bugfix g…
Browse files Browse the repository at this point in the history
…oing to previous screen
  • Loading branch information
itzandroidtab committed Feb 25, 2024
1 parent b295a70 commit d5c099b
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 40 deletions.
5 changes: 4 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "ui/splash.hpp"
#include "ui/settings.hpp"
#include "ui/numeric_popup.hpp"
#include "ui/popup.hpp"
#include "ui/time.hpp"
#include "ui/calibration.hpp"
#include "ui/config.hpp"
Expand Down Expand Up @@ -151,6 +152,7 @@ int main() {

// create the popup first as some other screens use it
menu::numeric_popup<fb_t> numeric_popup = {};
menu::popup<fb_t> string_popup = {};

// setup the first state. We initialize everything
// except the screen in the splash screen. This speeds
Expand All @@ -160,7 +162,7 @@ int main() {
menu::totp<fb_t, storage, rtc, usb_keyboard> totp = {};
menu::settings<fb_t> settings = {};
menu::time<fb_t, rtc_periph, rtc> time(numeric_popup);
menu::calibration<fb_t, rtc_periph> calibration(numeric_popup);
menu::calibration<fb_t, rtc_periph> calibration(numeric_popup, string_popup);
menu::config<
fb_t, storage, fat_helper,
usb_keyboard, usb_massstorage
Expand All @@ -177,6 +179,7 @@ int main() {
&config,
&mouse,
&numeric_popup,
&string_popup,
};

// get the current screen
Expand Down
63 changes: 37 additions & 26 deletions ui/calibration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

namespace menu {
template <typename FrameBuffer, typename RtcPeriph>
Expand All @@ -26,7 +27,8 @@ namespace menu {
bool direction;

// popup we use to show the numbers
numeric_popup<FrameBuffer>& popup;
numeric_popup<FrameBuffer>& num_popup;
popup<FrameBuffer>& str_popup;

void next(int32_t value) {
// store the value
Expand Down Expand Up @@ -73,61 +75,70 @@ namespace menu {
}

void cancel() {
// reset and go back to the previous screen
current = steps::enabled;
if (current == steps::enabled) {
// go back to the menu
screen_base::buffer.back();

return;
}

// go back
screen_base::buffer.back();
// go back one screen
current = static_cast<steps>(static_cast<uint32_t>(current) - 1);

// change to the previous screen
change_screen(current);
}

void change_screen(const steps current) {
// get what state we are in
switch (current) {
case steps::enabled:
popup.configure(
"Cal enabled", static_cast<bool>(RtcPeriph::port->CCR & (0x1 << 4)),
0, 1, [&](int32_t value){next(value);},
str_popup.configure(
"RTC calibration", static_cast<bool>(RtcPeriph::port->CCR & (0x1 << 4)),
"enabled", "disabled", [&](bool value){next(value);},
[&](){cancel();}
);
break;
case steps::direction:
popup.configure(
str_popup.configure(
"Cal direction", static_cast<bool>(RtcPeriph::port->CALIBRATION & (0x1 << 17)),
0, 1, [&](int32_t value){next(value);},
"backward", "forward", [&](bool value){next(value);},
[&](){cancel();}
);
break;
case steps::calibration:
popup.configure(
num_popup.configure(
"Cal value", RtcPeriph::port->CALIBRATION & 0x1ffff,
0, 0x1ffff, [&](int32_t value){next(value);},
[&](){cancel();}
);
break;
}

// change to the popup screen
screen_base::buffer.change(screen_id::numeric_popup);
if (current == steps::calibration) {
// change to the popup screen
screen_base::buffer.change(screen_id::numeric_popup);
}
else {
// change to the popup screen
screen_base::buffer.change(screen_id::string_popup);
}
}

public:
calibration(numeric_popup<FrameBuffer>& popup):
current(steps::enabled), popup(popup)
calibration(numeric_popup<FrameBuffer>& num_popup, popup<FrameBuffer>& str_popup):
current(steps::enabled), num_popup(num_popup), str_popup(str_popup)
{}

virtual void main(const klib::time::us delta, const input::buttons& buttons) override {
// check for the first entry
if (current == steps::enabled) {
// show the first screen
change_screen(current);
}
else {
// reset and go back to the previous screen
current = steps::enabled;
// 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::enabled;

// go back
screen_base::buffer.back();
}
// show the first screen
change_screen(current);
}

virtual void draw(FrameBuffer& frame_buffer, const klib::vector2u& offset) override {
Expand Down
165 changes: 165 additions & 0 deletions ui/popup.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#pragma once

#include <functional>

#include <klib/graphics/bitmap.hpp>

#include "screen.hpp"

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

// callbacks for when we press a button
std::function<void(bool)> next;
std::function<void()> cancel;

// pointer to the string we should show
const char *str;
const char *up_str;
const char *down_str;

// max length between the up and down strings
uint32_t max_length;

// range of the numeric popup
bool value;

public:
popup():
next(nullptr), cancel(nullptr), str(nullptr),
up_str(nullptr), down_str(nullptr),
max_length(0), value(false)
{}

/**
* @brief configure the popup
*
* @param str
* @param continue
* @param cancel
*/
void configure(const char* str, const bool value, const char* up_str,
const char *down_str, std::function<void(bool)> next,
std::function<void()> cancel)
{
// update the string
this->str = str;

// update the options
this->value = value;
this->up_str = up_str;
this->down_str = down_str;

this->max_length = klib::max(
klib::string::strlen(up_str),
klib::string::strlen(down_str)
);

// update the callbacks
this->next = next;
this->cancel = cancel;
}

virtual void main(const klib::time::us delta, const input::buttons& buttons) override {
// check what button is pressed
if (buttons.enter == input::state::pressed) {
// before we do anything go back to the previous item
// in the buffer
screen_base::buffer.back();

// the enter button is pressed. Call the next screen
if (next) {
// call the next function with the value we got
next(value);
}
}
else if (buttons.enter == input::state::long_pressed) {
// before we do anything go back to the previous item
// in the buffer
screen_base::buffer.back();

// the enter button is long pressed. Call the cancel screen
if (cancel) {
// call the next function with the value we got
cancel();
}
}
else if (input::is_pressed(buttons.down) || input::is_pressed(buttons.up)) {
// the down button is pressed
value ^= true;
}
}

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

// get the string we should display
const char *ptr = value ? up_str : down_str;

// set the height to 40 pixels
const int32_t h = 40;

// calculate the width. At least the same as the height
const int32_t w = klib::max(h,
((max_length * screen_base::large_text::font::width) + 20) / 2
);

// draw the rectangle arround the number
screen_base::draw_rectangle(frame_buffer, klib::graphics::blue,
klib::vector2i{(240 / 2) - w, (134 / 2) - h} - offset.cast<int32_t>(),
klib::vector2i{(240 / 2) + w, (134 / 2) + h} - offset.cast<int32_t>()
);

// add 2 lines on both sides of the rectangle to round it a bit
screen_base::draw_rectangle(frame_buffer, klib::graphics::blue,
klib::vector2i{(240 / 2) - (w - 1), (134 / 2) - (h - 1)} - offset.cast<int32_t>(),
klib::vector2i{(240 / 2) + w, (134 / 2) + (h - 1)} - offset.cast<int32_t>()
);
screen_base::draw_rectangle(frame_buffer, klib::graphics::blue,
klib::vector2i{(240 / 2) - (w - 2), (134 / 2) - (h - 2)} - offset.cast<int32_t>(),
klib::vector2i{(240 / 2) + (w - 1), (134 / 2) + (h - 2)} - offset.cast<int32_t>()
);

screen_base::draw_rectangle(frame_buffer, klib::graphics::blue,
klib::vector2i{(240 / 2) - w, (134 / 2) - (h - 1)} - offset.cast<int32_t>(),
klib::vector2i{(240 / 2) + (w + 1), (134 / 2) + (h - 1)} - offset.cast<int32_t>()
);
screen_base::draw_rectangle(frame_buffer, klib::graphics::blue,
klib::vector2i{(240 / 2) - (w + 1), (134 / 2) - (h - 2)} - offset.cast<int32_t>(),
klib::vector2i{(240 / 2) + (w + 2), (134 / 2) + (h - 2)} - offset.cast<int32_t>()
);

// draw the bigmaps of the arrows

// draw the string using the small font
screen_base::large_text::template draw<FrameBuffer>(
frame_buffer,
str,
klib::vector2i{
(240 / 2) -
static_cast<int32_t>(
(klib::string::strlen(str) * screen_base::large_text::font::width) / 2
), 2
} - offset.cast<int32_t>(),
klib::graphics::white
);

// draw the string using the small font
screen_base::large_text::template draw<FrameBuffer>(
frame_buffer,
ptr,
klib::vector2i{
(240 / 2) -
static_cast<int32_t>(
(klib::string::strlen(ptr) * screen_base::large_text::font::width) / 2
), (134 / 2) - (screen_base::large_text::font::height / 2)
} - offset.cast<int32_t>(),
klib::graphics::white
);
}
};
}
1 change: 1 addition & 0 deletions ui/screen_id.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ namespace menu {
config,
mouse,
numeric_popup,
string_popup,
};
}
4 changes: 2 additions & 2 deletions ui/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ namespace menu {
// all the text fields in the settings menu
constexpr static char labels[label_count][16] = {
"usb mode",
"set time",
"set cal",
"time",
"rtc cal",
"usb jiggler",
"version",
};
Expand Down
21 changes: 10 additions & 11 deletions ui/time.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ namespace menu {

// go back one screen
current = static_cast<steps>(static_cast<uint32_t>(current) - 1);

// change to the previous screen
change_screen(current);
}

void change_screen(const steps current) {
Expand Down Expand Up @@ -181,18 +184,14 @@ namespace menu {
{}

virtual void main(const klib::time::us delta, const input::buttons& buttons) override {
// check for the first entry
if (current == steps::timezone) {
// show the first screen
change_screen(current);
}
else {
// reset and go back to the previous screen
current = steps::year;
// 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;

// go back
screen_base::buffer.back();
}
// show the first screen
change_screen(current);
}

virtual void draw(FrameBuffer& frame_buffer, const klib::vector2u& offset) override {
Expand Down

0 comments on commit d5c099b

Please sign in to comment.