Move DB interactions to a background thread
This commit is contained in:
+21
-20
@@ -154,48 +154,49 @@ int CmdDbInit(int argc, char** argv) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
sInstance->database_->Initialise();
|
||||
sInstance->database_->Populate().get();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void RegisterDbInit() {
|
||||
esp_console_cmd_t cmd{.command = "db_init",
|
||||
.help = "scans for playable files and adds them to the database",
|
||||
.hint = NULL,
|
||||
.func = &CmdDbInit,
|
||||
.argtable = NULL};
|
||||
esp_console_cmd_t cmd{
|
||||
.command = "db_init",
|
||||
.help = "scans for playable files and adds them to the database",
|
||||
.hint = NULL,
|
||||
.func = &CmdDbInit,
|
||||
.argtable = NULL};
|
||||
esp_console_cmd_register(&cmd);
|
||||
}
|
||||
|
||||
int CmdDbTitles(int argc, char** argv) {
|
||||
static const std::string usage = "usage: db_titles";
|
||||
int CmdDbSongs(int argc, char** argv) {
|
||||
static const std::string usage = "usage: db_songs";
|
||||
if (argc != 1) {
|
||||
std::cout << usage << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
database::Iterator it = sInstance->database_->ByTitle();
|
||||
while (true) {
|
||||
std::optional<std::string> title = it.Next();
|
||||
if (!title) {
|
||||
break;
|
||||
}
|
||||
std::cout << *title << std::endl;
|
||||
database::DbResult<database::Song> res =
|
||||
sInstance->database_->GetSongs(10).get();
|
||||
for (database::Song s : res.values()) {
|
||||
std::cout << s.title << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void RegisterDbTitles() {
|
||||
esp_console_cmd_t cmd{.command = "db_titles",
|
||||
void RegisterDbSongs() {
|
||||
esp_console_cmd_t cmd{.command = "db_songs",
|
||||
.help = "lists titles of ALL songs in the database",
|
||||
.hint = NULL,
|
||||
.func = &CmdDbTitles,
|
||||
.func = &CmdDbSongs,
|
||||
.argtable = NULL};
|
||||
esp_console_cmd_register(&cmd);
|
||||
}
|
||||
|
||||
AppConsole::AppConsole(audio::AudioPlayback* playback, database::Database *database) : playback_(playback), database_(database) {
|
||||
AppConsole::AppConsole(audio::AudioPlayback* playback,
|
||||
database::Database* database)
|
||||
: playback_(playback), database_(database) {
|
||||
sInstance = this;
|
||||
}
|
||||
AppConsole::~AppConsole() {
|
||||
@@ -209,7 +210,7 @@ auto AppConsole::RegisterExtraComponents() -> void {
|
||||
RegisterVolume();
|
||||
RegisterAudioStatus();
|
||||
RegisterDbInit();
|
||||
RegisterDbTitles();
|
||||
RegisterDbSongs();
|
||||
}
|
||||
|
||||
} // namespace console
|
||||
|
||||
@@ -11,11 +11,12 @@ namespace console {
|
||||
|
||||
class AppConsole : public Console {
|
||||
public:
|
||||
explicit AppConsole(audio::AudioPlayback* playback, database::Database *database);
|
||||
explicit AppConsole(audio::AudioPlayback* playback,
|
||||
database::Database* database);
|
||||
virtual ~AppConsole();
|
||||
|
||||
audio::AudioPlayback* playback_;
|
||||
database::Database *database_;
|
||||
database::Database* database_;
|
||||
|
||||
protected:
|
||||
virtual auto RegisterExtraComponents() -> void;
|
||||
|
||||
+8
-33
@@ -37,29 +37,6 @@
|
||||
|
||||
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");
|
||||
|
||||
@@ -85,15 +62,6 @@ extern "C" void app_main(void) {
|
||||
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();
|
||||
@@ -108,11 +76,18 @@ extern "C" void app_main(void) {
|
||||
playback = std::make_unique<audio::AudioPlayback>(drivers.get());
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Init database");
|
||||
std::unique_ptr<database::Database> db;
|
||||
auto db_res = database::Database::Open();
|
||||
if (db_res.has_value()) {
|
||||
db.reset(db_res.value());
|
||||
}
|
||||
|
||||
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::AppConsole console(playback.get(), db.get());
|
||||
console.Launch();
|
||||
|
||||
uint8_t prev_position = 0;
|
||||
|
||||
Reference in New Issue
Block a user