Files
nuki_hub/src/Gpio.cpp
iranl 9a896a7ab1 Remove old and modified libs, switch to ESPAsyncWebserver, add support for ESP32-H2 and multiple Ethernet modules (#455)
* Asyncwebserver

* Squashed commit of the following:

commit 575ef02f593918ec6654c87407a4d11fc17071b8
Author: technyon <j.o.schuemann@gmx.de>
Date:   Mon Aug 12 17:56:11 2024 +0200

    merge master

commit 35e5adf4ecd80f9829e8801181f35dd2c1d94759
Merge: a2cc7be2 21adca01
Author: technyon <j.o.schuemann@gmx.de>
Date:   Mon Aug 12 17:41:04 2024 +0200

    Merge branch 'master' of github.com:technyon/nuki_hub into DM9051

commit a2cc7be2954cbd8767ab8186296c0b14134d1d0b
Author: technyon <j.o.schuemann@gmx.de>
Date:   Mon Aug 12 10:51:50 2024 +0200

    update nuki ble

commit 20c809f3dca28b29b219d1ff3a183f1981316de5
Author: technyon <j.o.schuemann@gmx.de>
Date:   Mon Aug 12 10:44:46 2024 +0200

    backup

commit dd41c218efb5270f5efeb734e64dff695920db16
Merge: 153000b5 e84b944a
Author: technyon <j.o.schuemann@gmx.de>
Date:   Mon Aug 12 10:40:03 2024 +0200

    Merge branch 'master' of github.com:technyon/nuki_hub into DM9051

commit 153000b5b1af7df1fbeb5263df94eb26f689cc0a
Author: technyon <j.o.schuemann@gmx.de>
Date:   Mon Aug 12 10:23:07 2024 +0200

    fix linker error

commit a93bbfbfc4301e46ff3696a763dd13c6c89efefb
Author: technyon <j.o.schuemann@gmx.de>
Date:   Sun Aug 11 11:27:07 2024 +0200

    backup

commit f611c75ce8c35f829bcad6cf7e86188f4b3ec331
Merge: f1964917 063fbab6
Author: technyon <j.o.schuemann@gmx.de>
Date:   Sun Aug 11 11:24:47 2024 +0200

    merge master

commit f1964917b4dade3920f1ecdb699c58630199e6da
Author: technyon <j.o.schuemann@gmx.de>
Date:   Sat Aug 10 15:17:45 2024 +0200

    update platformio.ini

commit f448e5e8a7e93be38e09e2ab0b622199a3721af6
Author: technyon <j.o.schuemann@gmx.de>
Date:   Sat Aug 10 11:28:09 2024 +0200

    add SPIClass instance for DM9051

commit 1f190e9aa08033535a2eb442a92e6e20409bbda1
Author: technyon <j.o.schuemann@gmx.de>
Date:   Sat Aug 10 11:22:26 2024 +0200

    add definitions and constructor for DM9051

commit 726b3602ae91594ee1210ad5b6714f75cc5e42a7
Merge: 50a2eb13 4af90cbc
Author: technyon <j.o.schuemann@gmx.de>
Date:   Sat Aug 10 10:19:34 2024 +0200

    merge master

commit 50a2eb136d75d90921f1c6974f18bc107bddc123
Author: technyon <j.o.schuemann@gmx.de>
Date:   Fri Aug 9 11:52:09 2024 +0200

    add comment

commit 9437e485cae169efdf8e5a7bf188a1c7e792d1e5
Author: technyon <j.o.schuemann@gmx.de>
Date:   Sun Aug 4 08:29:21 2024 +0200

    move LAN8720 definitions to seperate file

* Remove Core 2 Ethernet library

* Custom Ethernet

* GPIO and Preferences

* H2
2024-08-16 18:02:37 +07:00

526 lines
17 KiB
C++

#include <esp32-hal.h>
#include "Gpio.h"
#include "Config.h"
#include "Arduino.h"
#include "Logger.h"
#include "PreferencesKeys.h"
#include "RestartReason.h"
#include "Gpio2Go.h"
#include "networkDevices/LAN8720Definitions.h"
#include "networkDevices/DM9051Definitions.h"
#include "networkDevices/W5500Definitions.h"
Gpio* Gpio::_inst = nullptr;
int64_t Gpio::_debounceTs = 0;
const uint Gpio::_debounceTime = GPIO_DEBOUNCE_TIME;
Gpio::Gpio(Preferences* preferences)
: _preferences(preferences)
{
_inst = this;
loadPinConfiguration();
if(_preferences->getBool(preference_gpio_locking_enabled, false))
{
migrateObsoleteSetting();
}
_inst->init();
}
void Gpio::init()
{
for(const auto& entry : _inst->_pinConfiguration)
{
const auto it = std::find(_inst->availablePins().begin(), _inst->availablePins().end(), entry.pin);
if(it == _inst->availablePins().end())
{
continue;
}
switch(entry.role)
{
case PinRole::InputLock:
pinMode(entry.pin, INPUT_PULLUP);
attachInterrupt(entry.pin, isrLock, FALLING);
break;
case PinRole::InputUnlock:
pinMode(entry.pin, INPUT_PULLUP);
attachInterrupt(entry.pin, isrUnlock, FALLING);
break;
case PinRole::InputUnlatch:
pinMode(entry.pin, INPUT_PULLUP);
attachInterrupt(entry.pin, isrUnlatch, FALLING);
break;
case PinRole::InputLockNgo:
pinMode(entry.pin, INPUT_PULLUP);
attachInterrupt(entry.pin, isrLockNgo, FALLING);
break;
case PinRole::InputLockNgoUnlatch:
pinMode(entry.pin, INPUT_PULLUP);
attachInterrupt(entry.pin, isrLockNgoUnlatch, FALLING);
break;
case PinRole::InputElectricStrikeActuation:
pinMode(entry.pin, INPUT_PULLUP);
attachInterrupt(entry.pin, isrElectricStrikeActuation, FALLING);
break;
case PinRole::InputActivateRTO:
pinMode(entry.pin, INPUT_PULLUP);
attachInterrupt(entry.pin, isrActivateRTO, FALLING);
break;
case PinRole::InputActivateCM:
pinMode(entry.pin, INPUT_PULLUP);
attachInterrupt(entry.pin, isrActivateCM, FALLING);
break;
case PinRole::InputDeactivateRtoCm:
pinMode(entry.pin, INPUT_PULLUP);
attachInterrupt(entry.pin, isrDeactivateRtoCm, FALLING);
break;
case PinRole::InputDeactivateRTO:
pinMode(entry.pin, INPUT_PULLUP);
attachInterrupt(entry.pin, isrDeactivateRTO, FALLING);
break;
case PinRole::InputDeactivateCM:
pinMode(entry.pin, INPUT_PULLUP);
attachInterrupt(entry.pin, isrDeactivateCM, FALLING);
break;
case PinRole::OutputHighLocked:
case PinRole::OutputHighUnlocked:
case PinRole::OutputHighMotorBlocked:
case PinRole::OutputHighRtoActive:
case PinRole::OutputHighCmActive:
case PinRole::OutputHighRtoOrCmActive:
case PinRole::GeneralOutput:
pinMode(entry.pin, OUTPUT);
break;
case PinRole::GeneralInputPullDown:
Gpio2Go::configurePin(entry.pin, PinMode::InputPullDown, InterruptMode::Change, 300);
break;
case PinRole::GeneralInputPullUp:
Gpio2Go::configurePin(entry.pin, PinMode::InputPullup, InterruptMode::Change, 300);
break;
case PinRole::Ethernet:
break;
default:
break;
}
Gpio2Go::subscribe(Gpio::inputCallback);
}
}
const std::vector<uint8_t>& Gpio::availablePins() const
{
return _availablePins;
}
void Gpio::loadPinConfiguration()
{
Log->println("Load GPIO configuration");
size_t storedLength = _preferences->getBytesLength(preference_gpio_configuration);
if(storedLength == 0)
{
return;
}
uint8_t serialized[storedLength];
memset(serialized, 0, sizeof(serialized));
size_t size = _preferences->getBytes(preference_gpio_configuration, serialized, sizeof(serialized));
if(size == 0)
{
return;
}
size_t numEntries = size / 2;
_pinConfiguration.clear();
_pinConfiguration.reserve(numEntries);
std::vector<int> disabledPins = getDisabledPins();
for(int i=0; i < numEntries; i++)
{
PinEntry entry;
entry.pin = serialized[i * 2];
Log->print(F("Pin "));
Log->println(entry.pin);
if(std::find(disabledPins.begin(), disabledPins.end(), entry.pin) == disabledPins.end())
{
if(entry.role == PinRole::Ethernet) entry.role = PinRole::Disabled;
entry.role = (PinRole) serialized[(i * 2 + 1)];
Log->println("Not found in Ethernet disabled pins");
Log->print(F("Role: "));
Log->println(getRoleDescription(entry.role));
}
else
{
entry.role = PinRole::Ethernet;
Log->println("Found in Ethernet disabled pins");
Log->print(F("Role: "));
Log->println(getRoleDescription(entry.role));
}
if(entry.role != PinRole::Disabled) _pinConfiguration.push_back(entry);
}
}
const std::vector<int> Gpio::getDisabledPins() const
{
std::vector<int> disabledPins;
switch(_preferences->getInt(preference_network_hardware, 0))
{
case 2:
disabledPins.push_back(ETH_PHY_CS_GENERIC_W5500);
disabledPins.push_back(ETH_PHY_IRQ_GENERIC_W5500);
disabledPins.push_back(ETH_PHY_RST_GENERIC_W5500);
disabledPins.push_back(ETH_PHY_SPI_SCK_GENERIC_W5500);
disabledPins.push_back(ETH_PHY_SPI_MISO_GENERIC_W5500);
disabledPins.push_back(ETH_PHY_SPI_MOSI_GENERIC_W5500);
break;
case 3:
disabledPins.push_back(ETH_PHY_CS_M5_W5500);
disabledPins.push_back(ETH_PHY_IRQ_M5_W5500);
disabledPins.push_back(ETH_PHY_RST_M5_W5500);
disabledPins.push_back(ETH_PHY_SPI_SCK_M5_W5500);
disabledPins.push_back(ETH_PHY_SPI_MISO_M5_W5500);
disabledPins.push_back(ETH_PHY_SPI_MOSI_M5_W5500);
break;
case 10:
disabledPins.push_back(ETH_PHY_CS_M5_W5500_S3);
disabledPins.push_back(ETH_PHY_IRQ_M5_W5500);
disabledPins.push_back(ETH_PHY_RST_M5_W5500);
disabledPins.push_back(ETH_PHY_SPI_SCK_M5_W5500_S3);
disabledPins.push_back(ETH_PHY_SPI_MISO_M5_W5500_S3);
disabledPins.push_back(ETH_PHY_SPI_MOSI_M5_W5500_S3);
break;
case 9:
disabledPins.push_back(ETH_PHY_CS_ETH01EVO);
disabledPins.push_back(ETH_PHY_IRQ_ETH01EVO);
disabledPins.push_back(ETH_PHY_RST_ETH01EVO);
disabledPins.push_back(ETH_PHY_SPI_SCK_ETH01EVO);
disabledPins.push_back(ETH_PHY_SPI_MISO_ETH01EVO);
disabledPins.push_back(ETH_PHY_SPI_MOSI_ETH01EVO);
break;
case 6:
disabledPins.push_back(ETH_PHY_CS_M5_W5500);
disabledPins.push_back(ETH_PHY_IRQ_M5_W5500);
disabledPins.push_back(ETH_PHY_RST_M5_W5500);
disabledPins.push_back(ETH_PHY_SPI_SCK_M5_W5500);
disabledPins.push_back(ETH_PHY_SPI_MISO_M5_W5500);
disabledPins.push_back(ETH_PHY_SPI_MOSI_M5_W5500);
break;
case 11:
disabledPins.push_back(_preferences->getInt(preference_network_custom_cs, -1));
disabledPins.push_back(_preferences->getInt(preference_network_custom_irq, -1));
disabledPins.push_back(_preferences->getInt(preference_network_custom_rst, -1));
disabledPins.push_back(_preferences->getInt(preference_network_custom_sck, -1));
disabledPins.push_back(_preferences->getInt(preference_network_custom_miso, -1));
disabledPins.push_back(_preferences->getInt(preference_network_custom_mosi, -1));
disabledPins.push_back(_preferences->getInt(preference_network_custom_pwr, -1));
disabledPins.push_back(_preferences->getInt(preference_network_custom_mdc, -1));
disabledPins.push_back(_preferences->getInt(preference_network_custom_mdio, -1));
break;
#if defined(CONFIG_IDF_TARGET_ESP32)
case 4:
disabledPins.push_back(12);
disabledPins.push_back(ETH_RESET_PIN_LAN8720);
disabledPins.push_back(ETH_PHY_MDC_LAN8720);
disabledPins.push_back(ETH_PHY_MDIO_LAN8720);
break;
case 5:
disabledPins.push_back(16);
disabledPins.push_back(ETH_RESET_PIN_LAN8720);
disabledPins.push_back(ETH_PHY_MDC_LAN8720);
disabledPins.push_back(ETH_PHY_MDIO_LAN8720);
break;
case 8:
disabledPins.push_back(5);
disabledPins.push_back(ETH_RESET_PIN_LAN8720);
disabledPins.push_back(ETH_PHY_MDC_LAN8720);
disabledPins.push_back(ETH_PHY_MDIO_LAN8720);
break;
case 7:
disabledPins.push_back(-1);
disabledPins.push_back(ETH_RESET_PIN_LAN8720);
disabledPins.push_back(ETH_PHY_MDC_LAN8720);
disabledPins.push_back(ETH_PHY_MDIO_LAN8720);
break;
#endif
default:
break;
}
Log->print(F("GPIO Ethernet disabled pins:"));
for_each_n(disabledPins.begin(), disabledPins.size(),
[](int x) { Log->print(" "); Log->print(x); });
Log->println();
return disabledPins;
}
void Gpio::savePinConfiguration(const std::vector<PinEntry> &pinConfiguration)
{
Log->println("Save GPIO configuration");
int8_t serialized[std::max(pinConfiguration.size() * 2, _preferences->getBytesLength(preference_gpio_configuration))];
memset(serialized, 0, sizeof(serialized));
std::vector<int> disabledPins = getDisabledPins();
int len = pinConfiguration.size();
for(int i=0; i < len; i++)
{
const auto& entry = pinConfiguration[i];
Log->print(F("Pin "));
Log->println(entry.pin);
if(std::find(disabledPins.begin(), disabledPins.end(), entry.pin) != disabledPins.end())
{
serialized[i * 2] = entry.pin;
serialized[i * 2 + 1] = (int8_t)PinRole::Ethernet;
Log->println("Found in Ethernet disabled pins");
Log->print(F("Role: "));
Log->println(getRoleDescription(PinRole::Ethernet));
}
else
{
if(entry.role != PinRole::Disabled && entry.role != PinRole::Ethernet)
{
serialized[i * 2] = entry.pin;
serialized[i * 2 + 1] = (int8_t) entry.role;
Log->println("Not found in Ethernet disabled pins");
Log->print(F("Role: "));
Log->println(getRoleDescription(entry.role));
}
}
}
_preferences->putBytes(preference_gpio_configuration, serialized, sizeof(serialized));
}
const std::vector<PinEntry> &Gpio::pinConfiguration() const
{
return _pinConfiguration;
}
const PinRole Gpio::getPinRole(const int &pin) const
{
for(const auto& pinEntry : _pinConfiguration)
{
if(pinEntry.pin == pin)
{
return pinEntry.role;
}
}
return PinRole::Disabled;
}
String Gpio::getRoleDescription(PinRole role) const
{
switch(role)
{
case PinRole::Disabled:
return "Disabled";
case PinRole::InputLock:
return "Input: Lock";
case PinRole::InputUnlock:
return "Input: Unlock";
case PinRole::InputUnlatch:
return "Input: Unlatch";
case PinRole::InputLockNgo:
return "Input: Lock n Go";
case PinRole::InputLockNgoUnlatch:
return "Input: Lock n Go and unlatch";
case PinRole::InputElectricStrikeActuation:
return "Input: Electric strike actuation";
case PinRole::InputActivateRTO:
return "Input: Activate RTO";
case PinRole::InputActivateCM:
return "Input: Activate CM";
case PinRole::InputDeactivateRtoCm:
return "Input: Deactivate RTO/CM";
case PinRole::InputDeactivateRTO:
return "Input: Deactivate RTO";
case PinRole::InputDeactivateCM:
return "Input: Deactivate CM";
case PinRole::OutputHighLocked:
return "Output: High when locked";
case PinRole::OutputHighUnlocked:
return "Output: High when unlocked";
case PinRole::OutputHighMotorBlocked:
return "Output: High when motor blocked";
case PinRole::OutputHighRtoActive:
return "Output: High when RTO active";
case PinRole::OutputHighCmActive:
return "Output: High when CM active";
case PinRole::OutputHighRtoOrCmActive:
return "Output: High when RTO or CM active";
case PinRole::GeneralOutput:
return "General output";
case PinRole::GeneralInputPullDown:
return "General input (Pull-down)";
case PinRole::GeneralInputPullUp:
return "General input (Pull-up)";
case PinRole::Ethernet:
return "Ethernet";
default:
return "Unknown";
}
}
void Gpio::getConfigurationText(String& text, const std::vector<PinEntry>& pinConfiguration, const String& linebreak) const
{
for(const auto& entry : pinConfiguration)
{
if(entry.role != PinRole::Disabled)
{
text.concat("GPIO ");
text.concat(entry.pin);
if(entry.pin < 10)
{
text.concat(' ');
}
text.concat(": ");
text.concat(getRoleDescription(entry.role));
text.concat(linebreak);
}
}
}
const std::vector<PinRole>& Gpio::getAllRoles() const
{
return _allRoles;
}
void Gpio::notify(const GpioAction &action, const int& pin)
{
for(auto& callback : _callbacks)
{
callback(action, pin);
}
}
void Gpio::inputCallback(const int &pin)
{
_inst->notify(GpioAction::GeneralInput, pin);
}
void Gpio::addCallback(std::function<void(const GpioAction&, const int&)> callback)
{
_callbacks.push_back(callback);
}
void Gpio::isrLock()
{
if((esp_timer_get_time() / 1000) < _debounceTs) return;
_inst->notify(GpioAction::Lock, -1);
_debounceTs = (esp_timer_get_time() / 1000) + _debounceTime;
}
void Gpio::isrUnlock()
{
if((esp_timer_get_time() / 1000) < _debounceTs) return;
_inst->notify(GpioAction::Unlock, -1);
_debounceTs = (esp_timer_get_time() / 1000) + _debounceTime;
}
void Gpio::isrUnlatch()
{
if((esp_timer_get_time() / 1000) < _debounceTs) return;
_inst->notify(GpioAction::Unlatch, -1);
_debounceTs = (esp_timer_get_time() / 1000) + _debounceTime;
}
void Gpio::isrLockNgo()
{
if((esp_timer_get_time() / 1000) < _debounceTs) return;
_inst->notify(GpioAction::LockNgo, -1);
_debounceTs = (esp_timer_get_time() / 1000) + _debounceTime;
}
void Gpio::isrLockNgoUnlatch()
{
if((esp_timer_get_time() / 1000) < _debounceTs) return;
_inst->notify(GpioAction::LockNgoUnlatch, -1);
_debounceTs = (esp_timer_get_time() / 1000) + _debounceTime;
}
void Gpio::isrElectricStrikeActuation()
{
if((esp_timer_get_time() / 1000) < _debounceTs) return;
_inst->notify(GpioAction::ElectricStrikeActuation, -1);
_debounceTs = (esp_timer_get_time() / 1000) + _debounceTime;
}
void Gpio::isrActivateRTO()
{
if((esp_timer_get_time() / 1000) < _debounceTs) return;
_inst->notify(GpioAction::ActivateRTO, -1);
_debounceTs = (esp_timer_get_time() / 1000) + _debounceTime;
}
void Gpio::isrActivateCM()
{
if((esp_timer_get_time() / 1000) < _debounceTs) return;
_inst->notify(GpioAction::ActivateCM, -1);
_debounceTs = (esp_timer_get_time() / 1000) + _debounceTime;
}
void Gpio::isrDeactivateRtoCm()
{
if((esp_timer_get_time() / 1000) < _debounceTs) return;
_inst->notify(GpioAction::DeactivateRtoCm, -1);
_debounceTs = (esp_timer_get_time() / 1000) + _debounceTime;
}
void Gpio::isrDeactivateRTO()
{
if((esp_timer_get_time() / 1000) < _debounceTs) return;
_inst->notify(GpioAction::DeactivateRTO, -1);
_debounceTs = (esp_timer_get_time() / 1000) + _debounceTime;
}
void Gpio::isrDeactivateCM()
{
if((esp_timer_get_time() / 1000) < _debounceTs) return;
_inst->notify(GpioAction::DeactivateCM, -1);
_debounceTs = (esp_timer_get_time() / 1000) + _debounceTime;
}
void Gpio::setPinOutput(const uint8_t& pin, const uint8_t& state)
{
digitalWrite(pin, state);
}
void Gpio::migrateObsoleteSetting()
{
_pinConfiguration.clear();
PinEntry entry1;
entry1.pin = 27;
entry1.role = PinRole::InputUnlatch;
PinEntry entry2;
entry2.pin = 32;
entry2.role = PinRole::InputLock;
PinEntry entry3;
entry3.pin = 33;
entry3.role = PinRole::InputUnlock;
_pinConfiguration.push_back(entry1);
_pinConfiguration.push_back(entry2);
_pinConfiguration.push_back(entry3);
savePinConfiguration(_pinConfiguration);
_preferences->remove(preference_gpio_locking_enabled);
Log->println("Migrated gpio control setting");
delay(200);
restartEsp(RestartReason::GpioConfigurationUpdated);
}