make Gpio configuration dynamic
This commit is contained in:
81
Gpio.cpp
81
Gpio.cpp
@@ -3,23 +3,47 @@
|
|||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
#include "Pins.h"
|
#include "Pins.h"
|
||||||
#include "Logger.h"
|
#include "Logger.h"
|
||||||
|
#include "PreferencesKeys.h"
|
||||||
|
|
||||||
Gpio* Gpio::_inst = nullptr;
|
Gpio* Gpio::_inst = nullptr;
|
||||||
NukiWrapper* Gpio::_nuki = nullptr;
|
NukiWrapper* Gpio::_nuki = nullptr;
|
||||||
unsigned long Gpio::_lockedTs = 0;
|
unsigned long Gpio::_lockedTs = 0;
|
||||||
const uint Gpio::_debounceTime = 1000;
|
const uint Gpio::_debounceTime = 1000;
|
||||||
|
|
||||||
|
Gpio::Gpio(Preferences* preferences, NukiWrapper* nuki)
|
||||||
|
: _preferences(preferences)
|
||||||
|
{
|
||||||
|
loadPinConfiguration();
|
||||||
|
|
||||||
|
_inst = this;
|
||||||
|
_inst->init(nuki);
|
||||||
|
}
|
||||||
|
|
||||||
void Gpio::init(NukiWrapper* nuki)
|
void Gpio::init(NukiWrapper* nuki)
|
||||||
{
|
{
|
||||||
_nuki = nuki;
|
_nuki = nuki;
|
||||||
|
|
||||||
pinMode(TRIGGER_LOCK_PIN, INPUT_PULLUP);
|
for(const auto& entry : _inst->_pinConfiguration)
|
||||||
pinMode(TRIGGER_UNLOCK_PIN, INPUT_PULLUP);
|
{
|
||||||
pinMode(TRIGGER_UNLATCH_PIN, INPUT_PULLUP);
|
switch(entry.role)
|
||||||
|
{
|
||||||
attachInterrupt(TRIGGER_LOCK_PIN, isrLock, FALLING);
|
case PinRole::InputLock:
|
||||||
attachInterrupt(TRIGGER_UNLOCK_PIN, isrUnlock, FALLING);
|
pinMode(entry.pin, INPUT_PULLUP);
|
||||||
attachInterrupt(TRIGGER_UNLATCH_PIN, isrUnlatch, FALLING);
|
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;
|
||||||
|
default:
|
||||||
|
pinMode(entry.pin, OUTPUT);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Gpio::isrLock()
|
void Gpio::isrLock()
|
||||||
@@ -42,3 +66,46 @@ void Gpio::isrUnlatch()
|
|||||||
_nuki->unlatch();
|
_nuki->unlatch();
|
||||||
_lockedTs = millis() + _debounceTime;
|
_lockedTs = millis() + _debounceTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const std::vector<uint8_t>& Gpio::availablePins() const
|
||||||
|
{
|
||||||
|
return _availablePins;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Gpio::loadPinConfiguration()
|
||||||
|
{
|
||||||
|
uint8_t serialized[_availablePins.size() * 2];
|
||||||
|
|
||||||
|
size_t size = _preferences->getBytes(preference_gpio_configuration, serialized, _availablePins.size() * 2);
|
||||||
|
if(size == 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t numEntries = size / 2;
|
||||||
|
|
||||||
|
_pinConfiguration.clear();
|
||||||
|
_pinConfiguration.reserve(numEntries);
|
||||||
|
for(int i=0; i < numEntries; i++)
|
||||||
|
{
|
||||||
|
PinEntry entry;
|
||||||
|
entry.pin = i * 2;
|
||||||
|
entry.role = (PinRole)(i * 2 +1);
|
||||||
|
_pinConfiguration.push_back(entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Gpio::savePinConfiguration(const std::vector<PinEntry> &pinConfiguration)
|
||||||
|
{
|
||||||
|
uint8_t serialized[pinConfiguration.size() * 2];
|
||||||
|
|
||||||
|
int len = pinConfiguration.size();
|
||||||
|
for(int i=0; i < len; i++)
|
||||||
|
{
|
||||||
|
const auto& entry = pinConfiguration[i];
|
||||||
|
serialized[i * 2] = entry.pin;
|
||||||
|
serialized[i * 2 + 1] = (int8_t)entry.role;
|
||||||
|
}
|
||||||
|
|
||||||
|
_preferences->putBytes(preference_gpio_configuration, serialized, sizeof(serialized));
|
||||||
|
}
|
||||||
|
|||||||
28
Gpio.h
28
Gpio.h
@@ -3,13 +3,37 @@
|
|||||||
|
|
||||||
#include "NukiWrapper.h"
|
#include "NukiWrapper.h"
|
||||||
|
|
||||||
|
enum class PinRole
|
||||||
|
{
|
||||||
|
Undefined,
|
||||||
|
InputLock,
|
||||||
|
InputUnlock,
|
||||||
|
InputUnlatch,
|
||||||
|
OutputHighLocked,
|
||||||
|
OutputHighUnlocked,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct PinEntry
|
||||||
|
{
|
||||||
|
uint8_t pin = 0;
|
||||||
|
PinRole role = PinRole::Undefined;
|
||||||
|
};
|
||||||
|
|
||||||
class Gpio
|
class Gpio
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Gpio() = delete;
|
Gpio(Preferences* preferences, NukiWrapper* nuki);
|
||||||
static void init(NukiWrapper* nuki);
|
static void init(NukiWrapper* nuki);
|
||||||
|
|
||||||
|
void loadPinConfiguration();
|
||||||
|
void savePinConfiguration(const std::vector<PinEntry>& pinConfiguration);
|
||||||
|
|
||||||
|
const std::vector<uint8_t>& availablePins() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
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 };
|
||||||
|
|
||||||
|
std::vector<PinEntry> _pinConfiguration;
|
||||||
static const uint _debounceTime;
|
static const uint _debounceTime;
|
||||||
|
|
||||||
static void IRAM_ATTR isrLock();
|
static void IRAM_ATTR isrLock();
|
||||||
@@ -19,4 +43,6 @@ private:
|
|||||||
static Gpio* _inst;
|
static Gpio* _inst;
|
||||||
static NukiWrapper* _nuki;
|
static NukiWrapper* _nuki;
|
||||||
static unsigned long _lockedTs;
|
static unsigned long _lockedTs;
|
||||||
|
|
||||||
|
Preferences* _preferences = nullptr;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -44,6 +44,7 @@
|
|||||||
#define preference_cred_password "crdpass"
|
#define preference_cred_password "crdpass"
|
||||||
#define preference_publish_authdata "pubauth"
|
#define preference_publish_authdata "pubauth"
|
||||||
#define preference_gpio_locking_enabled "gpiolck"
|
#define preference_gpio_locking_enabled "gpiolck"
|
||||||
|
#define preference_gpio_configuration "gpiocfg"
|
||||||
#define preference_publish_debug_info "pubdbg"
|
#define preference_publish_debug_info "pubdbg"
|
||||||
#define preference_presence_detection_timeout "prdtimeout"
|
#define preference_presence_detection_timeout "prdtimeout"
|
||||||
#define preference_has_mac_saved "hasmac"
|
#define preference_has_mac_saved "hasmac"
|
||||||
|
|||||||
11
main.cpp
11
main.cpp
@@ -25,6 +25,7 @@ NukiOpenerWrapper* nukiOpener = nullptr;
|
|||||||
PresenceDetection* presenceDetection = nullptr;
|
PresenceDetection* presenceDetection = nullptr;
|
||||||
Preferences* preferences = nullptr;
|
Preferences* preferences = nullptr;
|
||||||
EthServer* ethServer = nullptr;
|
EthServer* ethServer = nullptr;
|
||||||
|
Gpio* gpio = nullptr;
|
||||||
|
|
||||||
bool lockEnabled = false;
|
bool lockEnabled = false;
|
||||||
bool openerEnabled = false;
|
bool openerEnabled = false;
|
||||||
@@ -213,10 +214,12 @@ void setup()
|
|||||||
nuki = new NukiWrapper("NukiHub", deviceId, bleScanner, networkLock, preferences);
|
nuki = new NukiWrapper("NukiHub", deviceId, bleScanner, networkLock, preferences);
|
||||||
nuki->initialize(firstStart);
|
nuki->initialize(firstStart);
|
||||||
|
|
||||||
if(preferences->getBool(preference_gpio_locking_enabled))
|
gpio = new Gpio(preferences, nuki);
|
||||||
{
|
|
||||||
Gpio::init(nuki);
|
// if(preferences->getBool(preference_gpio_locking_enabled))
|
||||||
}
|
// {
|
||||||
|
// Gpio::init(nuki);
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
Log->println(openerEnabled ? F("NUKI Opener enabled") : F("NUKI Opener disabled"));
|
Log->println(openerEnabled ? F("NUKI Opener enabled") : F("NUKI Opener disabled"));
|
||||||
|
|||||||
Reference in New Issue
Block a user