knx/src/knx_facade.h
nanosonde 77a796a39c
Add coupler support (#79)
* save work.

* save work

* save work

* save work

* save work

* Remember which interface received the cemi frame

* save work

* save work

* save work

* Use default value from PID_ROUTING_COUNT

* Add simple alternative to std::function without smart pointers or move semantics

* Remove include

* Add more comments about cleanup

* save work

* Remove forgotten code.

* Move crc16Ccitt to bits.c as it also used for PID_MCB

* save work

* move comment

* save work

* save work

* save work

* save work

* save work

* save work

* save work

* derive from TableObject instead of InterfaceObject

* save work

* save work

* Fix wrong pointer arithmetic in TableObject

* Filter table setting/clearing

* move comment

* save work

* save work

* save work

* handle SBC on closed media

* save work

* move coupler example to different dir

* Restore device example for linux

* save work

* Remove MEDIUM_TYPE and use MASK_VERSION

* save work

* save work

* save work

* save work

* save work

* save work

* save work

* save work

* save work

* save work

* save work

* save work

* save work

* save work

* Replace MEDIUM_TYPE by MASK_VERSION

* Remove adafruit/travis-ci tests

* Disable travis ci cache for platformio

* Fix missing changes

* Fix cemi server and add missing MASK_VERSION definitions

* Enable platformio caching on travis ci again

* Handle device address update for routing decision

* source address is set in network layer and not in data link layer

* Add remaining APCI types that are used with system broadcast

* Add debug print for routing

* Remove simple_functional

* Fix CMakLists.txt

* Use MASK_VERSION to conditionally compile code.

* Remove fixed version reuqirement from platform esp8266

* Add demo-coupler for MCUs

* Remove simple_functional.h from demo knx-linux

* Enable CI for coupler demos

* Correct path for knx-linux-coupler

* Fix knx_facade.h

* Refactor NetworkLayer to use getInterface() for devices and getPrimaryInterface(), getSecondaryInterface() for couplers

* Add platformio configs for other currently possible mask/platform combinations

* Add class diagrams and remove obsolete includes

* Add some minimal docs
2020-09-06 21:41:34 +02:00

363 lines
8.1 KiB
C++

#pragma once
#include "knx/bits.h"
#include "knx/config.h"
#ifdef ARDUINO_ARCH_SAMD
#include "samd_platform.h"
#include "knx/bau07B0.h"
#include "knx/bau27B0.h"
#include "knx/bau2920.h"
#elif ARDUINO_ARCH_ESP8266
#include "esp_platform.h"
#include "knx/bau57B0.h"
#elif ARDUINO_ARCH_ESP32
#define LED_BUILTIN 13
#include "esp32_platform.h"
#include "knx/bau07B0.h"
#include "knx/bau57B0.h"
#include "knx/bau091A.h"
#elif ARDUINO_ARCH_STM32
#include "stm32_platform.h"
#include "knx/bau07B0.h"
#else
#define LED_BUILTIN 0
#include "linux_platform.h"
#include "knx/bau57B0.h"
#include "knx/bau27B0.h"
#include "knx/bau07B0.h"
#include "knx/bau091A.h"
#include "knx/bau2920.h"
#endif
void buttonUp();
typedef uint8_t* (*SaveRestoreCallback)(uint8_t* buffer);
template <class P, class B> class KnxFacade : private SaveRestore
{
friend void buttonUp();
public:
KnxFacade() : _platformPtr(new P()), _bauPtr(new B(*_platformPtr)), _bau(*_bauPtr)
{
manufacturerId(0xfa);
_bau.addSaveRestore(this);
}
virtual ~KnxFacade()
{
if (_bauPtr)
delete _bauPtr;
if (_platformPtr)
delete _platformPtr;
}
KnxFacade(B& bau) : _bau(bau)
{
manufacturerId(0xfa);
_bau.addSaveRestore(this);
}
P& platform()
{
return *_platformPtr;
}
B& bau()
{
return _bau;
}
bool enabled()
{
return _bau.enabled();
}
void enabled(bool value)
{
_bau.enabled(value);
}
bool progMode()
{
return _bau.deviceObject().progMode();
}
void progMode(bool value)
{
_bau.deviceObject().progMode(value);
}
bool configured()
{
return _bau.configured();
}
/**
* returns HIGH if led is active on HIGH, LOW otherwise
*/
uint32_t ledPinActiveOn()
{
return _ledPinActiveOn;
}
/**
* Sets if the programming led is active on HIGH or LOW.
*
* Set to HIGH for GPIO--RESISTOR--LED--GND or to LOW for GPIO--LED--RESISTOR--VDD
*/
void ledPinActiveOn(uint32_t value)
{
_ledPinActiveOn = value;
}
uint32_t ledPin()
{
return _ledPin;
}
void ledPin(uint32_t value)
{
_ledPin = value;
}
/**
* returns RISING if interrupt is created in a rising signal, FALLING otherwise
*/
uint32_t buttonPinInterruptOn()
{
return _buttonPinInterruptOn;
}
/**
* Sets if the programming button creates a RISING or a FALLING signal.
*
* Set to RISING for GPIO--BUTTON--VDD or to FALLING for GPIO--BUTTON--GND
*/
void buttonPinInterruptOn(uint32_t value)
{
_buttonPinInterruptOn = value;
}
uint32_t buttonPin()
{
return _buttonPin;
}
void buttonPin(uint32_t value)
{
_buttonPin = value;
}
void readMemory()
{
_bau.readMemory();
}
void writeMemory()
{
_bau.writeMemory();
}
uint16_t induvidualAddress()
{
return _bau.deviceObject().induvidualAddress();
}
void loop()
{
if (progMode() != _progLedState)
{
_progLedState = progMode();
if (_progLedState)
{
println("progmode on");
digitalWrite(ledPin(), _ledPinActiveOn);
}
else
{
println("progmode off");
digitalWrite(ledPin(), HIGH - _ledPinActiveOn);
}
}
if (_toogleProgMode)
{
progMode(!progMode());
_toogleProgMode = false;
}
_bau.loop();
}
void manufacturerId(uint16_t value)
{
_bau.deviceObject().manufacturerId(value);
}
void bauNumber(uint32_t value)
{
_bau.deviceObject().bauNumber(value);
}
void orderNumber(const uint8_t* value)
{
_bau.deviceObject().orderNumber(value);
}
void hardwareType(const uint8_t* value)
{
_bau.deviceObject().hardwareType(value);
}
void version(uint16_t value)
{
_bau.deviceObject().version(value);
}
void start()
{
pinMode(_ledPin, OUTPUT);
digitalWrite(_ledPin, HIGH - _ledPinActiveOn);
pinMode(_buttonPin, INPUT_PULLUP);
attachInterrupt(_buttonPin, buttonUp, _buttonPinInterruptOn);
enabled(true);
}
void setSaveCallback(SaveRestoreCallback func)
{
_saveCallback = func;
}
void setRestoreCallback(SaveRestoreCallback func)
{
_restoreCallback = func;
}
uint8_t* paramData(uint32_t addr)
{
if (!_bau.configured())
return nullptr;
return _bau.parameters().data(addr);
}
uint8_t paramByte(uint32_t addr)
{
if (!_bau.configured())
return 0;
return _bau.parameters().getByte(addr);
}
uint16_t paramWord(uint32_t addr)
{
if (!_bau.configured())
return 0;
return _bau.parameters().getWord(addr);
}
uint32_t paramInt(uint32_t addr)
{
if (!_bau.configured())
return 0;
return _bau.parameters().getInt(addr);
}
#if (MASK_VERSION == 0x07B0) || (MASK_VERSION == 0x27B0) || (MASK_VERSION == 0x57B0)
GroupObject& getGroupObject(uint16_t goNr)
{
return _bau.groupObjectTable().get(goNr);
}
#endif
void restart(uint16_t individualAddress)
{
_bau.restartRequest(individualAddress);
}
private:
P* _platformPtr = 0;
B* _bauPtr = 0;
B& _bau;
uint32_t _ledPinActiveOn = LOW;
uint32_t _ledPin = LED_BUILTIN;
uint32_t _buttonPinInterruptOn = RISING;
uint32_t _buttonPin = 0;
SaveRestoreCallback _saveCallback = 0;
SaveRestoreCallback _restoreCallback = 0;
bool _toogleProgMode = false;
bool _progLedState = false;
uint16_t _saveSize = 0;
uint8_t* save(uint8_t* buffer)
{
if (_saveCallback != 0)
return _saveCallback(buffer);
return buffer;
}
uint8_t* restore(uint8_t* buffer)
{
if (_restoreCallback != 0)
return _restoreCallback(buffer);
return buffer;
}
uint16_t saveSize()
{
return _saveSize;
}
void saveSize(uint16_t size)
{
_saveSize = size;
}
};
#ifdef ARDUINO_ARCH_SAMD
// predefined global instance for TP or RF or TP/RF coupler
#if MASK_VERSION == 0x07B0
extern KnxFacade<SamdPlatform, Bau07B0> knx;
#elif MASK_VERSION == 0x27B0
extern KnxFacade<SamdPlatform, Bau27B0> knx;
#elif MASK_VERSION == 0x2920
extern KnxFacade<SamdPlatform, Bau2920> knx;
#else
#error "Mask version not supported on ARDUINO_ARCH_SAMD"
#endif
#elif ARDUINO_ARCH_ESP8266
// predefined global instance for IP only
#if MASK_VERSION == 0x57B0
extern KnxFacade<EspPlatform, Bau57B0> knx;
#else
#error "Mask version not supported on ARDUINO_ARCH_ESP8266"
#endif
#elif ARDUINO_ARCH_ESP32
// predefined global instance for TP or IP or TP/IP coupler
#if MASK_VERSION == 0x07B0
extern KnxFacade<Esp32Platform, Bau07B0> knx;
#elif MASK_VERSION == 0x57B0
extern KnxFacade<Esp32Platform, Bau57B0> knx;
#elif MASK_VERSION == 0x091A
extern KnxFacade<Esp32Platform, Bau091A> knx;
#else
#error "Mask version not supported on ARDUINO_ARCH_ESP32"
#endif
#elif ARDUINO_ARCH_STM32
// predefined global instance for TP only
#if MASK_VERSION == 0x07B0
extern KnxFacade<Stm32Platform, Bau07B0> knx;
#else
#error Mask version not supported on ARDUINO_ARCH_STM32
#endif
#elif __linux__
// no predefined global instance
#endif