From 6ef6307dfebd3143ce4525864fb18dc489a576c3 Mon Sep 17 00:00:00 2001 From: KaiNakamura Date: Tue, 23 Apr 2024 20:09:44 -0400 Subject: [PATCH] Add sleep mode --- src/Squidbox.cpp | 8 ++++++++ src/Squidbox.h | 1 + src/components/button/Button.h | 1 + src/gui/menu-item/quit/QuitMenuItem.cpp | 11 +++++++++++ src/gui/menu-item/quit/QuitMenuItem.h | 13 +++++++++++++ src/scene/main/MainScene.cpp | 6 ++++-- src/scene/main/MainScene.h | 2 +- 7 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 src/gui/menu-item/quit/QuitMenuItem.cpp create mode 100644 src/gui/menu-item/quit/QuitMenuItem.h diff --git a/src/Squidbox.cpp b/src/Squidbox.cpp index f1d0599..76ce5e3 100644 --- a/src/Squidbox.cpp +++ b/src/Squidbox.cpp @@ -18,6 +18,8 @@ Squidbox::Squidbox() { buttons[5] = new Button(PIN_BUTTON_5); buttons[6] = new Button(PIN_BUTTON_6); buttons[7] = new Button(PIN_BUTTON_7); + + esp_sleep_enable_ext0_wakeup(static_cast(PIN_BUTTON_0), 0); } void Squidbox::init() { @@ -50,6 +52,12 @@ void Squidbox::switchTo(SceneType scene) { } } +void Squidbox::sleep() { + screen->clear(); + screen->update(); + esp_deep_sleep_start(); +} + Screen *Squidbox::getScreen() { return screen; } Joystick *Squidbox::getJoystick() { return joystick; } diff --git a/src/Squidbox.h b/src/Squidbox.h index e782723..7c3b99a 100644 --- a/src/Squidbox.h +++ b/src/Squidbox.h @@ -37,6 +37,7 @@ class Squidbox { void init(); void update(); void switchTo(SceneType scene); + void sleep(); Screen *getScreen(); Joystick *getJoystick(); Knob *getKnob(); diff --git a/src/components/button/Button.h b/src/components/button/Button.h index 059be4c..c800d85 100644 --- a/src/components/button/Button.h +++ b/src/components/button/Button.h @@ -1,5 +1,6 @@ #pragma once +#include "esp_sleep.h" #include class Button { diff --git a/src/gui/menu-item/quit/QuitMenuItem.cpp b/src/gui/menu-item/quit/QuitMenuItem.cpp new file mode 100644 index 0000000..916352e --- /dev/null +++ b/src/gui/menu-item/quit/QuitMenuItem.cpp @@ -0,0 +1,11 @@ +#include "QuitMenuItem.h" + +QuitMenuItem::QuitMenuItem(Squidbox *squidbox) + : MenuItem("Quit"), squidbox(squidbox) { + setOnSelectCallback(onSelect, this); +} + +void QuitMenuItem::onSelect(void *arg) { + QuitMenuItem *self = static_cast(arg); + self->squidbox->sleep(); +} diff --git a/src/gui/menu-item/quit/QuitMenuItem.h b/src/gui/menu-item/quit/QuitMenuItem.h new file mode 100644 index 0000000..bde336d --- /dev/null +++ b/src/gui/menu-item/quit/QuitMenuItem.h @@ -0,0 +1,13 @@ +#pragma once + +#include "Squidbox.h" +#include "gui/menu-item/MenuItem.h" + +class QuitMenuItem : public MenuItem { +private: + Squidbox *squidbox; + static void onSelect(void *arg); + +public: + QuitMenuItem(Squidbox *squidbox); +}; \ No newline at end of file diff --git a/src/scene/main/MainScene.cpp b/src/scene/main/MainScene.cpp index d7c3712..c02aa5e 100644 --- a/src/scene/main/MainScene.cpp +++ b/src/scene/main/MainScene.cpp @@ -10,13 +10,15 @@ MainScene::MainScene(Squidbox *squidbox) : Scene(squidbox, nullptr) { new SwitchSceneMenuItem("Notes", squidbox, NOTE_SCENE); MenuItem *drumMenuItem = new SwitchSceneMenuItem("Drums", squidbox, DRUM_SCENE); + MenuItem *quitMenuItem = new QuitMenuItem(squidbox); - MenuItem **menuItems = new MenuItem *[3]; + MenuItem **menuItems = new MenuItem *[4]; menuItems[0] = chordMenuItem; menuItems[1] = noteMenuItem; menuItems[2] = drumMenuItem; + menuItems[3] = quitMenuItem; - menu = new Menu("Squidbox", 3, menuItems); + menu = new Menu("Squidbox", 4, menuItems); } void MainScene::init() { Scene::init(); } diff --git a/src/scene/main/MainScene.h b/src/scene/main/MainScene.h index 2f27967..01e5155 100644 --- a/src/scene/main/MainScene.h +++ b/src/scene/main/MainScene.h @@ -1,11 +1,11 @@ #pragma once +#include "gui/menu-item/quit/QuitMenuItem.h" #include "gui/menu-item/switch-scene/SwitchSceneMenuItem.h" #include "gui/menu/Menu.h" #include "scene/Scene.h" #include "util/Ascii.h" - class MainScene : public Scene { public: MainScene(Squidbox *squidbox);