Convert Arduino sketch to PlatformIO project

Restructure BikeAudio for PlatformIO while preserving the original
build specs from the .ino header:
  - platform espressif32 @ ~6.6.0  -> ESP32 Arduino core 2.0.17
  - board esp32dev (ESP32 DevKitC v4)
  - board_build.partitions = huge_app.csv (Huge APP, required for BT stack)
  - monitor_speed = 115200

Changes:
  - BikeAudio.ino -> src/main.cpp (+ #include <Arduino.h>, print_status()
    forward declaration; PlatformIO compiles .cpp directly and does not
    auto-generate prototypes like the Arduino IDE).
  - Add platformio.ini with pschatzmann ESP32-A2DP + arduino-audio-tools
    pinned to exact commits (ESP32-A2DP 1.8.11, audio-tools 1.2.4) for
    reproducible builds.
  - Adapt three spots to the current library API (behavior preserved):
      * RingBuffer<uint8_t>::read() -> bool read(T&)
      * connection-state callbacks: drop esp_bd_addr_t arg
        -> (esp_a2d_connection_state_t, void*)
      * BluetoothA2DPSource::start(name, bool) -> start(name)
        (auto-reconnect already configured via set_auto_reconnect(true))

Verified: pio run -e esp32dev succeeds.
RAM 14.5% (47.6 KB), Flash 42.0% (1.32 MB / 3 MB).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
blue — ESP32/PlatformIO firmware 2026-06-10 09:44:09 -04:00
parent 276489cb17
commit 04e7f20430
2 changed files with 47 additions and 11 deletions

27
platformio.ini Normal file
View File

@ -0,0 +1,27 @@
; PlatformIO project configuration for BikeAudio
; Converted from Arduino IDE sketch (BikeAudio.ino).
;
; Board: ESP32 Dev Module (DevKitC v4) -> esp32dev
; ESP32 Arduino core: 2.0.17 (provided by platform espressif32 @ ~6.6.0)
; Do NOT move to core 3.x — BT stack regression.
; Partition scheme: Huge APP (3MB No OTA / 1MB SPIFFS) -> huge_app.csv
; Serial monitor: 115200 baud
[env:esp32dev]
platform = espressif32 @ ~6.6.0
board = esp32dev
framework = arduino
; Huge APP partition table — required for the Bluetooth stack size.
board_build.partitions = huge_app.csv
monitor_speed = 115200
; Phil Schatzmann's libraries (referenced by git URL, as in the .ino header —
; not published in the PlatformIO registry under these names).
; ESP32-A2DP depends on arduino-audio-tools. Pinned to exact commits for
; reproducible builds (ESP32-A2DP 1.8.11, audio-tools 1.2.4); src/main.cpp
; was adapted to match this API surface.
lib_deps =
https://github.com/pschatzmann/ESP32-A2DP#42601717cd70d5300c9b519f3c2bf1d64d77ea2b
https://github.com/pschatzmann/arduino-audio-tools#64b64dcb9bde18a0a17766eeb6529c3a53d920a8

View File

