mirror of
https://github.com/HASwitchPlate/openHASP.git
synced 2025-07-24 11:46:34 +00:00
Improve backlight handling and idle can be set to a state
This commit is contained in:
parent
372150b7b6
commit
5ccf73695e
@ -73,9 +73,10 @@ LV_IMG_DECLARE(img_bubble_pattern)
|
||||
**********************/
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
uint8_t hasp_sleep_state = HASP_SLEEP_OFF; // Used in hasp_drv_touch.cpp
|
||||
static uint16_t sleepTimeShort = 60; // 1 second resolution
|
||||
static uint16_t sleepTimeLong = 120; // 1 second resolution
|
||||
uint8_t hasp_sleep_state = HASP_SLEEP_OFF; // Used in hasp_drv_touch.cpp
|
||||
static uint16_t sleepTimeShort = 60; // 1 second resolution
|
||||
static uint16_t sleepTimeLong = 120; // 1 second resolution
|
||||
static uint32_t sleepTimeOffset = 0; // 1 second resolution
|
||||
|
||||
uint8_t haspStartDim = 255;
|
||||
uint8_t haspStartPage = 1;
|
||||
@ -109,6 +110,7 @@ lv_font_t* hasp_get_font(uint8_t fontid)
|
||||
HASP_ATTRIBUTE_FAST_MEM void hasp_update_sleep_state()
|
||||
{
|
||||
uint32_t idle = lv_disp_get_inactive_time(lv_disp_get_default()) / 1000;
|
||||
idle += sleepTimeOffset; // To force a specific state
|
||||
|
||||
if(sleepTimeLong > 0 && idle >= (sleepTimeShort + sleepTimeLong)) {
|
||||
if(hasp_sleep_state != HASP_SLEEP_LONG) {
|
||||
@ -135,6 +137,20 @@ HASP_ATTRIBUTE_FAST_MEM void hasp_update_sleep_state()
|
||||
|
||||
void hasp_set_sleep_state(uint8_t state)
|
||||
{
|
||||
switch(state) {
|
||||
case HASP_SLEEP_LONG:
|
||||
sleepTimeOffset = (sleepTimeShort + sleepTimeLong);
|
||||
break;
|
||||
case HASP_SLEEP_SHORT:
|
||||
sleepTimeOffset = sleepTimeShort;
|
||||
break;
|
||||
case HASP_SLEEP_OFF:
|
||||
sleepTimeOffset = 0;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
lv_disp_trig_activity(NULL);
|
||||
hasp_sleep_state = state;
|
||||
}
|
||||
|
||||
|
@ -31,6 +31,7 @@ extern "C" {
|
||||
#define HASP_SLEEP_OFF 0
|
||||
#define HASP_SLEEP_SHORT 1
|
||||
#define HASP_SLEEP_LONG 2
|
||||
#define HASP_SLEEP_LAST 3
|
||||
|
||||
/**********************
|
||||
* TYPEDEFS
|
||||
|
@ -904,7 +904,8 @@ void dispatch_backlight_obsolete(const char* topic, const char* payload, uint8_t
|
||||
|
||||
void dispatch_backlight(const char*, const char* payload, uint8_t source)
|
||||
{
|
||||
bool power = haspDevice.get_backlight_power();
|
||||
bool power = haspDevice.get_backlight_power();
|
||||
uint8_t level = haspDevice.get_backlight_level();
|
||||
|
||||
// Set the current state
|
||||
if(strlen(payload) != 0) {
|
||||
@ -917,18 +918,25 @@ void dispatch_backlight(const char*, const char* payload, uint8_t source)
|
||||
json.shrinkToFit();
|
||||
|
||||
if(jsonError) { // Couldn't parse incoming payload as json
|
||||
power = Parser::is_true(payload);
|
||||
if(Parser::is_only_digits(payload)) {
|
||||
uint8_t temp_level = atoi(payload);
|
||||
if(temp_level <= 1)
|
||||
power = temp_level == 1;
|
||||
else
|
||||
level = temp_level;
|
||||
} else {
|
||||
power = Parser::is_true(payload);
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
// plain numbers are parsed as valid json object
|
||||
if(json.is<uint8_t>()) {
|
||||
uint8_t level = json.as<uint8_t>();
|
||||
|
||||
if(level <= 1)
|
||||
power = level;
|
||||
uint8_t temp_level = json.as<uint8_t>();
|
||||
if(temp_level <= 1)
|
||||
power = temp_level == 1;
|
||||
else
|
||||
haspDevice.set_backlight_level(level);
|
||||
level = temp_level;
|
||||
|
||||
// true and false are parsed as valid json object
|
||||
} else if(json.is<bool>()) {
|
||||
@ -939,16 +947,18 @@ void dispatch_backlight(const char*, const char* payload, uint8_t source)
|
||||
JsonVariant brightness = json[F("brightness")];
|
||||
|
||||
if(!state.isNull()) power = Parser::is_true(state);
|
||||
if(!brightness.isNull()) haspDevice.set_backlight_level(brightness.as<uint8_t>());
|
||||
if(!brightness.isNull()) level = brightness.as<uint8_t>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// toggle power and wakeup touch if changed
|
||||
if(power) haspDevice.set_backlight_level(level); // set level before power on
|
||||
if(haspDevice.get_backlight_power() != power) {
|
||||
haspDevice.set_backlight_power(power);
|
||||
hasp_set_wakeup_touch(!power);
|
||||
}
|
||||
if(!power) haspDevice.set_backlight_level(level); // set level after power off
|
||||
|
||||
// Return the current state
|
||||
char topic[10];
|
||||
@ -1249,18 +1259,27 @@ void dispatch_sleep(const char*, const char*, uint8_t source)
|
||||
|
||||
void dispatch_idle(const char*, const char* payload, uint8_t source)
|
||||
{
|
||||
char topic[6];
|
||||
char buffer[6];
|
||||
char topic[8];
|
||||
char buffer[8];
|
||||
|
||||
// idle off command
|
||||
if(payload && strlen(payload) && !Parser::is_true(payload)) {
|
||||
hasp_set_wakeup_touch(false);
|
||||
hasp_set_sleep_state(HASP_SLEEP_OFF);
|
||||
lv_disp_trig_activity(NULL);
|
||||
if(payload && strlen(payload)) {
|
||||
uint8_t state = HASP_SLEEP_LAST;
|
||||
if(!strcmp_P(payload, "off")) {
|
||||
hasp_set_wakeup_touch(false);
|
||||
state = HASP_SLEEP_OFF;
|
||||
} else if(!strcmp_P(payload, "short")) {
|
||||
state = HASP_SLEEP_SHORT;
|
||||
} else if(!strcmp_P(payload, "long")) {
|
||||
state = HASP_SLEEP_LONG;
|
||||
} else {
|
||||
state = HASP_SLEEP_LAST;
|
||||
}
|
||||
hasp_set_sleep_state(state);
|
||||
}
|
||||
|
||||
// idle state
|
||||
memcpy_P(topic, PSTR("idle"), 5);
|
||||
memcpy_P(topic, PSTR("idle"), 8);
|
||||
hasp_get_sleep_state(buffer);
|
||||
dispatch_state_subtopic(topic, buffer);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user