Skip to content

Commit

Permalink
Merge pull request #591 from rennancockles/dev
Browse files Browse the repository at this point in the history
refactor timer setup and fix misaligned setting underline
  • Loading branch information
rennancockles authored Dec 22, 2024
2 parents 966b7f1 + 858d5f6 commit bfcc813
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 70 deletions.
14 changes: 7 additions & 7 deletions src/core/menu_items/OthersMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ void OthersMenu::optionsMenu() {
#ifdef MIC_SPM1423
{"Mic Spectrum", [=]() { mic_test(); }},
#endif
{"BadUSB", [=]() { usb_setup(); }},
{"BadUSB", [=]() { usb_setup(); }},
#ifdef HAS_KEYBOARD_HID
{"USB Keyboard", [=]() { usb_keyboard(); }},
{"USB Keyboard", [=]() { usb_keyboard(); }},
#endif
#ifdef HAS_RGB_LED
{"LED Control", [=]() { ledrgb_setup(); }}, // IncursioHack
{"LED FLash", [=]() { ledrgb_flash(); }}, // IncursioHack
{"LED Control", [=]() { ledrgb_setup(); }}, // IncursioHack
{"LED FLash", [=]() { ledrgb_flash(); }}, // IncursioHack
#endif
#ifndef LITE_VERSION
{"Openhaystack", [=]() { openhaystack_setup(); }},
{"Openhaystack", [=]() { openhaystack_setup(); }},
#endif
#if !defined(CORE) && !defined(CORE2)
{"Interpreter", [=]() { run_bjs_script(); }},
{"Interpreter", [=]() { run_bjs_script(); }},
#endif
{"Timer", [=]() { Timer(); }},
{"Main Menu", [=]() { backToMenu(); }},
{"Main Menu", [=]() { backToMenu(); }},
};

delay(200);
Expand Down
155 changes: 92 additions & 63 deletions src/modules/others/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,84 +16,37 @@ Timer::Timer() {
setup();
}

