Skip to content

Commit

Permalink
Merge pull request #194 from technyon/gpio
Browse files Browse the repository at this point in the history
Gpio
  • Loading branch information
technyon authored Jun 8, 2023
2 parents 872a2f8 + 16cf4ae commit a3b9afb
Show file tree
Hide file tree
Showing 25 changed files with 710 additions and 66 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
build
cmake-build-debug
cmake-build-release
cmake-build-release-s3
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ set(SRCFILES
lib/nuki_ble/src/NukiUtils.cpp
lib/nuki_ble/src/NukiLockUtils.cpp
lib/nuki_ble/src/NukiOpenerUtils.cpp
lib/gpio2go/src/Gpio2Go.cpp
lib/gpio2go/src/InterruptMode.h
lib/gpio2go/src/PinMode.h
lib/BleScanner/src/BleInterfaces.h
lib/BleScanner/src/BleScanner.cpp
lib/MqttLogger/src/MqttLogger.cpp
Expand Down
4 changes: 3 additions & 1 deletion Config.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#define NUKI_HUB_VERSION "8.23"
#define NUKI_HUB_VERSION "8.24"

#define MQTT_QOS_LEVEL 1
#define MQTT_CLEAN_SESSIONS false

#define GPIO_DEBOUNCE_TIME 200
65 changes: 54 additions & 11 deletions Gpio.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#include <esp32-hal.h>
#include "Gpio.h"
#include "Config.h"
#include "Arduino.h"
#include "Logger.h"
#include "PreferencesKeys.h"
#include "RestartReason.h"
#include "lib/gpio2go/src/Gpio2Go.h"

Gpio* Gpio::_inst = nullptr;
unsigned long Gpio::_debounceTs = 0;
const uint Gpio::_debounceTime = 1000;
const uint Gpio::_debounceTime = GPIO_DEBOUNCE_TIME;

Gpio::Gpio(Preferences* preferences)
: _preferences(preferences)
Expand Down Expand Up @@ -64,10 +66,27 @@ void Gpio::init()
pinMode(entry.pin, INPUT_PULLUP);
attachInterrupt(entry.pin, isrDeactivateRtoCm, 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;
default:
pinMode(entry.pin, OUTPUT);
break;
}

Gpio2Go::subscribe(Gpio::inputCallback);
}
}

Expand Down Expand Up @@ -136,6 +155,18 @@ 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)
Expand Down Expand Up @@ -168,6 +199,12 @@ String Gpio::getRoleDescription(PinRole role) const
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)";
default:
return "Unknown";
}
Expand Down Expand Up @@ -197,65 +234,70 @@ const std::vector<PinRole>& Gpio::getAllRoles() const
return _allRoles;
}

void Gpio::notify(const GpioAction &action)
void Gpio::notify(const GpioAction &action, const int& pin)
{
for(auto& callback : _callbacks)
{
callback(action);
callback(action, pin);
}
}

void Gpio::addCallback(std::function<void(const GpioAction&)> callback)
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(millis() < _debounceTs) return;
_inst->notify(GpioAction::Lock);
_inst->notify(GpioAction::Lock, -1);
_debounceTs = millis() + _debounceTime;
}

void Gpio::isrUnlock()
{
if(millis() < _debounceTs) return;
_inst->notify(GpioAction::Unlock);
_inst->notify(GpioAction::Unlock, -1);
_debounceTs = millis() + _debounceTime;
}

void Gpio::isrUnlatch()
{
if(millis() < _debounceTs) return;
_inst->notify(GpioAction::Unlatch);
_inst->notify(GpioAction::Unlatch, -1);
_debounceTs = millis() + _debounceTime;
}

void Gpio::isrElectricStrikeActuation()
{
if(millis() < _debounceTs) return;
_inst->notify(GpioAction::ElectricStrikeActuation);
_inst->notify(GpioAction::ElectricStrikeActuation, -1);
_debounceTs = millis() + _debounceTime;
}

void Gpio::isrActivateRTO()
{
if(millis() < _debounceTs) return;
_inst->notify(GpioAction::ActivateRTO);
_inst->notify(GpioAction::ActivateRTO, -1);
_debounceTs = millis() + _debounceTime;
}

