Skip to content

Commit

Permalink
feat: deep sleep mode when user is afk
Browse files Browse the repository at this point in the history
  • Loading branch information
Otrebor671 committed Dec 12, 2024
1 parent 926f05e commit b92d558
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 19 deletions.
2 changes: 1 addition & 1 deletion firmware/components/openthread/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ idf_component_register(SRC_DIRS "${src_dirs}"
REQUIRES esp_netif lwip esp_driver_uart driver
LDFRAGMENTS linker.lf
PRIV_REQUIRES console esp_event esp_partition esp_timer
ieee802154 mbedtls nvs_flash)
ieee802154 mbedtls nvs_flash spi_flash)

if(CONFIG_OPENTHREAD_ENABLED OR CONFIG_OPENTHREAD_SPINEL_ONLY)
if(CONFIG_OPENTHREAD_RADIO)
Expand Down
2 changes: 1 addition & 1 deletion firmware/main/modules/keyboard/keyboard_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ static int IDLE_TIMEOUT_S = 30;

static const char* TAG = "keyboard";
static input_callback_t input_callback = NULL;
esp_timer_handle_t idle_timer;
static esp_timer_handle_t idle_timer;
static bool is_idle = false;
static bool lock_input = false;

Expand Down
31 changes: 18 additions & 13 deletions firmware/main/modules/menus_module/menus_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "oled_screen.h"
#include "preferences.h"
#include "screen_saver.h"
#include "sleep_mode.h"

#ifdef CONFIG_RESOLUTION_128X64
#define SCREEN_WIDTH 128
Expand All @@ -20,9 +21,9 @@

static menus_manager_t* menus_ctx;
static void menus_input_cb(uint8_t button_name, uint8_t button_event);
static app_state2_t app_state2 = {.in_app = false,
.input_callback = NULL,
.input_last_callback = NULL};
static app_state2_t app_state = {.in_app = false,
.input_callback = NULL,
.input_last_callback = NULL};
static TaskHandle_t screen_saver_task = NULL;
static bool screen_saver_running = false;

Expand Down Expand Up @@ -139,19 +140,19 @@ static void menus_input_cb(uint8_t button_name, uint8_t button_event) {
return;
}

if (app_state2.in_app) {
if (app_state2.input_callback) {
app_state2.input_callback(button_name, button_event);
if (app_state.in_app) {
if (app_state.input_callback) {
app_state.input_callback(button_name, button_event);
return;
}
app_state2.in_app = false;
app_state.in_app = false;
return;
}

if (button_event != BUTTON_PRESS_DOWN) {
return;
}

sleep_mode_reset_timer();
if (screen_saver_get_idle_state()) {
display_menus();
return;
Expand Down Expand Up @@ -193,6 +194,7 @@ static void get_reset_menu() {
show_logo();
} else {
preferences_put_int("MENUNUMBER", MENU_MAIN);
sleep_mode_reset_timer();
screen_saver_get_idle_state();
refresh_menus();
}
Expand All @@ -206,14 +208,15 @@ void menus_module_disable_input() {
}

void menus_module_set_app_state(bool in_app, input_callback_t input_cb) {
app_state2.in_app = in_app;
app_state2.input_last_callback = app_state2.input_callback;
app_state2.input_callback = input_cb;
app_state.in_app = in_app;
app_state.input_last_callback = app_state.input_callback;
app_state.input_callback = input_cb;
sleep_mode_reset_timer();
screen_saver_get_idle_state();
}

void menus_module_set_app_state_last() {
app_state2.input_callback = app_state2.input_last_callback;
app_state.input_callback = app_state.input_last_callback;
}

void menus_module_restart() {
Expand All @@ -228,6 +231,7 @@ void menus_module_reset() {

void menus_module_exit_app() {
menus_module_set_app_state(false, menus_input_cb);
sleep_mode_reset_timer();
screen_saver_get_idle_state();
navigation_exit();
}
Expand All @@ -237,7 +241,7 @@ menu_idx_t menus_module_get_current_menu() {
}

bool menus_module_get_app_state() {
return app_state2.in_app;
return app_state.in_app;
}

void menus_module_set_menu(menu_idx_t menu_idx) {
Expand Down Expand Up @@ -277,6 +281,7 @@ void menus_module_begin() {
menus_ctx = calloc(1, sizeof(menus_manager_t));
menus_ctx->menus_count = sizeof(menus) / sizeof(menu_t);
screen_saver_begin();
sleep_mode_begin();
keyboard_module_set_input_callback(menus_input_cb);
oled_screen_begin();
get_reset_menu();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ static int IDLE_TIMEOUT_S = 30;
static const char* TAG = "screen_saver";

static volatile bool screen_saver_running;
esp_timer_handle_t screen_savar_idle_timer2;
static esp_timer_handle_t screen_saver_idle_timer;

void screen_saver_run();

Expand Down Expand Up @@ -88,13 +88,13 @@ void screen_saver_set_idle_timeout(uint8_t timeout_seconds) {
bool screen_saver_get_idle_state() {
bool idle = screen_saver_running;
screen_saver_stop();
esp_timer_stop(screen_savar_idle_timer2);
esp_timer_start_once(screen_savar_idle_timer2, IDLE_TIMEOUT_S * 1000 * 1000);
esp_timer_stop(screen_saver_idle_timer);
esp_timer_start_once(screen_saver_idle_timer, IDLE_TIMEOUT_S * 1000 * 1000);
return idle;
}

void screen_saver_begin() {
esp_timer_create_args_t timer_args = {
.callback = timer_callback, .arg = NULL, .name = "idle_timer"};
esp_err_t err = esp_timer_create(&timer_args, &screen_savar_idle_timer2);
esp_err_t err = esp_timer_create(&timer_args, &screen_saver_idle_timer);
}
51 changes: 51 additions & 0 deletions firmware/main/modules/sleep_mode/sleep_mode.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "sleep_mode.h"

#include "driver/gpio.h"
#include "driver/rtc_io.h"
#include "esp_sleep.h"
#include "esp_system.h"
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "menus_module.h"
#include "oled_screen.h"

#define WAKEUP_PIN GPIO_NUM_1

static int AFK_TIMEOUT_S = 10;
static const char* TAG = "sleep_mode";
static esp_timer_handle_t afk_timer;

void sleep_mode_reset_timer();

static void sleep_mode_sleep() {
esp_sleep_enable_ext1_wakeup_io((1ULL << WAKEUP_PIN),
ESP_EXT1_WAKEUP_ANY_LOW);
esp_deep_sleep_start();
}

static void timer_callback() {
if (menus_module_get_app_state()) {
return;
}
oled_screen_clear();
sleep_mode_sleep();
}

void sleep_mode_reset_timer() {
esp_timer_stop(afk_timer);
esp_timer_start_once(afk_timer, AFK_TIMEOUT_S * 1000 * 1000);
}

void sleep_mode_set_afk_timeout(uint8_t timeout_seconds) {
AFK_TIMEOUT_S = timeout_seconds;
sleep_mode_reset_timer();
}

void sleep_mode_begin() {
esp_timer_create_args_t timer_args = {
.callback = timer_callback, .arg = NULL, .name = "afk_timer"};
esp_err_t err = esp_timer_create(&timer_args, &afk_timer);
sleep_mode_reset_timer();
}
6 changes: 6 additions & 0 deletions firmware/main/modules/sleep_mode/sleep_mode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#include <stdio.h>

void sleep_mode_begin();
void sleep_mode_reset_timer();

0 comments on commit b92d558

Please sign in to comment.