@ -27,11 +27,18 @@
* - Done play audio, both speakers output simultaneously * - Done play audio, both speakers output simultaneously
*/ */
#include <Arduino.h>
#include "AudioTools.h" #include "AudioTools.h"
#include "BluetoothA2DPSink.h" #include "BluetoothA2DPSink.h"
#include "BluetoothA2DPSource.h" #include "BluetoothA2DPSource.h"
#include "BluetoothA2DPCommon.h" #include "BluetoothA2DPCommon.h"
// Forward declaration — defined below. Required in .cpp builds: the Arduino IDE
// auto-generates prototypes for .ino files, but PlatformIO compiles .cpp directly
// and print_status() is called in setup()/loop() before its definition.
void print_status();
// ─── CONFIGURATION ──────────────────────────────────────────────────────────── // ─── CONFIGURATION ────────────────────────────────────────────────────────────
// Name this device shows to iPhone // Name this device shows to iPhone
@ -104,8 +111,9 @@ void audio_received_mirror_cb(const uint8_t *data, uint32_t len) {
int32_t get_audio_for_jbl(uint8_t *data, int32_t len) { int32_t get_audio_for_jbl(uint8_t *data, int32_t len) {
int32_t bytes_read = 0; int32_t bytes_read = 0;
while (bytes_read < len && !ring_buf.isEmpty()) { uint8_t b;
data[bytes_read++] = ring_buf.read(); while (bytes_read < len && ring_buf.read(b)) {
data[bytes_read++] = b;
} }
// Pad with silence if buffer underrun // Pad with silence if buffer underrun
if (bytes_read < len) { if (bytes_read < len) {
@ -116,8 +124,9 @@ int32_t get_audio_for_jbl(uint8_t *data, int32_t len) {
int32_t get_audio_for_cardo(uint8_t *data, int32_t len) { int32_t get_audio_for_cardo(uint8_t *data, int32_t len) {
int32_t bytes_read = 0; int32_t bytes_read = 0;
while (bytes_read < len && !ring_buf_cardo.isEmpty()) { uint8_t b;
data[bytes_read++] = ring_buf_cardo.read(); while (bytes_read < len && ring_buf_cardo.read(b)) {
data[bytes_read++] = b;
} }
if (bytes_read < len) { if (bytes_read < len) {
memset(data + bytes_read, 0, len - bytes_read); memset(data + bytes_read, 0, len - bytes_read);
@ -127,7 +136,7 @@ int32_t get_audio_for_cardo(uint8_t *data, int32_t len) {
// ─── CONNECTION CALLBACKS ───────────────────────────────────────────────────── // ─── CONNECTION CALLBACKS ─────────────────────────────────────────────────────
void sink_connected_cb(esp_bd_addr_t addr, esp_a2d_connection_state_t state, void *obj) { void sink_connected_cb(esp_a2d_connection_state_t state, void *obj) {
if (state == ESP_A2D_CONNECTION_STATE_CONNECTED) { if (state == ESP_A2D_CONNECTION_STATE_CONNECTED) {
Serial.println("[SINK] iPhone connected"); Serial.println("[SINK] iPhone connected");
iphone_connected = true; iphone_connected = true;
@ -137,7 +146,7 @@ void sink_connected_cb(esp_bd_addr_t addr, esp_a2d_connection_state_t state, voi
} }
} }
void jbl_connected_cb(esp_bd_addr_t addr, esp_a2d_connection_state_t state, void *obj) { void jbl_connected_cb(esp_a2d_connection_state_t state, void *obj) {
if (state == ESP_A2D_CONNECTION_STATE_CONNECTED) { if (state == ESP_A2D_CONNECTION_STATE_CONNECTED) {
Serial.println("[JBL] Connected"); Serial.println("[JBL] Connected");
jbl_connected = true; jbl_connected = true;
@ -148,7 +157,7 @@ void jbl_connected_cb(esp_bd_addr_t addr, esp_a2d_connection_state_t state, void
} }
} }
void cardo_connected_cb(esp_bd_addr_t addr, esp_a2d_connection_state_t state, void *obj) { void cardo_connected_cb(esp_a2d_connection_state_t state, void *obj) {
if (state == ESP_A2D_CONNECTION_STATE_CONNECTED) { if (state == ESP_A2D_CONNECTION_STATE_CONNECTED) {
Serial.println("[CARDO] Connected"); Serial.println("[CARDO] Connected");
cardo_connected = true; cardo_connected = true;
@ -171,7 +180,7 @@ void setup() {
src_jbl.set_data_callback(get_audio_for_jbl); src_jbl.set_data_callback(get_audio_for_jbl);
src_jbl.set_on_connection_state_changed(jbl_connected_cb); src_jbl.set_on_connection_state_changed(jbl_connected_cb);
src_jbl.set_auto_reconnect(true); src_jbl.set_auto_reconnect(true);
src_jbl.start(JBL_NAME, true); // true = reconnect if known device src_jbl.start(JBL_NAME); // auto-reconnect handled by set_auto_reconnect() above
// Give it time to connect before starting second source // Give it time to connect before starting second source
// (Bluedroid needs sequential connection setup) // (Bluedroid needs sequential connection setup)
@ -190,7 +199,7 @@ void setup() {
src_cardo.set_data_callback(get_audio_for_cardo); src_cardo.set_data_callback(get_audio_for_cardo);
src_cardo.set_on_connection_state_changed(cardo_connected_cb); src_cardo.set_on_connection_state_changed(cardo_connected_cb);
src_cardo.set_auto_reconnect(true); src_cardo.set_auto_reconnect(true);
src_cardo.start(CARDO_NAME, true); src_cardo.start(CARDO_NAME);
t = millis(); t = millis();
while (!cardo_connected && millis() - t < 10000) { while (!cardo_connected && millis() - t < 10000) {
@ -219,14 +228,14 @@ void loop() {
// Auto-reconnect JBL if lost // Auto-reconnect JBL if lost
if (!jbl_connected && millis() - last_reconnect_jbl > RECONNECT_MS) { if (!jbl_connected && millis() - last_reconnect_jbl > RECONNECT_MS) {
Serial.println("[JBL] Retrying connection..."); Serial.println("[JBL] Retrying connection...");
src_jbl.start(JBL_NAME, true); src_jbl.start(JBL_NAME);
last_reconnect_jbl = millis(); last_reconnect_jbl = millis();
} }
// Auto-reconnect Cardo if lost // Auto-reconnect Cardo if lost
if (!cardo_connected && millis() - last_reconnect_cardo > RECONNECT_MS) { if (!cardo_connected && millis() - last_reconnect_cardo > RECONNECT_MS) {
Serial.println("[CARDO] Retrying connection..."); Serial.println("[CARDO] Retrying connection...");
src_cardo.start(CARDO_NAME, true); src_cardo.start(CARDO_NAME);
last_reconnect_cardo = millis(); last_reconnect_cardo = millis();
} }