You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
279 lines
6.3 KiB
279 lines
6.3 KiB
2 years ago
|
#include "app_console.hpp"
|
||
|
|
||
|
#include <dirent.h>
|
||
2 years ago
|
|
||
2 years ago
|
#include <cstdint>
|
||
2 years ago
|
#include <cstdio>
|
||
2 years ago
|
#include <cstdlib>
|
||
2 years ago
|
#include <iostream>
|
||
|
#include <string>
|
||
2 years ago
|
|
||
2 years ago
|
#include "database.hpp"
|
||
2 years ago
|
#include "esp_console.h"
|
||
2 years ago
|
#include "esp_log.h"
|
||
2 years ago
|
|
||
|
namespace console {
|
||
|
|
||
2 years ago
|
static AppConsole* sInstance = nullptr;
|
||
|
|
||
|
std::string toSdPath(const std::string& filepath) {
|
||
2 years ago
|
return std::string("/") + filepath;
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
int CmdListDir(int argc, char** argv) {
|
||
|
static const std::string usage = "usage: ls [directory]";
|
||
|
if (argc > 2) {
|
||
|
std::cout << usage << std::endl;
|
||
|
return 1;
|
||
|
}
|
||
2 years ago
|
|
||
|
std::string path;
|
||
2 years ago
|
if (argc == 2) {
|
||
2 years ago
|
path = toSdPath(argv[1]);
|
||
|
} else {
|
||
|
path = toSdPath("");
|
||
2 years ago
|
}
|
||
|
|
||
|
DIR* dir;
|
||
|
struct dirent* ent;
|
||
|
dir = opendir(path.c_str());
|
||
|
while ((ent = readdir(dir))) {
|
||
|
std::cout << ent->d_name << std::endl;
|
||
|
}
|
||
|
closedir(dir);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
void RegisterListDir() {
|
||
|
esp_console_cmd_t cmd{.command = "ls",
|
||
|
.help = "Lists SD contents",
|
||
|
.hint = NULL,
|
||
|
.func = &CmdListDir,
|
||
|
.argtable = NULL};
|
||
|
esp_console_cmd_register(&cmd);
|
||
|
}
|
||
|
|
||
2 years ago
|
/*
|
||
2 years ago
|
int CmdPlayFile(int argc, char** argv) {
|
||
|
static const std::string usage = "usage: play [file]";
|
||
2 years ago
|
if (argc != 2) {
|
||
2 years ago
|
std::cout << usage << std::endl;
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
2 years ago
|
std::string path = "/";
|
||
|
sInstance->playback_->Play(path + argv[1]);
|
||
2 years ago
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
void RegisterPlayFile() {
|
||
|
esp_console_cmd_t cmd{.command = "play",
|
||
|
.help = "Begins playback of the file at the given path",
|
||
|
.hint = "filepath",
|
||
|
.func = &CmdPlayFile,
|
||
|
.argtable = NULL};
|
||
|
esp_console_cmd_register(&cmd);
|
||
|
}
|
||
|
|
||
|
int CmdToggle(int argc, char** argv) {
|
||
|
static const std::string usage = "usage: toggle";
|
||
|
if (argc != 1) {
|
||
|
std::cout << usage << std::endl;
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
2 years ago
|
// sInstance->playback_->Toggle();
|
||
2 years ago
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
void RegisterToggle() {
|
||
|
esp_console_cmd_t cmd{.command = "toggle",
|
||
|
.help = "Toggles between play and pause",
|
||
|
.hint = NULL,
|
||
|
.func = &CmdToggle,
|
||
|
.argtable = NULL};
|
||
|
esp_console_cmd_register(&cmd);
|
||
|
}
|
||
|
|
||
2 years ago
|
int CmdVolume(int argc, char** argv) {
|
||
|
static const std::string usage = "usage: volume [0-255]";
|
||
|
if (argc != 2) {
|
||
|
std::cout << usage << std::endl;
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
long int raw_vol = strtol(argv[1], NULL, 10);
|
||
|
if (raw_vol < 0 || raw_vol > 255) {
|
||
|
std::cout << usage << std::endl;
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
2 years ago
|
// sInstance->playback_->SetVolume((uint8_t)raw_vol);
|
||
2 years ago
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
void RegisterVolume() {
|
||
|
esp_console_cmd_t cmd{
|
||
|
.command = "vol",
|
||
|
.help = "Changes the volume (between 0 and 254. 255 is mute.)",
|
||
|
.hint = NULL,
|
||
|
.func = &CmdVolume,
|
||
|
.argtable = NULL};
|
||
|
esp_console_cmd_register(&cmd);
|
||
|
}
|
||
|
|
||
2 years ago
|
int CmdAudioStatus(int argc, char** argv) {
|
||
|
static const std::string usage = "usage: audio";
|
||
|
if (argc != 1) {
|
||
|
std::cout << usage << std::endl;
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
sInstance->playback_->LogStatus();
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
void RegisterAudioStatus() {
|
||
|
esp_console_cmd_t cmd{.command = "audio",
|
||
|
.help = "logs the current status of the audio pipeline",
|
||
|
.hint = NULL,
|
||
|
.func = &CmdAudioStatus,
|
||
|
.argtable = NULL};
|
||
|
esp_console_cmd_register(&cmd);
|
||
|
}
|
||
2 years ago
|
*/
|
||
2 years ago
|
|
||
2 years ago
|
int CmdDbInit(int argc, char** argv) {
|
||
|
static const std::string usage = "usage: db_init";
|
||
|
if (argc != 1) {
|
||
|
std::cout << usage << std::endl;
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
2 years ago
|
auto db = sInstance->database_.lock();
|
||
|
if (!db) {
|
||
|
std::cout << "no database open" << std::endl;
|
||
|
return 1;
|
||
|
}
|
||
|
db->Update();
|
||
2 years ago
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
void RegisterDbInit() {
|
||
2 years ago
|
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};
|
||
2 years ago
|
esp_console_cmd_register(&cmd);
|
||
|
}
|
||
|
|
||
2 years ago
|
int CmdDbSongs(int argc, char** argv) {
|
||
|
static const std::string usage = "usage: db_songs";
|
||
2 years ago
|
if (argc != 1) {
|
||
|
std::cout << usage << std::endl;
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
2 years ago
|
auto db = sInstance->database_.lock();
|
||
|
if (!db) {
|
||
|
std::cout << "no database open" << std::endl;
|
||
|
return 1;
|
||
|
}
|
||
|
std::unique_ptr<database::Result<database::Song>> res(db->GetSongs(5).get());
|
||
2 years ago
|
while (true) {
|
||
2 years ago
|
for (database::Song s : res->values()) {
|
||
2 years ago
|
std::cout << s.tags().title.value_or("[BLANK]") << std::endl;
|
||
2 years ago
|
}
|
||
2 years ago
|
if (res->next_page()) {
|
||
|
auto continuation = res->next_page().value();
|
||
2 years ago
|
res.reset(db->GetPage(&continuation).get());
|
||
2 years ago
|
} else {
|
||
|
break;
|
||
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
return 0;
|
||
|
}
|
||
|
|
||
2 years ago
|
void RegisterDbSongs() {
|
||
|
esp_console_cmd_t cmd{.command = "db_songs",
|
||
2 years ago
|
.help = "lists titles of ALL songs in the database",
|
||
|
.hint = NULL,
|
||
2 years ago
|
.func = &CmdDbSongs,
|
||
2 years ago
|
.argtable = NULL};
|
||
|
esp_console_cmd_register(&cmd);
|
||
|
}
|
||
|
|
||
2 years ago
|
int CmdDbDump(int argc, char** argv) {
|
||
|
static const std::string usage = "usage: db_dump";
|
||
|
if (argc != 1) {
|
||
|
std::cout << usage << std::endl;
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
2 years ago
|
auto db = sInstance->database_.lock();
|
||
|
if (!db) {
|
||
|
std::cout << "no database open" << std::endl;
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
2 years ago
|
std::cout << "=== BEGIN DUMP ===" << std::endl;
|
||
|
|
||
2 years ago
|
std::unique_ptr<database::Result<std::string>> res(db->GetDump(5).get());
|
||
2 years ago
|
while (true) {
|
||
2 years ago
|
for (std::string s : res->values()) {
|
||
2 years ago
|
std::cout << s << std::endl;
|
||
|
}
|
||
2 years ago
|
if (res->next_page()) {
|
||
|
auto continuation = res->next_page().value();
|
||
2 years ago
|
res.reset(db->GetPage<std::string>(&continuation).get());
|
||
2 years ago
|
} else {
|
||
|
break;
|
||
2 years ago
|
}
|
||
|
}
|
||
|
|
||
|
std::cout << "=== END DUMP ===" << std::endl;
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
void RegisterDbDump() {
|
||
|
esp_console_cmd_t cmd{.command = "db_dump",
|
||
|
.help = "prints every key/value pair in the db",
|
||
|
.hint = NULL,
|
||
|
.func = &CmdDbDump,
|
||
|
.argtable = NULL};
|
||
|
esp_console_cmd_register(&cmd);
|
||
|
}
|
||
|
|
||
2 years ago
|
AppConsole::AppConsole(std::weak_ptr<database::Database> database)
|
||
|
: database_(database) {
|
||
2 years ago
|
sInstance = this;
|
||
|
}
|
||
|
AppConsole::~AppConsole() {
|
||
|
sInstance = nullptr;
|
||
|
}
|
||
|
|
||
|
auto AppConsole::RegisterExtraComponents() -> void {
|
||
|
RegisterListDir();
|
||
2 years ago
|
/*
|
||
2 years ago
|
RegisterPlayFile();
|
||
|
RegisterToggle();
|
||
2 years ago
|
RegisterVolume();
|
||
2 years ago
|
RegisterAudioStatus();
|
||
2 years ago
|
*/
|
||
2 years ago
|
RegisterDbInit();
|
||
2 years ago
|
RegisterDbSongs();
|
||
2 years ago
|
RegisterDbDump();
|
||
2 years ago
|
}
|
||
|
|
||
|
} // namespace console
|