Add Slice 5 ADAU1701 runtime audio control (firmware 0.5.0).

Safeload mixer/EQ/master on the ADAU1701, persist AudioProfile in NVS,
expose /api/audio routes and web UI controls, with host tests and manual sync.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-07-06 16:47:58 +02:00
co-authored by Cursor
parent 4b931b0aad
commit ab3651e678
58 changed files with 3191 additions and 41 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ idf_component_register(
"src/NetBootstrap.cpp"
INCLUDE_DIRS "include"
EMBED_FILES "www/index.html.gz"
REQUIRES core esp_wifi esp_netif esp_event nvs_flash esp_http_server tuner
REQUIRES core esp_wifi esp_netif esp_event nvs_flash esp_http_server tuner audio
)
target_compile_features(${COMPONENT_LIB} PUBLIC cxx_std_23)
@@ -27,6 +27,10 @@
#include <expected>
#include <optional>
namespace audio {
class AudioService;
} // namespace audio
namespace tuner {
class TunerService;
} // namespace tuner
@@ -52,6 +56,7 @@ public:
* @dname start
* @param store Secure store consulted for saved STA credentials.
* @param tuner Tuner service exposed by the HTTP API.
* @param audio Audio service exposed by the HTTP API.
* @return NetBootstrap on success, or a NetError.
* @pubstate none
*
@@ -59,7 +64,8 @@ public:
* @date 2026-07-06
*/
[[nodiscard]] static std::expected<NetBootstrap, NetError>
start(core::ISecureStore& store, tuner::TunerService& tuner);
start(core::ISecureStore& store, tuner::TunerService& tuner,
audio::AudioService& audio);
NetBootstrap(const NetBootstrap&) = delete;
NetBootstrap& operator=(const NetBootstrap&) = delete;
@@ -25,6 +25,10 @@
struct httpd_req;
namespace audio {
class AudioService;
} // namespace audio
namespace tuner {
class TunerService;
} // namespace tuner
@@ -45,8 +49,9 @@ namespace net {
* @date 2026-07-06
*/
struct HttpRouteContext {
core::ISecureStore* store; ///< Secure store for Wi-Fi provisioning.
tuner::TunerService* tuner; ///< Tuner service for tuner REST routes.
core::ISecureStore* store; ///< Secure store for Wi-Fi provisioning.
tuner::TunerService* tuner; ///< Tuner service for tuner REST routes.
audio::AudioService* audio; ///< Audio service for ADAU1701 REST routes.
};
/**
@@ -119,21 +124,24 @@ public:
* @param store Secure store for POST /api/wifi persistence.
* @param netState Active network phase exposed to handlers.
* @param tuner Tuner service for the tuner REST routes.
* @param audio Audio service for the audio REST routes.
* @return Ok on success, or NetError::HttpServerStartFailed.
* @pubstate writes server_, store_, netState_, and tuner_ on success.
* @pubstate writes server_, store_, netState_, tuner_, and audio_ on success.
*
* @author Michele Bigi
* @date 2026-07-06
*/
[[nodiscard]] std::expected<void, NetError> start(core::ISecureStore& store,
NetState netState,
tuner::TunerService& tuner);
tuner::TunerService& tuner,
audio::AudioService& audio);
private:
httpd_handle* server_;
core::ISecureStore* store_;
NetState netState_;
tuner::TunerService* tuner_;
audio::AudioService* audio_;
HttpRouteContext routeContext_;
};
+13 -7
View File
@@ -23,6 +23,7 @@
#include "esp_netif.h"
#include "esp_wifi.h"
#include "nvs_flash.h"
#include "audio/AudioService.hpp"
#include "tuner/TunerService.hpp"
namespace net {
@@ -98,7 +99,8 @@ constexpr char kTag[] = "NetBootstrap";
* @date 2026-07-06
*/
[[nodiscard]] std::expected<NetBootstrap, NetError>
startSetupMode(core::ISecureStore& store, tuner::TunerService& tuner)
startSetupMode(core::ISecureStore& store, tuner::TunerService& tuner,
audio::AudioService& audio)
{
esp_netif_create_default_wifi_ap();
@@ -108,7 +110,8 @@ startSetupMode(core::ISecureStore& store, tuner::TunerService& tuner)
}
SetupWebServer webServer;
if (auto webResult = webServer.start(store, NetState::SoftApSetup, tuner);
if (auto webResult =
webServer.start(store, NetState::SoftApSetup, tuner, audio);
!webResult) {
return std::unexpected(webResult.error());
}
@@ -130,7 +133,8 @@ startSetupMode(core::ISecureStore& store, tuner::TunerService& tuner)
* @date 2026-07-06
*/
[[nodiscard]] std::expected<NetBootstrap, NetError>
startStaMode(core::ISecureStore& store, tuner::TunerService& tuner)
startStaMode(core::ISecureStore& store, tuner::TunerService& tuner,
audio::AudioService& audio)
{
auto credsResult = store.loadWifiCredentials();
if (!credsResult) {
@@ -146,7 +150,8 @@ startStaMode(core::ISecureStore& store, tuner::TunerService& tuner)
}
SetupWebServer webServer;
if (auto webResult = webServer.start(store, NetState::StaConnected, tuner);
if (auto webResult =
webServer.start(store, NetState::StaConnected, tuner, audio);
!webResult) {
return std::unexpected(webResult.error());
}
@@ -159,7 +164,8 @@ startStaMode(core::ISecureStore& store, tuner::TunerService& tuner)
} // namespace
std::expected<NetBootstrap, NetError>
NetBootstrap::start(core::ISecureStore& store, tuner::TunerService& tuner)
NetBootstrap::start(core::ISecureStore& store, tuner::TunerService& tuner,
audio::AudioService& audio)
{
if (auto platform = initPlatform(); !platform) {
return std::unexpected(platform.error());
@@ -170,14 +176,14 @@ NetBootstrap::start(core::ISecureStore& store, tuner::TunerService& tuner)
}
if (store.hasWifiCredentials()) {
auto staResult = startStaMode(store, tuner);
auto staResult = startStaMode(store, tuner, audio);
if (staResult) {
return staResult;
}
ESP_LOGW(kTag, "STA join failed — falling back to setup SoftAP");
}
return startSetupMode(store, tuner);
return startSetupMode(store, tuner, audio);
}
NetBootstrap::NetBootstrap(std::optional<SoftApHost> softAp,
+145 -7
View File
@@ -18,6 +18,8 @@
#include "net/SetupWebServer.hpp"
#include "core/AudioProfile.hpp"
#include "core/AudioProfileJson.hpp"
#include "core/FirmwareVersion.hpp"
#include "core/HealthStatus.hpp"
#include "core/HealthStatusJson.hpp"
@@ -26,6 +28,7 @@
#include "core/TunerJson.hpp"
#include "core/WifiProvisionJson.hpp"
#include "tuner/TunerService.hpp"
#include "audio/AudioService.hpp"
#include "esp_http_server.h"
#include "esp_log.h"
@@ -40,7 +43,7 @@ namespace net {
namespace {
constexpr char kTag[] = "SetupWebServer";
constexpr char kFirmwareVersion[] = "0.4.0";
constexpr char kFirmwareVersion[] = "0.5.0";
constexpr unsigned kRebootDelaySec = 3;
extern const uint8_t www_index_html_gz_start[] asm(
@@ -363,6 +366,109 @@ esp_err_t tunerSeekPostHandler(httpd_req_t* req)
return httpd_resp_send(req, json.c_str(), json.size());
}
/**
* @brief audioProfileGetHandler — serve GET /api/audio/profile JSON.
*
* @dname audioProfileGetHandler
* @param req HTTP request handle from esp_http_server.
* @return ESP_OK on success, or an esp_err_t error code.
* @pubstate reads route context audio service snapshot.
*
* @author Michele Bigi
* @date 2026-07-06
*/
esp_err_t audioProfileGetHandler(httpd_req_t* req)
{
auto* ctx = routeContextFrom(req);
if (ctx == nullptr || ctx->audio == nullptr) {
httpd_resp_set_status(req, "503 Service Unavailable");
return httpd_resp_send(req, nullptr, 0);
}
const std::string json =
core::serializeAudioProfileJson(ctx->audio->currentProfile());
httpd_resp_set_type(req, "application/json");
return httpd_resp_send(req, json.c_str(), json.size());
}
/**
* @brief audioProfilePutHandler — accept PUT /api/audio/profile JSON.
*
* @dname audioProfilePutHandler
* @param req HTTP request handle from esp_http_server.
* @return ESP_OK on success, or an esp_err_t error code.
* @pubstate parses profile, safeloads ADAU1701, persists to NVS.
*
* @author Michele Bigi
* @date 2026-07-06
*/
esp_err_t audioProfilePutHandler(httpd_req_t* req)
{
auto* ctx = routeContextFrom(req);
if (ctx == nullptr || ctx->audio == nullptr) {
httpd_resp_set_status(req, "503 Service Unavailable");
return httpd_resp_send(req, nullptr, 0);
}
std::array<char, 2048> body{};
if (!readRequestBody(req, body)) {
httpd_resp_set_status(req, "400 Bad Request");
return httpd_resp_send(req, nullptr, 0);
}
const auto parsed =
core::parseAudioProfileJson(std::string_view(body.data()));
if (!parsed) {
const std::string json =
core::serializeAudioErrorJson(parseErrorToken(parsed.error()));
httpd_resp_set_status(req, "400 Bad Request");
httpd_resp_set_type(req, "application/json");
return httpd_resp_send(req, json.c_str(), json.size());
}
if (auto applied = ctx->audio->applyProfile(*parsed, true); !applied) {
const std::string json = core::serializeAudioErrorJson("store_failed");
httpd_resp_set_status(req, "500 Internal Server Error");
httpd_resp_set_type(req, "application/json");
return httpd_resp_send(req, json.c_str(), json.size());
}
const std::string json = core::serializeAudioSavedJson();
httpd_resp_set_type(req, "application/json");
return httpd_resp_send(req, json.c_str(), json.size());
}
/**
* @brief audioResetPostHandler — restore factory-flat profile.
*
* @dname audioResetPostHandler
* @param req HTTP request handle from esp_http_server.
* @return ESP_OK on success, or an esp_err_t error code.
* @pubstate applies AudioProfile::factoryDefault() and persists to NVS.
*
* @author Michele Bigi
* @date 2026-07-06
*/
esp_err_t audioResetPostHandler(httpd_req_t* req)
{
auto* ctx = routeContextFrom(req);
if (ctx == nullptr || ctx->audio == nullptr) {
httpd_resp_set_status(req, "503 Service Unavailable");
return httpd_resp_send(req, nullptr, 0);
}
const core::AudioProfile defaults = core::AudioProfile::factoryDefault();
if (auto applied = ctx->audio->applyProfile(defaults, true); !applied) {
const std::string json = core::serializeAudioErrorJson("store_failed");
httpd_resp_set_status(req, "500 Internal Server Error");
httpd_resp_set_type(req, "application/json");
return httpd_resp_send(req, json.c_str(), json.size());
}
const std::string json = core::serializeAudioSavedJson();
httpd_resp_set_type(req, "application/json");
return httpd_resp_send(req, json.c_str(), json.size());
}
/**
* @brief indexGetHandler — serve gzipped setup page from flash.
*
@@ -450,7 +556,8 @@ SetupWebServer::SetupWebServer()
, store_(nullptr)
, netState_(NetState::Uninitialized)
, tuner_(nullptr)
, routeContext_{nullptr, nullptr}
, audio_(nullptr)
, routeContext_{nullptr, nullptr, nullptr}
{
}
@@ -459,13 +566,15 @@ SetupWebServer::SetupWebServer(SetupWebServer&& other) noexcept
, store_(other.store_)
, netState_(other.netState_)
, tuner_(other.tuner_)
, audio_(other.audio_)
, routeContext_(other.routeContext_)
{
other.server_ = nullptr;
other.store_ = nullptr;
other.netState_ = NetState::Uninitialized;
other.tuner_ = nullptr;
other.routeContext_ = {nullptr, nullptr};
other.audio_ = nullptr;
other.routeContext_ = {nullptr, nullptr, nullptr};
}
SetupWebServer& SetupWebServer::operator=(SetupWebServer&& other) noexcept
@@ -478,12 +587,14 @@ SetupWebServer& SetupWebServer::operator=(SetupWebServer&& other) noexcept
store_ = other.store_;
netState_ = other.netState_;
tuner_ = other.tuner_;
audio_ = other.audio_;
routeContext_ = other.routeContext_;
other.server_ = nullptr;
other.store_ = nullptr;
other.netState_ = NetState::Uninitialized;
other.tuner_ = nullptr;
other.routeContext_ = {nullptr, nullptr};
other.audio_ = nullptr;
other.routeContext_ = {nullptr, nullptr, nullptr};
}
return *this;
}
@@ -494,13 +605,14 @@ SetupWebServer::~SetupWebServer()
httpd_stop(server_);
server_ = nullptr;
}
routeContext_ = {nullptr, nullptr};
routeContext_ = {nullptr, nullptr, nullptr};
}
std::expected<void, NetError> SetupWebServer::start(
core::ISecureStore& store,
NetState netState,
tuner::TunerService& tuner)
tuner::TunerService& tuner,
audio::AudioService& audio)
{
if (server_ != nullptr) {
return {};
@@ -509,8 +621,10 @@ std::expected<void, NetError> SetupWebServer::start(
store_ = &store;
netState_ = netState;
tuner_ = &tuner;
audio_ = &audio;
routeContext_.store = &store;
routeContext_.tuner = &tuner;
routeContext_.audio = &audio;
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
config.server_port = 80;
@@ -518,7 +632,7 @@ std::expected<void, NetError> SetupWebServer::start(
if (httpd_start(&server_, &config) != ESP_OK) {
ESP_LOGE(kTag, "httpd_start failed");
routeContext_ = {nullptr, nullptr};
routeContext_ = {nullptr, nullptr, nullptr};
return std::unexpected(NetError::HttpServerStartFailed);
}
@@ -588,6 +702,30 @@ std::expected<void, NetError> SetupWebServer::start(
};
httpd_register_uri_handler(server_, &tunerSeekUri);
const httpd_uri_t audioProfileGetUri = {
.uri = "/api/audio/profile",
.method = HTTP_GET,
.handler = audioProfileGetHandler,
.user_ctx = routeCtx,
};
httpd_register_uri_handler(server_, &audioProfileGetUri);
const httpd_uri_t audioProfilePutUri = {
.uri = "/api/audio/profile",
.method = HTTP_PUT,
.handler = audioProfilePutHandler,
.user_ctx = routeCtx,
};
httpd_register_uri_handler(server_, &audioProfilePutUri);
const httpd_uri_t audioResetUri = {
.uri = "/api/audio/reset",
.method = HTTP_POST,
.handler = audioResetPostHandler,
.user_ctx = routeCtx,
};
httpd_register_uri_handler(server_, &audioResetUri);
ESP_LOGI(kTag, "HTTP server listening on port 80");
return {};
}
+81
View File
@@ -61,6 +61,7 @@
color: var(--text);
font: inherit;
}
input[type="range"] { padding: 0; }
button {
width: 100%;
padding: var(--space-2);
@@ -196,6 +197,22 @@
<p class="msg" id="tuner-msg" aria-live="polite"></p>
</section>
<section id="audio-section">
<h2>Audio</h2>
<p>ADAU1701 mixer and master volume (safeload at runtime).</p>
<label for="master-db">Master (dB)</label>
<input id="master-db" type="range" min="-60" max="12" step="0.5" value="0">
<label for="si4684-db">Radio path Si4684 (dB)</label>
<input id="si4684-db" type="range" min="-60" max="12" step="0.5" value="0">
<label for="esp32-db">ESP32 path (dB)</label>
<input id="esp32-db" type="range" min="-60" max="12" step="0.5" value="0">
<div class="row">
<button type="button" id="save-audio">Save profile</button>
<button type="button" class="secondary" id="reset-audio">Reset flat</button>
</div>
<p class="msg" id="audio-msg" aria-live="polite"></p>
</section>
</main>
<script>
function showMsg(el, text, ok) {
@@ -397,6 +414,70 @@
});
refreshTunerStatus();
var audioProfile = null;
function loadAudioProfile() {
var msg = document.getElementById("audio-msg");
return fetch("/api/audio/profile")
.then(function (r) { return r.json(); })
.then(function (d) {
audioProfile = d;
document.getElementById("master-db").value = d.master.left_db;
document.getElementById("si4684-db").value = d.mixer.si4684_left_db;
document.getElementById("esp32-db").value = d.mixer.esp32_left_db;
showMsg(msg, "", true);
})
.catch(function () { showMsg(msg, "Audio profile load failed.", false); });
}
document.getElementById("save-audio").addEventListener("click", function () {
var msg = document.getElementById("audio-msg");
if (!audioProfile) {
showMsg(msg, "Profile not loaded.", false);
return;
}
var master = parseFloat(document.getElementById("master-db").value, 10);
var si4684 = parseFloat(document.getElementById("si4684-db").value, 10);
var esp32 = parseFloat(document.getElementById("esp32-db").value, 10);
audioProfile.master.left_db = master;
audioProfile.master.right_db = master;
audioProfile.mixer.si4684_left_db = si4684;
audioProfile.mixer.si4684_right_db = si4684;
audioProfile.mixer.esp32_left_db = esp32;
audioProfile.mixer.esp32_right_db = esp32;
fetch("/api/audio/profile", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(audioProfile)
})
.then(function (r) { return r.json().then(function (d) { return { ok: r.ok, d: d }; }); })
.then(function (res) {
if (res.ok && res.d.status === "saved") {
showMsg(msg, "Audio profile saved.", true);
} else {
showMsg(msg, "Save failed: " + (res.d.reason || "unknown"), false);
}
})
.catch(function () { showMsg(msg, "Save request failed.", false); });
});
document.getElementById("reset-audio").addEventListener("click", function () {
var msg = document.getElementById("audio-msg");
fetch("/api/audio/reset", { method: "POST" })
.then(function (r) { return r.json().then(function (d) { return { ok: r.ok, d: d }; }); })
.then(function (res) {
if (res.ok && res.d.status === "saved") {
showMsg(msg, "Factory-flat profile applied.", true);
loadAudioProfile();
} else {
showMsg(msg, "Reset failed: " + (res.d.reason || "unknown"), false);
}
})
.catch(function () { showMsg(msg, "Reset request failed.", false); });
});
loadAudioProfile();
</script>
</body>
</html>
Binary file not shown.