245 lines
8.4 KiB
C
245 lines
8.4 KiB
C
#include "lvgl.h"
|
|
#include "watch_face.h"
|
|
#include "menu_screen.h"
|
|
#include <stdio.h>
|
|
|
|
static void gesture_event_cb(lv_event_t * e)
|
|
{
|
|
WatchFace_t *watchFace = e->user_data;
|
|
|
|
lv_dir_t gesture;
|
|
switch(gesture = lv_indev_get_gesture_dir(lv_indev_get_act()))
|
|
{
|
|
case LV_DIR_LEFT:
|
|
LV_LOG_USER("GESTURE : LEFT");
|
|
break;
|
|
case LV_DIR_RIGHT:
|
|
LV_LOG_USER("GESTURE : RIGHT");
|
|
// We delete the timer
|
|
lv_timer_del(watchFace->handAnimationTimer);
|
|
// We create the menu screen and switch to it
|
|
extern MenuScreen_t menuScreen;
|
|
menu_screen_create(&menuScreen);
|
|
lv_scr_load_anim(menuScreen.display, LV_SCR_LOAD_ANIM_MOVE_RIGHT, 400, 0, true);
|
|
break;
|
|
case LV_DIR_TOP:
|
|
LV_LOG_USER("GESTURE : TOP");
|
|
break;
|
|
case LV_DIR_BOTTOM:
|
|
LV_LOG_USER("GESTURE : BOTTOM");
|
|
break;
|
|
default:
|
|
LV_LOG_USER("GESTURE : %u", gesture);
|
|
}
|
|
}
|
|
|
|
static void cleanup_event_cb(lv_event_t * e)
|
|
{
|
|
WatchFace_t *watchFace = e->user_data;
|
|
watch_face_destroy(watchFace);
|
|
LV_LOG_USER("cleanup");
|
|
}
|
|
|
|
static void update_watch_hands_angles(WatchFace_t * const watchFace, uint8_t increment)
|
|
{
|
|
if(!watchFace)
|
|
{
|
|
LV_LOG_ERROR("NULL pointer given !");
|
|
return;
|
|
}
|
|
|
|
//We retrieve the current time:
|
|
if(watchFace->dateTimeCb)
|
|
{
|
|
//We compute each hand angle
|
|
if(!increment || watchFace->secondHand.handAngle >= 3660)
|
|
{
|
|
watchFace->dateTimeCb(&watchFace->dateTime);
|
|
watchFace->secondHand.handAngle = 60 * watchFace->dateTime.tm_sec;
|
|
|
|
//Don't forget to update the day date window
|
|
sprintf(watchFace->dateWindow.dateWindowText, "%s%d", watchFace->dateTime.tm_mday < 10 ? " " : "", watchFace->dateTime.tm_mday);
|
|
lv_obj_invalidate(watchFace->dateWindow.dateWindowWidget);
|
|
}
|
|
else
|
|
{
|
|
watchFace->secondHand.handAngle += increment;
|
|
}
|
|
watchFace->minuteHand.handAngle = 60 * watchFace->dateTime.tm_min + watchFace->secondHand.handAngle / 60;
|
|
watchFace->hourHand.handAngle = 300 * watchFace->dateTime.tm_hour + watchFace->minuteHand.handAngle / 12;
|
|
watchFace->mediumHand24h.handAngle = watchFace->hourHand.handAngle / 2;
|
|
//LV_LOG_USER("angle : %f", watchFace->secondHand.handAngle);
|
|
|
|
//We update the angle
|
|
lv_img_set_angle(watchFace->secondHand.handImg, (uint16_t) watchFace->secondHand.handAngle % 3600);
|
|
lv_img_set_angle(watchFace->minuteHand.handImg, (uint16_t) watchFace->minuteHand.handAngle % 3600);
|
|
lv_img_set_angle(watchFace->hourHand.handImg, (uint16_t) watchFace->hourHand.handAngle % 3600);
|
|
lv_img_set_angle(watchFace->mediumHand24h.handImg, (uint16_t) watchFace->mediumHand24h.handAngle % 3600);
|
|
}
|
|
else
|
|
{
|
|
LV_LOG_USER("DateTimeCb is NULL, be sure to register a callback !");
|
|
}
|
|
}
|
|
|
|
static void hand_timer_anim_cb(lv_timer_t *timer)
|
|
{
|
|
WatchFace_t *watchFace = timer->user_data;
|
|
update_watch_hands_angles(watchFace, 12);
|
|
}
|
|
|
|
void watch_face_init(WatchFace_t * const watchFace)
|
|
{
|
|
if(!watchFace)
|
|
{
|
|
LV_LOG_ERROR("NULL pointer given !");
|
|
return;
|
|
}
|
|
memset(watchFace, 0, sizeof(WatchFace_t));
|
|
}
|
|
|
|
void watch_face_register_cb(WatchFace_t * const watchFace, DateTimeCb_t dateTimeCb)
|
|
{
|
|
if(!watchFace)
|
|
{
|
|
LV_LOG_ERROR("NULL pointer given !");
|
|
return;
|
|
}
|
|
|
|
watchFace->dateTimeCb = dateTimeCb;
|
|
}
|
|
|
|
void watch_face_create(WatchFace_t * const watchFace)
|
|
{
|
|
if(!watchFace)
|
|
{
|
|
LV_LOG_ERROR("NULL pointer given !");
|
|
return;
|
|
}
|
|
|
|
//We declare the needed assets for the watch face:
|
|
LV_IMG_DECLARE(watch_casio_face_asset)
|
|
LV_IMG_DECLARE(watch_casio_hour_hand_asset)
|
|
LV_IMG_DECLARE(watch_casio_minute_hand_asset)
|
|
LV_IMG_DECLARE(watch_casio_second_hand_asset)
|
|
LV_IMG_DECLARE(watch_casio_medium_hand_asset)
|
|
LV_IMG_DECLARE(watch_casio_small_hand_asset)
|
|
|
|
//We create our parent screen :
|
|
if(watchFace->display)
|
|
{
|
|
LV_LOG_ERROR("display should be NULL here !");
|
|
lv_obj_del(watchFace->display);
|
|
watchFace->display = NULL;
|
|
}
|
|
watchFace->display = lv_obj_create(NULL);
|
|
//We load our assets :
|
|
lv_obj_t *watchFaceImg = lv_img_create(watchFace->display);
|
|
lv_img_set_src(watchFaceImg, &watch_casio_face_asset);
|
|
|
|
lv_obj_t *smallHandImg = lv_img_create(watchFace->display);
|
|
lv_img_set_src(smallHandImg, &watch_casio_small_hand_asset);
|
|
lv_obj_set_pos(smallHandImg, 69, 98);
|
|
lv_img_set_pivot(smallHandImg, 4, 20);
|
|
|
|
if(watchFace->mediumHand24h.handImg)
|
|
{
|
|
LV_LOG_ERROR("mediumHand24hImg should be NULL here !");
|
|
lv_obj_del(watchFace->mediumHand24h.handImg);
|
|
watchFace->mediumHand24h.handImg = NULL;
|
|
}
|
|
|
|
watchFace->mediumHand24h.handImg = lv_img_create(watchFace->display);
|
|
lv_img_set_src(watchFace->mediumHand24h.handImg, &watch_casio_medium_hand_asset);
|
|
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_img_set_src(mediumHandChronoImg, &watch_casio_medium_hand_asset);
|
|
lv_obj_set_pos(mediumHandChronoImg, 115, 140);
|
|
lv_img_set_pivot(mediumHandChronoImg, 4, 25);
|
|
|
|
//Date window is created here
|
|
if(watchFace->dateWindow.dateWindowWidget)
|
|
{
|
|
LV_LOG_ERROR("dateWindow should be NULL here !");
|
|
lv_obj_del(watchFace->dateWindow.dateWindowWidget);
|
|
watchFace->dateWindow.dateWindowWidget = NULL;
|
|
}
|
|
watchFace->dateWindow.dateWindowWidget = lv_label_create(watchFace->display);
|
|
lv_label_set_text_static(watchFace->dateWindow.dateWindowWidget, watchFace->dateWindow.dateWindowText);
|
|
lv_obj_set_pos(watchFace->dateWindow.dateWindowWidget, 182,112);
|
|
|
|
if(watchFace->hourHand.handImg)
|
|
{
|
|
LV_LOG_ERROR("hourHandImg should be NULL here !");
|
|
lv_obj_del(watchFace->hourHand.handImg);
|
|
watchFace->hourHand.handImg = NULL;
|
|
}
|
|
|
|
watchFace->hourHand.handImg = lv_img_create(watchFace->display);
|
|
lv_img_set_src(watchFace->hourHand.handImg, &watch_casio_hour_hand_asset);
|
|
lv_obj_set_pos(watchFace->hourHand.handImg, 112, 60);
|
|
lv_img_set_pivot(watchFace->hourHand.handImg, 8, 60);
|
|
|
|
if(watchFace->minuteHand.handImg)
|
|
{
|
|
LV_LOG_ERROR("minuteHandImg should be NULL here !");
|
|
lv_obj_del(watchFace->minuteHand.handImg);
|
|
watchFace->minuteHand.handImg = NULL;
|
|
}
|
|
|
|
watchFace->minuteHand.handImg = lv_img_create(watchFace->display);
|
|
lv_img_set_src(watchFace->minuteHand.handImg, &watch_casio_minute_hand_asset);
|
|
lv_obj_set_pos(watchFace->minuteHand.handImg, 112, 28);
|
|
lv_img_set_pivot(watchFace->minuteHand.handImg, 7, 92);
|
|
|
|
if(watchFace->secondHand.handImg)
|
|
{
|
|
LV_LOG_ERROR("secondHandImg should be NULL here !");
|
|
lv_obj_del(watchFace->secondHand.handImg);
|
|
watchFace->secondHand.handImg = NULL;
|
|
}
|
|
|
|
watchFace->secondHand.handImg = lv_img_create(watchFace->display);
|
|
lv_img_set_src(watchFace->secondHand.handImg, &watch_casio_second_hand_asset);
|
|
lv_obj_set_pos(watchFace->secondHand.handImg, 115, 28);
|
|
lv_img_set_pivot(watchFace->secondHand.handImg, 5, 92);
|
|
|
|
//We set the appropriate angles to each of the hands
|
|
update_watch_hands_angles(watchFace, 0);
|
|
|
|
//We register the event callback to handle gestures
|
|
lv_obj_add_event_cb(watchFace->display, &(gesture_event_cb), LV_EVENT_GESTURE, watchFace);
|
|
//We register the event callback to handle the cleanup
|
|
lv_obj_add_event_cb(watchFace->display, &(cleanup_event_cb), LV_EVENT_DELETE, watchFace);
|
|
|
|
//We create the timer to run the watch animations
|
|
if(watchFace->handAnimationTimer)
|
|
{
|
|
LV_LOG_ERROR("handAnimationTimer should be NULL here !");
|
|
lv_timer_del(watchFace->handAnimationTimer);
|
|
watchFace->handAnimationTimer = NULL;
|
|
}
|
|
|
|
watchFace->handAnimationTimer = lv_timer_create(&(hand_timer_anim_cb), 199, watchFace);
|
|
}
|
|
|
|
void watch_face_destroy(WatchFace_t * const watchFace)
|
|
{
|
|
if(!watchFace)
|
|
{
|
|
LV_LOG_ERROR("NULL pointer given !");
|
|
return;
|
|
}
|
|
|
|
watchFace->display = NULL;
|
|
watchFace->handAnimationTimer = NULL;
|
|
watchFace->dateWindow.dateWindowWidget = NULL;
|
|
watchFace->hourHand.handImg = NULL;
|
|
watchFace->minuteHand.handImg = NULL;
|
|
watchFace->secondHand.handImg = NULL;
|
|
watchFace->mediumHand24h.handImg = NULL;
|
|
}
|
|
|