add WifiManger to examples, remove states for wps configuration

This commit is contained in:
Thomas Kunze
2019-02-26 22:42:06 +01:00
parent 5903651cfa
commit f2b97ecd09
24 changed files with 86 additions and 838 deletions

View File

@@ -1,7 +0,0 @@
#pragma once
#ifdef ARDUINO_ARCH_ESP8266
#ifndef USE_STATES
#define USE_STATES
#endif
#endif

View File

@@ -1,40 +0,0 @@
#include "button.h"
#include "state.h"
#include "knx_facade.h"
#ifdef USE_STATES
unsigned long buttonTimestamp = 0;
void buttonDown()
{
buttonTimestamp = millis();
attachInterrupt(knx.buttonPin(), buttonUp, RISING);
}
#endif
void buttonUp()
{
#ifdef USE_STATES
if (millis() - buttonTimestamp > 1000)
{
Serial.println("long button press");
currentState->longButtonPress();
}
else
{
Serial.println("short button press");
currentState->shortButtonPress();
}
attachInterrupt(knx.buttonPin(), buttonDown, FALLING);
#else
if (knx.progMode())
{
digitalWrite(knx.ledPin(), LOW);
knx.progMode(false);
}
else
{
digitalWrite(knx.ledPin(), HIGH);
knx.progMode(true);
}
#endif

View File

@@ -1,8 +0,0 @@
#pragma once
#include "arch_config.h"
#ifdef USE_STATES
void buttonDown();
#endif
void buttonUp();

View File

@@ -1,8 +1,4 @@
#include "knx_facade.h"
#include "state.h"
#include "button.h"
#include "led.h"
#include "nowifistate.h"
#ifdef ARDUINO_ARCH_SAMD
SamdPlatform platform;
@@ -13,7 +9,20 @@ Bau57B0 bau(platform);
#endif
KnxFacade knx(bau);
void buttonUp()
{
if (knx.progMode())
{
digitalWrite(knx.ledPin(), LOW);
knx.progMode(false);
}
else
{
digitalWrite(knx.ledPin(), HIGH);
knx.progMode(true);
}
}
KnxFacade::KnxFacade(BauSystemB& bau) : _bau(bau)
{
manufacturerId(0xfa);
@@ -76,16 +85,6 @@ void KnxFacade::writeMemory()
}
void KnxFacade::loop()
{
#ifdef USE_STATES
if (currentState)
currentState->loop();
#else
knxLoop();
#endif
}
void KnxFacade::knxLoop()
{
_bau.loop();
}
@@ -126,15 +125,8 @@ void KnxFacade::start()
pinMode(_buttonPin, INPUT_PULLUP);
#ifdef USE_STATES
attachInterrupt(_buttonPin, buttonDown, FALLING);
switchToSate(noWifiState);
checkStates();
_ticker.attach_ms(100, doLed);
#else
attachInterrupt(knx.buttonPin(), buttonUp, RISING);
attachInterrupt(_buttonPin, buttonUp, RISING);
enabled(true);
#endif
}
uint8_t* KnxFacade::paramData(uint32_t addr)

View File

@@ -1,7 +1,5 @@
#pragma once
#include "arch_config.h"
#ifdef ARDUINO_ARCH_SAMD
#include "samd_platform.h"
#include "knx/bau07B0.h"
@@ -13,9 +11,6 @@
#include "knx/bau57B0.h"
#endif
#ifdef USE_STATES
class RunningState;
#endif
typedef uint8_t* (*saveRestoreCallback)(uint8_t* buffer);
@@ -35,7 +30,6 @@ public:
void readMemory();
void writeMemory();
void loop();
void knxLoop();
void registerGroupObjects(GroupObject* groupObjects, uint16_t count);
void manufacturerId(uint16_t value);
void bauNumber(uint32_t value);

View File

