summaryrefslogtreecommitdiff
path: root/src/main/main.cpp
blob: 685e1fc1f15017f2106bdac7e1f06e8ec903e39a (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
/*
 * Copyright 2023 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#include "freertos/portmacro.h"

#include "gpios.hpp"
#include "i2c.hpp"
#include "system_events.hpp"
#include "tinyfsm.hpp"

#include "audio_fsm.hpp"
#include "event_queue.hpp"
#include "system_fsm.hpp"
#include "ui_fsm.hpp"

extern "C" void app_main(void) {
  ESP_ERROR_CHECK(drivers::init_i2c());
  drivers::Gpios* gpios = system_fsm::SystemState::early_init_gpios();

  // Semaphores must be empty before being added to a queue set. Hence all this
  // weird early init stuff; by being explicit about initialisation order, we're
  // able to handle GPIO ISR notifcations + system events from the same task,
  // and a little mess with worth not needing to allocate a whole extra stack.
  QueueSetHandle_t set = xQueueCreateSet(2);
  auto* event_queue = events::queues::SystemAndAudio();
  xQueueAddToSet(event_queue->has_events(), set);
  xQueueAddToSet(gpios->IsReadPending(), set);

  tinyfsm::FsmList<system_fsm::SystemState, ui::UiState,
                   audio::AudioState>::start();

  while (1) {
    QueueSetMemberHandle_t member = xQueueSelectFromSet(set, portMAX_DELAY);
    if (member == event_queue->has_events()) {
      event_queue->Service(0);
    } else if (member == gpios->IsReadPending()) {
      xSemaphoreTake(member, 0);
      events::System().Dispatch(system_fsm::internal::GpioInterrupt{});
    }
  }
}