Added the battery indicator to the watch face

This commit is contained in:
Th3maz1ng 2023-02-05 12:14:27 +01:00
parent 93c0ab97ec
commit 3cee4efa91
3 changed files with 1082 additions and 968 deletions

File diff suppressed because one or more lines are too long

View File

@ -62,6 +62,9 @@ static void update_watch_hands_angles(WatchFace_t * const watchFace, uint8_t inc
sprintf(watchFace->dateWindow.dateWindowText, "%s%d", watchFace->dateTime.tm_mday < 10 ? " " : "", watchFace->dateTime.tm_mday);
lv_obj_invalidate(watchFace->dateWindow.dateWindowWidget);
APP_LOG_DEBUG("Syncing time");
if(watchFace->batteryIndicatorCb)
watch_face_set_battery_indicator(watchFace, watchFace->batteryIndicatorCb());
}
else
{
@ -100,7 +103,7 @@ void watch_face_init(WatchFace_t * const watchFace)
memset(watchFace, 0, sizeof(WatchFace_t));
}
void watch_face_register_cb(WatchFace_t * const watchFace, DateTimeCb_t dateTimeCb)
void watch_face_register_date_time_cb(WatchFace_t * const watchFace, DateTimeCb_t dateTimeCb)
{
if(!watchFace)
{
@ -111,6 +114,17 @@ void watch_face_register_cb(WatchFace_t * const watchFace, DateTimeCb_t dateTime
watchFace->dateTimeCb = dateTimeCb;
}
void watch_face_register_battery_indicator_cb(WatchFace_t *const watchFace, BatteryIndicatorCb_t BatteryIndicatorCb)
{
if(!watchFace)
{
LV_LOG_ERROR("NULL pointer given !");
return;
}
watchFace->batteryIndicatorCb = BatteryIndicatorCb;
}
void watch_face_create(WatchFace_t * const watchFace)
{
if(!watchFace)
@ -145,6 +159,39 @@ void watch_face_create(WatchFace_t * const watchFace)
lv_obj_set_pos(smallHandImg, 69, 98);
lv_img_set_pivot(smallHandImg, 4, 20);
//Battery arc is created here
if(watchFace->batteryIndicator.battery_arc)
{
LV_LOG_ERROR("battery_arc should be NULL here !");
lv_obj_del(watchFace->batteryIndicator.battery_arc);
watchFace->batteryIndicator.battery_arc = NULL;
}
watchFace->batteryIndicator.battery_arc = lv_arc_create(watchFace->display);
lv_obj_remove_style(watchFace->batteryIndicator.battery_arc, NULL, LV_PART_KNOB);
lv_obj_clear_flag(watchFace->batteryIndicator.battery_arc, LV_OBJ_FLAG_CLICKABLE);
lv_obj_set_size(watchFace->batteryIndicator.battery_arc, 60, 60);
lv_obj_align(watchFace->batteryIndicator.battery_arc, LV_ALIGN_CENTER, -1, 45);
lv_obj_set_style_arc_width(watchFace->batteryIndicator.battery_arc, 5, LV_PART_INDICATOR);
lv_obj_set_style_arc_width(watchFace->batteryIndicator.battery_arc, 0, LV_PART_MAIN);
lv_obj_set_style_arc_color(watchFace->batteryIndicator.battery_arc, lv_color_make(228, 233, 236), LV_PART_INDICATOR);
lv_arc_set_value(watchFace->batteryIndicator.battery_arc, 100);
if(watchFace->batteryIndicator.label)
{
LV_LOG_ERROR("battery_label should be NULL here !");
lv_obj_del(watchFace->batteryIndicator.label);
watchFace->batteryIndicator.label = NULL;
}
watchFace->batteryIndicator.label = lv_label_create(watchFace->display);
strcpy(watchFace->batteryIndicator.text, "100 %");
lv_label_set_text_static(watchFace->batteryIndicator.label, watchFace->batteryIndicator.text);
lv_obj_set_style_text_color(watchFace->batteryIndicator.label, lv_color_white(), LV_PART_MAIN);
lv_obj_align_to(watchFace->batteryIndicator.label, watchFace->batteryIndicator.battery_arc, LV_ALIGN_CENTER, 0, 0);
if(watchFace->mediumHand24h.handImg)
{
LV_LOG_ERROR("mediumHand24hImg should be NULL here !");
@ -157,10 +204,10 @@ void watch_face_create(WatchFace_t * const watchFace)
lv_obj_set_pos(watchFace->mediumHand24h.handImg, 115, 48);
lv_img_set_pivot(watchFace->mediumHand24h.handImg, 4, 25);
lv_obj_t *mediumHandChronoImg = lv_img_create(watchFace->display);
/*lv_obj_t *mediumHandChronoImg = lv_img_create(watchFace->display);
lv_img_set_src(mediumHandChronoImg, &watch_casio_medium_hand_asset);
lv_obj_set_pos(mediumHandChronoImg, 115, 140);
lv_img_set_pivot(mediumHandChronoImg, 4, 25);
lv_img_set_pivot(mediumHandChronoImg, 4, 25);*/
//Date window is created here
if(watchFace->dateWindow.dateWindowWidget)
@ -241,6 +288,32 @@ void watch_face_force_sync(WatchFace_t *const watchFace)
update_watch_hands_angles(watchFace, 0);
}
void watch_face_set_battery_indicator(WatchFace_t * const watchFace, uint8_t percentage)
{
if(!watchFace)
{
LV_LOG_ERROR("NULL pointer given !");
return;
}
if(!watchFace->display) return;
lv_color_t arc_color = lv_color_make(228, 233, 236);
if(percentage <= 10)
arc_color = lv_color_make(228, 33, 81);
else if(percentage <= 30)
arc_color = lv_color_make(247, 148, 29);
else if(percentage <= 50)
arc_color = lv_color_make(226, 175, 58);
lv_arc_set_value(watchFace->batteryIndicator.battery_arc, percentage);
lv_obj_set_style_arc_color(watchFace->batteryIndicator.battery_arc, arc_color, LV_PART_INDICATOR);
sprintf(watchFace->batteryIndicator.text, "%u %%", percentage);
lv_label_set_text_static(watchFace->batteryIndicator.label, watchFace->batteryIndicator.text);
lv_obj_align_to(watchFace->batteryIndicator.label, watchFace->batteryIndicator.battery_arc, LV_ALIGN_CENTER, 0, 0);
}
void watch_face_destroy(WatchFace_t * const watchFace)
{
if(!watchFace)
@ -256,5 +329,7 @@ void watch_face_destroy(WatchFace_t * const watchFace)
watchFace->minuteHand.handImg = NULL;
watchFace->secondHand.handImg = NULL;
watchFace->mediumHand24h.handImg = NULL;
watchFace->batteryIndicator.battery_arc = NULL;
watchFace->batteryIndicator.label = NULL;
}

View File

@ -5,6 +5,7 @@
#include <time.h>
typedef void (*DateTimeCb_t)(struct tm * const dateTime);
typedef uint8_t (*BatteryIndicatorCb_t)(void);
typedef struct DateWindow
{
@ -18,10 +19,18 @@ typedef struct WatchHand
float handAngle;
}WatchHand_t;
typedef struct BatteryIndicator
{
lv_obj_t *label;
lv_obj_t *battery_arc;
char text[7];
} BatteryIndicator_t;
/* Watch face context object */
typedef struct WatchFace
{
DateTimeCb_t dateTimeCb; //Call back function used to retrieve the date and time needed by the watch face
BatteryIndicatorCb_t batteryIndicatorCb; //Call back function used to update the battery level every minute
WatchHand_t hourHand;
WatchHand_t minuteHand;
WatchHand_t secondHand;
@ -29,6 +38,7 @@ typedef struct WatchFace
lv_timer_t *handAnimationTimer;
lv_obj_t *display;
DateWindow_t dateWindow;
BatteryIndicator_t batteryIndicator;
struct tm dateTime;
} WatchFace_t;
@ -36,12 +46,38 @@ typedef struct WatchFace
/* Initializes the watch face context object */
void watch_face_init(WatchFace_t * const watchFace);
/* Registers a call back function to retrieve the time and date */
void watch_face_register_cb(WatchFace_t * const watchFace, DateTimeCb_t DateTimeCb);
/**
* @brief Registers a call back function used by the watch face to retrieve the time and date
*
* @param watchFace a pointer to the watch face context structure.
* @param DateTimeCb a pointer to a function having the right definition.
*/
void watch_face_register_date_time_cb(WatchFace_t * const watchFace, DateTimeCb_t DateTimeCb);
/* Builds the watch face graphically */
/**
* @brief Registers a call back function used to refresh the battery indicator.
* The refreshing is done every minute or every time the @ref watch_face_force_sync is called.
*
* @param watchFace a pointer to the watch face context structure.
* @param BatteryIndicatorCb a pointer to a function having the right definition.
*/
void watch_face_register_battery_indicator_cb(WatchFace_t * const watchFace, BatteryIndicatorCb_t BatteryIndicatorCb);
/**
* @brief Graphically builds the watch face
*
* @param watchFace a pointer to the watch face context structure.
*/
void watch_face_create(WatchFace_t * const watchFace);
/**
* @brief Sets the battery indicator to the given value in percent.
*
* @param watchFace a pointer to the watch face context structure.
* @param percentage the value to set the indicator to in percent.
*/
void watch_face_set_battery_indicator(WatchFace_t * const watchFace, uint8_t percentage);
/**
* @brief Forces the watch face to sync up with the RTC by calling the provided date_time_cb
*
@ -49,7 +85,11 @@ void watch_face_create(WatchFace_t * const watchFace);
*/
void watch_face_force_sync(WatchFace_t * const watchFace);
/* Frees all resources used by the WatchFace object */
/**
* @brief Frees all resources used by the WatchFace object
*
* @param watchFace a pointer to the watch face context structure.
*/
void watch_face_destroy(WatchFace_t * const watchFace);
#endif // WATCH_FACE_H