mirror of
https://github.com/thelsing/knx.git
synced 2026-02-23 13:50:35 +01:00
move more code from demo to knx-esp
This commit is contained in:
25
src/button.cpp
Normal file
25
src/button.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include "button.h"
|
||||
#include "state.h"
|
||||
#include "knx_facade.h"
|
||||
|
||||
unsigned long buttonTimestamp = 0;
|
||||
void buttonUp()
|
||||
{
|
||||
if (millis() - buttonTimestamp > 1000)
|
||||
{
|
||||
Serial.println("long button press");
|
||||
currentState->longButtonPress();
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("short button press");
|
||||
currentState->shortButtonPress();
|
||||
}
|
||||
attachInterrupt(knx.buttonPin(), buttonDown, FALLING);
|
||||
}
|
||||
|
||||
void buttonDown()
|
||||
{
|
||||
buttonTimestamp = millis();
|
||||
attachInterrupt(knx.buttonPin(), buttonUp, RISING);
|
||||
}
|
||||
4
src/button.h
Normal file
4
src/button.h
Normal file
@@ -0,0 +1,4 @@
|
||||
#pragma once
|
||||
|
||||
void buttonDown();
|
||||
void buttonUp();
|
||||
@@ -1,4 +1,8 @@
|
||||
#include "knx_facade.h"
|
||||
#include "state.h"
|
||||
#include "button.h"
|
||||
#include "led.h"
|
||||
#include "nowifistate.h"
|
||||
|
||||
KnxFacade knx;
|
||||
|
||||
@@ -32,12 +36,38 @@ bool KnxFacade::configured()
|
||||
return _bau.configured();
|
||||
}
|
||||
|
||||
uint32_t KnxFacade::ledPin()
|
||||
{
|
||||
return _ledPin;
|
||||
}
|
||||
|
||||
void KnxFacade::ledPin(uint32_t value)
|
||||
{
|
||||
_ledPin = value;
|
||||
}
|
||||
|
||||
uint32_t KnxFacade::buttonPin()
|
||||
{
|
||||
return _buttonPin;
|
||||
}
|
||||
|
||||
void KnxFacade::buttonPin(uint32_t value)
|
||||
{
|
||||
_buttonPin = value;
|
||||
}
|
||||
|
||||
void KnxFacade::readMemory()
|
||||
{
|
||||
_bau.readMemory();
|
||||
}
|
||||
|
||||
void KnxFacade::loop()
|
||||
{
|
||||
if (currentState)
|
||||
currentState->loop();
|
||||
}
|
||||
|
||||
void KnxFacade::knxLoop()
|
||||
{
|
||||
_bau.loop();
|
||||
}
|
||||
@@ -72,6 +102,18 @@ void KnxFacade::version(uint16_t value)
|
||||
_bau.deviceObject().version(value);
|
||||
}
|
||||
|
||||
void KnxFacade::start()
|
||||
{
|
||||
pinMode(_ledPin, OUTPUT);
|
||||
|
||||
pinMode(_buttonPin, INPUT);
|
||||
attachInterrupt(_buttonPin, buttonDown, FALLING);
|
||||
|
||||
switchToSate(noWifiState);
|
||||
checkStates();
|
||||
_ticker.attach_ms(100, doLed);
|
||||
}
|
||||
|
||||
uint8_t* KnxFacade::paramData(uint32_t addr)
|
||||
{
|
||||
if (!_bau.configured())
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <Ticker.h>
|
||||
#include "esp_platform.h"
|
||||
#include "knx/bau57B0.h"
|
||||
|
||||
class RunningState;
|
||||
|
||||
class KnxFacade
|
||||
{
|
||||
public:
|
||||
@@ -12,14 +14,20 @@ public:
|
||||
bool progMode();
|
||||
void progMode(bool value);
|
||||
bool configured();
|
||||
uint32_t ledPin();
|
||||
void ledPin(uint32_t value);
|
||||
uint32_t buttonPin();
|
||||
void buttonPin(uint32_t value);
|
||||
void readMemory();
|
||||
void loop();
|
||||
void knxLoop();
|
||||
void registerGroupObjects(GroupObject* groupObjects, uint16_t count);
|
||||
void manufacturerId(uint16_t value);
|
||||
void bauNumber(uint32_t value);
|
||||
void orderNumber(const char* value);
|
||||
void hardwareType(uint8_t* value);
|
||||
void version(uint16_t value);
|
||||
void start();
|
||||
uint8_t* paramData(uint32_t addr);
|
||||
uint8_t paramByte(uint32_t addr);
|
||||
uint16_t paramWord(uint32_t addr);
|
||||
@@ -27,6 +35,9 @@ public:
|
||||
private:
|
||||
EspPlatform _platform;
|
||||
Bau57B0 _bau;
|
||||
uint32_t _ledPin = 16;
|
||||
uint32_t _buttonPin = 0;
|
||||
Ticker _ticker;
|
||||
};
|
||||
|
||||
extern KnxFacade knx;
|
||||
28
src/led.cpp
Normal file
28
src/led.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include "led.h"
|
||||
#include "knx_facade.h"
|
||||
#include "state.h"
|
||||
|
||||
void doLed()
|
||||
{
|
||||
if (!currentState)
|
||||
return;
|
||||
|
||||
if (!currentState->ledOn())
|
||||
{
|
||||
digitalWrite(knx.ledPin(), HIGH);
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned int period = currentState->blinkPeriod();
|
||||
|
||||
if (!currentState->ledBlink() || period == 0)
|
||||
{
|
||||
digitalWrite(knx.ledPin(), LOW);
|
||||
return;
|
||||
}
|
||||
|
||||
if ((millis() % period) > (period / 2))
|
||||
digitalWrite(knx.ledPin(), HIGH);
|
||||
else
|
||||
digitalWrite(knx.ledPin(), LOW);
|
||||
}
|
||||
35
src/nowifistate.cpp
Normal file
35
src/nowifistate.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
|
||||
#include "nowifistate.h"
|
||||
#include "wpsstate.h"
|
||||
#include "runningstate.h"
|
||||
|
||||
NoWifiState noWifiState = NoWifiState();
|
||||
|
||||
void NoWifiState::shortButtonPress()
|
||||
{
|
||||
switchToSate(wpsState);
|
||||
}
|
||||
|
||||
void NoWifiState::longButtonPress()
|
||||
{
|
||||
switchToSate(wpsState);
|
||||
}
|
||||
|
||||
void NoWifiState::enterState()
|
||||
{
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin("", "");
|
||||
while (WiFi.status() == WL_DISCONNECTED)
|
||||
{
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
|
||||
wl_status_t status = WiFi.status();
|
||||
if (status == WL_CONNECTED)
|
||||
{
|
||||
Serial.printf("\nConnected successful to SSID '%s'\n", WiFi.SSID().c_str());
|
||||
switchToSate(runningState);
|
||||
}
|
||||
}
|
||||
16
src/nowifistate.h
Normal file
16
src/nowifistate.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "state.h"
|
||||
|
||||
class NoWifiState : public State
|
||||
{
|
||||
public:
|
||||
NoWifiState() : State(true, false, 0)
|
||||
{}
|
||||
virtual void shortButtonPress();
|
||||
virtual void longButtonPress();
|
||||
virtual void enterState();
|
||||
virtual const char* name() { return "NoWifi"; }
|
||||
};
|
||||
|
||||
extern NoWifiState noWifiState;
|
||||
26
src/programmingmodestate.cpp
Normal file
26
src/programmingmodestate.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include "programmingmodestate.h"
|
||||
#include "runningstate.h"
|
||||
#include "knx_facade.h"
|
||||
|
||||
ProgramModeState programModeState = ProgramModeState();
|
||||
|
||||
void ProgramModeState::enterState()
|
||||
{
|
||||
knx.progMode(true);
|
||||
}
|
||||
|
||||
void ProgramModeState::leaveState()
|
||||
{
|
||||
knx.progMode(false);
|
||||
}
|
||||
|
||||
void ProgramModeState::shortButtonPress()
|
||||
{
|
||||
switchToSate(runningState);
|
||||
}
|
||||
|
||||
void ProgramModeState::loop()
|
||||
{
|
||||
State::loop();
|
||||
knx.knxLoop();
|
||||
}
|
||||
17
src/programmingmodestate.h
Normal file
17
src/programmingmodestate.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include "state.h"
|
||||
|
||||
class ProgramModeState : public State
|
||||
{
|
||||
public:
|
||||
ProgramModeState() : State(true, true, 200)
|
||||
{}
|
||||
virtual void enterState();
|
||||
virtual void leaveState();
|
||||
virtual void shortButtonPress();
|
||||
virtual void loop();
|
||||
virtual const char* name() { return "ProgramMode"; }
|
||||
};
|
||||
|
||||
extern ProgramModeState programModeState;
|
||||
40
src/runningstate.cpp
Normal file
40
src/runningstate.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#include "runningstate.h"
|
||||
#include "programmingmodestate.h"
|
||||
#include "wpsstate.h"
|
||||
#include "knx_facade.h"
|
||||
|
||||
RunningState runningState = RunningState();
|
||||
|
||||
void RunningState::shortButtonPress()
|
||||
{
|
||||
switchToSate(programModeState);
|
||||
}
|
||||
|
||||
void RunningState::longButtonPress()
|
||||
{
|
||||
switchToSate(wpsState);
|
||||
}
|
||||
|
||||
void RunningState::enterState()
|
||||
{
|
||||
if (_initialized)
|
||||
return;
|
||||
|
||||
knx.enabled(true);
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
void RunningState::leaveState()
|
||||
{
|
||||
if (nextState != &programModeState)
|
||||
{
|
||||
_initialized = false;
|
||||
knx.enabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
void RunningState::loop()
|
||||
{
|
||||
State::loop();
|
||||
knx.knxLoop();
|
||||
}
|
||||
20
src/runningstate.h
Normal file
20
src/runningstate.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "state.h"
|
||||
|
||||
class RunningState : public State
|
||||
{
|
||||
public:
|
||||
RunningState() : State(false, false, 0)
|
||||
{}
|
||||
virtual void shortButtonPress();
|
||||
virtual void longButtonPress();
|
||||
virtual void enterState();
|
||||
virtual void leaveState();
|
||||
virtual void loop();
|
||||
virtual const char* name() { return "Running"; }
|
||||
private:
|
||||
bool _initialized = false;
|
||||
};
|
||||
|
||||
extern RunningState runningState;
|
||||
53
src/state.cpp
Normal file
53
src/state.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
#include "state.h"
|
||||
#include "Arduino.h"
|
||||
|
||||
State* volatile currentState = 0;
|
||||
State* volatile nextState = 0;
|
||||
|
||||
void switchToSate(State& state)
|
||||
{
|
||||
nextState = &state;
|
||||
}
|
||||
|
||||
void checkStates()
|
||||
{
|
||||
if (!nextState)
|
||||
return;
|
||||
|
||||
if (nextState == currentState)
|
||||
return;
|
||||
|
||||
if (currentState)
|
||||
{
|
||||
printf("Leave %s\n", currentState->name());
|
||||
currentState->leaveState();
|
||||
}
|
||||
|
||||
currentState = nextState;
|
||||
|
||||
if (currentState)
|
||||
{
|
||||
printf("Enter %s\n", currentState->name());
|
||||
currentState->enterState();
|
||||
}
|
||||
}
|
||||
|
||||
bool State::ledOn()
|
||||
{
|
||||
return _ledOn;
|
||||
}
|
||||
|
||||
bool State::ledBlink()
|
||||
{
|
||||
return _ledBlink;
|
||||
}
|
||||
|
||||
unsigned int State::blinkPeriod()
|
||||
{
|
||||
return _blinkPeriod;
|
||||
}
|
||||
|
||||
void State::loop()
|
||||
{
|
||||
checkStates();
|
||||
}
|
||||
29
src/state.h
Normal file
29
src/state.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
class State
|
||||
{
|
||||
public:
|
||||
State(bool led, bool blink, int period) :
|
||||
_ledOn(led), _ledBlink(blink), _blinkPeriod(period)
|
||||
{}
|
||||
virtual ~State() {}
|
||||
bool ledOn();
|
||||
bool ledBlink();
|
||||
unsigned int blinkPeriod();
|
||||
virtual void shortButtonPress() {}
|
||||
virtual void longButtonPress() {}
|
||||
virtual void enterState() {}
|
||||
virtual void leaveState() {}
|
||||
virtual void loop();
|
||||
virtual const char* name() = 0;
|
||||
private:
|
||||
bool _ledOn;
|
||||
bool _ledBlink;
|
||||
int _blinkPeriod;
|
||||
};
|
||||
|
||||
void switchToSate(State& state);
|
||||
void checkStates();
|
||||
|
||||
extern State* volatile currentState;
|
||||
extern State* volatile nextState;
|
||||
28
src/wpsstate.cpp
Normal file
28
src/wpsstate.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
|
||||
#include "wpsstate.h"
|
||||
#include "runningstate.h"
|
||||
#include "nowifistate.h"
|
||||
|
||||
WpsState wpsState = WpsState();
|
||||
|
||||
void WpsState::enterState()
|
||||
{
|
||||
//invalidate old wifi settings first
|
||||
WiFi.begin("fobar", "a12");
|
||||
Serial.println("WPS config start");
|
||||
bool wpsSuccess = WiFi.beginWPSConfig();
|
||||
if (wpsSuccess) {
|
||||
String newSSID = WiFi.SSID();
|
||||
if (newSSID.length() > 0)
|
||||
{
|
||||
Serial.printf("WPS finished. Connected successfull to SSID '%s'\n", newSSID.c_str());
|
||||
switchToSate(runningState);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.printf("WPS failed.");
|
||||
switchToSate(noWifiState);
|
||||
}
|
||||
}
|
||||
}
|
||||
14
src/wpsstate.h
Normal file
14
src/wpsstate.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "state.h"
|
||||
|
||||
class WpsState : public State
|
||||
{
|
||||
public:
|
||||
WpsState() : State(true, true, 400)
|
||||
{}
|
||||
virtual void enterState();
|
||||
virtual const char* name() { return "Wps"; }
|
||||
};
|
||||
|
||||
extern WpsState wpsState;
|
||||
Reference in New Issue
Block a user