Code: Select all
//Codemercs used a Arduino/Genuino 101 !
#include <Wire.h>
//Defines: DALI commands (Datasheet page 4)
#define DALI_OFF 0x00
#define DALI_UP 0x01
#define DALI_DOWN 0x02
#define DALI_STEP_UP 0x03
#define DALI_STEP_DOWN 0x04
#define DALI_MAX 0x05
#define DALI_MIN 0x06
#define DALI_STEP_DOWN_OFF 0x07
#define DALI_ON_STEP_UP 0x08
#define DALI_SCENE 0x10 //0x10 - 0x1F
//Defines: I2C register (Datasheet page 3)
#define REG_STATUS 0x00
#define REG_CMD 0x01
#define REG_CONFIG 0x02
#define REG_I2CADR 0x03
//Defines: DALI constants
#define DALI_MODE_DACP 0x00 //dimm values
#define DALI_MODE_COMMAND 0x01 //commands (MAX, MIN, OFF, etc.)
#define DALI_MAX_SHORT 0x3F //0...63 -> 64 devices
#define DALI_MAX_GROUP 0x0F //0...15 -> 16 groups
#define DALI_MAX_SCENE 0x0F //0...15 -> 16 scenes
//Defines: dafauls I2C address
#define I2C_ADDR 0x20
//Some buttons to execute events
const int btn1 = 12;
const int btn2 = 13;
void setup()
{
pinMode(btn1, INPUT);
pinMode(btn2, INPUT);
Wire.begin();
}
void loop()
{
//Use an input from arduino as button
if(digitalRead(btn1) == HIGH)
{
SendCommand(0, DALI_MAX);
}
//Use an input from arduino as button
if(digitalRead(btn2) == HIGH)
{
SendCommand(0, DALI_MIN);
}
delay(100); //Slow down
}
//mode -> 0 = dacp, 1 = command
byte GetShortAddress(int dali_adr, int mode)
{
//DALI address format: 0 AAA AAA S
return ((0x00 & ((dali_adr & DALI_MAX_SHORT) << 1)) | mode);
}
//mode -> 0 = dacp, 1 = command
byte GetGroupAddress(int dali_grp, int mode)
{
//DALI address format: 1 AAA AAA S
return ((0x80 & ((dali_grp & DALI_MAX_GROUP) << 1)) | mode);
}
//mode -> 0 = dacp, 1 = command
byte GetBroadcastAddress(int mode)
{
//DALI address format: 1 111 111 S
return (0x80 | 0x7E | mode);
}
void SendCommand(int dali_adr, int value)
{
//DALI address format: Y AAA AAA S
byte adr = GetShortAddress(dali_adr, DALI_MODE_COMMAND);
Wire.beginTransmission(I2C_ADDR);
Wire.write(REG_CMD); //Register: command
Wire.write(adr); //DALI address
Wire.write((byte)value); //Value
Wire.endTransmission();
}
void SendDACP(int dali_adr, int value)
{
//DALI address format: Y AAA AAA S
byte adr = GetShortAddress(dali_adr, DALI_MODE_DACP);
Wire.beginTransmission(I2C_ADDR);
Wire.write(REG_CMD); //Register: command
Wire.write(adr); //DALI address
Wire.write((byte)(value & 0xFE)); //Value 0...254
Wire.endTransmission();
}