From e2d27cdcff4181630d99414256c1a6dacfdefc43 Mon Sep 17 00:00:00 2001 From: sallar Date: Tue, 25 Nov 2025 18:41:10 +0100 Subject: [PATCH] main.cpp been adjusted to communicate with the dac --- test-pico-io/src/main.cpp | 75 ++++++++++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 17 deletions(-) diff --git a/test-pico-io/src/main.cpp b/test-pico-io/src/main.cpp index e285abb..32a7d52 100644 --- a/test-pico-io/src/main.cpp +++ b/test-pico-io/src/main.cpp @@ -1,27 +1,24 @@ #include +#include #include "Heavy_temp_a2960967.hpp" // Erzeuge eine globale Heavy-Instanz Heavy_temp_a2960967* hv = nullptr; +I2S i2s(OUTPUT); -void setup() { - Serial.begin(115200); - delay(1000); +#define SAMPLERATE 44100 +#define BLOCKSIZE 64 // Heavy block size +#define BUFFERSIZE (2 * BLOCKSIZE) // Stereo interleaved - Serial.println("Heavy DSP Test startet..."); +int16_t buffer[BUFFERSIZE]; - // Heavy-Context initialisieren (44.1 kHz z. B.) - hv = new Heavy_temp_a2960967(44100.0); - - if (hv) { - Serial.println("Heavy context erfolgreich erstellt!"); - } else { - Serial.println("Fehler: Heavy context konnte nicht erstellt werden."); - } +inline int16_t float_to_i16(float x) { + if (x > 1.0f) x = 1.0f; + if (x < -1.0f) x = -1.0f; + return (int16_t)(x * 32767.0f); } -void loop() { - if (!hv) return; +void codec_transmit() { // Testausgabe mit einem leeren Audiobuffer const int blockSize = 64; @@ -30,8 +27,52 @@ void loop() { // DSP verarbeiten hv->processInlineInterleaved(nullptr, output, blockSize); - // Optional: etwas ausgeben (z. B. ein Samplewert) - Serial.print("Sample L[0]: "); - Serial.println(output[0]); + for (int i = 0; i < 2*blockSize; i++) { + buffer[i] = float_to_i16(output[i]); + //buffer[i] = random(-12000, 12000); + } + + i2s.write((const uint8_t *)&buffer, BUFFERSIZE * sizeof(int16_t)); +} + +void setup() { + i2s.setSysClk(SAMPLERATE); + Serial.begin(115200); + while (!Serial) { + // Ausgabe auf USB-Seriell *versuchen* (zeigt sich erst bei Verbindung) + Serial.print("."); + delay(100); + } + Serial.println("\nSerial connected!"); + Serial.println("Heavy DSP Test startet..."); + + // Heavy-Context initialisieren (44.1 kHz z. B.) + hv = new Heavy_temp_a2960967(SAMPLERATE); + + if (hv) { + Serial.println("Heavy context erfolgreich erstellt!"); + } else { + Serial.println("Fehler: Heavy context konnte nicht erstellt werden."); + } + i2s.onTransmit(codec_transmit); + i2s.setDATA(8); // DAC DIN + i2s.setBCLK(6); // Note: LRCLK = BCLK + 1 + i2s.setMCLK(9); + i2s.setMCLKmult(256); // 256 = 12.288.000Hz 512 = 25MHz +// i2s.swapClocks(); + i2s.setFrequency(SAMPLERATE); + i2s.setBitsPerSample(16); + + i2s.setBuffers(4, BUFFERSIZE * sizeof(int16_t) / sizeof(uint32_t)); + + if (!i2s.begin(SAMPLERATE)) { + Serial.println("0: I2S error!"); + while (100); + } + delay(100); } + +void loop() { + +}