Skip to content

Commit

Permalink
Add callback functions for prog mode and improve documentation (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Geramb committed Feb 19, 2022
1 parent d26771c commit 53425e2
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 12 deletions.
51 changes: 45 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,55 @@
knx
===
# knx



This projects provides a knx-device stack for arduino (ESP8266, SAMD21) and linux. (more are quite easy to add)
This projects provides a knx-device stack for arduino (ESP8266, ESP32, SAMD21, RP2040, STM32), CC1310 and linux. (more are quite easy to add)
It implements most of System-B specification and can be configured with ETS.
The necessary knxprod-files can be generated with my [CreateKnxProd](https://github.com/thelsing/CreateKnxProd) tool.

For esp8266 [WifiManager](https://github.com/tzapu/WiFiManager) is used to configure wifi.
For ESP8266 and ESP32 [WifiManager](https://github.com/tzapu/WiFiManager) is used to configure wifi.

Don't forget to reset ESP8266 manually (disconnect power) after flashing. The reboot doen't work during configuration with ETS otherwise.

The SAMD21 version uses my version of the [FlashStorage](https://github.com/thelsing/FlashStorage) lib (Pull request pending).

Generated documentation can be found [here](https://knx.readthedocs.io/en/latest/).

## Stack configuration possibilities

Specify prog button GPIO other then `GPIO0`:
```C++
knx.buttonPin(3); // Use GPIO3 Pin
```

Specify a LED GPIO for programming mode other then the `LED_BUILTIN`:
```C++
knx.ledPin(5);
```

Use a custom function instead of a LED connected to GPIO to indicate the programming mode:
```C++
#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
#include <knx.h>
// create a pixel strand with 1 pixel on PIN_NEOPIXEL
Adafruit_NeoPixel pixels(1, PIN_NEOPIXEL);

void progLedOff()
{
pixels.clear();
pixels.show();
}

void progLedOn()
{
pixels.setPixelColor(0, pixels.Color(20, 0, 0));
pixels.show();
}

void main ()
{
knx.setProgLedOffCallback(progLedOff);
knx.setProgLedOnCallback(progLedOn);
[...]
}
```
More configuration options can be found in the examples.
48 changes: 42 additions & 6 deletions src/knx_facade.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
void buttonUp();
#endif
#elif defined(ARDUINO_ARCH_ESP32)
#if !defined(LED_BUILTIN)
#define LED_BUILTIN 13
#endif
#include "esp32_platform.h"
#ifndef KNX_NO_AUTOMATIC_GLOBAL_INSTANCE
void buttonUp();
Expand All @@ -39,10 +41,14 @@
void buttonUp();
#endif
#elif __linux__
#if !defined(LED_BUILTIN)
#define LED_BUILTIN 0
#endif
#include "linux_platform.h"
#else
#if !defined(LED_BUILTIN)
#define LED_BUILTIN 5 // see GPIO_PinConfig gpioPinConfigs[]
#endif
#include "cc1310_platform.h"
#ifndef KNX_NO_AUTOMATIC_GLOBAL_INSTANCE
extern void buttonUp();
Expand All @@ -52,7 +58,9 @@
typedef const uint8_t* (*RestoreCallback)(const uint8_t* buffer);
typedef uint8_t* (*SaveCallback)(uint8_t* buffer);
typedef void (*IsrFunctionPtr)();

typedef void (*ProgLedOnCallback)();
typedef void (*ProgLedOffCallback)();

template <class P, class B> class KnxFacade : private SaveRestore
{
public:
Expand Down Expand Up @@ -158,6 +166,16 @@ template <class P, class B> class KnxFacade : private SaveRestore
_ledPin = value;
}

void setProgLedOffCallback(ProgLedOffCallback progLedOffCallback)
{
_progLedOffCallback = progLedOffCallback;
}

void setProgLedOnCallback(ProgLedOnCallback progLedOnCallback)
{
_progLedOnCallback = progLedOnCallback;
}

/**
* returns RISING if interrupt is created in a rising signal, FALLING otherwise
*/
Expand Down Expand Up @@ -209,12 +227,12 @@ template <class P, class B> class KnxFacade : private SaveRestore
if (_progLedState)
{
println("progmode on");
digitalWrite(ledPin(), _ledPinActiveOn);
progLedOn();
}
else
{
println("progmode off");
digitalWrite(ledPin(), HIGH - _ledPinActiveOn);
progLedOff();
}
}
if (_toggleProgMode)
Expand Down Expand Up @@ -252,10 +270,10 @@ template <class P, class B> class KnxFacade : private SaveRestore

void start()
{
pinMode(ledPin(), OUTPUT);

digitalWrite(ledPin(), HIGH - _ledPinActiveOn);
if (_progLedOffCallback == 0 || _progLedOnCallback == 0)
pinMode(ledPin(), OUTPUT);

progLedOff();
pinMode(buttonPin(), INPUT_PULLUP);

if (_progButtonISRFuncPtr)
Expand Down Expand Up @@ -389,6 +407,8 @@ template <class P, class B> class KnxFacade : private SaveRestore
P* _platformPtr = 0;
B* _bauPtr = 0;
B& _bau;
ProgLedOnCallback _progLedOnCallback = 0;
ProgLedOffCallback _progLedOffCallback = 0;
uint32_t _ledPinActiveOn = LOW;
uint32_t _ledPin = LED_BUILTIN;
uint32_t _buttonPinInterruptOn = RISING;
Expand Down Expand Up @@ -425,6 +445,22 @@ template <class P, class B> class KnxFacade : private SaveRestore
{
_saveSize = size;
}

void progLedOn()
{
if (_progLedOnCallback == 0)
digitalWrite(ledPin(), _ledPinActiveOn);
else
_progLedOnCallback();
}

void progLedOff()
{
if (_progLedOffCallback == 0)
digitalWrite(ledPin(), HIGH - _ledPinActiveOn);
else
_progLedOffCallback();
}
};

#ifndef KNX_NO_AUTOMATIC_GLOBAL_INSTANCE
Expand Down

0 comments on commit 53425e2

Please sign in to comment.