Skip to content

Commit

Permalink
More clock-like lvgl
Browse files Browse the repository at this point in the history
  • Loading branch information
alevy committed Oct 4, 2024
1 parent 1fa1092 commit e5d7b31
Showing 1 changed file with 72 additions and 27 deletions.
99 changes: 72 additions & 27 deletions examples/lvgl/main.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,74 @@
#include <stdio.h>

#include <libtock-sync/display/screen.h>
#include <libtock-sync/peripherals/rtc.h>
#include <libtock-sync/services/alarm.h>

#include <lvgl/lvgl.h>

#include "lvgl_driver.h"

/*static void event_handler(lv_event_t* e) {
lv_event_code_t code = lv_event_get_code(e);
unsigned int* seconds = (unsigned int*)lv_event_get_user_data(e);
libtock_rtc_date_t date;
uint32_t reference = 0;
lv_obj_t* label1;
lv_obj_t* seconds_lbl;
lv_obj_t* batt_lbl;
libtock_alarm_t rtc_alarm;
libtock_alarm_t update_alarm;
char label1_buffer[8];
char seconds_buffer[4];
char batt_buffer[8];

void rtc_callback(uint32_t now, uint32_t scheduled, void* ud);

void rtc_done(returncode_t success, libtock_rtc_date_t new_date) {
date = new_date;
libtock_alarm_command_read(&reference);
date.hour += 24 - 7;
date.hour = date.hour % 24;
libtock_alarm_in_ms(60000, rtc_callback, &date, &rtc_alarm);
}

void rtc_callback(uint32_t now, uint32_t scheduled, void* ud) {
//libtock_rtc_get_date(rtc_done);
}

void update_callback(uint32_t now, uint32_t scheduled, void* ud) {

uint16_t sample;
int err = libtocksync_adc_sample(0, &sample);
if (err == 0) {
sprintf(batt_buffer, "%d", sample);
} else {
sprintf(batt_buffer, "Err");
}
lv_label_set_text(batt_lbl, batt_buffer);

uint32_t diff_seconds = libtock_alarm_ticks_to_ms(now - reference) / 1000;

if (code == LV_EVENT_CLICKED) {
LV_LOG_USER("Clicked");
*seconds = 0;
} else if (code == LV_EVENT_VALUE_CHANGED) {
LV_LOG_USER("Toggled");
int seconds = date.seconds + diff_seconds;
int minutes = date.minute + seconds / 60;
int hours = date.hour + minutes / 60;

seconds = seconds % 60;
minutes = minutes % 60;
hours = hours % 24;

const char *ampm = "am";
if (date.hour > 12) {
ampm = "pm";
}
}*/
hours = hours % 12;

sprintf(label1_buffer, "%02d:%02d%s", hours, minutes, ampm);
lv_label_set_text(label1, label1_buffer);
sprintf(seconds_buffer, "%02d", seconds);
lv_label_set_text(seconds_lbl, seconds_buffer);

lv_task_handler();
lv_tick_inc(LV_DEF_REFR_PERIOD);
libtock_alarm_in_ms(1000, update_callback, &date, &update_alarm);
}

int main(void) {
libtocksync_screen_set_brightness(100);
Expand All @@ -31,31 +82,25 @@ int main(void) {
lv_obj_set_height(obj1, lv_pct(100));
lv_obj_set_width(obj1, lv_pct(100));

lv_obj_t* label1 = lv_label_create(scr);

lv_label_set_text(label1, "Hello world!");
batt_lbl = lv_label_create(scr);
lv_label_set_text(batt_lbl, "???");
lv_obj_align(batt_lbl, LV_ALIGN_TOP_LEFT, 0, 0);

label1 = lv_label_create(scr);
lv_label_set_text(label1, "00:00am");
lv_obj_align(label1, LV_ALIGN_CENTER, 0, 0);

lv_obj_t* btn1 = lv_btn_create(scr);
//lv_obj_add_event_cb(btn1, event_handler, LV_EVENT_ALL, &seconds);
lv_obj_align(btn1, LV_ALIGN_CENTER, 0, -40);

lv_obj_t* label = lv_label_create(btn1);
lv_label_set_text(label, "Reset");
lv_obj_center(label);
seconds_lbl = lv_label_create(scr);
lv_label_set_text(seconds_lbl, "00");
lv_obj_align(seconds_lbl, LV_ALIGN_CENTER, 0, 40);

lv_task_handler();

int seconds = 0;
libtock_alarm_in_ms(1, rtc_callback, &date, &rtc_alarm);
libtock_alarm_in_ms(1, update_callback, &date, &update_alarm);

while (1) {
libtocksync_alarm_delay_ms(1000);
seconds++;
char buffer[100];
int written = snprintf(buffer, sizeof(buffer), "%d", seconds);
lv_label_set_text(label1, buffer);
lv_task_handler();
lv_tick_inc(LV_DEF_REFR_PERIOD);
yield();
}

} else {
Expand Down

0 comments on commit e5d7b31

Please sign in to comment.