Skip to content

Commit

Permalink
Update ledc API to V3.0
Browse files Browse the repository at this point in the history
The ledc API for ESP32 has changed significantly from Arduino Core 2.X to 3.0
  • Loading branch information
pgrawehr authored and mchesser committed Nov 26, 2023
1 parent 0bc689d commit acee726
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 112 deletions.
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
43 changes: 0 additions & 43 deletions src/EspNetworkFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
#include "esp_wifi.h"
#include <WiFi.h>


tcpip_adapter_if_t tcpip_if[MAX_ACTIVE_INTERFACES] = { TCPIP_ADAPTER_IF_MAX };

const char* NETWORK_TAG = "[NET]";
/// <summary>
/// Checks whether there's data on the socket
Expand Down Expand Up @@ -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)
{
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)
{
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;
Expand Down

0 comments on commit acee726

Please sign in to comment.