Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Dali.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@
*/

void DaliClass::begin(byte tx_pin, byte rx_pin, bool active_low) {
DaliBus.begin(tx_pin, rx_pin, active_low);
DaliBus.begin(tx_pin, rx_pin, active_low, active_low);
}

void DaliClass::begin(byte tx_pin, byte rx_pin, bool tx_active_low, bool rx_active_low) {
DaliBus.begin(tx_pin, rx_pin, tx_active_low, rx_active_low);
}

void DaliClass::setCallback(EventHandlerReceivedDataFuncPtr callback)
Expand Down
13 changes: 12 additions & 1 deletion src/Dali.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
#ifndef DALI_H
#define DALI_H

#include <arduino.h>
#include "DaliBus.h"
#include "DaliCommands.h"

Expand All @@ -61,6 +60,18 @@ class DaliClass {
* is used by most DALI hardware interfaces. The same logic applies to the rx pin. */
void begin(byte tx_pin, byte rx_pin, bool active_low = true);

/** Start the DALI bus
* @param tx_pin Pin to use for transmission
* @param rx_pin Pin to use for reception. Must support Pin Change Interrupt.
* @param tx_active_low set to false if bus is active when tx is high
* @param rx_active_low set to false if bus is active when rx is high
*
* Initialize the hardware for DALI usage (i.e. set pin modes, timer and interrupts). By default the bus is
* driven active-low, meaning with the µC tx pin being low the DALI bus will be high (idle). For transmission
* the µC pin will be set high, which will pull the DALI voltage low. This behaviour
* is used by most DALI hardware interfaces. The logic can be set separately for the rx pin. */
void begin(byte tx_pin, byte rx_pin, bool tx_active_low, bool rx_active_low);

/** Send a direct arc level command
* @param address destination address
* @param value arc level
Expand Down
32 changes: 25 additions & 7 deletions src/DaliBus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@

#include "DaliBus.h"

#include "TimerInterrupt_Generic.h"

#ifdef DALI_TIMER
#if defined(ARDUINO_ARCH_RP2040)
RPI_PICO_Timer timer2(DALI_TIMER);
// Check for MBED-based core vs Philhower core
#if defined(ARDUINO_ARCH_MBED)
MBED_RPI_PICO_Timer timer2(DALI_TIMER);
#else
RPI_PICO_Timer timer2(DALI_TIMER);
#endif
void __isr __time_critical_func(DaliBus_wrapper_pinchangeISR)() { DaliBus.pinchangeISR(); }
#elif defined(ARDUINO_ARCH_ESP32)
ESP32Timer timer2(DALI_TIMER);
Expand All @@ -42,10 +49,11 @@ void DaliBus_wrapper_pinchangeISR() { DaliBus.pinchangeISR(); }
#endif
#endif

void DaliBusClass::begin(byte tx_pin, byte rx_pin, bool active_low) {
void DaliBusClass::begin(byte tx_pin, byte rx_pin, bool tx_active_low, bool rx_active_low) {
txPin = tx_pin;
rxPin = rx_pin;
activeLow = active_low;
txActiveLow = tx_active_low;
rxActiveLow = rx_active_low;

// init bus state
busState = IDLE;
Expand All @@ -61,10 +69,20 @@ void DaliBusClass::begin(byte tx_pin, byte rx_pin, bool active_low) {

#ifdef DALI_TIMER
#if defined(ARDUINO_ARCH_RP2040)
timer2.attachInterrupt(2398, [](repeating_timer *t) -> bool {
DaliBus.timerISR();
return true;
});
#if defined(ARDUINO_ARCH_MBED) // Arduino MBED core
int ret = timer2.attachInterrupt(2398, [](unsigned int) {
timer2.restartTimer();
DaliBus.timerISR();
});
if (ret < 0) {
Serial.println("Failed to attach timer interrupt");
}
#else // Philhower core
timer2.attachInterrupt(2398, [](repeating_timer *t) -> bool {
DaliBus.timerISR();
return true;
});
#endif
#elif defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
timer2.attachInterrupt(2398, +[](void * timer) -> bool {
DaliBus.timerISR();
Expand Down
19 changes: 9 additions & 10 deletions src/DaliBus.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@

#include "Arduino.h"

#include "TimerInterrupt_Generic.h"

#ifndef DALI_NO_TIMER
#ifndef DALI_TIMER
#warning DALI_TIMER not set; default will be set (0)
Expand Down Expand Up @@ -61,14 +59,14 @@ const unsigned long DALI_TE_MAX = (120 * DALI_TE) / 100; // 500u
#define isDeltaWithinTE(delta) (DALI_TE_MIN <= delta && delta <= DALI_TE_MAX)
#define isDeltaWithin2TE(delta) (2*DALI_TE_MIN <= delta && delta <= 2*DALI_TE_MAX)
#if defined(ARDUINO_ARCH_RP2040)
#define getBusLevel (activeLow ? !gpio_get(rxPin) : gpio_get(rxPin))
#define setBusLevel(level) gpio_put(txPin, (activeLow ? !level : level)); txBusLevel = level;
#define getBusLevel (rxActiveLow ? !gpio_get(rxPin) : gpio_get(rxPin))
#define setBusLevel(level) gpio_put(txPin, (txActiveLow ? !level : level)); txBusLevel = level;
#elif defined(ARDUINO_ARCH_ESP32)
#define getBusLevel (activeLow ? !(DaliBus.fastRead(rxPin)) : DaliBus.fastRead(rxPin))
#define setBusLevel(level) DaliBus.fastWrite(txPin, (activeLow ? !level : level)); txBusLevel = level;
#define getBusLevel (rxActiveLow ? !(DaliBus.fastRead(rxPin)) : DaliBus.fastRead(rxPin))
#define setBusLevel(level) DaliBus.fastWrite(txPin, (txActiveLow ? !level : level)); txBusLevel = level;
#elif defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_STM32)
#define getBusLevel (activeLow ? !digitalRead(rxPin) : digitalRead(rxPin))
#define setBusLevel(level) digitalWrite(txPin, (activeLow ? !level : level)); txBusLevel = level;
#define getBusLevel (rxActiveLow ? !digitalRead(rxPin) : digitalRead(rxPin))
#define setBusLevel(level) digitalWrite(txPin, (txActiveLow ? !level : level)); txBusLevel = level;
#else
#error not supported Hardware
#endif
Expand Down Expand Up @@ -96,7 +94,7 @@ typedef void (*EventHandlerErrorFuncPtr)(daliReturnValue errorCode);

class DaliBusClass {
public:
void begin(byte tx_pin, byte rx_pin, bool active_low = true);
void begin(byte tx_pin, byte rx_pin, bool tx_active_low = true, bool rx_active_low = true);
daliReturnValue sendRaw(const byte * message, uint8_t bits);

int getLastResponse();
Expand Down Expand Up @@ -130,7 +128,8 @@ class DaliBusClass {

protected:
byte txPin, rxPin;
bool activeLow;
bool txActiveLow;
bool rxActiveLow;
byte txMessage[4];
uint8_t txLength;

Expand Down