Skip to content

Commit

Permalink
Merge pull request #161 from pgrawehr/ESP32_Update
Browse files Browse the repository at this point in the history
Update ESP32 support to SDK V3.0
  • Loading branch information
pgrawehr authored Nov 25, 2023
2 parents 2757eb7 + acc956b commit e130f9b
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 145 deletions.
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "git",
"url": "https://github.com/firmata/ConfigurableFirmata.git"
},
"version": "3.2.0",
"version": "3.3.0",
"exclude": [
"extras",
"test"
Expand Down
3 changes: 2 additions & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
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.
paragraph=ConfigurableFirmata is an implementation of the Firmata protocol that breaks features such as Digital Input, Digital Output, Analog Input, Analog Output, I2C, etc into individual classes making it easier to mix and match standard features with custom features.
category=Device Control
url=https://github.com/firmata/ConfigurableFirmata
architectures=*
depends=DHT sensor library
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 0 additions & 3 deletions src/AnalogOutputFirmata.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
74 changes: 10 additions & 64 deletions src/AnalogOutputFirmataEsp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@

AnalogOutputFirmata::AnalogOutputFirmata()
{
for (int i = 0; i < MAX_PWM_CHANNELS; i++)
{
_pwmChannelMap[i] = 255;
}
}

void AnalogOutputFirmata::reset()
Expand All @@ -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;
}
}

Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ConfigurableFirmata.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
72 changes: 2 additions & 70 deletions src/EspNetworkFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
#include <sys/poll.h>

#include "esp_wifi.h"


tcpip_adapter_if_t tcpip_if[MAX_ACTIVE_INTERFACES] = { TCPIP_ADAPTER_IF_MAX };
#include <WiFi.h>

const char* NETWORK_TAG = "[NET]";
/// <summary>
Expand Down Expand Up @@ -81,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)
{
Expand All @@ -137,34 +95,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
Expand Down
4 changes: 2 additions & 2 deletions src/WifiCachingStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ void WifiCachingStream::flush()

size_t WifiCachingStream::readBytes(char* buffer, size_t length)
{
int 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;
Expand Down
6 changes: 3 additions & 3 deletions src/WifiCachingStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e130f9b

Please sign in to comment.