convert KnxFacade to a template class

This commit is contained in:
Thomas Kunze 2019-08-22 22:57:35 +02:00
parent db740d6687
commit d23613ad65
8 changed files with 314 additions and 294 deletions

View File

@ -6,15 +6,14 @@
#include <stdlib.h>
#include <stdio.h>
KnxFacade* knx = 0;
Platform* platform = 0;
KnxFacade<LinuxPlatform, Bau57B0> knx;
long lastsend = 0;
#define CURR knx->getGroupObject(1)
#define MAX knx->getGroupObject(2)
#define MIN knx->getGroupObject(3)
#define RESET knx->getGroupObject(4)
#define CURR knx.getGroupObject(1)
#define MAX knx.getGroupObject(2)
#define MIN knx.getGroupObject(3)
#define RESET knx.getGroupObject(4)
void measureTemp()
{
@ -52,7 +51,7 @@ void resetCallback(GroupObject& go)
void appLoop()
{
if (!knx->configured())
if (!knx.configured())
return;
measureTemp();
@ -61,12 +60,12 @@ void appLoop()
void setup()
{
srand((unsigned int)time(NULL));
knx->readMemory();
knx.readMemory();
if (knx->induvidualAddress() == 0)
knx->progMode(true);
if (knx.induvidualAddress() == 0)
knx.progMode(true);
if (knx->configured())
if (knx.configured())
{
CURR.dataPointType(Dpt(9, 1));
MIN.dataPointType(Dpt(9, 1));
@ -75,27 +74,25 @@ void setup()
MAX.valueNoSend(-273.0);
RESET.dataPointType(Dpt(1, 15));
RESET.callback(resetCallback);
printf("Timeout: %d\n", knx->paramWord(0));
printf("Zykl. senden: %d\n", knx->paramByte(2));
printf("Min/Max senden: %d\n", knx->paramByte(3));
printf("Aenderung senden: %d\n", knx->paramByte(4));
printf("Abgleich %d\n", knx->paramByte(5));
printf("Timeout: %d\n", knx.paramWord(0));
printf("Zykl. senden: %d\n", knx.paramByte(2));
printf("Min/Max senden: %d\n", knx.paramByte(3));
printf("Aenderung senden: %d\n", knx.paramByte(4));
printf("Abgleich %d\n", knx.paramByte(5));
}
knx->start();
knx.start();
}
int main(int argc, char **argv)
{
platform = new LinuxPlatform(argc, argv);
Bau57B0 bau(*platform);
knx = new KnxFacade(bau);
knx.platform().cmdLineArgs(argc, argv);
setup();
while (1)
{
knx->loop();
if(knx->configured())
knx.loop();
if(knx.configured())
appLoop();
delay(100);
}

View File

@ -5,6 +5,7 @@
class BusAccessUnit
{
public:
virtual ~BusAccessUnit() {}
virtual void groupValueReadLocalConfirm(AckType ack, uint16_t asap, Priority priority, HopCountType hopType, bool status);
virtual void groupValueReadIndication(uint16_t asap, Priority priority, HopCountType hopType);
virtual void groupValueReadResponseConfirm(AckType ack, uint16_t asap, Priority priority, HopCountType hopTtype,

View File

@ -14,8 +14,23 @@
#define DEC 10
#define HEX 16
#define INPUT (0x0)
#define OUTPUT (0x1)
#define INPUT_PULLUP (0x2)
#define INPUT_PULLDOWN (0x3)
#define LOW (0x0)
#define HIGH (0x1)
#define CHANGE 2
#define FALLING 3
#define RISING 4
void delay(uint32_t millis);
uint32_t millis();
void pinMode(uint32_t dwPin, uint32_t dwMode);
void digitalWrite(uint32_t dwPin, uint32_t dwVal);
typedef void (*voidFuncPtr)(void);
void attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode);
#elif ARDUINO_ARCH_SAMD
#include <Arduino.h>

View File

@ -13,10 +13,8 @@ class Platform
virtual uint32_t currentDefaultGateway() = 0;
virtual void macAddress(uint8_t* data) = 0;
//virtual uint32_t millis() = 0;
virtual void restart() = 0;
virtual void fatalError() = 0;
//virtual void mdelay(uint32_t millis) = 0;
virtual void setupMultiCast(uint32_t addr, uint16_t port) = 0;
virtual void closeMultiCast() = 0;

View File

@ -3,232 +3,17 @@
#include "knx/bits.h"
#ifdef ARDUINO_ARCH_SAMD
SamdPlatform platform;
Bau07B0 bau(platform);
KnxFacade<SamdPlatform, Bau07B0> knx;
#define ICACHE_RAM_ATTR
#elif ARDUINO_ARCH_ESP8266
EspPlatform platform;
Bau57B0 bau(platform);
#elif __linux__ //linux
// noops on linux
#define digitalWrite(a, b)
#define pinMode(a, b)
#define attachInterrupt(a, b, c)
KnxFacade<EspPlatform, Bau57B0> knx;
#elif __linux__
#define ICACHE_RAM_ATTR
#endif
bool _toogleProgMode = false;
bool _progLedState = false;
#ifndef __linux__
KnxFacade knx(bau);
ICACHE_RAM_ATTR void buttonUp()
{
_toogleProgMode = true;
}
#endif
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::ledPinActiveOn()
{
return _ledPinActiveOn;
}
void KnxFacade::ledPinActiveOn(uint32_t value)
{
_ledPinActiveOn = value;
}
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()
{
if (progMode() != _progLedState)
{
_progLedState = progMode();
if (_progLedState)
{
println("progmode on");
digitalWrite(knx.ledPin(), _ledPinActiveOn);
}
else
{
println("progmode off");
digitalWrite(knx.ledPin(), HIGH - _ledPinActiveOn);
}
}
if (_toogleProgMode)
{
progMode(!progMode());
_toogleProgMode = false;
}
_bau.loop();
}
void KnxFacade::manufacturerId(uint16_t value)
{
_bau.deviceObject().manufacturerId(value);
}
void KnxFacade::bauNumber(uint32_t value)
{
_bau.deviceObject().bauNumber(value);
}
uint16_t KnxFacade::induvidualAddress()
{
return _bau.deviceObject().induvidualAddress();
}
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);
digitalWrite(_ledPin, HIGH - _ledPinActiveOn);
pinMode(_buttonPin, INPUT_PULLUP);
attachInterrupt(_buttonPin, buttonUp, RISING);
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);
}
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;
}
GroupObject &KnxFacade::getGroupObject(uint16_t goNr)
{
return _bau.groupObjectTable().get(goNr);
}
void KnxFacade::restart(uint16_t individualAddress)
{
_bau.restartRequest(individualAddress);
}
#ifndef __linux__
knx._toogleProgMode = true;
#endif
}

