add new ClockScreenSaver
Some checks failed
ESP-IDF Build / build (esp32s3, latest) (push) Failing after 22s
ESP-IDF Build / build (esp32s3, release-v5.4) (push) Failing after 18s
ESP-IDF Build / build (esp32s3, release-v5.5) (push) Failing after 21s

Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
2025-08-21 23:11:42 +02:00
parent f199f0d781
commit 1ead66520b
23 changed files with 939 additions and 40 deletions

View File

@@ -0,0 +1,122 @@
#include "ui/ClockScreenSaver.h"
#include <cstring>
#include <ctime>
ClockScreenSaver::ClockScreenSaver(menu_options_t *options)
: Widget(options->u8g2), m_options(options), m_moveTimer(0), m_posX(0), m_posY(0), m_velocityX(X_VELOCITY),
m_velocityY(Y_VELOCITY), m_textWidth(0), m_textHeight(0)
{
initPosition();
}
void ClockScreenSaver::initPosition()
{
// Start in the center of the screen
updateTextDimensions();
m_posX = (u8g2->width - m_textWidth) / 2;
m_posY = (u8g2->height - m_textHeight) / 2;
// Set initial velocity
m_velocityX = X_VELOCITY;
m_velocityY = Y_VELOCITY;
}
void ClockScreenSaver::updateTextDimensions()
{
char timeBuffer[32];
getCurrentTimeString(timeBuffer, sizeof(timeBuffer));
// Set font (use a large font for better visibility)
u8g2_SetFont(u8g2, FONT);
// Calculate text dimensions
m_textWidth = u8g2_GetStrWidth(u8g2, timeBuffer);
m_textHeight = u8g2_GetAscent(u8g2) - u8g2_GetDescent(u8g2);
}
void ClockScreenSaver::getCurrentTimeString(char *buffer, size_t bufferSize) const
{
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
// Format time as HH:MM:SS
strftime(buffer, bufferSize, "%H:%M:%S", timeinfo);
}
void ClockScreenSaver::update(const uint64_t dt)
{
m_moveTimer += dt;
// Move the clock position at regular intervals
if (m_moveTimer > MOVE_INTERVAL)
{
m_moveTimer = 0;
// Update text dimensions (in case time format changes)
updateTextDimensions();
// Update position
m_posX += m_velocityX;
m_posY += m_velocityY;
// Check for collisions with screen boundaries
checkBoundaryCollision();
}
}
void ClockScreenSaver::checkBoundaryCollision()
{
// Check horizontal boundaries
if (m_posX <= TEXT_PADDING)
{
m_posX = TEXT_PADDING;
m_velocityX = X_VELOCITY; // Bounce right
}
else if (m_posX + m_textWidth >= u8g2->width - TEXT_PADDING)
{
m_posX = u8g2->width - m_textWidth - TEXT_PADDING;
m_velocityX = -X_VELOCITY; // Bounce left
}
// Check vertical boundaries
if (m_posY <= TEXT_PADDING + m_textHeight)
{
m_posY = TEXT_PADDING + m_textHeight;
m_velocityY = Y_VELOCITY; // Bounce down
}
else if (m_posY >= u8g2->height - TEXT_PADDING)
{
m_posY = u8g2->height - TEXT_PADDING;
m_velocityY = -Y_VELOCITY; // Bounce up
}
}
void ClockScreenSaver::render()
{
// Clear screen with a black background
u8g2_SetDrawColor(u8g2, 0);
u8g2_DrawBox(u8g2, 0, 0, u8g2->width, u8g2->height);
u8g2_SetDrawColor(u8g2, 1);
// Get current time
char timeBuffer[32];
getCurrentTimeString(timeBuffer, sizeof(timeBuffer));
// Set font
u8g2_SetFont(u8g2, FONT);
// Draw the time at current position
u8g2_DrawStr(u8g2, m_posX, m_posY, timeBuffer);
}
void ClockScreenSaver::onButtonClicked(ButtonType button)
{
// Exit screensaver on any button press
if (m_options && m_options->popScreen)
{
m_options->popScreen();
}
}

View File

@@ -9,15 +9,15 @@
*/
namespace LightMenuItem
{
constexpr uint8_t ACTIVATE = 0; ///< ID for the light activation toggle
constexpr uint8_t MODE = 1; ///< ID for the light mode selection
constexpr uint8_t LED_SETTINGS = 2; ///< ID for the LED settings menu item
constexpr auto ACTIVATE = 0; ///< ID for the light activation toggle
constexpr auto MODE = 1; ///< ID for the light mode selection
constexpr auto LED_SETTINGS = 2; ///< ID for the LED settings menu item
} // namespace LightMenuItem
namespace LightMenuOptions
{
constexpr std::string LIGHT_ACTIVE = "light_active";
constexpr std::string LIGHT_MODE = "light_mode";
constexpr auto LIGHT_ACTIVE = "light_active";
constexpr auto LIGHT_MODE = "light_mode";
} // namespace LightMenuOptions
LightMenu::LightMenu(menu_options_t *options) : Menu(options), m_options(options)
@@ -57,7 +57,8 @@ void LightMenu::onButtonPressed(const MenuItem &menuItem, const ButtonType butto
if (button == ButtonType::SELECT)
{
toggle(menuItem);
if (getItem(menuItem.getId()).getValue() == "1")
const auto value = getItem(menuItem.getId()).getValue() == "1";
if (value)
{
led_event_data_t payload = {.value = 42};
send_event(EVENT_LED_ON, &payload);
@@ -69,7 +70,6 @@ void LightMenu::onButtonPressed(const MenuItem &menuItem, const ButtonType butto
}
if (m_options && m_options->persistenceManager)
{
const auto value = getItem(menuItem.getId()).getValue() == "1";
m_options->persistenceManager->SetValue(LightMenuOptions::LIGHT_ACTIVE, value);
m_options->persistenceManager->Save();
}
@@ -82,9 +82,12 @@ void LightMenu::onButtonPressed(const MenuItem &menuItem, const ButtonType butto
const auto item = switchValue(menuItem, button);
if (button == ButtonType::LEFT || button == ButtonType::RIGHT)
{
const auto value = getItem(item.getId()).getIndex();
led_event_data_t payload = {.value = value};
send_event(EVENT_LED_DAY + value, &payload);
if (m_options && m_options->persistenceManager)
{
const auto value = getItem(item.getId()).getIndex();
m_options->persistenceManager->SetValue(LightMenuOptions::LIGHT_MODE, value);
m_options->persistenceManager->Save();
}