summaryrefslogtreecommitdiff
path: root/src/system_fsm/include/system_fsm.hpp
blob: 4413f64e1069436e6037ece1d53e8b61fa6c9855 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 * Copyright 2023 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#pragma once

#include <memory>

#include "app_console.hpp"
#include "battery.hpp"
#include "dac.hpp"
#include "database.hpp"
#include "display.hpp"
#include "gpio_expander.hpp"
#include "relative_wheel.hpp"
#include "samd.hpp"
#include "storage.hpp"
#include "tinyfsm.hpp"
#include "touchwheel.hpp"

#include "system_events.hpp"

namespace system_fsm {

/*
 * State machine for the overall system state. Responsible for managing
 * peripherals, and bringing the rest of the system up and down.
 */
class SystemState : public tinyfsm::Fsm<SystemState> {
 public:
  virtual ~SystemState() {}

  virtual void entry() {}
  virtual void exit() {}

  /* Fallback event handler. Does nothing. */
  void react(const tinyfsm::Event& ev) {}

  void react(const FatalError&);

  virtual void react(const DisplayReady&) {}
  virtual void react(const BootComplete&) {}
  virtual void react(const StorageUnmountRequested&) {}
  virtual void react(const internal::ReadyToUnmount&) {}
  virtual void react(const StorageMounted&) {}
  virtual void react(const StorageError&) {}

 protected:
  static std::shared_ptr<drivers::GpioExpander> sGpioExpander;
  static std::shared_ptr<drivers::Samd> sSamd;

  static std::shared_ptr<drivers::TouchWheel> sTouch;
  static std::shared_ptr<drivers::RelativeWheel> sRelativeTouch;
  static std::shared_ptr<drivers::Battery> sBattery;
  static std::shared_ptr<drivers::SdStorage> sStorage;
  static std::shared_ptr<drivers::Display> sDisplay;
  static std::shared_ptr<drivers::AudioDac> sDac;
  static std::shared_ptr<database::Database> sDatabase;
};

namespace states {

/*
 * Initial state. Initialises peripherals, starts up lvgl, checks everything
 * looks good.
 */
class Booting : public SystemState {
 private:
  static console::AppConsole* sAppConsole;

 public:
  void entry() override;
  void exit() override;

  void react(const BootComplete&) override;
  using SystemState::react;
};

/*
 * Most common state. Everything is going full bore!
 */
class Running : public SystemState {
 public:
  void entry() override;
  void exit() override;

  void react(const StorageUnmountRequested&) override;
  void react(const internal::ReadyToUnmount&) override;
  void react(const StorageError&) override;
  using SystemState::react;
};

class Unmounted : public SystemState {};

/*
 * Something unrecoverably bad went wrong. Shows an error (if possible), awaits
 * reboot.
 */
class Error : public SystemState {};

}  // namespace states

}  // namespace system_fsm