void Gpio::isrActivateCM()
{
if(millis() < _debounceTs) return;
_inst->notify(GpioAction::ActivateCM);
_inst->notify(GpioAction::ActivateCM, -1);
_debounceTs = millis() + _debounceTime;
}

void Gpio::isrDeactivateRtoCm()
{
if(millis() < _debounceTs) return;
_inst->notify(GpioAction::DeactivateRtoCm);
_inst->notify(GpioAction::DeactivateRtoCm, -1);
_debounceTs = millis() + _debounceTime;
}

Expand Down Expand Up @@ -291,3 +333,4 @@ void Gpio::migrateObsoleteSetting()
delay(200);
restartEsp(RestartReason::GpioConfigurationUpdated);
}

19 changes: 14 additions & 5 deletions Gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ enum class PinRole
OutputHighMotorBlocked,
OutputHighRtoActive,
OutputHighCmActive,
OutputHighRtoOrCmActive
OutputHighRtoOrCmActive,
GeneralOutput,
GeneralInputPullDown,
GeneralInputPullUp
};

enum class GpioAction
Expand All @@ -30,7 +33,8 @@ enum class GpioAction
ElectricStrikeActuation,
ActivateRTO,
ActivateCM,
DeactivateRtoCm
DeactivateRtoCm,
GeneralInput
};

struct PinEntry
Expand All @@ -47,13 +51,14 @@ class Gpio

void migrateObsoleteSetting();

void addCallback(std::function<void(const GpioAction&)> callback);
void addCallback(std::function<void(const GpioAction&, const int&)> callback);

void loadPinConfiguration();
void savePinConfiguration(const std::vector<PinEntry>& pinConfiguration);

const std::vector<uint8_t>& availablePins() const;
const std::vector<PinEntry>& pinConfiguration() const;
const PinRole getPinRole(const int& pin) const;

String getRoleDescription(PinRole role) const;
void getConfigurationText(String& text, const std::vector<PinEntry>& pinConfiguration, const String& linebreak = "\n") const;
Expand All @@ -63,7 +68,8 @@ class Gpio
void setPinOutput(const uint8_t& pin, const uint8_t& state);

private:
void notify(const GpioAction& action);
void IRAM_ATTR notify(const GpioAction& action, const int& pin);
static void inputCallback(const int & pin);

const std::vector<uint8_t> _availablePins = { 2, 4, 5, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 32, 33 };
const std::vector<PinRole> _allRoles =
Expand All @@ -81,6 +87,9 @@ class Gpio
PinRole::OutputHighRtoActive,
PinRole::OutputHighCmActive,
PinRole::OutputHighRtoOrCmActive,
PinRole::GeneralInputPullDown,
PinRole::GeneralInputPullUp,
PinRole::GeneralOutput
};

std::vector<PinEntry> _pinConfiguration;
Expand All @@ -94,7 +103,7 @@ class Gpio
static void IRAM_ATTR isrActivateCM();
static void IRAM_ATTR isrDeactivateRtoCm();

std::vector<std::function<void(const GpioAction&)>> _callbacks;
std::vector<std::function<void(const GpioAction&, const int&)>> _callbacks;

static Gpio* _inst;
static unsigned long _debounceTs;
Expand Down
7 changes: 6 additions & 1 deletion MqttTopics.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,9 @@
#define mqtt_topic_restart_reason_fw "/maintenance/restartReasonNukiHub"
#define mqtt_topic_restart_reason_esp "/maintenance/restartReasonNukiEsp"
#define mqtt_topic_mqtt_connection_state "/maintenance/mqttConnectionState"
#define mqtt_topic_network_device "/maintenance/networkDevice"
#define mqtt_topic_network_device "/maintenance/networkDevice"

#define mqtt_topic_gpio_prefix "/gpio"
#define mqtt_topic_gpio_pin "/pin_"
#define mqtt_topic_gpio_role "/role"
#define mqtt_topic_gpio_state "/state"
Loading

0 comments on commit a3b9afb

Please sign in to comment.