aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorConfuSomu2024-02-14 15:18:29 -0500
committerConfuSomu2024-02-14 15:18:29 -0500
commit29875b53cf74d4e7a4e3e461a12c49c030b5b22c (patch)
tree6710f16e6b877b0040ca44cf7ebdd573c0da46f3
parente953d44fb79e77ee300f67d1f18107cd95bbc6dc (diff)
downloadpico-watch-29875b53cf74d4e7a4e3e461a12c49c030b5b22c.tar
pico-watch-29875b53cf74d4e7a4e3e461a12c49c030b5b22c.tar.gz
pico-watch-29875b53cf74d4e7a4e3e461a12c49c030b5b22c.zip
Implement getting current time in SDL build
-rw-r--r--apps/home_menu/main.cpp2
-rw-r--r--hal/api.cpp2
-rw-r--r--hal/sdl2/display.cpp30
-rw-r--r--hal/sdl2/display.hpp2
4 files changed, 28 insertions, 8 deletions
diff --git a/apps/home_menu/main.cpp b/apps/home_menu/main.cpp
index ffa279c..200a71f 100644
--- a/apps/home_menu/main.cpp
+++ b/apps/home_menu/main.cpp
@@ -5,8 +5,6 @@
#include "../../app/app_manager.hpp"
#include "main.hpp"
-extern bool rtc_get_datetime(datetime_t *t);
-
void app_home_menu::title_str(char *buf, uint buf_size, const datetime_t *t) {
snprintf(buf,
buf_size,
diff --git a/hal/api.cpp b/hal/api.cpp
index a983c9b..961cdd5 100644
--- a/hal/api.cpp
+++ b/hal/api.cpp
@@ -328,7 +328,7 @@ bool Api::datetime_get(datetime_t *t) {
#ifndef SDL2_BUILD
return rtc_get_datetime(t);
#else
- return false;
+ return m_sdl_display.get_datetime(t);
#endif
}
diff --git a/hal/sdl2/display.cpp b/hal/sdl2/display.cpp
index 64b92cd..a5cff6a 100644
--- a/hal/sdl2/display.cpp
+++ b/hal/sdl2/display.cpp
@@ -1,7 +1,9 @@
#ifdef SDL2_BUILD
#include "display.hpp"
#include "../../buttons.hpp"
+#include "pico/types.h"
#include <iostream>
+#include <time.h>
#define SCREEN_HEIGHT 64
#define SCREEN_WIDTH 128
@@ -43,6 +45,13 @@ SDLDisplay::SDLDisplay() {
SDL_SetPaletteColors(m_surface->format->palette, colors, 0, 2);
}
+SDLDisplay::~SDLDisplay() {
+ SDL_DestroyTexture(m_texture);
+ SDL_DestroyRenderer(m_renderer);
+ SDL_DestroyWindow(m_window);
+ SDL_Quit();
+}
+
bool SDLDisplay::process_input() {
SDL_Event e;
while (SDL_PollEvent(&e) != 0) {
@@ -75,11 +84,22 @@ bool SDLDisplay::update_display(const uint8_t* oled_screen) {
return false;
}
-SDLDisplay::~SDLDisplay() {
- SDL_DestroyTexture(m_texture);
- SDL_DestroyRenderer(m_renderer);
- SDL_DestroyWindow(m_window);
- SDL_Quit();
+bool SDLDisplay::get_datetime(datetime_t* t) {
+ // Get current date and time
+ time_t rawtime;
+ struct tm* timeinfo;
+ time(&rawtime);
+ timeinfo = localtime(&rawtime);
+
+ // Map ISO C tm struct to Pico's datetime_t
+ t->day = timeinfo->tm_mday;
+ t->dotw = timeinfo->tm_wday;
+ t->month = timeinfo->tm_mon;
+ t->year = timeinfo->tm_year + 1900;
+ t->hour = timeinfo->tm_hour;
+ t->min = timeinfo->tm_min;
+ t->sec = timeinfo->tm_sec;
+ return true;
}
unsigned char rorb(unsigned char x, unsigned char n) {
diff --git a/hal/sdl2/display.hpp b/hal/sdl2/display.hpp
index a267cae..e9302bc 100644
--- a/hal/sdl2/display.hpp
+++ b/hal/sdl2/display.hpp
@@ -1,6 +1,7 @@
#pragma once
#ifdef SDL2_BUILD
#include <SDL.h>
+#include "pico/types.h"
class SDLDisplay {
public:
@@ -10,6 +11,7 @@ public:
// When returning false, please quit the program (return from main)
bool update_display(const uint8_t* oled_screen);
bool process_input();
+ bool get_datetime(datetime_t* t);
private:
SDL_Window* m_window = nullptr;
SDL_Renderer* m_renderer = nullptr;