Browse Source

Added esp light sleep, still buggy state

master
Macoy Madson 12 months ago
parent
commit
d7d0c1bd8e
  1. 1
      app/src/CMakeLists.txt
  2. 47
      app/src/main.c
  3. 64
      app/src/odroid/sleep.c
  4. 4
      app/src/odroid/sleep.h

1
app/src/CMakeLists.txt

@ -6,6 +6,7 @@ idf_component_register(
"odroid/display.c"
"odroid/input.c"
"odroid/sdcard.c"
"odroid/sleep.c"
"text.c"
INCLUDE_DIRS

47
app/src/main.c

@ -1,15 +1,16 @@
#include <esp_log.h>
#include <esp_timer.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <string.h>
#include "macros.h"
#include "odroid/audio.h"
#include "odroid/battery.h"
#include "odroid/display.h"
#include "odroid/input.h"
#include "odroid/sdcard.h"
#include "macros.h"
#include "odroid/sleep.h"
#include "text.h"
#include <esp_log.h>
#include <esp_timer.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <string.h>
#define ArraySize(array) sizeof((array)) / sizeof((array)[0])
@ -24,6 +25,7 @@ static bool printInput = false;
static bool enableLowFrameRate = true;
static int64_t autoSleepModeMicroseconds = 10 * 1000000;
static int64_t lightSleepMicroseconds = 30 * 1000000;
static const char* LOG_TAG = "Main";
static uint16_t gFramebuffer[LCD_WIDTH * LCD_HEIGHT];
@ -195,6 +197,7 @@ void app_main(void)
Odroid_Input lastFrameInput = {0};
bool sleepMode = false;
bool backlightOn = true;
bool waitForSleepActivateRelease = false;
@ -221,24 +224,28 @@ void app_main(void)
Odroid_PrintInputState(&input);
// Sleep mode
if (!backlightOn)
if (sleepMode)
{
if (!waitForSleepActivateRelease && hasInputsThisFrame)
// Try to save battery while still listening to inputs
while (Odroid_EnterLightSleep(lightSleepMicroseconds))
{
Odroid_BacklightInit();
backlightOn = true;
float batteryPercentage = Odroid_ReadBatteryLevelPercentage();
if (batteryPercentage < 15.f)
Odroid_EnableBatteryLight();
else
Odroid_DisableBatteryLight();
}
else
{
if (!input.b)
waitForSleepActivateRelease = false;
// Try to save battery while still listening to inputs
// TODO: Use the ESP sleep modes
vTaskDelay(250);
sleepMode = false;
timeSinceLastInputMicroSecs = esp_timer_get_time();
continue;
}
continue;
}
if (!sleepMode && !backlightOn)
{
Odroid_BacklightInit();
backlightOn = true;
}
if (input.a && !lastFrameInput.a)
@ -276,12 +283,12 @@ void app_main(void)
// TODO: Tell LCD driver to enter sleep
// TODO: Use esp light sleep mode
Odroid_BacklightDeinit();
waitForSleepActivateRelease = true;
}
else
Odroid_BacklightInit();
backlightOn = !backlightOn;
sleepMode = !sleepMode;
}
// Movement

64
app/src/odroid/sleep.c

@ -0,0 +1,64 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include "driver/gpio.h"
#include "driver/uart.h"
#include "esp_log.h"
#include "esp_sleep.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
// Button B
#define BUTTON_GPIO_NUM_DEFAULT GPIO_NUM_32
#define BUTTON_WAKEUP_LEVEL_DEFAULT 1
// Returns whether button was pressed to wake
// See Dependencies/esp-idf/examples/system/light_sleep/main/light_sleep_example_main.c
bool Odroid_EnterLightSleep(int64_t timerAutoWakeupMicroSecs)
{
esp_sleep_enable_timer_wakeup(timerAutoWakeupMicroSecs);
const int button_gpio_num = BUTTON_GPIO_NUM_DEFAULT;
const int wakeup_level = BUTTON_WAKEUP_LEVEL_DEFAULT;
gpio_config_t config = {.pin_bit_mask = BIT64(button_gpio_num), .mode = GPIO_MODE_INPUT};
ESP_ERROR_CHECK(gpio_config(&config));
gpio_wakeup_enable(button_gpio_num,
wakeup_level == 0 ? GPIO_INTR_LOW_LEVEL : GPIO_INTR_HIGH_LEVEL);
esp_sleep_enable_gpio_wakeup();
printf("Entering light sleep\n");
/* To make sure the complete line is printed before entering sleep mode, */
/* need to wait until UART TX FIFO is empty: */
uart_wait_tx_idle_polling(CONFIG_ESP_CONSOLE_UART_NUM);
/* Get timestamp before entering sleep */
int64_t t_before_us = esp_timer_get_time();
esp_light_sleep_start();
/* Get timestamp after waking up from sleep */
int64_t t_after_us = esp_timer_get_time();
/* Determine wake up reason */
const char* wakeup_reason;
esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
switch (cause)
{
case ESP_SLEEP_WAKEUP_TIMER:
wakeup_reason = "timer";
break;
case ESP_SLEEP_WAKEUP_GPIO:
wakeup_reason = "pin";
break;
default:
wakeup_reason = "other";
break;
}
printf("Returned from light sleep, reason: %s, t=%lld ms, slept for %lld ms\n", wakeup_reason,
t_after_us / 1000, (t_after_us - t_before_us) / 1000);
return cause == ESP_SLEEP_WAKEUP_GPIO;
}

4
app/src/odroid/sleep.h

@ -0,0 +1,4 @@
#pragma once
// Returns whether button was pressed to wake
bool Odroid_EnterLightSleep(int64_t timerAutoWakeupMicroSecs);
Loading…
Cancel
Save