From 1a89fde123e2acd63c57acfdb0b01d3dd1a1d0e1 Mon Sep 17 00:00:00 2001 From: Birger Koblitz Date: Mon, 4 Sep 2023 11:15:52 +0200 Subject: [PATCH 1/5] SPI: fix bug in csPin data type The data type of the csPin member of spi_device_config was wrong. The field will be set to -1 if no pin is set or manual cs-pin control is used. Then the comparisons with -1 in the code will not work as expected, see https://stackoverflow.com/questions/36770376/comparing-unsigned-char-to-a-negative-number So we use "char" instead of "byte" and the code behaves as expected. --- src/SpiFirmata.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SpiFirmata.h b/src/SpiFirmata.h index f405d04..9300d09 100644 --- a/src/SpiFirmata.h +++ b/src/SpiFirmata.h @@ -40,7 +40,7 @@ struct spi_device_config { byte deviceIdChannel; byte csPinOptions; - byte csPin; + char csPin; boolean packedData; SPISettings spi_settings; boolean used; From 564338ce0b4ca00f05e1a4c4fec13ed021cf9eb0 Mon Sep 17 00:00:00 2001 From: Patrick Grawehr Date: Sun, 12 Nov 2023 09:05:10 +0100 Subject: [PATCH 2/5] Update version, mention dependency --- library.json | 2 +- library.properties | 3 ++- src/ConfigurableFirmata.h | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/library.json b/library.json index 9ba4602..08ffd3f 100644 --- a/library.json +++ b/library.json @@ -6,7 +6,7 @@ "type": "git", "url": "https://github.com/firmata/ConfigurableFirmata.git" }, - "version": "3.2.0", + "version": "3.3.0", "exclude": [ "extras", "test" diff --git a/library.properties b/library.properties index 9b9f5d8..a95374e 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ConfigurableFirmata -version=3.2.0 +version=3.3.0 author=Firmata Developers maintainer=https://github.com/firmata/ConfigurableFirmata sentence=This library implements the Firmata protocol as a set of plugins that can be used to create applications to remotely interface with an Arduino board. @@ -7,3 +7,4 @@ paragraph=ConfigurableFirmata is an implementation of the Firmata protocol that category=Device Control url=https://github.com/firmata/ConfigurableFirmata architectures=* +depends=DHT sensor library diff --git a/src/ConfigurableFirmata.h b/src/ConfigurableFirmata.h index 332b5bc..c1f4f5b 100644 --- a/src/ConfigurableFirmata.h +++ b/src/ConfigurableFirmata.h @@ -32,7 +32,7 @@ * Query using the REPORT_FIRMWARE message. */ #define FIRMATA_FIRMWARE_MAJOR_VERSION 3 // for non-compatible changes -#define FIRMATA_FIRMWARE_MINOR_VERSION 2 // for backwards compatible changes +#define FIRMATA_FIRMWARE_MINOR_VERSION 3 // for backwards compatible changes #define FIRMATA_FIRMWARE_BUGFIX_VERSION 0 // for bugfix releases #ifdef LARGE_MEM_DEVICE From 8b70cdd53366841ceddbbca3422a02a0d8312b82 Mon Sep 17 00:00:00 2001 From: Patrick Grawehr Date: Sun, 12 Nov 2023 09:20:50 +0100 Subject: [PATCH 3/5] Add comment about dependency --- readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/readme.md b/readme.md index 84385e1..1023e3d 100644 --- a/readme.md +++ b/readme.md @@ -10,6 +10,8 @@ Firmata is a protocol for communicating with microcontrollers from software on a - **If you are using an older version of the Arduino IDE**, download or clone ConfigurableFirmata to your Arduino sketchbook library folder. This is typically `/Documents/Arduino/libraries/` on Mac or Linux or `\My Documents\Arduino\libraries\` on Windows. - **If you want to edit things yourself or help in development**, clone this repo to `\My Documents\Arduino\libraries\ConfigurableFirmata` and start hacking. Just delete the folder if it exists already. +The library has, by default, a dependency on "DHT Sensor library". From version 3.3, it is installed automatically, but with IDE versions prior to 1.8 or for a development installation, it needs to be installed manually using the library manager. Other dependencies, such as Wire or Servo, are usually part of the default toolkit installation. + ## Release 3.0 ConfigurableFirmata 3.0 contains some internal breaking changes for external modules. If you need to use a particular module (such as FirmataEncoder), either use a 2.xx version or ask the maintainer to update the module to work with V3.0. From 403cfaa8e15bd84fec102adc399a0f644a88a32f Mon Sep 17 00:00:00 2001 From: Patrick Grawehr Date: Sun, 12 Nov 2023 09:25:16 +0100 Subject: [PATCH 4/5] Update network functions to ESP32 SDK 3.0.x --- src/EspNetworkFunctions.cpp | 29 ++--------------------------- src/WifiCachingStream.cpp | 2 +- src/WifiCachingStream.h | 6 +++--- 3 files changed, 6 insertions(+), 31 deletions(-) diff --git a/src/EspNetworkFunctions.cpp b/src/EspNetworkFunctions.cpp index 68a40a3..104b679 100644 --- a/src/EspNetworkFunctions.cpp +++ b/src/EspNetworkFunctions.cpp @@ -8,6 +8,7 @@ #include #include "esp_wifi.h" +#include tcpip_adapter_if_t tcpip_if[MAX_ACTIVE_INTERFACES] = { TCPIP_ADAPTER_IF_MAX }; @@ -137,34 +138,8 @@ network_result_t network_wait_for_connection(int32_t listeningSocket, int32_t* c // error return E_NETWORK_RESULT_FAILED; } - if (ip_addr) { - // check on which network interface the client was connected and save the IP address - tcpip_adapter_ip_info_t ip_info = { 0 }; - int n_if = network_get_active_interfaces(); - - if (n_if > 0) { - struct sockaddr_in clientAddr; - in_addrSize = sizeof(struct sockaddr_in); - getpeername(_sd, (struct sockaddr*)&clientAddr, (socklen_t*)&in_addrSize); - ESP_LOGI(NETWORK_TAG, "Client IP: %08x", clientAddr.sin_addr.s_addr); - *ip_addr = 0; - for (int i = 0; i < n_if; i++) { - tcpip_adapter_get_ip_info(tcpip_if[i], &ip_info); - ESP_LOGI(NETWORK_TAG, "Adapter: %08x, %08x", ip_info.ip.addr, ip_info.netmask.addr); - if ((ip_info.ip.addr & ip_info.netmask.addr) == (ip_info.netmask.addr & clientAddr.sin_addr.s_addr)) { - *ip_addr = ip_info.ip.addr; - ESP_LOGI(NETWORK_TAG, "Client connected on interface %d", tcpip_if[i]); - break; - } - } - if (*ip_addr == 0) { - ESP_LOGE(NETWORK_TAG, "No IP address detected (?!)"); - } - } - else { - ESP_LOGE(NETWORK_TAG, "No active interface (?!)"); - } + *ip_addr = WiFi.localIP(); } // enable non-blocking mode if not data channel connection diff --git a/src/WifiCachingStream.cpp b/src/WifiCachingStream.cpp index 9b05d95..fcd419e 100644 --- a/src/WifiCachingStream.cpp +++ b/src/WifiCachingStream.cpp @@ -99,7 +99,7 @@ void WifiCachingStream::flush() size_t WifiCachingStream::readBytes(char* buffer, size_t length) { - int received = 0; + size_t received = 0; auto result = network_recv_non_blocking(_connection_sd, buffer, (int)length, &received); if (received >= 1) { diff --git a/src/WifiCachingStream.h b/src/WifiCachingStream.h index b16145f..2331fc1 100644 --- a/src/WifiCachingStream.h +++ b/src/WifiCachingStream.h @@ -13,10 +13,10 @@ class WifiCachingStream : public Stream { private: static constexpr int SendBufferSize = 500; - int _sd; - int _port; + int32_t _sd; + int32_t _port; - int _connection_sd; + int32_t _connection_sd; byte _sendBuffer[SendBufferSize]; int _sendBufferIndex; From acc956b97cb57e095841ea932dd946adf9c8b6a3 Mon Sep 17 00:00:00 2001 From: Patrick Grawehr Date: Sun, 12 Nov 2023 11:35:06 +0100 Subject: [PATCH 5/5] 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;