mirror of
https://github.com/thelsing/knx.git
synced 2026-04-16 23:12:36 +02:00
Move arduino platform files to seperate platform and move stack files one level up
This commit is contained in:
7
platform/arduino/arch_config.h
Normal file
7
platform/arduino/arch_config.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
#ifndef USE_STATES
|
||||
#define USE_STATES
|
||||
#endif
|
||||
#endif
|
||||
40
platform/arduino/button.cpp
Normal file
40
platform/arduino/button.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#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
|
||||
8
platform/arduino/button.h
Normal file
8
platform/arduino/button.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "arch_config.h"
|
||||
|
||||
#ifdef USE_STATES
|
||||
void buttonDown();
|
||||
#endif
|
||||
void buttonUp();
|
||||
174
platform/arduino/esp_platform.cpp
Normal file
174
platform/arduino/esp_platform.cpp
Normal file
@@ -0,0 +1,174 @@
|
||||
#include "esp_platform.h"
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
#include <user_interface.h>
|
||||
#include <Arduino.h>
|
||||
#include <EEPROM.h>
|
||||
|
||||
#include "knx/bits.h"
|
||||
|
||||
EspPlatform::EspPlatform()
|
||||
{
|
||||
}
|
||||
|
||||
uint32_t EspPlatform::currentIpAddress()
|
||||
{
|
||||
return WiFi.localIP();
|
||||
}
|
||||
|
||||
uint32_t EspPlatform::currentSubnetMask()
|
||||
{
|
||||
return WiFi.subnetMask();
|
||||
}
|
||||
|
||||
uint32_t EspPlatform::currentDefaultGateway()
|
||||
{
|
||||
return WiFi.gatewayIP();
|
||||
}
|
||||
|
||||
void EspPlatform::macAddress(uint8_t * addr)
|
||||
{
|
||||
wifi_get_macaddr(STATION_IF, addr);
|
||||
}
|
||||
|
||||
uint32_t EspPlatform::millis()
|
||||
{
|
||||
return ::millis();
|
||||
}
|
||||
|
||||
void EspPlatform::mdelay(uint32_t millis)
|
||||
{
|
||||
delay(millis);
|
||||
}
|
||||
|
||||
void EspPlatform::restart()
|
||||
{
|
||||
Serial.println("restart");
|
||||
ESP.reset();
|
||||
}
|
||||
|
||||
void EspPlatform::fatalError()
|
||||
{
|
||||
const int period = 200;
|
||||
while (true)
|
||||
{
|
||||
if ((millis() % period) > (period / 2))
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
else
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
void EspPlatform::setupMultiCast(uint32_t addr, uint16_t port)
|
||||
{
|
||||
_mulitcastAddr = htonl(addr);
|
||||
_mulitcastPort = port;
|
||||
IPAddress mcastaddr(_mulitcastAddr);
|
||||
|
||||
Serial.printf("setup multicast addr: %s port: %d ip: %s\n", mcastaddr.toString().c_str(), port,
|
||||
WiFi.localIP().toString().c_str());
|
||||
uint8 result = _udp.beginMulticast(WiFi.localIP(), mcastaddr, port);
|
||||
Serial.printf("result %d\n", result);
|
||||
}
|
||||
|
||||
void EspPlatform::closeMultiCast()
|
||||
{
|
||||
_udp.stop();
|
||||
}
|
||||
|
||||
bool EspPlatform::sendBytes(uint8_t * buffer, uint16_t len)
|
||||
{
|
||||
//printHex("<- ",buffer, len);
|
||||
int result = 0;
|
||||
result = _udp.beginPacketMulticast(_mulitcastAddr, _mulitcastPort, WiFi.localIP());
|
||||
result = _udp.write(buffer, len);
|
||||
result = _udp.endPacket();
|
||||
return true;
|
||||
}
|
||||
|
||||
int EspPlatform::readBytes(uint8_t * buffer, uint16_t maxLen)
|
||||
{
|
||||
int len = _udp.parsePacket();
|
||||
if (len == 0)
|
||||
return 0;
|
||||
|
||||
if (len > maxLen)
|
||||
{
|
||||
Serial.printf("udp buffer to small. was %d, needed %d\n", maxLen, len);
|
||||
fatalError();
|
||||
}
|
||||
|
||||
_udp.read(buffer, len);
|
||||
//printHex("-> ", buffer, len);
|
||||
return len;
|
||||
}
|
||||
|
||||
uint8_t * EspPlatform::getEepromBuffer(uint16_t size)
|
||||
{
|
||||
EEPROM.begin(size);
|
||||
return EEPROM.getDataPtr();
|
||||
}
|
||||
|
||||
void EspPlatform::commitToEeprom()
|
||||
{
|
||||
EEPROM.commit();
|
||||
}
|
||||
|
||||
void EspPlatform::setupUart()
|
||||
{
|
||||
Serial.begin(19200, SERIAL_8E1);
|
||||
while (!Serial) ;
|
||||
}
|
||||
|
||||
|
||||
void EspPlatform::closeUart()
|
||||
{
|
||||
Serial.end();
|
||||
}
|
||||
|
||||
|
||||
int EspPlatform::uartAvailable()
|
||||
{
|
||||
return Serial.available();
|
||||
}
|
||||
|
||||
|
||||
size_t EspPlatform::writeUart(const uint8_t data)
|
||||
{
|
||||
printHex("<p", &data, 1);
|
||||
return Serial.write(data);
|
||||
}
|
||||
|
||||
|
||||
size_t EspPlatform::writeUart(const uint8_t *buffer, size_t size)
|
||||
{
|
||||
printHex("<p", buffer, size);
|
||||
return Serial.write(buffer, size);
|
||||
}
|
||||
|
||||
|
||||
int EspPlatform::readUart()
|
||||
{
|
||||
int val = Serial.read();
|
||||
if (val > 0)
|
||||
printHex("p>", (uint8_t*)&val, 1);
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
size_t EspPlatform::readBytesUart(uint8_t *buffer, size_t length)
|
||||
{
|
||||
size_t toRead = length;
|
||||
uint8_t* pos = buffer;
|
||||
while (toRead > 0)
|
||||
{
|
||||
size_t val = Serial.readBytes(pos, toRead);
|
||||
pos += val;
|
||||
toRead -= val;
|
||||
}
|
||||
|
||||
|
||||
printHex("p>", buffer, length);
|
||||
return length;
|
||||
}
|
||||
#endif
|
||||
49
platform/arduino/esp_platform.h
Normal file
49
platform/arduino/esp_platform.h
Normal file
@@ -0,0 +1,49 @@
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
#include "knx/platform.h"
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <WifiUDP.h>
|
||||
|
||||
#define SerialDBG Serial
|
||||
|
||||
class EspPlatform : public Platform
|
||||
{
|
||||
public:
|
||||
EspPlatform();
|
||||
|
||||
// ip stuff
|
||||
uint32_t currentIpAddress();
|
||||
uint32_t currentSubnetMask();
|
||||
uint32_t currentDefaultGateway();
|
||||
void macAddress(uint8_t* addr);
|
||||
|
||||
// basic stuff
|
||||
uint32_t millis();
|
||||
void mdelay(uint32_t millis);
|
||||
void restart();
|
||||
void fatalError();
|
||||
|
||||
//multicast
|
||||
void setupMultiCast(uint32_t addr, uint16_t port);
|
||||
void closeMultiCast();
|
||||
bool sendBytes(uint8_t* buffer, uint16_t len);
|
||||
int readBytes(uint8_t* buffer, uint16_t maxLen);
|
||||
|
||||
//uart
|
||||
void setupUart();
|
||||
void closeUart();
|
||||
int uartAvailable();
|
||||
size_t writeUart(const uint8_t data);
|
||||
size_t writeUart(const uint8_t *buffer, size_t size);
|
||||
int readUart();
|
||||
size_t readBytesUart(uint8_t *buffer, size_t length);
|
||||
|
||||
//memory
|
||||
uint8_t* getEepromBuffer(uint16_t size);
|
||||
void commitToEeprom();
|
||||
private:
|
||||
uint32_t _mulitcastAddr;
|
||||
uint16_t _mulitcastPort;
|
||||
WiFiUDP _udp;
|
||||
};
|
||||
|
||||
#endif
|
||||
3
platform/arduino/knx.h
Normal file
3
platform/arduino/knx.h
Normal file
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
#include "knx_facade.h"
|
||||
200
platform/arduino/knx_facade.cpp
Normal file
200
platform/arduino/knx_facade.cpp
Normal file
@@ -0,0 +1,200 @@
|
||||
#include "knx_facade.h"
|
||||
#include "state.h"
|
||||
#include "button.h"
|
||||
#include "led.h"
|
||||
#include "nowifistate.h"
|
||||
|
||||
#ifdef ARDUINO_ARCH_SAMD
|
||||
SamdPlatform platform;
|
||||
Bau07B0 bau(platform);
|
||||
#else
|
||||
EspPlatform platform;
|
||||
Bau57B0 bau(platform);
|
||||
#endif
|
||||
KnxFacade knx(bau);
|
||||
|
||||
|
||||
KnxFacade::KnxFacade(BauSystemB& bau) : _bau(bau)
|
||||
{
|
||||
manufacturerId(0xfa);
|
||||
_bau.addSaveRestore(this);
|
||||
}
|
||||
|
||||
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::writeMemory()
|
||||
{
|
||||
_bau.writeMemory();
|
||||
}
|
||||
|
||||
void KnxFacade::loop()
|
||||
{
|
||||
#ifdef USE_STATES
|
||||
if (currentState)
|
||||
currentState->loop();
|
||||
#else
|
||||
knxLoop();
|
||||
#endif
|
||||
}
|
||||
|
||||
void KnxFacade::knxLoop()
|
||||
{
|
||||
_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);
|
||||
|
||||
#ifdef USE_STATES
|
||||
attachInterrupt(_buttonPin, buttonDown, FALLING);
|
||||
switchToSate(noWifiState);
|
||||
checkStates();
|
||||
_ticker.attach_ms(100, doLed);
|
||||
#else
|
||||
attachInterrupt(knx.buttonPin(), buttonUp, RISING);
|
||||
enabled(true);
|
||||
#endif
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
void KnxFacade::setSaveCallback(saveRestoreCallback func)
|
||||
{
|
||||
_saveCallback = func;
|
||||
}
|
||||
|
||||
|
||||
void KnxFacade::setRestoreCallback(saveRestoreCallback func)
|
||||
{
|
||||
_restoreCallback = func;
|
||||
}
|
||||
|
||||
|
||||
uint8_t* KnxFacade::save(uint8_t* buffer)
|
||||
{
|
||||
if (_saveCallback != 0)
|
||||
return _saveCallback(buffer);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
uint8_t* KnxFacade::restore(uint8_t* buffer)
|
||||
{
|
||||
if (_restoreCallback != 0)
|
||||
return _restoreCallback(buffer);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
66
platform/arduino/knx_facade.h
Normal file
66
platform/arduino/knx_facade.h
Normal file
@@ -0,0 +1,66 @@
|
||||
#pragma once
|
||||
|
||||
#include "arch_config.h"
|
||||
|
||||
#ifdef ARDUINO_ARCH_SAMD
|
||||
#include "samd_platform.h"
|
||||
#include "knx/bau07B0.h"
|
||||
#endif
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
#include <Ticker.h>
|
||||
#include "esp_platform.h"
|
||||
#include "knx/bau57B0.h"
|
||||
#endif
|
||||
|
||||
#ifdef USE_STATES
|
||||
class RunningState;
|
||||
#endif
|
||||
|
||||
typedef uint8_t* (*saveRestoreCallback)(uint8_t* buffer);
|
||||
|
||||
class KnxFacade : private SaveRestore
|
||||
{
|
||||
public:
|
||||
KnxFacade(BauSystemB& bau);
|
||||
bool enabled();
|
||||
void enabled(bool value);
|
||||
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 writeMemory();
|
||||
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();
|
||||
void setSaveCallback(saveRestoreCallback func);
|
||||
void setRestoreCallback(saveRestoreCallback func);
|
||||
uint8_t* paramData(uint32_t addr);
|
||||
uint8_t paramByte(uint32_t addr);
|
||||
uint16_t paramWord(uint32_t addr);
|
||||
uint32_t paramInt(uint32_t addr);
|
||||
private:
|
||||
BauSystemB& _bau;
|
||||
uint32_t _ledPin = 16;
|
||||
uint32_t _buttonPin = 0;
|
||||
#ifdef USE_STATES
|
||||
Ticker _ticker;
|
||||
#endif
|
||||
saveRestoreCallback _saveCallback = 0;
|
||||
saveRestoreCallback _restoreCallback = 0;
|
||||
|
||||
uint8_t* save(uint8_t* buffer);
|
||||
uint8_t* restore(uint8_t* buffer);
|
||||
};
|
||||
|
||||
extern KnxFacade knx;
|
||||
174
platform/arduino/knx_facade_samd.cpp
Normal file
174
platform/arduino/knx_facade_samd.cpp
Normal file
@@ -0,0 +1,174 @@
|
||||
#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
|
||||
30
platform/arduino/led.cpp
Normal file
30
platform/arduino/led.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#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
|
||||
7
platform/arduino/led.h
Normal file
7
platform/arduino/led.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "arch_config.h"
|
||||
|
||||
#ifdef USE_STATES
|
||||
void doLed();
|
||||
#endif
|
||||
39
platform/arduino/nowifistate.cpp
Normal file
39
platform/arduino/nowifistate.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#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
|
||||
20
platform/arduino/nowifistate.h
Normal file
20
platform/arduino/nowifistate.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#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
|
||||
28
platform/arduino/programmingmodestate.cpp
Normal file
28
platform/arduino/programmingmodestate.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#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
|
||||
21
platform/arduino/programmingmodestate.h
Normal file
21
platform/arduino/programmingmodestate.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#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
|
||||
43
platform/arduino/runningstate.cpp
Normal file
43
platform/arduino/runningstate.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#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
|
||||
21
platform/arduino/runningstate.h
Normal file
21
platform/arduino/runningstate.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#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
|
||||
156
platform/arduino/samd_platform.cpp
Normal file
156
platform/arduino/samd_platform.cpp
Normal file
@@ -0,0 +1,156 @@
|
||||
#include "samd_platform.h"
|
||||
|
||||
#ifdef ARDUINO_ARCH_SAMD
|
||||
#include <knx/bits.h>
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <FlashAsEEPROM.h>
|
||||
|
||||
SamdPlatform::SamdPlatform()
|
||||
{
|
||||
}
|
||||
|
||||
uint32_t SamdPlatform::currentIpAddress()
|
||||
{
|
||||
// not needed
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t SamdPlatform::currentSubnetMask()
|
||||
{
|
||||
// not needed
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t SamdPlatform::currentDefaultGateway()
|
||||
{
|
||||
// not needed
|
||||
return 0;
|
||||
}
|
||||
|
||||
void SamdPlatform::macAddress(uint8_t * addr)
|
||||
{
|
||||
// not needed
|
||||
}
|
||||
|
||||
uint32_t SamdPlatform::millis()
|
||||
{
|
||||
return::millis();
|
||||
}
|
||||
|
||||
void SamdPlatform::mdelay(uint32_t millis)
|
||||
{
|
||||
delay(millis);
|
||||
}
|
||||
|
||||
void SamdPlatform::restart()
|
||||
{
|
||||
SerialUSB.println("restart");
|
||||
NVIC_SystemReset();
|
||||
}
|
||||
|
||||
void SamdPlatform::fatalError()
|
||||
{
|
||||
const int period = 200;
|
||||
while (true)
|
||||
{
|
||||
if ((millis() % period) > (period / 2))
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
else
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
void SamdPlatform::setupMultiCast(uint32_t addr, uint16_t port)
|
||||
{
|
||||
//not needed
|
||||
}
|
||||
|
||||
void SamdPlatform::closeMultiCast()
|
||||
{
|
||||
//not needed
|
||||
}
|
||||
|
||||
bool SamdPlatform::sendBytes(uint8_t * buffer, uint16_t len)
|
||||
{
|
||||
//not needed
|
||||
}
|
||||
|
||||
int SamdPlatform::readBytes(uint8_t * buffer, uint16_t maxLen)
|
||||
{
|
||||
//not needed
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t * SamdPlatform::getEepromBuffer(uint16_t size)
|
||||
{
|
||||
//EEPROM.begin(size);
|
||||
if(size > EEPROM_EMULATION_SIZE)
|
||||
fatalError();
|
||||
|
||||
return EEPROM.getDataPtr();
|
||||
}
|
||||
|
||||
void SamdPlatform::commitToEeprom()
|
||||
{
|
||||
EEPROM.commit();
|
||||
}
|
||||
|
||||
|
||||
void SamdPlatform::setupUart()
|
||||
{
|
||||
SerialKNX.begin(19200, SERIAL_8E1);
|
||||
while (!SerialKNX)
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
void SamdPlatform::closeUart()
|
||||
{
|
||||
SerialKNX.end();
|
||||
}
|
||||
|
||||
|
||||
int SamdPlatform::uartAvailable()
|
||||
{
|
||||
return SerialKNX.available();
|
||||
}
|
||||
|
||||
|
||||
size_t SamdPlatform::writeUart(const uint8_t data)
|
||||
{
|
||||
//printHex("<p", &data, 1);
|
||||
return SerialKNX.write(data);
|
||||
}
|
||||
|
||||
|
||||
size_t SamdPlatform::writeUart(const uint8_t *buffer, size_t size)
|
||||
{
|
||||
//printHex("<p", buffer, size);
|
||||
return SerialKNX.write(buffer, size);
|
||||
}
|
||||
|
||||
|
||||
int SamdPlatform::readUart()
|
||||
{
|
||||
int val = SerialKNX.read();
|
||||
//if(val > 0)
|
||||
// printHex("p>", (uint8_t*)&val, 1);
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
size_t SamdPlatform::readBytesUart(uint8_t *buffer, size_t length)
|
||||
{
|
||||
size_t toRead = length;
|
||||
uint8_t* pos = buffer;
|
||||
while (toRead > 0)
|
||||
{
|
||||
size_t val = SerialKNX.readBytes(pos, toRead);
|
||||
pos += val;
|
||||
toRead -= val;
|
||||
}
|
||||
//printHex("p>", buffer, length);
|
||||
return length;
|
||||
}
|
||||
#endif
|
||||
50
platform/arduino/samd_platform.h
Normal file
50
platform/arduino/samd_platform.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#include "knx/platform.h"
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
#ifdef ARDUINO_ARCH_SAMD
|
||||
|
||||
#define SerialDBG SerialUSB
|
||||
#define SerialKNX Serial1
|
||||
|
||||
class SamdPlatform : public Platform
|
||||
{
|
||||
public:
|
||||
SamdPlatform();
|
||||
|
||||
// ip stuff
|
||||
uint32_t currentIpAddress();
|
||||
uint32_t currentSubnetMask();
|
||||
uint32_t currentDefaultGateway();
|
||||
void macAddress(uint8_t* addr);
|
||||
|
||||
// basic stuff
|
||||
uint32_t millis();
|
||||
void mdelay(uint32_t millis);
|
||||
void restart();
|
||||
void fatalError();
|
||||
|
||||
//multicast
|
||||
void setupMultiCast(uint32_t addr, uint16_t port);
|
||||
void closeMultiCast();
|
||||
bool sendBytes(uint8_t* buffer, uint16_t len);
|
||||
int readBytes(uint8_t* buffer, uint16_t maxLen);
|
||||
|
||||
//uart
|
||||
virtual void setupUart();
|
||||
virtual void closeUart();
|
||||
virtual int uartAvailable();
|
||||
virtual size_t writeUart(const uint8_t data);
|
||||
virtual size_t writeUart(const uint8_t *buffer, size_t size);
|
||||
virtual int readUart();
|
||||
virtual size_t readBytesUart(uint8_t *buffer, size_t length);
|
||||
|
||||
//memory
|
||||
uint8_t* getEepromBuffer(uint16_t size);
|
||||
void commitToEeprom();
|
||||
private:
|
||||
uint32_t _mulitcastAddr;
|
||||
uint16_t _mulitcastPort;
|
||||
};
|
||||
|
||||
#endif
|
||||
57
platform/arduino/state.cpp
Normal file
57
platform/arduino/state.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#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
|
||||
34
platform/arduino/state.h
Normal file
34
platform/arduino/state.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#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
|
||||
33
platform/arduino/wpsstate.cpp
Normal file
33
platform/arduino/wpsstate.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#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
|
||||
17
platform/arduino/wpsstate.h
Normal file
17
platform/arduino/wpsstate.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#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
|
||||
Reference in New Issue
Block a user