blob: d21e8bcbba5946ec879b93e2bd9ec2965f68e817 (
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
106
|
/*
* Copyright 2023 jacqueline <me@jacqueline.id.au>
*
* SPDX-License-Identifier: GPL-3.0-only
*/
#include "system_fsm.hpp"
#include "audio_fsm.hpp"
#include "event_queue.hpp"
#include "gpios.hpp"
#include "relative_wheel.hpp"
#include "system_events.hpp"
#include "tag_parser.hpp"
#include "track_queue.hpp"
static const char kTag[] = "system";
namespace system_fsm {
std::shared_ptr<drivers::Gpios> SystemState::sGpios;
std::shared_ptr<drivers::Samd> SystemState::sSamd;
std::shared_ptr<drivers::NvsStorage> SystemState::sNvs;
std::shared_ptr<drivers::TouchWheel> SystemState::sTouch;
std::shared_ptr<drivers::RelativeWheel> SystemState::sRelativeTouch;
std::shared_ptr<drivers::AdcBattery> SystemState::sAdc;
std::shared_ptr<battery::Battery> SystemState::sBattery;
std::shared_ptr<drivers::SdStorage> SystemState::sStorage;
std::shared_ptr<drivers::Display> SystemState::sDisplay;
std::shared_ptr<drivers::Bluetooth> SystemState::sBluetooth;
std::shared_ptr<database::Database> SystemState::sDatabase;
std::shared_ptr<database::TagParserImpl> SystemState::sTagParser;
std::shared_ptr<audio::TrackQueue> SystemState::sTrackQueue;
console::AppConsole* SystemState::sAppConsole;
void SystemState::react(const FatalError& err) {
if (!is_in_state<states::Error>()) {
transit<states::Error>();
}
}
void SystemState::react(const internal::GpioInterrupt&) {
bool prev_key_up = sGpios->Get(drivers::Gpios::Pin::kKeyUp);
bool prev_key_down = sGpios->Get(drivers::Gpios::Pin::kKeyDown);
bool prev_key_lock = sGpios->Get(drivers::Gpios::Pin::kKeyLock);
bool prev_has_headphones = !sGpios->Get(drivers::Gpios::Pin::kPhoneDetect);
sGpios->Read();
bool key_up = sGpios->Get(drivers::Gpios::Pin::kKeyUp);
bool key_down = sGpios->Get(drivers::Gpios::Pin::kKeyDown);
bool key_lock = sGpios->Get(drivers::Gpios::Pin::kKeyLock);
bool has_headphones = !sGpios->Get(drivers::Gpios::Pin::kPhoneDetect);
if (key_up != prev_key_up) {
KeyUpChanged ev{.falling = prev_key_up};
events::Audio().Dispatch(ev);
events::Ui().Dispatch(ev);
}
if (key_down != prev_key_down) {
KeyDownChanged ev{.falling = prev_key_down};
events::Audio().Dispatch(ev);
events::Ui().Dispatch(ev);
}
if (key_lock != prev_key_lock) {
KeyLockChanged ev{.falling = key_lock};
events::System().Dispatch(ev);
events::Ui().Dispatch(ev);
}
if (has_headphones != prev_has_headphones) {
HasPhonesChanged ev{.falling = prev_has_headphones};
events::Audio().Dispatch(ev);
}
}
void SystemState::react(const internal::SamdInterrupt&) {
auto prev_charge_status = sSamd->GetChargeStatus();
auto prev_usb_status = sSamd->GetUsbStatus();
sSamd->UpdateChargeStatus();
sSamd->UpdateUsbStatus();
auto charge_status = sSamd->GetChargeStatus();
auto usb_status = sSamd->GetUsbStatus();
if (charge_status != prev_charge_status) {
ChargingStatusChanged ev{};
events::System().Dispatch(ev);
events::Ui().Dispatch(ev);
}
if (usb_status != prev_usb_status) {
ESP_LOGI(kTag, "usb status changed");
}
}
auto SystemState::IdleCondition() -> bool {
return !sGpios->Get(drivers::IGpios::Pin::kKeyLock) &&
audio::AudioState::is_in_state<audio::states::Standby>();
}
} // namespace system_fsm
FSM_INITIAL_STATE(system_fsm::SystemState, system_fsm::states::Booting)
|