summaryrefslogtreecommitdiff
path: root/src/system_fsm/booting.cpp
blob: b166a02c123a3c09b300fa4dede5e35534372d4b (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Copyright 2023 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#include "collation.hpp"
#include "haptics.hpp"
#include "spiffs.hpp"
#include "system_fsm.hpp"

#include <stdint.h>
#include <memory>

#include "assert.h"
#include "esp_err.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/portmacro.h"
#include "freertos/projdefs.h"
#include "freertos/timers.h"

#include "adc.hpp"
#include "audio_fsm.hpp"
#include "battery.hpp"
#include "bluetooth.hpp"
#include "bluetooth_types.hpp"
#include "display_init.hpp"
#include "event_queue.hpp"
#include "gpios.hpp"
#include "i2c.hpp"
#include "nvs.hpp"
#include "samd.hpp"
#include "service_locator.hpp"
#include "spi.hpp"
#include "system_events.hpp"
#include "tag_parser.hpp"
#include "tasks.hpp"
#include "touchwheel.hpp"
#include "track_queue.hpp"
#include "ui_fsm.hpp"

namespace system_fsm {
namespace states {

[[maybe_unused]] static const char kTag[] = "BOOT";

static auto bt_event_cb(drivers::bluetooth::Event ev) -> void {
  events::Ui().Dispatch(BluetoothEvent{.event = ev});
}

static const TickType_t kInterruptCheckPeriod = pdMS_TO_TICKS(100);

auto Booting::entry() -> void {
  ESP_LOGI(kTag, "beginning tangara boot");
  sServices.reset(new ServiceLocator());

  ESP_LOGI(kTag, "installing early 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_spi());
  sServices->gpios(std::unique_ptr<drivers::Gpios>(drivers::Gpios::Create()));

  ESP_LOGI(kTag, "starting ui");
  if (!ui::UiState::InitBootSplash(sServices->gpios())) {
    events::System().Dispatch(FatalError{});
    return;
  }

  ESP_LOGI(kTag, "starting bg worker");
  sServices->bg_worker(std::make_unique<tasks::WorkerPool>());

  ESP_LOGI(kTag, "installing remaining drivers");
  drivers::spiffs_mount();
  sServices->samd(std::unique_ptr<drivers::Samd>(drivers::Samd::Create()));
  sServices->nvs(
      std::unique_ptr<drivers::NvsStorage>(drivers::NvsStorage::OpenSync()));
  sServices->touchwheel(
      std::unique_ptr<drivers::TouchWheel>{drivers::TouchWheel::Create()});
  sServices->haptics(std::make_unique<drivers::Haptics>());

  auto adc = drivers::AdcBattery::Create();
  sServices->battery(std::make_unique<battery::Battery>(
      sServices->samd(), std::unique_ptr<drivers::AdcBattery>(adc)));

  sServices->track_queue(
      std::make_unique<audio::TrackQueue>(sServices->bg_worker()));
  sServices->tag_parser(std::make_unique<database::TagParserImpl>());
  sServices->collator(locale::CreateCollator());

  ESP_LOGI(kTag, "init bluetooth");
  sServices->bluetooth(std::make_unique<drivers::Bluetooth>(
      sServices->nvs(), sServices->bg_worker()));
  sServices->bluetooth().SetEventHandler(bt_event_cb);

  if (sServices->nvs().OutputMode() ==
      drivers::NvsStorage::Output::kBluetooth) {
    ESP_LOGI(kTag, "enabling bluetooth");
    sServices->bluetooth().Enable();
  }

  sServices->nvs().LockPolarity(true);

  BootComplete ev{.services = sServices};
  events::Audio().Dispatch(ev);
  events::Ui().Dispatch(ev);
  events::System().Dispatch(ev);
}

auto Booting::exit() -> void {
  // TODO(jacqueline): Gate this on something. Debug flag? Flashing mode?
  sAppConsole = new console::AppConsole();
  sAppConsole->sServices = sServices;
  sAppConsole->Launch();

  TimerHandle_t timer = xTimerCreate("INTERRUPTS", kInterruptCheckPeriod, true,
                                     NULL, check_interrupts_cb);
  xTimerStart(timer, portMAX_DELAY);
}

auto Booting::react(const BootComplete& ev) -> void {
  ESP_LOGI(kTag, "bootup completely successfully");

  if (sServices->gpios().IsLocked()) {
    transit<Idle>();
  } else {
    transit<Running>();
  }
}

}  // namespace states
}  // namespace system_fsm