From acee72621dfcbd357f229e7e2702f428adcff07a Mon Sep 17 00:00:00 2001 From: Patrick Grawehr Date: Sun, 12 Nov 2023 11:35:06 +0100 Subject: [PATCH] Update ledc API to V3.0 The ledc API for ESP32 has changed significantly from Arduino Core 2.X to 3.0 --- src/AnalogOutputFirmata.h | 3 -- src/AnalogOutputFirmataEsp32.cpp | 74 +++++--------------------------- src/EspNetworkFunctions.cpp | 43 ------------------- src/WifiCachingStream.cpp | 4 +- 4 files changed, 12 insertions(+), 112 deletions(-) diff --git a/src/AnalogOutputFirmata.h b/src/AnalogOutputFirmata.h index 1e9e9b3..e582fff 100644 --- a/src/AnalogOutputFirmata.h +++ b/src/AnalogOutputFirmata.h @@ -31,10 +31,7 @@ class AnalogOutputFirmata: public FirmataFeature private: void setupPwmPin(byte pin); #if ESP32 - int getChannelForPin(byte pin); void internalReset(); - // This gives the active pin for each pwm channel. -1 if unused - byte _pwmChannelMap[16]; #endif boolean handleSysex(byte command, byte argc, byte* argv) { diff --git a/src/AnalogOutputFirmataEsp32.cpp b/src/AnalogOutputFirmataEsp32.cpp index 3918eed..e897f8b 100644 --- a/src/AnalogOutputFirmataEsp32.cpp +++ b/src/AnalogOutputFirmataEsp32.cpp @@ -26,10 +26,6 @@ AnalogOutputFirmata::AnalogOutputFirmata() { - for (int i = 0; i < MAX_PWM_CHANNELS; i++) - { - _pwmChannelMap[i] = 255; - } } void AnalogOutputFirmata::reset() @@ -42,75 +38,26 @@ void AnalogOutputFirmata::analogWriteInternal(uint8_t pin, uint32_t value) { // calculate duty, 8191 from 2 ^ 13 - 1 uint32_t valueMax = (1 << DEFAULT_PWM_RESOLUTION) - 1; uint32_t duty = min(value, valueMax); - // write duty to matching channel number - int channel = getChannelForPin(pin); - if (channel != 255) - { - ledcWrite(channel, duty); - // Firmata.sendStringf(F("Channel %d has duty %d/%d"), channel, duty, valueMax); - } - else - { - Firmata.sendStringf(F("Error: Pin %d is not set to PWM"), pin); - } -} - -int AnalogOutputFirmata::getChannelForPin(byte pin) -{ - for (int i = 0; i < MAX_PWM_CHANNELS; i++) - { - if (_pwmChannelMap[i] == pin) - { - return i; - } - } - - return 255; + ledcWrite(pin, duty); } void AnalogOutputFirmata::setupPwmPin(byte pin) { - // Setup timer and attach timer to a led pin - int channel = getChannelForPin(pin); - if (channel == 255) // pin already assigned to a channel? - { - int i; - for (i = 0; i < MAX_PWM_CHANNELS; i++) - { - if (_pwmChannelMap[i] == 255) - { - channel = i; - break; - } - } - if (i >= MAX_PWM_CHANNELS) - { - Firmata.sendStringf(F("Unable to setup pin %d for PWM - no more channels."), pin); - return; - } - - // Firmata.sendStringf(F("Assigning channel %d to pin %d"), channel, pin); - _pwmChannelMap[channel] = pin; - pinMode(pin, OUTPUT); - ledcSetup(channel, LEDC_BASE_FREQ, DEFAULT_PWM_RESOLUTION); - ledcAttachPin(pin, channel); - ledcWrite(pin, 0); - return; + if (!ledcAttach(pin, LEDC_BASE_FREQ, DEFAULT_PWM_RESOLUTION)) + { + Firmata.sendStringf(F("Warning: Pin %d could not be configured for PWM (too many channels?)"), pin); } - - Firmata.sendStringf(F("Warning: Pin %d already assigned to channel %d"), pin, channel); ledcWrite(pin, 0); } void AnalogOutputFirmata::internalReset() { - for (int i = 0; i < MAX_PWM_CHANNELS; i++) + for (int i = 0; i < TOTAL_PINS; i++) { - if (_pwmChannelMap[i] != 255) + if (Firmata.getPinMode(i) == PIN_MODE_PWM) { - ledcDetachPin(_pwmChannelMap[i]); + ledcDetach(i); } - _pwmChannelMap[i] = 255; } } @@ -120,13 +67,12 @@ boolean AnalogOutputFirmata::handlePinMode(byte pin, int mode) setupPwmPin(pin); return true; } - int channel = 255; + // Unlink the channel for this pin - if (mode != PIN_MODE_PWM && (channel = getChannelForPin(pin)) != 255) + if (mode != PIN_MODE_PWM && Firmata.getPinMode(pin) == PIN_MODE_PWM) { // Firmata.sendStringf(F("Detaching pin %d"), pin); - ledcDetachPin(pin); - _pwmChannelMap[channel] = 255; + ledcDetach(pin); } return false; } diff --git a/src/EspNetworkFunctions.cpp b/src/EspNetworkFunctions.cpp index 104b679..f5c9454 100644 --- a/src/EspNetworkFunctions.cpp +++ b/src/EspNetworkFunctions.cpp @@ -10,9 +10,6 @@ #include "esp_wifi.h" #include - -tcpip_adapter_if_t tcpip_if[MAX_ACTIVE_INTERFACES] = { TCPIP_ADAPTER_IF_MAX }; - const char* NETWORK_TAG = "[NET]"; /// /// Checks whether there's data on the socket @@ -82,46 +79,6 @@ network_result_t network_send(int32_t socket, const byte* data, size_t length) return (network_result_t)send(socket, data, length, 0); } - -int network_get_active_interfaces() -{ - int n_if = 0; - - for (int i = 0; i < MAX_ACTIVE_INTERFACES; i++) { - tcpip_if[i] = TCPIP_ADAPTER_IF_MAX; - } - //if ((wifi_network_state == WIFI_STATE_STARTED) && (wifi_is_started())) { - wifi_mode_t mode; - esp_err_t ret = esp_wifi_get_mode(&mode); - if (ret == ESP_OK) { - if (mode == WIFI_MODE_STA) { - n_if = 1; - tcpip_if[0] = TCPIP_ADAPTER_IF_STA; - } - else if (mode == WIFI_MODE_AP) { - n_if = 1; - tcpip_if[0] = TCPIP_ADAPTER_IF_AP; - } - else if (mode == WIFI_MODE_APSTA) { - n_if = 2; - tcpip_if[0] = TCPIP_ADAPTER_IF_STA; - tcpip_if[1] = TCPIP_ADAPTER_IF_AP; - } - } - //} - -#if 0 -#ifdef CONFIG_MICROPY_USE_ETHERNET - if (lan_eth_active) { - n_if++; - tcpip_if[n_if - 1] = TCPIP_ADAPTER_IF_ETH; - } -#endif -#endif - - return n_if; -} - //-------------------------------------------------------------------------------------------- network_result_t network_wait_for_connection(int32_t listeningSocket, int32_t* connectionSocket, uint32_t* ip_addr, bool nonblocking) { diff --git a/src/WifiCachingStream.cpp b/src/WifiCachingStream.cpp index fcd419e..fc4520d 100644 --- a/src/WifiCachingStream.cpp +++ b/src/WifiCachingStream.cpp @@ -99,8 +99,8 @@ void WifiCachingStream::flush() size_t WifiCachingStream::readBytes(char* buffer, size_t length) { - size_t received = 0; - auto result = network_recv_non_blocking(_connection_sd, buffer, (int)length, &received); + int32_t received = 0; + auto result = network_recv_non_blocking(_connection_sd, buffer, (int32_t)length, &received); if (received >= 1) { return received;