Skip to content

Commit

Permalink
Add display timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
terratec committed Dec 9, 2024
1 parent 19d610c commit 0533d24
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 2 deletions.
66 changes: 64 additions & 2 deletions main/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,23 @@
#define LCD_CMD_BITS 8
#define LCD_PARAM_BITS 8

#define DISPLAY_TIMEOUT_UPDATE_MS 60000

static const char * TAG = "display";

static esp_lcd_panel_handle_t panel_handle = NULL;
lv_timer_t * display_timeout_timer = NULL;
static bool display_state_on = false;
static uint16_t display_timeout_counter = 0;

static lv_theme_t theme;
static lv_style_t scr_style;

extern const lv_font_t lv_font_portfolio_6x8;

esp_err_t display_on(void);
esp_err_t display_off(void);

static void theme_apply(lv_theme_t *theme, lv_obj_t *obj) {
if (lv_obj_get_parent(obj) == NULL) {
lv_obj_add_style(obj, &scr_style, LV_PART_MAIN);
Expand Down Expand Up @@ -61,7 +71,6 @@ esp_err_t display_init(void * pvParameters)
ESP_RETURN_ON_ERROR(esp_lcd_new_panel_io_i2c(i2c_master_bus_handle, &io_config, &io_handle), TAG, "Failed to initialise i2c panel bus");

ESP_LOGI(TAG, "Install SSD1306 panel driver");
esp_lcd_panel_handle_t panel_handle = NULL;
esp_lcd_panel_dev_config_t panel_config = {
.bits_per_pixel = 1,
.reset_gpio_num = -1,
Expand Down Expand Up @@ -122,7 +131,10 @@ esp_err_t display_init(void * pvParameters)

if (esp_lcd_panel_init_err == ESP_OK) {
// Only turn on the screen when it has been cleared
ESP_RETURN_ON_ERROR(esp_lcd_panel_disp_on_off(panel_handle, true), TAG, "Panel display on failed");
esp_err_t esp_err = display_on();
if (ESP_OK != esp_err) {
return esp_err;
}

GLOBAL_STATE->SYSTEM_MODULE.is_screen_active = true;
} else {
Expand All @@ -131,3 +143,53 @@ esp_err_t display_init(void * pvParameters)

return ESP_OK;
}

static void display_timeout_cb(lv_timer_t * timer)
{
const uint16_t display_timeout_config = nvs_config_get_u16(NVS_CONFIG_DISPLAY_TIMEOUT, 0);

if (0 < display_timeout_config) {
if (display_timeout_config <= display_timeout_counter) {
display_off();
} else {
display_timeout_counter++;
}
} else {
display_on();
}
}

esp_err_t display_timeout_start(void * pvParameters)
{
GlobalState * GLOBAL_STATE = (GlobalState *) pvParameters;

if (GLOBAL_STATE->SYSTEM_MODULE.is_screen_active) {
display_timeout_timer = lv_timer_create(display_timeout_cb, DISPLAY_TIMEOUT_UPDATE_MS, NULL);
}

return ESP_OK;
}

esp_err_t display_on(void)
{
if (!display_state_on && (NULL != panel_handle)) {
ESP_RETURN_ON_ERROR(esp_lcd_panel_disp_on_off(panel_handle, true), TAG, "Panel display on failed");
display_state_on = true;
display_timeout_counter = 0;
if (NULL != display_timeout_timer) {
lv_timer_reset(display_timeout_timer);
}
}

return ESP_OK;
}

esp_err_t display_off(void)
{
if (display_state_on && (NULL != panel_handle)) {
ESP_RETURN_ON_ERROR(esp_lcd_panel_disp_on_off(panel_handle, false), TAG, "Panel display off failed");
display_state_on = false;
}

return ESP_OK;
}
3 changes: 3 additions & 0 deletions main/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
#define DISPLAY_H_

esp_err_t display_init(void * pvParameters);
esp_err_t display_timeout_start(void * pvParameters);
esp_err_t display_on(void);
esp_err_t display_off(void);

#endif /* DISPLAY_H_ */
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@
</small>
</div>

<div class="field grid p-fluid">
<label htmlFor="displayTimeout" class="col-12 mb-2 md:col-2 md:mb-0">Display Timeout</label>
<div class="col-12 md:col-10">
<label>{{form.controls['displayTimeout'].value}} Minute(s)
<b *ngIf="form.controls['displayTimeout'].value == 0" style="color: #F2A900">Disabled</b></label>
<p-slider formControlName="displayTimeout" [min]="0" [max]="60"></p-slider>
</div>
</div>

<div class="col-12 md:col-4">
<div class="field-checkbox">
<p-checkbox name="flipscreen" formControlName="flipscreen" inputId="flipscreen"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export class EditComponent implements OnInit {
this.form = this.fb.group({
flipscreen: [info.flipscreen == 1],
invertscreen: [info.invertscreen == 1],
displayTimeout: [info.displayTimeout, [Validators.required]],
stratumURL: [info.stratumURL, [
Validators.required,
Validators.pattern(/^(?!.*stratum\+tcp:\/\/).*$/),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export class SettingsComponent {
this.form = this.fb.group({
flipscreen: [info.flipscreen == 1],
invertscreen: [info.invertscreen == 1],
displayTimeout: [info.displayTimeout, [Validators.required]],
stratumURL: [info.stratumURL, [
Validators.required,
Validators.pattern(/^(?!.*stratum\+tcp:\/\/).*$/),
Expand Down
1 change: 1 addition & 0 deletions main/http_server/axe-os/src/app/services/system.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export class SystemService {
boardVersion: "204",
flipscreen: 1,
invertscreen: 0,
displayTimeout: 0,
invertfanpolarity: 1,
autofanspeed: 1,
fanspeed: 100,
Expand Down
1 change: 1 addition & 0 deletions main/http_server/axe-os/src/models/ISystemInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface ISystemInfo {

flipscreen: number;
invertscreen: number;
displayTimeout: number;
power: number,
voltage: number,
current: number,
Expand Down
4 changes: 4 additions & 0 deletions main/http_server/http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ static esp_err_t PATCH_update_settings(httpd_req_t * req)
if ((item = cJSON_GetObjectItem(root, "invertscreen")) != NULL) {
nvs_config_set_u16(NVS_CONFIG_INVERT_SCREEN, item->valueint);
}
if ((item = cJSON_GetObjectItem(root, "displayTimeout")) != NULL) {
nvs_config_set_u16(NVS_CONFIG_DISPLAY_TIMEOUT, item->valueint);
}
if ((item = cJSON_GetObjectItem(root, "invertfanpolarity")) != NULL) {
nvs_config_set_u16(NVS_CONFIG_INVERT_FAN_POLARITY, item->valueint);
}
Expand Down Expand Up @@ -427,6 +430,7 @@ static esp_err_t GET_system_info(httpd_req_t * req)
cJSON_AddNumberToObject(root, "flipscreen", nvs_config_get_u16(NVS_CONFIG_FLIP_SCREEN, 1));
cJSON_AddNumberToObject(root, "overheat_mode", nvs_config_get_u16(NVS_CONFIG_OVERHEAT_MODE, 0));
cJSON_AddNumberToObject(root, "invertscreen", nvs_config_get_u16(NVS_CONFIG_INVERT_SCREEN, 0));
cJSON_AddNumberToObject(root, "displayTimeout", nvs_config_get_u16(NVS_CONFIG_DISPLAY_TIMEOUT, 0));

cJSON_AddNumberToObject(root, "invertfanpolarity", nvs_config_get_u16(NVS_CONFIG_INVERT_FAN_POLARITY, 1));
cJSON_AddNumberToObject(root, "autofanspeed", nvs_config_get_u16(NVS_CONFIG_AUTO_FAN_SPEED, 1));
Expand Down
3 changes: 3 additions & 0 deletions main/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "esp_lvgl_port.h"
#include "driver/gpio.h"

#include "display.h"

#define BUTTON_BOOT_GPIO GPIO_NUM_0
#define ESP_INTR_FLAG_DEFAULT 0
#define LONG_PRESS_DURATION_MS 2000
Expand All @@ -31,6 +33,7 @@ static void IRAM_ATTR button_isr_handler(void *arg)
static void button_short_clicked_event_cb(lv_event_t *e)
{
ESP_LOGI(TAG, "Short button click detected");
display_on();
button_short_clicked_fn();
}

Expand Down
1 change: 1 addition & 0 deletions main/nvs_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#define NVS_CONFIG_BOARD_VERSION "boardversion"
#define NVS_CONFIG_FLIP_SCREEN "flipscreen"
#define NVS_CONFIG_INVERT_SCREEN "invertscreen"
#define NVS_CONFIG_DISPLAY_TIMEOUT "displayTimeout"
#define NVS_CONFIG_INVERT_FAN_POLARITY "invertfanpol"
#define NVS_CONFIG_AUTO_FAN_SPEED "autofanspeed"
#define NVS_CONFIG_FAN_SPEED "fanspeed"
Expand Down
4 changes: 4 additions & 0 deletions main/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ void SYSTEM_init_peripherals(GlobalState * GLOBAL_STATE) {
ESP_LOGW(TAG, "Screen init failed");
}

if (display_timeout_start(GLOBAL_STATE) != ESP_OK) {
ESP_LOGW(TAG, "Display timeout init failed");
}

netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
}

Expand Down

0 comments on commit 0533d24

Please sign in to comment.