Fix playback and console issues

This commit is contained in:
jacqueline
2022-11-16 11:13:21 +11:00
parent e802b8583f
commit 8c51280bc6
9 changed files with 129 additions and 59 deletions
+46 -10
View File
@@ -1,7 +1,9 @@
#include "app_console.hpp"
#include <dirent.h>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include "esp_console.h"
@@ -10,16 +12,22 @@ namespace console {
static AppConsole* sInstance = nullptr;
std::string toSdPath(std::string filepath) {
return std::string(drivers::kStoragePath) + "/" + filepath;
}
int CmdListDir(int argc, char** argv) {
static const std::string usage = "usage: ls [directory]";
if (argc > 2) {
std::cout << usage << std::endl;
return 1;
}
std::string path = drivers::kStoragePath;
std::string path;
if (argc == 2) {
path += "/";
path += argv[1];
path = toSdPath(argv[1]);
} else {
path = toSdPath("");
}
DIR* dir;
@@ -44,15 +52,15 @@ void RegisterListDir() {
int CmdPlayFile(int argc, char** argv) {
static const std::string usage = "usage: play [file]";
if (argc != 2) {
if (argc < 2 || argc > 3) {
std::cout << usage << std::endl;
return 1;
}
std::string path = drivers::kStoragePath;
path += "/";
path += argv[1];
sInstance->playback_->Play(path.c_str());
sInstance->playback_->Play(toSdPath(argv[1]));
if (argc == 3) {
sInstance->playback_->SetNextFile(toSdPath(argv[2]));
}
return 0;
}
@@ -87,8 +95,35 @@ void RegisterToggle() {
esp_console_cmd_register(&cmd);
}
AppConsole::AppConsole(std::unique_ptr<drivers::AudioPlayback> playback)
: playback_(std::move(playback)) {
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;
}
sInstance->playback_->SetVolume((uint8_t)raw_vol);
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);
}
AppConsole::AppConsole(drivers::AudioPlayback* playback) : playback_(playback) {
sInstance = this;
}
AppConsole::~AppConsole() {
@@ -99,6 +134,7 @@ auto AppConsole::RegisterExtraComponents() -> void {
RegisterListDir();
RegisterPlayFile();
RegisterToggle();
RegisterVolume();
}
} // namespace console
+2 -2
View File
@@ -9,10 +9,10 @@ namespace console {
class AppConsole : public Console {
public:
AppConsole(std::unique_ptr<drivers::AudioPlayback> playback);
AppConsole(drivers::AudioPlayback* playback);
virtual ~AppConsole();
std::unique_ptr<drivers::AudioPlayback> playback_;
drivers::AudioPlayback* playback_;
protected:
virtual auto RegisterExtraComponents() -> void;
+2 -1
View File
@@ -130,9 +130,10 @@ extern "C" void app_main(void) {
}
std::unique_ptr<drivers::AudioPlayback> playback =
std::move(playback_res.value());
playback->SetVolume(130);
ESP_LOGI(TAG, "Launch console");
console::AppConsole console(std::move(playback));
console::AppConsole console(playback.get());
console.Launch();
while (1) {