@@ -1,174 +0,0 @@
#include "knx_facade.h"
#if 0
KnxFacade knx;
#define SerialDBG SerialUSB
void buttonUp();
long buttonTimestamp = 0;
void buttonDown()
{
buttonTimestamp = millis();
attachInterrupt(knx.buttonPin(), buttonUp, RISING);
}
void buttonUp()
{
// keep short/long for now
if (millis() - buttonTimestamp > 1000)
{
SerialDBG.println("long button press");
}
else
{
SerialDBG.println("short button press");
}
if (knx.progMode())
{
digitalWrite(knx.ledPin(), LOW);
knx.progMode(false);
}
else
{
digitalWrite(knx.ledPin(), HIGH);
knx.progMode(true);
}
attachInterrupt(knx.buttonPin(), buttonDown, FALLING);
}
KnxFacade::KnxFacade() : _bau(_platform)
{
manufacturerId(0xfa);
}
bool KnxFacade::enabled()
{
return _bau.enabled();
}
void KnxFacade::enabled(bool value)
{
_bau.enabled(true);
}
bool KnxFacade::progMode()
{
return _bau.deviceObject().progMode();
}
void KnxFacade::progMode(bool value)
{
_bau.deviceObject().progMode(value);
}
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()
{
_bau.loop();
}
void KnxFacade::registerGroupObjects(GroupObject* groupObjects, uint16_t count)
{
_bau.groupObjectTable().groupObjects(groupObjects, count);
}
void KnxFacade::manufacturerId(uint16_t value)
{
_bau.deviceObject().manufacturerId(value);
}
void KnxFacade::bauNumber(uint32_t value)
{
_bau.deviceObject().bauNumber(value);
}
void KnxFacade::orderNumber(const char* value)
{
_bau.deviceObject().orderNumber(value);
}
void KnxFacade::hardwareType(uint8_t* value)
{
_bau.deviceObject().hardwareType(value);
}
void KnxFacade::version(uint16_t value)
{
_bau.deviceObject().version(value);
}
void KnxFacade::start()
{
pinMode(_ledPin, OUTPUT);
pinMode(_buttonPin, INPUT_PULLUP);
attachInterrupt(_buttonPin, buttonDown, FALLING);
enabled(true);
}
uint8_t* KnxFacade::paramData(uint32_t addr)
{
if (!_bau.configured())
return nullptr;
return _bau.parameters().data(addr);
}
uint8_t KnxFacade::paramByte(uint32_t addr)
{
if (!_bau.configured())
return 0;
return _bau.parameters().getByte(addr);
}
uint16_t KnxFacade::paramWord(uint32_t addr)
{
if (!_bau.configured())
return 0;
return _bau.parameters().getWord(addr);
}
uint32_t KnxFacade::paramInt(uint32_t addr)
{
if (!_bau.configured())
return 0;
return _bau.parameters().getInt(addr);
}
#endif

View File

@@ -1,30 +0,0 @@
#include "led.h"
#include "knx_facade.h"
#include "state.h"
#ifdef USE_STATES
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);
}
#endif

View File

@@ -1,7 +0,0 @@
#pragma once
#include "arch_config.h"
#ifdef USE_STATES
void doLed();
#endif

View File

@@ -1,39 +0,0 @@
#include "nowifistate.h"
#include "wpsstate.h"
#include "runningstate.h"
#ifdef USE_STATES
#include <ESP8266WiFi.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);
}
}
#endif

View File

@@ -1,20 +0,0 @@
#pragma once
#include "state.h"
#ifdef USE_STATES
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;
#endif

View File

@@ -1,28 +0,0 @@
#include "programmingmodestate.h"
#include "runningstate.h"
#include "knx_facade.h"
#ifdef USE_STATES
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();
}
#endif

View File

@@ -1,21 +0,0 @@
#pragma once
#include "state.h"
#ifdef USE_STATES
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;
#endif

View File

@@ -1,43 +0,0 @@
#include "runningstate.h"
#include "programmingmodestate.h"
#include "wpsstate.h"
#include "knx_facade.h"
#ifdef USE_STATES
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();
}
#endif

View File

@@ -1,21 +0,0 @@
#pragma once
#include "state.h"
#ifdef USE_STATES
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;
#endif

View File

@@ -1,57 +0,0 @@
#include "state.h"
#include "Arduino.h"
#ifdef USE_STATES
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();
}
#endif

View File

@@ -1,34 +0,0 @@
#pragma once
#include "arch_config.h"
#ifdef USE_STATES
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;
#endif

View File

@@ -1,33 +0,0 @@
#include "arch_config.h"
#ifdef USE_STATES
#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);
}
}
}
#endif

View File

@@ -1,17 +0,0 @@
#pragma once
#include "state.h"
#ifdef USE_STATES
class WpsState : public State
{
public:
WpsState() : State(true, true, 400)
{}
virtual void enterState();
virtual const char* name() { return "Wps"; }
};
extern WpsState wpsState;
#endif