View File

@ -1,5 +1,7 @@
#pragma once
#include "knx/bits.h"
#ifdef ARDUINO_ARCH_SAMD
#include "samd_platform.h"
#include "knx/bau07B0.h"
@ -10,67 +12,268 @@
#include "linux_platform.h"
#include "knx/bau57B0.h"
#define LED_BUILTIN 0
#define HIGH 1
#define LOW 0
#endif
void buttonUp();
typedef uint8_t* (*saveRestoreCallback)(uint8_t* buffer);
class KnxFacade : private SaveRestore
template <class P, class B> class KnxFacade : private SaveRestore
{
public:
KnxFacade(BauSystemB& bau);
bool enabled();
void enabled(bool value);
bool progMode();
void progMode(bool value);
bool configured();
friend void buttonUp();
public:
KnxFacade() : _platformPtr(new P()), _bauPtr(new B(*_platformPtr)), _bau(*_bauPtr)
{}
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(true);
}
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();
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);
uint32_t ledPin();
void ledPin(uint32_t value);
uint32_t buttonPin();
void buttonPin(uint32_t value);
void readMemory();
void writeMemory();
uint16_t induvidualAddress();
void loop();
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);
GroupObject& getGroupObject(uint16_t goNr);
void restart(uint16_t individualAddress);
void ledPinActiveOn(uint32_t value)
{
_ledPinActiveOn = value;
}
uint32_t ledPin()
{
return _ledPin;
}
void ledPin(uint32_t value)
{
_ledPin = 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 char* value)
{
_bau.deviceObject().orderNumber(value);
}
void hardwareType(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, RISING);
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);
}
GroupObject& getGroupObject(uint16_t goNr)
{
return _bau.groupObjectTable().get(goNr);
}
void restart(uint16_t individualAddress)
{
_bau.restartRequest(individualAddress);
}
private:
BauSystemB& _bau;
P* _platformPtr = 0;
B* _bauPtr = 0;
B& _bau;
uint32_t _ledPinActiveOn = LOW;
uint32_t _ledPin = LED_BUILTIN;
uint32_t _buttonPin = 0;
saveRestoreCallback _saveCallback = 0;
saveRestoreCallback _restoreCallback = 0;
bool _toogleProgMode = false;
bool _progLedState = false;
uint8_t* save(uint8_t* buffer);
uint8_t* restore(uint8_t* buffer);
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;
}
};
#ifndef __linux__
extern KnxFacade knx;
#ifdef ARDUINO_ARCH_SAMD
extern KnxFacade<SamdPlatform, Bau07B0> knx;
#elif ARDUINO_ARCH_ESP8266
extern KnxFacade<EspPlatform, Bau57B0> knx;
#elif __linux__
// no predefined global instance
#endif

View File

@ -29,13 +29,10 @@
#define MAX_MEM 4096
LinuxPlatform::LinuxPlatform(int argc, char** argv)
LinuxPlatform::LinuxPlatform()
{
Platform::_memoryReference = (uint8_t*)malloc(MAX_MEM);
_currentMaxMem = Platform::_memoryReference;
_args = new char*[argc + 1];
memcpy(_args, argv, argc * sizeof(char*));
_args[argc] = 0;
}
LinuxPlatform::~LinuxPlatform()
@ -500,4 +497,26 @@ void println(void)
printf("\n");
}
#endif
void pinMode(uint32_t dwPin, uint32_t dwMode)
{
}
void digitalWrite(uint32_t dwPin, uint32_t dwVal)
{
}
typedef void (*voidFuncPtr)(void);
void attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode)
{
}
#endif
void LinuxPlatform::cmdLineArgs(int argc, char** argv)
{
if (_args)
delete[] _args;
_args = new char*[argc + 1];
memcpy(_args, argv, argc * sizeof(char*));
_args[argc] = 0;
}

View File

@ -10,9 +10,11 @@ class LinuxPlatform: public Platform
using Platform::_memoryReference;
using Platform::memoryReference;
public:
LinuxPlatform(int argc, char** argv);
LinuxPlatform();
virtual ~LinuxPlatform();
void cmdLineArgs(int argc, char** argv);
std::string flashFilePath();
void flashFilePath(const std::string path);