Please help me understand how to use this library

Hello Arduino Forum,

I am trying to implement DALI (digitally addressable lighting interface) on a project, but am have difficulty understanding how to use a library I found. There wasn’t any readme and the demo program is so basic that I cannot “see” how it works. There is also what I think are shorthand tricks in the example, but not being a “pro” I have no idea what is going on.

Can you guys take a look and help me understand how to access the data being received? I think it has to do with the section below, but I can’t put it together. I looked around in the .cpp and .h files for clues but still couldn’t find anything that “enlightened” me.

void dali2_receiver(Dali *d, uint8_t *data, uint8_t len) {
  Serial.print("RX");
  if(len>=2) Serial.println((int)(data[0]<<8) + data[1]); else  Serial.println((int)data[0]);
}

I know that’s an ISR, but how it works escapes me.

Here is a link to where I found the library: http://blog.perquin.com/blog/arduino-dali-interface/

This example program is below:

/*###########################################################################
DALI Interface Demo

On the Arduino connect pin18 and pin19 together

Dali interface1 transmit on pin18, interface2 receives on pin19

----------------------------------------------------------------------------
Changelog:
2014-02-07 Created & tested on ATMega328 @ 16Mhz
----------------------------------------------------------------------------
		pq_Dali_Demo.ino
		
        This program is free software: you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation, either version 3 of the License, or
        (at your option) any later version.

        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.

        You should have received a copy of the GNU General Public License
        along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
        see http://blog.perquin.com for latest, bugs and info
###########################################################################*/

#include "pq_Dali.h"

//create the DALI interfaces
Dali dali1; 
Dali dali2; 

//callback to handle received data on dali2 interface
void dali2_receiver(Dali *d, uint8_t *data, uint8_t len) {
  Serial.print("RX");
  if(len>=2) Serial.println((int)(data[0]<<8) + data[1]); else  Serial.println((int)data[0]);
}

void setup() {
  Serial.begin(115200);
  Serial.println("DALI Master/Slave Demo");
  
  //start the DALI interfaces
  //arguments: tx_pin, rx_pin (negative values disable transmitter / receiver)
  //Note: the receiver pins should be on different PCINT groups, e.g one 
  //receiver on pin0-7 (PCINT2) one on pin8-13 (PCINT0) and one on pin14-19 (PCINT1)
  dali1.begin(18,-3); 
  dali2.begin(-2,19);
  
  //attach a received data handler
  dali2.EventHandlerReceivedData = dali2_receiver;
}

uint16_t i=0;

void loop() {
  Serial.print("tx");
  Serial.println(i);
  dali1.sendwait_int(i);
  //delay(200);
  i+=1;
}

Any help would be greatly appreciated!

-Tony

You get the data right here:

void dali2_receiver(Dali *d, uint8_t *data, uint8_t len) {
  Serial.print("RX");
  if(len>=2) Serial.println((int)(data[0]<<8) + data[1]); else  Serial.println((int)data[0]);
}

This gets called with a few arguments when data is received. For example, len (the length of the data received) and data, an array of bytes containing the data. In this case they’re writing it to serial, but you could store it in a global variable or do whatever else you wanted with it. You do need to check length, because this is C, and nothing stops you from reading beyond the end of the array (overrun).

DrAzzy, thank you for the response!

I think the idea of "arguments" is where my breakdown is, please let me know if I'm going down the right path here.

Is this telling the library where to put the data?

dali2.EventHandlerReceivedData = dali2_receiver;

I have a few questions about this too...

dali2_receiver(Dali *d, uint8_t *data, uint8_t len)

So I understand that uint8_t is the same as byte, but what are the *(asterisks) doing?

len tells me how long the array in *data is, correct?

Is there enough information to help me understand what "Dali *d" is doing?

Sorry if I seem dense, I normally work with more simple programs...

-Tony

An * (asterisk) is a common symbol in C and C++ programs. Get used to it.

Sometimes it means multiplication, but not here. In the definition of parameters for a function, it means pointer. You read most such definitions in reverse order.

d is a pointer to a Dali object. That is, d contains the address of a Dali object of interest. What is contained in a Dali object and what you can do with it is beyond me since I don't have the Dali library.

Similarly, data is a pointer to a variable of type uint8_t. That is, data contains the address of a variable of interest of type uint8_t.

Sometimes, pointers are just a handy way for a C/C++ programmer to get things done. Other times, pointers are necessary so that the function (in this case, Dali2_receiver) can make changes to the thing pointed to by the pointer.

Most programming in C and C++ is an exercise in understanding pointers. Good Luck!

Vaj4088, thanks for the info. I've heard the term pointer before, but didn't know an asterisk could refer to them or really what they are...guess I've got some home work to do. I have a sneaking suspicion that by the end of this I will have learned something very important...

Is there anything I could look for in the .cpp / .h files that could provide more insight? I know not everyone has these files or wants to inspect them, I mean what could I look for in a very generic sense.

Thanks again!

-Tony

No doubt that pointers are used in .cpp and .h files, but they won't add any insight. You need a tutorial or class in C.
Perhaps you need a tutorial or class in C++ too, but start with C.

Hi,
I’ve also been trying to connect Arduino to DALI with the same code, but don’t know which hardware is needed to connect it do DALI bus.

All,

Excellent DALI Application Note, and interface hardware (FIGUREs 5 &6), in link:

cplusplus.com has tutorials that I generally find to be good. Here is their page on pointers:

http://www.cplusplus.com/doc/tutorial/pointers/

best,
Michael
[edit: fixed typo]

Hi !

Did you reach any success with your project (HW & SW) ?

turbosharp:
Hi !

Did you reach any success with your project (HW & SW) ?

You expect an answer here after 6 months?

If that dali2_receiver function is called from an ISR its broken - you cannot make
Serial calls in an ISR, it will deadlock the system.

Is this telling the library where to put the data?

dali2.EventHandlerReceivedData = dali2_receiver;

Not quite, its installing a handler. A handler is a function you provide that will get called
back in the future when some event happens.