summaryrefslogtreecommitdiff
path: root/src/main/main.cpp
blob: 6ef7c61b23b15473ec394f45f6891a678d605ed3 (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
#include <dirent.h>
#include <stdint.h>
#include <stdio.h>

#include <cstddef>
#include <cstdint>
#include <memory>

#include "driver/gpio.h"
#include "driver/i2c.h"
#include "driver/sdspi_host.h"
#include "driver/spi_common.h"
#include "driver/spi_master.h"
#include "driver_cache.hpp"
#include "esp_freertos_hooks.h"
#include "esp_heap_caps.h"
#include "esp_intr_alloc.h"
#include "esp_log.h"
#include "freertos/portmacro.h"
#include "freertos/projdefs.h"
#include "hal/gpio_types.h"
#include "hal/spi_types.h"

#include "app_console.hpp"
#include "audio_playback.hpp"
#include "battery.hpp"
#include "dac.hpp"
#include "database.hpp"
#include "display.hpp"
#include "display_init.hpp"
#include "gpio_expander.hpp"
#include "i2c.hpp"
#include "lvgl_task.hpp"
#include "spi.hpp"
#include "storage.hpp"
#include "touchwheel.hpp"

static const char* TAG = "MAIN";

void db_main(void* whatever) {
  database::Database **arg_db = reinterpret_cast<database::Database**>(whatever);
  ESP_LOGI(TAG, "Init database");
  std::unique_ptr<database::Database> db;
  auto db_res = database::Database::Open();
  if (db_res.has_error()) {
    ESP_LOGE(TAG, "database failed :(");
  } else {
    db.reset(db_res.value());
    ESP_LOGI(TAG, "database good :)");
  }

  *arg_db = db.get();

  db->ByTitle();

  while (1) {
    vTaskDelay(portMAX_DELAY);
  }

  vTaskDelete(NULL);
}

extern "C" void app_main(void) {
  ESP_LOGI(TAG, "Initialising peripherals");

  ESP_ERROR_CHECK(drivers::init_i2c());
  ESP_ERROR_CHECK(drivers::init_spi());
  std::unique_ptr<drivers::DriverCache> drivers =
      std::make_unique<drivers::DriverCache>();

  ESP_LOGI(TAG, "Init GPIOs");
  drivers::GpioExpander* expander = drivers->AcquireGpios();

  ESP_LOGI(TAG, "Enable power rails for development");
  expander->with(
      [&](auto& gpio) { gpio.set_pin(drivers::GpioExpander::AMP_EN, 1); });

  ESP_LOGI(TAG, "Init battery measurement");
  drivers::Battery* battery = new drivers::Battery();
  ESP_LOGI(TAG, "it's reading %d mV!", (int)battery->Millivolts());

  ESP_LOGI(TAG, "Init SD card");
  auto storage = drivers->AcquireStorage();
  if (!storage) {
    ESP_LOGE(TAG, "Failed! Do you have an SD card?");
  }

  ESP_LOGI(TAG, "Launch database task");
  std::size_t db_stack_size = 256 * 1024;
  StaticTask_t database_task_buffer = {};
  StackType_t* database_stack = reinterpret_cast<StackType_t*>(
      heap_caps_malloc(db_stack_size, MALLOC_CAP_SPIRAM));
  database::Database *db;
  xTaskCreateStatic(&db_main, "LEVELDB", db_stack_size, &db, 1, database_stack,
                    &database_task_buffer);

  ESP_LOGI(TAG, "Init touch wheel");
  std::shared_ptr<drivers::TouchWheel> touchwheel =
      drivers->AcquireTouchWheel();

  std::atomic<bool> lvgl_quit;
  TaskHandle_t lvgl_task_handle;
  ui::StartLvgl(drivers.get(), &lvgl_quit, &lvgl_task_handle);

  std::unique_ptr<audio::AudioPlayback> playback;
  if (storage) {
    ESP_LOGI(TAG, "Init audio pipeline");
    playback = std::make_unique<audio::AudioPlayback>(drivers.get());
  }

  ESP_LOGI(TAG, "Waiting for background tasks before launching console...");
  vTaskDelay(pdMS_TO_TICKS(1000));

  ESP_LOGI(TAG, "Launch console");
  console::AppConsole console(playback.get(), db);
  console.Launch();

  uint8_t prev_position = 0;
  while (1) {
    touchwheel->Update();
    auto wheel_data = touchwheel->GetTouchWheelData();
    if (wheel_data.wheel_position != prev_position) {
      prev_position = wheel_data.wheel_position;
      ESP_LOGI(TAG, "Touch wheel pos: %u", prev_position);
    }
    vTaskDelay(pdMS_TO_TICKS(100));
  }
}