tierneytim / btAudio

Bluetooth Audio for Arduino
196 stars 29 forks source link

Fix compiling on ESP Arduino core 2.0.x #16

Closed easytarget closed 2 years ago

easytarget commented 2 years ago

This fixes compiling on the ESP Arduino Core 2.0.1 (current) for all the examples, and my sketch based off of those examples now compiles and runs correctly with the latest ESP release.

TL;DR

The first error was due to ESP expanding and separating discoverability vs connectability

[exec] /home/user/Arduino/libraries/btAudio/src/btAudio.cpp: In member function 'void btAudio::begin()':
[exec] /home/user/Arduino/libraries/btAudio/src/btAudio.cpp:54:28: error: 'ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE' was not declared in this scope
[exec]    esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
[exec]                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[exec] /home/user/Arduino/libraries/btAudio/src/btAudio.cpp:54:28: note: suggested alternative: 'ESP_BT_GENERAL_DISCOVERABLE'
[exec]    esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
[exec]                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[exec]                             ESP_BT_GENERAL_DISCOVERABLE

I fixed this by copying the modified ESP examples, using two separate defines for discoverability + connectability.

The second seems to be a renaming of the comms format defines, fixed by following the depreciation warning and suggestion.

[exec] /home/user/Arduino/libraries/btAudio/src/btAudio.cpp: In member function 'void btAudio::I2S(int, int, int)':
[exec] /home/user/Arduino/libraries/btAudio/src/btAudio.cpp:164:60: warning: 'I2S_COMM_FORMAT_I2S' is deprecated [-Wdeprecated-declarations]
[exec]      .communication_format = static_cast<i2s_comm_format_t>(I2S_COMM_FORMAT_I2S|I2S_COMM_FORMAT_I2S_MSB),
[exec]                                                             ^~~~~~~~~~~~~~~~~~~
[exec] In file included from /home/user/.arduino15/packages/esp32/hardware/esp32/2.0.0/tools/sdk/esp32/include/hal/esp32/include/hal/i2s_ll.h:30,
[exec]                  from /home/user/.arduino15/packages/esp32/hardware/esp32/2.0.0/tools/sdk/esp32/include/hal/include/hal/i2s_hal.h:28,
[exec]                  from /home/user/.arduino15/packages/esp32/hardware/esp32/2.0.0/tools/sdk/esp32/include/driver/include/driver/i2s.h:16,
[exec]                  from /home/user/Arduino/libraries/btAudio/src/btAudio.h:10,
[exec]                  from /home/user/Arduino/libraries/btAudio/src/btAudio.cpp:1:
[exec] /home/user/.arduino15/packages/esp32/hardware/esp32/2.0.0/tools/sdk/esp32/include/hal/include/hal/i2s_types.h:70:5: note: declared here
[exec]      I2S_COMM_FORMAT_I2S       __attribute__((deprecated)) = 0x01, /*!< I2S communication format I2S, correspond to `I2S_COMM_FORMAT_STAND_I2S`*/
[exec]      ^~~~~~~~~~~~~~~~~~~
[exec] /home/user/Arduino/libraries/btAudio/src/btAudio.cpp:164:80: warning: 'I2S_COMM_FORMAT_I2S_MSB' is deprecated [-Wdeprecated-declarations]
[exec]      .communication_format = static_cast<i2s_comm_format_t>(I2S_COMM_FORMAT_I2S|I2S_COMM_FORMAT_I2S_MSB),
[exec]                                                                                 ^~~~~~~~~~~~~~~~~~~~~~~
[exec] In file included from /home/user/.arduino15/packages/esp32/hardware/esp32/2.0.0/tools/sdk/esp32/include/hal/esp32/include/hal/i2s_ll.h:30,
[exec]                  from /home/user/.arduino15/packages/esp32/hardware/esp32/2.0.0/tools/sdk/esp32/include/hal/include/hal/i2s_hal.h:28,
[exec]                  from /home/user/.arduino15/packages/esp32/hardware/esp32/2.0.0/tools/sdk/esp32/include/driver/include/driver/i2s.h:16,
[exec]                  from /home/user/Arduino/libraries/btAudio/src/btAudio.h:10,
[exec]                  from /home/user/Arduino/libraries/btAudio/src/btAudio.cpp:1:
[exec] /home/user/.arduino15/packages/esp32/hardware/esp32/2.0.0/tools/sdk/esp32/include/hal/include/hal/i2s_types.h:71:5: note: declared here
[exec]      I2S_COMM_FORMAT_I2S_MSB   __attribute__((deprecated)) = 0x01, /*!< I2S format MSB, (I2S_COMM_FORMAT_I2S |I2S_COMM_FORMAT_I2S_MSB) correspond to `I2S_COMM_FORMAT_STAND_I2S`*/
[exec]      ^~~~~~~~~~~~~~~~~~~~~~~

Finally there was a non-fatal warning.

[exec] /home/user/Arduino/libraries/btAudio/src/btAudio.cpp:170:3: warning: narrowing conversion of 'btAudio::_sampleRate' from 'uint32_t' {aka 'unsigned int'} to 'int' inside { } [-Wnarrowing]
[exec]    };
[exec]    ^
[exec] exit status 1
[exec] Error compiling for board TTGO T-Watch.

I 'fixed' this by changing the sampleRate type to a signed integer, which I assume matches a change upstream. But didnt investigate too much so this should be reviewed/checked as to whether it is the correct solution.

easytarget commented 2 years ago

Ref #15

tierneytim commented 2 years ago

Great work, Could you wrap the changes up in ifdef statements that check for these new defines and if they exist uses them and if not uses the old ones. think that should preserve backwards compatibility.

easytarget commented 2 years ago

C is such fun, pretty certain what I'm hitting is same as this: https://stackoverflow.com/questions/34677148/why-does-the-c-preprocessor-consider-enum-values-as-equal

easytarget commented 2 years ago

Learned some interesting lessons about enums and defines. For the purposes of this library it seems that matching on the ESP core version is the way to go; it should keep compatibility for platformio and the esp IDE ecosystem too.