summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2023-08-22 13:39:49 +1000
committerjacqueline <me@jacqueline.id.au>2023-08-22 13:39:49 +1000
commit9c105cf613fb849268d619dc887dc2b54dcd4ec6 (patch)
tree6b3e26524fa948ec3e527268a55c64110f291c77 /src
parent588857fef81ccb980c64b6dec0ef3cd5241aef88 (diff)
downloadtangara-fw-9c105cf613fb849268d619dc887dc2b54dcd4ec6.tar.gz
Good idea to commit all the files!
Diffstat (limited to 'src')
-rw-r--r--src/system_fsm/idle.cpp87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/system_fsm/idle.cpp b/src/system_fsm/idle.cpp
new file mode 100644
index 00000000..caba3954
--- /dev/null
+++ b/src/system_fsm/idle.cpp
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2023 jacqueline <me@jacqueline.id.au>
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+#include "app_console.hpp"
+#include "file_gatherer.hpp"
+#include "freertos/portmacro.h"
+#include "freertos/projdefs.h"
+#include "gpios.hpp"
+#include "result.hpp"
+
+#include "audio_fsm.hpp"
+#include "event_queue.hpp"
+#include "storage.hpp"
+#include "system_events.hpp"
+#include "system_fsm.hpp"
+#include "ui_fsm.hpp"
+
+namespace system_fsm {
+namespace states {
+
+static const char kTag[] = "IDLE";
+static const TickType_t kTicksBeforeSleep = pdMS_TO_TICKS(10000);
+
+static void timer_callback(TimerHandle_t timer) {
+ events::System().Dispatch(internal::IdleTimeout{});
+}
+
+/*
+ * Ensure the storage and database are both available. If either of these fails
+ * to open, then we assume it's an issue with the underlying SD card.
+ */
+void Idle::entry() {
+ ESP_LOGI(kTag, "system became idle");
+ events::Audio().Dispatch(OnIdle{});
+ events::Ui().Dispatch(OnIdle{});
+
+ sIdleTimeout = xTimerCreate("idle_timeout", kTicksBeforeSleep, false, NULL,
+ timer_callback);
+ xTimerStart(sIdleTimeout, portMAX_DELAY);
+}
+
+void Idle::exit() {
+ xTimerStop(sIdleTimeout, portMAX_DELAY);
+ xTimerDelete(sIdleTimeout, portMAX_DELAY);
+ ESP_LOGI(kTag, "system left idle");
+}
+
+void Idle::react(const KeyLockChanged& ev) {
+ if (!ev.falling) {
+ transit<Running>();
+ }
+}
+
+void Idle::react(const internal::IdleTimeout& ev) {
+ ESP_LOGI(kTag, "system shutting down");
+
+ // FIXME: It would be neater to just free a bunch of our pointers, deinit the
+ // other state machines, etc.
+ if (sTouch) {
+ sTouch->PowerDown();
+ }
+
+ // Pull down to turn things off
+ sGpios->WriteBuffered(drivers::IGpios::Pin::kAmplifierEnable, false);
+ sGpios->WriteBuffered(drivers::IGpios::Pin::kSdPowerEnable, false);
+ sGpios->WriteBuffered(drivers::IGpios::Pin::kDisplayEnable, false);
+
+ // Leave up to match the external pullups
+ sGpios->WriteBuffered(drivers::IGpios::Pin::kSdMuxSwitch, true);
+ sGpios->WriteBuffered(drivers::IGpios::Pin::kSdMuxDisable, true);
+
+
+ // Pull down to prevent sourcing current uselessly from input pins.
+ sGpios->WriteBuffered(drivers::IGpios::Pin::kSdCardDetect, false);
+ sGpios->WriteBuffered(drivers::IGpios::Pin::kKeyUp, false);
+ sGpios->WriteBuffered(drivers::IGpios::Pin::kKeyDown, false);
+
+ sGpios->Flush();
+
+ sSamd->PowerDown();
+}
+
+} // namespace states
+} // namespace system_fsm