Skip to content

Commit

Permalink
Nightstand: Show battery percentage and show AM/PM in timer mode (#42)
Browse files Browse the repository at this point in the history
* Nightstand: Show battery percentage

- Adds battery percentage to the right of the date on 12hr time format "08-01-2025  94%", or in place of the meridian "AM"/"PM" on 24hr. Checks every 1 second time tick.

* AM/PM in timer mode

---------

Co-authored-by: Willy-JL <[email protected]>
  • Loading branch information
956MB and Willy-JL authored Jan 21, 2025
1 parent 4e9e45f commit 5ce3339
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
46 changes: 36 additions & 10 deletions nightstand_clock/clock_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ void elements_progress_bar_vertical(
uint8_t height,
float progress) {
furi_assert(canvas);
furi_assert((progress >= 0) && (progress <= 1.0));
furi_assert(((float)progress >= 0.0f) && ((float)progress <= 1.0f));

uint8_t width = 9;

uint8_t progress_length = roundf((1.f - progress) * (height - 2));
Expand Down Expand Up @@ -142,6 +143,7 @@ static void clock_render_callback(Canvas* const canvas, void* ctx) {
char time_string[TIME_LEN];
char date_string[DATE_LEN];
char meridian_string[MERIDIAN_LEN];
char date_pct_string[DATE_PCT_LEN];
char timer_string[20];

if(state->time_format == LocaleTimeFormat24h) {
Expand Down Expand Up @@ -188,18 +190,42 @@ static void clock_render_callback(Canvas* const canvas, void* ctx) {
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
if(state->time_format == LocaleTimeFormat12h) {
canvas_draw_str_aligned(
canvas, 56, 8, AlignCenter, AlignCenter, time_string); // DRAW TIME
} else {
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
if(state->time_format == LocaleTimeFormat12h) {
canvas_draw_str_aligned(canvas, 112, 8, AlignCenter, AlignCenter, meridian_string);
}

snprintf(
date_pct_string, sizeof(date_pct_string), "%s %u%%", date_string, state->battery_pct);
canvas_draw_str_aligned(
canvas, 64, 20, AlignCenter, AlignTop, date_pct_string); // DRAW DATE + BATTERY
elements_button_left(canvas, "Reset");
} else {
canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignCenter, time_string);
canvas_set_font(canvas, FontSecondary);
canvas_draw_str_aligned(canvas, 65, 17, AlignCenter, AlignCenter, date_string);

if(state->time_format == LocaleTimeFormat12h)
canvas_draw_str_aligned(canvas, 64, 47, AlignCenter, AlignCenter, meridian_string);
if(state->time_format == LocaleTimeFormat12h) {
snprintf(
date_pct_string,
sizeof(date_pct_string),
"%s %u%%",
date_string,
state->battery_pct);
canvas_draw_str_aligned(canvas, 64, 17, AlignCenter, AlignCenter, date_pct_string);
canvas_draw_str_aligned(canvas, 64, 48, AlignCenter, AlignCenter, meridian_string);
} else {
canvas_draw_str_aligned(canvas, 64, 17, AlignCenter, AlignCenter, date_string);
snprintf(date_pct_string, sizeof(date_pct_string), "%u%%", state->battery_pct);
canvas_draw_str_aligned(canvas, 64, 48, AlignCenter, AlignCenter, date_pct_string);
}
}
if(timer_running) {
elements_button_center(canvas, "Stop");
Expand All @@ -210,8 +236,8 @@ static void clock_render_callback(Canvas* const canvas, void* ctx) {

static void clock_state_init(ClockState* const state) {
state->time_format = locale_get_time_format();

state->date_format = locale_get_date_format();
state->battery_pct = furi_hal_power_get_pct();

//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");
Expand Down Expand Up @@ -344,9 +370,9 @@ int32_t clock_app(void* p) {
break;
}
}
} /*else if(event.type == EventTypeTick) {
furi_hal_rtc_get_datetime(&plugin_state->datetime);
}*/
} else if(event.type == EventTypeTick) {
plugin_state->battery_pct = furi_hal_power_get_pct();
}

furi_mutex_release(plugin_state->mutex);
view_port_update(view_port);
Expand Down
3 changes: 3 additions & 0 deletions nightstand_clock/clock_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#define TIME_LEN 12
#define DATE_LEN 14
#define MERIDIAN_LEN 3
#define BATTERY_LEN 4
#define DATE_PCT_LEN 21

typedef enum {
EventTypeTick,
Expand All @@ -36,4 +38,5 @@ typedef struct {
uint32_t timer_start_timestamp;
uint32_t timer_stopped_seconds;
bool timer_running;
uint8_t battery_pct;
} ClockState;

0 comments on commit 5ce3339

Please sign in to comment.