2022-06-01 18:27:32 +00:00
|
|
|
#include <furi.h>
|
|
|
|
#include <furi_hal.h>
|
2022-08-04 02:44:40 +00:00
|
|
|
|
2022-06-01 18:27:32 +00:00
|
|
|
#include <gui/elements.h>
|
2022-08-04 02:44:40 +00:00
|
|
|
|
2022-06-01 18:27:32 +00:00
|
|
|
#include <gui/gui.h>
|
|
|
|
#include <input/input.h>
|
|
|
|
|
|
|
|
#define TAG "Clock"
|
2022-08-04 02:44:40 +00:00
|
|
|
#define CLOCK_DATE_FORMAT "%.4d-%.2d-%.2d"
|
|
|
|
#define CLOCK_TIME_FORMAT "%.2d:%.2d:%.2d"
|
2022-06-01 18:27:32 +00:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
EventTypeTick,
|
|
|
|
EventTypeKey,
|
|
|
|
} EventType;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
EventType type;
|
|
|
|
InputEvent input;
|
|
|
|
} PluginEvent;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
FuriHalRtcDateTime datetime;
|
|
|
|
} ClockState;
|
|
|
|
|
2022-07-20 16:01:38 +00:00
|
|
|
static void clock_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
|
2022-06-24 10:01:41 +00:00
|
|
|
furi_assert(event_queue);
|
2022-08-04 02:44:40 +00:00
|
|
|
|
2022-06-01 18:27:32 +00:00
|
|
|
PluginEvent event = {.type = EventTypeKey, .input = *input_event};
|
2022-07-20 16:01:38 +00:00
|
|
|
furi_message_queue_put(event_queue, &event, FuriWaitForever);
|
2022-06-01 18:27:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void clock_render_callback(Canvas* const canvas, void* ctx) {
|
|
|
|
canvas_clear(canvas);
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
2022-08-04 02:44:40 +00:00
|
|
|
|
2022-06-01 18:27:32 +00:00
|
|
|
ClockState* state = (ClockState*)acquire_mutex((ValueMutex*)ctx, 25);
|
2022-08-04 02:44:40 +00:00
|
|
|
|
|
|
|
char strings[2][20];
|
|
|
|
|
2022-08-03 19:09:39 +00:00
|
|
|
snprintf(
|
2022-06-24 10:01:41 +00:00
|
|
|
strings[0],
|
2022-08-03 19:09:39 +00:00
|
|
|
sizeof(strings[0]),
|
2022-08-04 02:44:40 +00:00
|
|
|
CLOCK_DATE_FORMAT,
|
2022-06-24 10:01:41 +00:00
|
|
|
state->datetime.year,
|
|
|
|
state->datetime.month,
|
|
|
|
state->datetime.day);
|
2022-08-03 19:09:39 +00:00
|
|
|
snprintf(
|
2022-06-24 10:01:41 +00:00
|
|
|
strings[1],
|
2022-08-03 19:09:39 +00:00
|
|
|
sizeof(strings[1]),
|
2022-08-04 02:44:40 +00:00
|
|
|
CLOCK_TIME_FORMAT,
|
2022-06-24 10:01:41 +00:00
|
|
|
state->datetime.hour,
|
|
|
|
state->datetime.minute,
|
|
|
|
state->datetime.second);
|
2022-08-04 02:44:40 +00:00
|
|
|
|
2022-06-01 18:27:32 +00:00
|
|
|
release_mutex((ValueMutex*)ctx, state);
|
|
|
|
canvas_set_font(canvas, FontBigNumbers);
|
2022-08-04 02:44:40 +00:00
|
|
|
canvas_draw_str_aligned(canvas, 64, 42 - 16, AlignCenter, AlignCenter, strings[1]);
|
2022-06-01 18:27:32 +00:00
|
|
|
canvas_set_font(canvas, FontSecondary);
|
2022-08-04 02:44:40 +00:00
|
|
|
canvas_draw_str_aligned(canvas, 64, 52 - 8, AlignCenter, AlignTop, strings[0]);
|
2022-06-01 18:27:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void clock_state_init(ClockState* const state) {
|
|
|
|
furi_hal_rtc_get_datetime(&state->datetime);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Runs every 1000ms by default
|
|
|
|
static void clock_tick(void* ctx) {
|
|
|
|
furi_assert(ctx);
|
2022-07-20 16:01:38 +00:00
|
|
|
FuriMessageQueue* event_queue = ctx;
|
2022-06-01 18:27:32 +00:00
|
|
|
PluginEvent event = {.type = EventTypeTick};
|
|
|
|
// It's OK to loose this event if system overloaded
|
2022-07-20 16:01:38 +00:00
|
|
|
furi_message_queue_put(event_queue, &event, 0);
|
2022-06-01 18:27:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t clock_app(void* p) {
|
|
|
|
UNUSED(p);
|
2022-07-20 16:01:38 +00:00
|
|
|
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
|
2022-08-04 02:44:40 +00:00
|
|
|
|
2022-06-01 18:27:32 +00:00
|
|
|
ClockState* plugin_state = malloc(sizeof(ClockState));
|
|
|
|
clock_state_init(plugin_state);
|
|
|
|
ValueMutex state_mutex;
|
2022-06-24 10:01:41 +00:00
|
|
|
if(!init_mutex(&state_mutex, plugin_state, sizeof(ClockState))) {
|
2022-06-01 18:27:32 +00:00
|
|
|
FURI_LOG_E(TAG, "cannot create mutex\r\n");
|
2022-08-10 02:03:01 +00:00
|
|
|
furi_message_queue_free(event_queue);
|
2022-06-01 18:27:32 +00:00
|
|
|
free(plugin_state);
|
|
|
|
return 255;
|
|
|
|
}
|
2022-08-04 02:44:40 +00:00
|
|
|
|
2022-06-01 18:27:32 +00:00
|
|
|
// 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);
|
2022-07-20 16:01:38 +00:00
|
|
|
FuriTimer* timer = furi_timer_alloc(clock_tick, FuriTimerTypePeriodic, event_queue);
|
|
|
|
furi_timer_start(timer, furi_kernel_get_tick_frequency());
|
2022-08-04 02:44:40 +00:00
|
|
|
|
2022-06-01 18:27:32 +00:00
|
|
|
// Open GUI and register view_port
|
2022-08-13 14:58:46 +00:00
|
|
|
Gui* gui = furi_record_open(RECORD_GUI);
|
2022-06-01 18:27:32 +00:00
|
|
|
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
|
2022-08-04 02:44:40 +00:00
|
|
|
|
2022-06-01 18:27:32 +00:00
|
|
|
// Main loop
|
|
|
|
PluginEvent event;
|
2022-06-24 10:01:41 +00:00
|
|
|
for(bool processing = true; processing;) {
|
2022-07-20 16:01:38 +00:00
|
|
|
FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
|
2022-06-01 18:27:32 +00:00
|
|
|
ClockState* plugin_state = (ClockState*)acquire_mutex_block(&state_mutex);
|
2022-08-04 02:44:40 +00:00
|
|
|
|
2022-07-20 16:01:38 +00:00
|
|
|
if(event_status == FuriStatusOk) {
|
2022-06-01 18:27:32 +00:00
|
|
|
// press events
|
2022-06-24 10:01:41 +00:00
|
|
|
if(event.type == EventTypeKey) {
|
|
|
|
if(event.input.type == InputTypeShort || event.input.type == InputTypeRepeat) {
|
2022-06-01 18:27:32 +00:00
|
|
|
switch(event.input.key) {
|
|
|
|
case InputKeyUp:
|
|
|
|
case InputKeyDown:
|
|
|
|
case InputKeyRight:
|
|
|
|
case InputKeyLeft:
|
2022-06-24 10:01:41 +00:00
|
|
|
case InputKeyOk:
|
2022-06-01 18:27:32 +00:00
|
|
|
break;
|
|
|
|
case InputKeyBack:
|
|
|
|
// Exit the plugin
|
|
|
|
processing = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if(event.type == EventTypeTick) {
|
|
|
|
furi_hal_rtc_get_datetime(&plugin_state->datetime);
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-04 02:44:40 +00:00
|
|
|
FURI_LOG_D(TAG, "furi_message_queue: event timeout");
|
2022-06-01 18:27:32 +00:00
|
|
|
// event timeout
|
|
|
|
}
|
2022-08-04 02:44:40 +00:00
|
|
|
|
2022-06-01 18:27:32 +00:00
|
|
|
view_port_update(view_port);
|
|
|
|
release_mutex(&state_mutex, plugin_state);
|
|
|
|
}
|
2022-08-04 02:44:40 +00:00
|
|
|
|
2022-07-20 16:01:38 +00:00
|
|
|
furi_timer_free(timer);
|
2022-06-01 18:27:32 +00:00
|
|
|
view_port_enabled_set(view_port, false);
|
|
|
|
gui_remove_view_port(gui, view_port);
|
2022-08-13 14:58:46 +00:00
|
|
|
furi_record_close(RECORD_GUI);
|
2022-06-01 18:27:32 +00:00
|
|
|
view_port_free(view_port);
|
2022-07-20 16:01:38 +00:00
|
|
|
furi_message_queue_free(event_queue);
|
|
|
|
delete_mutex(&state_mutex);
|
2022-07-26 18:52:34 +00:00
|
|
|
|
2022-06-01 18:27:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|