new clock

timer by GMMan and settings by kowalski7cc
This commit is contained in:
MX 2022-09-14 19:25:31 +03:00
parent abce958db9
commit 7a2b5be137
No known key found for this signature in database
GPG Key ID: 6C4C311DFD4B4AB5
6 changed files with 367 additions and 81 deletions

View File

@ -5,7 +5,21 @@ App(
entry_point="clock_app",
cdefines=["APP_CLOCK"],
requires=["gui"],
provides=["clock_settings"],
icon="A_Clock_14",
stack_size=2 * 1024,
order=9,
)
App(
appid="clock_settings",
name="Clock",
apptype=FlipperAppType.SETTINGS,
entry_point="clock_settings_app",
requires=["gui","clock"],
stack_size=1 * 1024,
order=20,
)

View File

@ -1,68 +1,110 @@
#include <furi.h>
#include <furi_hal.h>
#include <gui/gui.h>
#include <gui/elements.h>
#include <gui/gui.h>
#include <input/input.h>
#define TAG "Clock"
#define CLOCK_DATE_FORMAT "%.4d-%.2d-%.2d"
#define CLOCK_TIME_FORMAT "%.2d:%.2d:%.2d"
typedef enum {
EventTypeTick,
EventTypeKey,
} EventType;
typedef struct {
EventType type;
InputEvent input;
} PluginEvent;
typedef struct {
FuriHalRtcDateTime datetime;
} ClockState;
#include "clock_app.h"
static void clock_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
furi_assert(event_queue);
PluginEvent event = {.type = EventTypeKey, .input = *input_event};
furi_message_queue_put(event_queue, &event, FuriWaitForever);
}
static void clock_render_callback(Canvas* const canvas, void* ctx) {
canvas_clear(canvas);
canvas_set_color(canvas, ColorBlack);
//canvas_clear(canvas);
//canvas_set_color(canvas, ColorBlack);
ClockState* state = (ClockState*)acquire_mutex((ValueMutex*)ctx, 25);
ClockState* state = ctx;
if(furi_mutex_acquire(state->mutex, 200) != FuriStatusOk) {
//FURI_LOG_D(TAG, "Can't obtain mutex, requeue render");
PluginEvent event = {.type = EventTypeTick};
furi_message_queue_put(state->event_queue, &event, 0);
return;
}
char strings[2][20];
FuriHalRtcDateTime curr_dt;
furi_hal_rtc_get_datetime(&curr_dt);
uint32_t curr_ts = furi_hal_rtc_datetime_to_timestamp(&curr_dt);
snprintf(
strings[0],
sizeof(strings[0]),
CLOCK_DATE_FORMAT,
state->datetime.year,
state->datetime.month,
state->datetime.day);
snprintf(
strings[1],
sizeof(strings[1]),
CLOCK_TIME_FORMAT,
state->datetime.hour,
state->datetime.minute,
state->datetime.second);
char time_string[TIME_LEN];
char date_string[DATE_LEN];
char meridian_string[MERIDIAN_LEN];
char timer_string[20];
if(state->settings.time_format == H24) {
snprintf(
time_string, TIME_LEN, CLOCK_TIME_FORMAT, curr_dt.hour, curr_dt.minute, curr_dt.second);
} else {
bool pm = curr_dt.hour > 12;
snprintf(
time_string,
TIME_LEN,
CLOCK_TIME_FORMAT,
pm ? curr_dt.hour - 12 : curr_dt.hour,
curr_dt.minute,
curr_dt.second);
snprintf(
meridian_string,
MERIDIAN_LEN,
MERIDIAN_FORMAT,
pm ? MERIDIAN_STRING_PM : MERIDIAN_STRING_AM);
}
if(state->settings.date_format == Iso) {
snprintf(
date_string, DATE_LEN, CLOCK_ISO_DATE_FORMAT, curr_dt.year, curr_dt.month, curr_dt.day);
} else {
snprintf(
date_string, DATE_LEN, CLOCK_RFC_DATE_FORMAT, curr_dt.day, curr_dt.month, curr_dt.year);
}
bool timer_running = state->timer_running;
uint32_t timer_start_timestamp = state->timer_start_timestamp;
uint32_t timer_stopped_seconds = state->timer_stopped_seconds;
furi_mutex_release(state->mutex);
release_mutex((ValueMutex*)ctx, state);
canvas_set_font(canvas, FontBigNumbers);
canvas_draw_str_aligned(canvas, 64, 42 - 16, AlignCenter, AlignCenter, strings[1]);
canvas_set_font(canvas, FontSecondary);
canvas_draw_str_aligned(canvas, 64, 52 - 8, AlignCenter, AlignTop, strings[0]);
if(timer_start_timestamp != 0) {
int32_t elapsed_secs = timer_running ? (curr_ts - timer_start_timestamp) :
timer_stopped_seconds;
snprintf(timer_string, 20, "%.2ld:%.2ld", elapsed_secs / 60, elapsed_secs % 60);
canvas_draw_str_aligned(canvas, 64, 8, AlignCenter, AlignCenter, time_string); // DRAW TIME
canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignTop, timer_string); // DRAW TIMER
canvas_set_font(canvas, FontSecondary);
canvas_draw_str_aligned(canvas, 64, 20, AlignCenter, AlignTop, date_string); // DRAW DATE
elements_button_left(canvas, "Reset");
} else {
canvas_draw_str_aligned(canvas, 64, 28, AlignCenter, AlignCenter, time_string);
canvas_set_font(canvas, FontSecondary);
canvas_draw_str_aligned(canvas, 64, 42, AlignCenter, AlignTop, date_string);
if(state->settings.time_format == H12)
canvas_draw_str_aligned(canvas, 65, 12, AlignCenter, AlignCenter, meridian_string);
}
if(timer_running) {
elements_button_center(canvas, "Stop");
} else if(timer_start_timestamp != 0 && !timer_running) {
elements_button_center(canvas, "Start");
}
}
static void clock_state_init(ClockState* const state) {
furi_hal_rtc_get_datetime(&state->datetime);
LOAD_CLOCK_SETTINGS(&state->settings);
if(state->settings.time_format != H12 && state->settings.time_format != H24) {
state->settings.time_format = H12;
}
if(state->settings.date_format != Iso && state->settings.date_format != Rfc) {
state->settings.date_format = Iso;
}
FURI_LOG_D(TAG, "Time format: %s", state->settings.time_format == H12 ? "12h" : "24h");
FURI_LOG_D(
TAG, "Date format: %s", state->settings.date_format == Iso ? "ISO 8601" : "RFC 5322");
//furi_hal_rtc_get_datetime(&state->datetime);
}
// Runs every 1000ms by default
@ -76,62 +118,111 @@ static void clock_tick(void* ctx) {
int32_t clock_app(void* p) {
UNUSED(p);
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
ClockState* plugin_state = malloc(sizeof(ClockState));
clock_state_init(plugin_state);
ValueMutex state_mutex;
if(!init_mutex(&state_mutex, plugin_state, sizeof(ClockState))) {
FURI_LOG_E(TAG, "cannot create mutex\r\n");
furi_message_queue_free(event_queue);
plugin_state->event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
if(plugin_state->event_queue == NULL) {
FURI_LOG_E(TAG, "Cannot create event queue");
free(plugin_state);
return 255;
}
//FURI_LOG_D(TAG, "Event queue created");
plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
if(plugin_state->mutex == NULL) {
FURI_LOG_E(TAG, "Cannot create mutex");
furi_message_queue_free(plugin_state->event_queue);
free(plugin_state);
return 255;
}
//FURI_LOG_D(TAG, "Mutex created");
clock_state_init(plugin_state);
// Set system callbacks
ViewPort* view_port = view_port_alloc();
view_port_draw_callback_set(view_port, clock_render_callback, &state_mutex);
view_port_input_callback_set(view_port, clock_input_callback, event_queue);
FuriTimer* timer = furi_timer_alloc(clock_tick, FuriTimerTypePeriodic, event_queue);
furi_timer_start(timer, furi_kernel_get_tick_frequency());
view_port_draw_callback_set(view_port, clock_render_callback, plugin_state);
view_port_input_callback_set(view_port, clock_input_callback, plugin_state->event_queue);
FuriTimer* timer =
furi_timer_alloc(clock_tick, FuriTimerTypePeriodic, plugin_state->event_queue);
if(timer == NULL) {
FURI_LOG_E(TAG, "Cannot create timer");
furi_mutex_free(plugin_state->mutex);
furi_message_queue_free(plugin_state->event_queue);
free(plugin_state);
return 255;
}
//FURI_LOG_D(TAG, "Timer created");
// Open GUI and register view_port
Gui* gui = furi_record_open(RECORD_GUI);
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
furi_timer_start(timer, furi_kernel_get_tick_frequency());
//FURI_LOG_D(TAG, "Timer started");
// Main loop
PluginEvent event;
for(bool processing = true; processing;) {
FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
ClockState* plugin_state = (ClockState*)acquire_mutex_block(&state_mutex);
FuriStatus event_status = furi_message_queue_get(plugin_state->event_queue, &event, 100);
if(event_status == FuriStatusOk) {
// press events
if(event.type == EventTypeKey) {
if(event.input.type == InputTypeShort || event.input.type == InputTypeRepeat) {
switch(event.input.key) {
case InputKeyUp:
case InputKeyDown:
case InputKeyRight:
case InputKeyLeft:
case InputKeyOk:
break;
case InputKeyBack:
// Exit the plugin
processing = false;
break;
if(event_status != FuriStatusOk) continue;
if(furi_mutex_acquire(plugin_state->mutex, FuriWaitForever) != FuriStatusOk) continue;
// press events
if(event.type == EventTypeKey) {
if(event.input.type == InputTypeShort || event.input.type == InputTypeRepeat) {
switch(event.input.key) {
case InputKeyUp:
case InputKeyDown:
case InputKeyRight:
break;
case InputKeyLeft:
if(plugin_state->timer_start_timestamp != 0) {
// Reset seconds
plugin_state->timer_running = false;
plugin_state->timer_start_timestamp = 0;
plugin_state->timer_stopped_seconds = 0;
}
break;
case InputKeyOk:;
// START/STOP TIMER
FuriHalRtcDateTime curr_dt;
furi_hal_rtc_get_datetime(&curr_dt);
uint32_t curr_ts = furi_hal_rtc_datetime_to_timestamp(&curr_dt);
if(plugin_state->timer_running) {
// Update stopped seconds
plugin_state->timer_stopped_seconds =
curr_ts - plugin_state->timer_start_timestamp;
} else {
if(plugin_state->timer_start_timestamp == 0) {
// Set starting timestamp if this is first time
plugin_state->timer_start_timestamp = curr_ts;
} else {
// Timer was already running, need to slightly readjust so we don't
// count the intervening time
plugin_state->timer_start_timestamp =
curr_ts - plugin_state->timer_stopped_seconds;
}
}
plugin_state->timer_running = !plugin_state->timer_running;
break;
case InputKeyBack:
// Exit the plugin
processing = false;
break;
}
} else if(event.type == EventTypeTick) {
furi_hal_rtc_get_datetime(&plugin_state->datetime);
}
} else {
FURI_LOG_D(TAG, "furi_message_queue: event timeout");
// event timeout
}
} /*else if(event.type == EventTypeTick) {
furi_hal_rtc_get_datetime(&plugin_state->datetime);
}*/
view_port_update(view_port);
release_mutex(&state_mutex, plugin_state);
furi_mutex_release(plugin_state->mutex);
}
furi_timer_free(timer);
@ -139,8 +230,9 @@ int32_t clock_app(void* p) {
gui_remove_view_port(gui, view_port);
furi_record_close(RECORD_GUI);
view_port_free(view_port);
furi_message_queue_free(event_queue);
delete_mutex(&state_mutex);
furi_message_queue_free(plugin_state->event_queue);
furi_mutex_free(plugin_state->mutex);
free(plugin_state);
return 0;
}

View File

@ -0,0 +1,38 @@
#pragma once
#include <input/input.h>
#include "clock_settings.h"
#define TAG "Clock"
#define CLOCK_ISO_DATE_FORMAT "%.4d-%.2d-%.2d"
#define CLOCK_RFC_DATE_FORMAT "%.2d-%.2d-%.4d"
#define CLOCK_TIME_FORMAT "%.2d:%.2d:%.2d"
#define MERIDIAN_FORMAT "%s"
#define MERIDIAN_STRING_AM "AM"
#define MERIDIAN_STRING_PM "PM"
#define TIME_LEN 12
#define DATE_LEN 14
#define MERIDIAN_LEN 3
typedef enum {
EventTypeTick,
EventTypeKey,
} EventType;
typedef struct {
EventType type;
InputEvent input;
} PluginEvent;
typedef struct {
ClockSettings settings;
FuriHalRtcDateTime datetime;
FuriMutex* mutex;
FuriMessageQueue* event_queue;
uint32_t timer_start_timestamp;
uint32_t timer_stopped_seconds;
bool timer_running;
} ClockState;

View File

@ -0,0 +1,37 @@
#pragma once
#include "clock_settings_filename.h"
#include <furi_hal.h>
#include <stdint.h>
#include <stdbool.h>
#include <toolbox/saved_struct.h>
#include <storage/storage.h>
#define CLOCK_SETTINGS_VER (1)
#define CLOCK_SETTINGS_PATH EXT_PATH(CLOCK_SETTINGS_FILE_NAME)
#define CLOCK_SETTINGS_MAGIC (0xC1)
#define SAVE_CLOCK_SETTINGS(x) \
saved_struct_save( \
CLOCK_SETTINGS_PATH, (x), sizeof(ClockSettings), CLOCK_SETTINGS_MAGIC, CLOCK_SETTINGS_VER)
#define LOAD_CLOCK_SETTINGS(x) \
saved_struct_load( \
CLOCK_SETTINGS_PATH, (x), sizeof(ClockSettings), CLOCK_SETTINGS_MAGIC, CLOCK_SETTINGS_VER)
typedef enum {
H12 = 1,
H24 = 2,
} TimeFormat;
typedef enum {
Iso = 1, // ISO 8601: yyyy-mm-dd
Rfc = 2, // RFC 5322: dd-mm-yyyy
} DateFormat;
typedef struct {
TimeFormat time_format;
DateFormat date_format;
uint8_t increment_precision;
} ClockSettings;

View File

@ -0,0 +1,102 @@
#include <furi.h>
#include <gui/modules/variable_item_list.h>
#include <gui/view_dispatcher.h>
#include <lib/toolbox/value_index.h>
#include "clock_settings.h"
#define TAG "Clock"
typedef struct {
ClockSettings clock_settings;
Gui* gui;
ViewDispatcher* view_dispatcher;
VariableItemList* variable_item_list;
} ClockAppSettings;
static uint32_t clock_app_settings_exit(void* context) {
UNUSED(context);
return VIEW_NONE;
}
#define TIME_FORMAT_COUNT 2
const char* const time_format_text[TIME_FORMAT_COUNT] = {
"12h",
"24h",
};
const uint32_t time_format_value[TIME_FORMAT_COUNT] = {H12, H24};
#define DATE_FORMAT_COUNT 2
const char* const date_format_text[DATE_FORMAT_COUNT] = {
"mm-dd", // ISO 8601
"dd-mm", // RFC 5322
};
const uint32_t date_format_value[DATE_FORMAT_COUNT] = {Iso, Rfc};
static void time_format_changed(VariableItem* item) {
ClockAppSettings* app = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
variable_item_set_current_value_text(item, time_format_text[index]);
app->clock_settings.time_format = time_format_value[index];
}
static void date_format_changed(VariableItem* item) {
ClockAppSettings* app = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
variable_item_set_current_value_text(item, date_format_text[index]);
app->clock_settings.date_format = date_format_value[index];
}
static ClockAppSettings* alloc_settings() {
ClockAppSettings* app = malloc(sizeof(ClockAppSettings));
LOAD_CLOCK_SETTINGS(&app->clock_settings);
app->gui = furi_record_open(RECORD_GUI);
app->variable_item_list = variable_item_list_alloc();
View* view = variable_item_list_get_view(app->variable_item_list);
view_set_previous_callback(view, clock_app_settings_exit);
VariableItem* item;
uint8_t value_index;
item = variable_item_list_add(
app->variable_item_list, "Clock format", TIME_FORMAT_COUNT, time_format_changed, app);
value_index = value_index_uint32(
(uint32_t)(app->clock_settings.time_format), time_format_value, TIME_FORMAT_COUNT);
//FURI_LOG_T(TAG, "Time format index: %u", value_index);
variable_item_set_current_value_index(item, value_index);
variable_item_set_current_value_text(item, time_format_text[value_index]);
item = variable_item_list_add(
app->variable_item_list, "Date format", DATE_FORMAT_COUNT, date_format_changed, app);
value_index = value_index_uint32(
(uint32_t)(app->clock_settings.date_format), date_format_value, DATE_FORMAT_COUNT);
//FURI_LOG_T(TAG, "Date format index: %u", value_index);
variable_item_set_current_value_index(item, value_index);
variable_item_set_current_value_text(item, date_format_text[value_index]);
app->view_dispatcher = view_dispatcher_alloc();
view_dispatcher_enable_queue(app->view_dispatcher);
view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
view_dispatcher_add_view(app->view_dispatcher, 0, view);
view_dispatcher_switch_to_view(app->view_dispatcher, 0);
return app;
}
static void free_settings(ClockAppSettings* app) {
view_dispatcher_remove_view(app->view_dispatcher, 0);
variable_item_list_free(app->variable_item_list);
view_dispatcher_free(app->view_dispatcher);
furi_record_close(RECORD_GUI);
SAVE_CLOCK_SETTINGS(&app->clock_settings);
free(app);
}
extern int32_t clock_settings_app(void* p) {
UNUSED(p);
ClockAppSettings* app = alloc_settings();
view_dispatcher_run(app->view_dispatcher);
free_settings(app);
return 0;
}

View File

@ -0,0 +1,3 @@
#pragma once
#define CLOCK_SETTINGS_FILE_NAME ".clock.settings"