AR sound sync - document data offsets

The compiler has added gaps, to align our struct for hardware compatibility.

* made the gaps explicit
* added  `__attribute__ ((packed))`, to ensure that the data layout is the same on all platforms (extensa, risc-v, 8266)
This commit is contained in:
Frank
2024-01-26 22:26:09 +01:00
committed by GitHub
parent 27e0bff4ee
commit 14ab70dffe

View File

@@ -994,19 +994,21 @@ class AudioReactive : public Usermod {
int8_t mclkPin = MCLK_PIN;
#endif
#endif
// new "V2" audiosync struct - 40 Bytes
struct audioSyncPacket {
char header[6]; // 06 Bytes
float sampleRaw; // 04 Bytes - either "sampleRaw" or "rawSampleAgc" depending on soundAgc setting
float sampleSmth; // 04 Bytes - either "sampleAvg" or "sampleAgc" depending on soundAgc setting
uint8_t samplePeak; // 01 Bytes - 0 no peak; >=1 peak detected. In future, this will also provide peak Magnitude
uint8_t frameCounter; // 01 Bytes - track duplicate/out of order packets
uint8_t fftResult[16]; // 16 Bytes
float FFT_Magnitude; // 04 Bytes
float FFT_MajorPeak; // 04 Bytes
// new "V2" audiosync struct - 44 Bytes
struct __attribute__ ((packed)) audioSyncPacket { // WLEDMM "packed" ensures that there are no additional gaps
char header[6]; // 06 Bytes offset 0
uint8_t gap1[2]; // gap added by compiler: 02 Bytes, offset 6
float sampleRaw; // 04 Bytes offset 8 - either "sampleRaw" or "rawSampleAgc" depending on soundAgc setting
float sampleSmth; // 04 Bytes offset 12 - either "sampleAvg" or "sampleAgc" depending on soundAgc setting
uint8_t samplePeak; // 01 Bytes offset 16 - 0 no peak; >=1 peak detected. In future, this will also provide peak Magnitude
uint8_t frameCounter; // 01 Bytes offset 17 - track duplicate/out of order packets
uint8_t fftResult[16]; // 16 Bytes offset 18
uint8_t gap2[2]; // gap added by compiler: 02 Bytes, offset 34
float FFT_Magnitude; // 04 Bytes offset 36
float FFT_MajorPeak; // 04 Bytes offset 40
};
// old "V1" audiosync struct - 83 Bytes - for backwards compatibility
// old "V1" audiosync struct - 83 Bytes payload, 88 bytes total - for backwards compatibility
struct audioSyncPacket_v1 {
char header[6]; // 06 Bytes
uint8_t myVals[32]; // 32 Bytes
@@ -1897,6 +1899,26 @@ class AudioReactive : public Usermod {
DEBUGSR_PRINTLN(enabled ? F("true.") : F("false."));
USER_FLUSH();
// dump audiosync data layout
#if defined(SR_DEBUG)
{
audioSyncPacket data;
USER_PRINTF("\naudioSyncPacket_v1 size = %d\n", sizeof(audioSyncPacket_v1)); // size 88
USER_PRINTF("audioSyncPacket size = %d\n", sizeof(audioSyncPacket)); // size 44
USER_PRINTF("| char header[6] offset = %2d size = %2d\n", offsetof(audioSyncPacket, header[0]), sizeof(data.header)); // offset 0 size 6
USER_PRINTF("| uint8_t gap1[2] offset = %2d size = %2d\n", offsetof(audioSyncPacket, gap1[0]), sizeof(data.gap1)); // offset 6 size 2
USER_PRINTF("| float sampleRaw offset = %2d size = %2d\n", offsetof(audioSyncPacket, sampleRaw), sizeof(data.sampleRaw)); // offset 8 size 4
USER_PRINTF("| float sampleSmth offset = %2d size = %2d\n", offsetof(audioSyncPacket, sampleSmth), sizeof(data.sampleSmth)); // offset 12 size 4
USER_PRINTF("| uint8_t samplePeak offset = %2d size = %2d\n", offsetof(audioSyncPacket, samplePeak), sizeof(data.samplePeak)); // offset 16 size 1
USER_PRINTF("| uint8_t frameCounter offset = %2d size = %2d\n", offsetof(audioSyncPacket, frameCounter), sizeof(data.frameCounter)); // offset 17 size 1
USER_PRINTF("| uint8_t fftResult[16] offset = %2d size = %2d\n", offsetof(audioSyncPacket, fftResult[0]), sizeof(data.fftResult)); // offset 18 size 16
USER_PRINTF("| uint8_t gap2[2] offset = %2d size = %2d\n", offsetof(audioSyncPacket, gap2[0]), sizeof(data.gap2)); // offset 34 size 2
USER_PRINTF("| float FFT_Magnitude offset = %2d size = %2d\n", offsetof(audioSyncPacket, FFT_Magnitude), sizeof(data.FFT_Magnitude));// offset 36 size 4
USER_PRINTF("| float FFT_MajorPeak offset = %2d size = %2d\n", offsetof(audioSyncPacket, FFT_MajorPeak), sizeof(data.FFT_MajorPeak));// offset 40 size 4
USER_PRINTLN(); USER_FLUSH();
}
#endif
#if defined(ARDUINO_ARCH_ESP32) && defined(SR_DEBUG)
DEBUGSR_PRINTF("|| %-9s min free stack %d\n", pcTaskGetTaskName(NULL), uxTaskGetStackHighWaterMark(NULL)); //WLEDMM
#endif