Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed clicks being handled when exiting sleep #67

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <M5Dial-LVGL.h>
#include <Arduino.h>
#include "ui/ui.h"
#include "sys/sleep_mgmt.h"
#include <ha_api.h>
#include <aero_preferences.h>
// #include "ui/screens/sree.h"
Expand All @@ -28,16 +27,18 @@ Screen *global_screens[] = {
&screen_settings,
&screen_timer_alert,
&screen_timer_countdown,
&screen_timer_set};
&screen_timer_set,
&screen_sleep};
;
void log_cb(lv_log_level_t level, const char *buf)
{
Serial.println(buf);
}
void update_timezone() {
void update_timezone()
{
String timezone = ha->getTimezone();
log_d("Timezone: %s", timezone);
if (timezone.length() ==0)
if (timezone.length() == 0)
return;
String tz = lookup_posix_timezone_tz(timezone.c_str());
settings.putString("ntp_timezone", timezone);
Expand Down Expand Up @@ -128,16 +129,15 @@ void loop()
{
improvSerial.handleSerial();
m5dial_lvgl_next();
server.handleClient();
monitor_sleep();

server.handleClient();

if (initialized)
{
clock_timer.update();
// log_d("HA is setup? %d %s", ha->isSetup(), settings.getString("ha_refresh", "none"));
if (ha->isSetup() and not initialized_ha)
{
log_d("Initializing HA");
{
log_d("Initializing HA");
ha->createEntities();
ha->updateAllStates();
initialized_ha = true;
Expand Down
28 changes: 0 additions & 28 deletions src/sys/sleep_mgmt.h

This file was deleted.

4 changes: 3 additions & 1 deletion src/ui/screens/Screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ void Screen::initialize()
{
_initialize();
create();
};
};
size_t Screen::instanceCount;
Screen * Screen::instances[30];
24 changes: 23 additions & 1 deletion src/ui/screens/Screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Screen
lv_obj_t *_lv_screen;
lv_group_t *_lv_group;
lv_timer_t *_updateTimer;
Screen *last_screen = NULL;
inline void _initialize()
{
_lv_screen = lv_obj_create(NULL);
Expand All @@ -21,14 +22,27 @@ class Screen
static void _load(lv_event_t *event);
static void _unload(lv_event_t *event);
static void _update(lv_timer_t *timer);
private:
static size_t instanceCount;
static Screen *instances[30];

public:
Screen(){};
Screen(){
instances[instanceCount++] = this;
};

virtual void makeActive() {
if (lv_screen_active() != _lv_screen)
last_screen = getActiveInstance();
if (last_screen != NULL)
last_screen->unload(NULL);
load(NULL);
lv_screen_load(_lv_screen);
};
virtual void makeLastActive() {
if (last_screen != NULL)
last_screen->makeActive();
};
virtual bool isActive() { return lv_screen_active() == _lv_screen; };
void initialize();
virtual void create() = 0;
Expand All @@ -52,6 +66,14 @@ class Screen
inline lv_obj_t * getLvScreen() {
return _lv_screen;
}
static Screen * getActiveInstance() {
for (size_t i = 0; i < instanceCount; i++)
{
if (instances[i]->isActive())
return instances[i];
};
return NULL;
}
};


Expand Down
33 changes: 33 additions & 0 deletions src/ui/screens/Screen_Sleep.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "Screen_Sleep.h"
#include <M5Unified.h>


void Screen_Sleep::load(lv_event_t *event)
{
M5.Lcd.setBrightness(0);
};
void Screen_Sleep::unload(lv_event_t *event)
{
M5.Lcd.setBrightness(255);
};
void Screen_Sleep::create() {
lv_timer_create(monitorSleep, 100, this);
};
void Screen_Sleep::monitorSleep(lv_timer_t *timer) {
Screen_Sleep *screen = (Screen_Sleep*)timer->user_data;
if (screen->isActive())
{
if (lv_display_get_inactive_time(NULL) < 1000)
{
screen->makeLastActive();
}
}
else
{
if (lv_display_get_inactive_time(NULL) > 10 * 1000)
{
screen->makeActive();
}
}
};
Screen_Sleep screen_sleep;
15 changes: 15 additions & 0 deletions src/ui/screens/Screen_Sleep.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once
#ifndef SCREEN_SLEEP
#define SCREEN_SLEEP
#include <lvgl.h>
#include "Screen.h"
class Screen_Sleep : public Screen
{
void create();
void load(lv_event_t *event);
void unload(lv_event_t *event);
static void monitorSleep(lv_timer_t *timer);
};
extern Screen_Sleep screen_sleep;

#endif
1 change: 1 addition & 0 deletions src/ui/screens/screens.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "Screen_On_Off.h"
#include "Screen_Main_Menu.h"
#include "Screen_Settings.h"
#include "Screen_Sleep.h"
#include "timer/Screen_Timer.h"

extern Screen * global_screens[];
Expand Down