void Timer::loop() {
unsigned long startMillis = millis();
unsigned long currentMillis;
unsigned long elapsedMillis;

char timeString[9];

Timer::~Timer() {
tft.fillScreen(bruceConfig.bgColor);

for (;;) {
currentMillis = millis();
elapsedMillis = currentMillis - startMillis;

unsigned long remainingMillis = duration + 1000 - elapsedMillis;

int seconds = (remainingMillis / 1000) % 60;
int minutes = (remainingMillis / 60000) % 60;
int hours = (remainingMillis / 3600000);

snprintf(timeString, sizeof(timeString), "%02d:%02d:%02d", hours, minutes, seconds);

tft.setTextColor(bruceConfig.priColor, bruceConfig.bgColor);
drawMainBorder(false);
tft.setTextSize(4);
tft.drawCentreString(timeString, WIDTH / 2, HEIGHT / 2 - 13, 1);

if (checkEscPress()) {
duration = 0;
tft.fillScreen(bruceConfig.bgColor);
returnToMenu = true;
break;
}

if (elapsedMillis >= duration) {
tft.fillScreen(bruceConfig.bgColor);
_tone(2000, 1000);
returnToMenu = true;
break;
}

delay(DELAY_VALUE);
}
backToMenu();
}


void Timer::setup() {

int hours = 0;
int minutes = 0;
int seconds = 0;
int settingMode = 0;
int underlineHeight = HEIGHT / 3 * 2;
int underlineWidth = WIDTH / 5;

char timeString[9];

tft.fillScreen(bruceConfig.bgColor);

delay(DELAY_VALUE);

for (;;) {
while (true) {
snprintf(timeString, sizeof(timeString), "%02d:%02d:%02d", hours, minutes, seconds);

tft.setTextColor(bruceConfig.priColor, bruceConfig.bgColor);
tft.setTextSize(2);
drawMainBorderWithTitle("Set a timer", false);
tft.setTextSize(4);
tft.drawCentreString(timeString, WIDTH / 2, HEIGHT / 2 - 13, 1);

if (settingMode == 0) {
tft.drawLine(WIDTH / 10 * 7, underlineHeight, WIDTH / 10 * 7 + underlineWidth, underlineHeight, bruceConfig.bgColor);
tft.drawLine(WIDTH / 10, underlineHeight, WIDTH / 10 + underlineWidth, underlineHeight, bruceConfig.priColor);
} else if (settingMode == 1) {
tft.drawLine(WIDTH / 10, underlineHeight, WIDTH / 10 + underlineWidth, underlineHeight, bruceConfig.bgColor);
tft.drawLine(WIDTH / 10 * 4, underlineHeight, WIDTH / 10 * 4 + underlineWidth, underlineHeight, bruceConfig.priColor);
} else if (settingMode == 2) {
tft.drawLine(WIDTH / 10 * 4, underlineHeight, WIDTH / 10 * 4 + underlineWidth, underlineHeight, bruceConfig.bgColor);
tft.drawLine(WIDTH / 10 * 7, underlineHeight, WIDTH / 10 * 7 + underlineWidth, underlineHeight, bruceConfig.priColor);
}
tft.setTextSize(fontSize);
tft.drawCentreString(timeString, timerX, timerY, 1);

clearUnderline();

if (settingMode == 0) underlineHours();
else if (settingMode == 1) underlineMinutes();
else if (settingMode == 2) underlineSeconds();

if (checkNextPress()) {
if (settingMode == 0 && ++hours > 99) {
Expand All @@ -118,15 +71,91 @@ void Timer::setup() {
if (checkSelPress()) {
if (++settingMode > 2 && (hours > 0 || minutes > 0 || seconds > 0)) {
duration = (hours * 3600 + minutes * 60 + seconds) * 1000;
loop();
break;
}

if (settingMode > 2 && hours == 0 && minutes == 0 && seconds == 0) {
settingMode = 0;
}
if (settingMode > 2) settingMode = 0;
}

delay(DELAY_VALUE);
}

return loop();
}


void Timer::loop() {
unsigned long startMillis = millis();
unsigned long currentMillis;
unsigned long elapsedMillis;

char timeString[9];

tft.fillScreen(bruceConfig.bgColor);

while (true) {
currentMillis = millis();
elapsedMillis = currentMillis - startMillis;

unsigned long remainingMillis = duration + 1000 - elapsedMillis;

int seconds = (remainingMillis / 1000) % 60;
int minutes = (remainingMillis / 60000) % 60;
int hours = (remainingMillis / 3600000);

snprintf(timeString, sizeof(timeString), "%02d:%02d:%02d", hours, minutes, seconds);

drawMainBorder(false);

tft.setTextSize(4);
tft.setTextColor(bruceConfig.priColor, bruceConfig.bgColor);
tft.drawCentreString(timeString, timerX, timerY, 1);

if (checkEscPress()) {
break;
}

if (elapsedMillis >= duration) {
_tone(2000, 1000);
break;
}

delay(DELAY_VALUE);
}
}


void Timer::clearUnderline() {
tft.drawLine(
BORDER_PAD_X, underlineY,
WIDTH - BORDER_PAD_X, underlineY,
bruceConfig.bgColor
);
}


void Timer::underlineHours() {
tft.drawLine(
timerX - (4 * LW * fontSize), underlineY,
timerX - (2 * LW * fontSize), underlineY,
bruceConfig.priColor
);
}


void Timer::underlineMinutes() {
tft.drawLine(
timerX - (LW * fontSize), underlineY,
timerX + (LW * fontSize), underlineY,
bruceConfig.priColor
);
}


void Timer::underlineSeconds() {
tft.drawLine(
timerX + (2 * LW * fontSize), underlineY,
timerX + (4 * LW * fontSize), underlineY,
bruceConfig.priColor
);
}
10 changes: 10 additions & 0 deletions src/modules/others/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,21 @@
class Timer {
private:

int fontSize = 4;
int duration = 0;
int timerX = WIDTH / 2;
int timerY = HEIGHT / 2;
int underlineY = timerY + (fontSize + 1) * LH;

void clearUnderline();
void underlineHours();
void underlineMinutes();
void underlineSeconds();

public:

Timer();
~Timer();

void setup();
void loop();
Expand Down

0 comments on commit bfcc813

Please sign in to comment.