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
106
107
|
/*
* Copyright 2023 jacqueline <me@jacqueline.id.au>
*
* SPDX-License-Identifier: GPL-3.0-only
*/
#include "assert.h"
#include "audio_fsm.hpp"
#include "core/lv_obj.h"
#include "display_init.hpp"
#include "esp_err.h"
#include "esp_log.h"
#include "event_queue.hpp"
#include "gpio_expander.hpp"
#include "lvgl/lvgl.h"
#include "relative_wheel.hpp"
#include "spi.hpp"
#include "system_events.hpp"
#include "system_fsm.hpp"
#include "ui_fsm.hpp"
#include "i2c.hpp"
#include "touchwheel.hpp"
namespace system_fsm {
namespace states {
static const char kTag[] = "BOOT";
console::AppConsole* Booting::sAppConsole;
auto Booting::entry() -> void {
ESP_LOGI(kTag, "beginning tangara boot");
ESP_LOGI(kTag, "installing bare minimum drivers");
// I2C and SPI are both always needed. We can't even power down or show an
// error without these.
ESP_ERROR_CHECK(drivers::init_i2c());
ESP_ERROR_CHECK(drivers::init_spi());
// These drivers are the bare minimum to even show an error. If these fail,
// then the system is completely hosed.
sGpioExpander.reset(drivers::GpioExpander::Create());
assert(sGpioExpander != nullptr);
// Start bringing up LVGL now, since we have all of its prerequisites.
ESP_LOGI(kTag, "installing ui drivers");
lv_init();
sDisplay.reset(drivers::Display::Create(sGpioExpander.get(),
drivers::displays::kST7735R));
assert(sDisplay != nullptr);
sTouch.reset(drivers::TouchWheel::Create());
if (sTouch != nullptr) {
sRelativeTouch.reset(new drivers::RelativeWheel(sTouch.get()));
}
// The UI FSM now has everything it needs to start setting up. Do this now,
// so that we can properly show the user any errors that appear later.
ui::UiState::Init(sGpioExpander.get(), sRelativeTouch, sDisplay);
events::Dispatch<DisplayReady, ui::UiState>(DisplayReady());
// These drivers are required for normal operation, but aren't critical for
// booting. We will transition to the error state if these aren't present.
ESP_LOGI(kTag, "installing required drivers");
sSamd.reset(drivers::Samd::Create());
auto dac_res = drivers::AudioDac::create(sGpioExpander.get());
if (dac_res.has_error() || !sSamd || !sRelativeTouch) {
events::Dispatch<FatalError, SystemState, ui::UiState, audio::AudioState>(
FatalError());
return;
}
sDac.reset(dac_res.value());
// These drivers are initialised on boot, but are recoverable (if weird) if
// they fail.
ESP_LOGI(kTag, "installing optional drivers");
sBattery.reset(drivers::Battery::Create());
// All drivers are now loaded, so we can finish initing the other state
// machines.
audio::AudioState::Init(sGpioExpander.get(), sDac, sDatabase);
events::Dispatch<BootComplete, SystemState, ui::UiState, audio::AudioState>(
BootComplete());
}
auto Booting::exit() -> void {
// TODO(jacqueline): Gate this on something. Debug flag? Flashing mode?
sAppConsole = new console::AppConsole(sDatabase);
sAppConsole->Launch();
}
auto Booting::react(const BootComplete& ev) -> void {
ESP_LOGI(kTag, "bootup completely successfully");
// It's possible that the SAMD is currently exposing the SD card as a USB
// device. Make sure we don't immediately try to claim it.
if (sSamd && sSamd->ReadUsbMscStatus() ==
drivers::Samd::UsbMscStatus::kAttachedMounted) {
transit<Unmounted>();
}
transit<Running>();
}
} // namespace states
} // namespace system_fsm
|