CANx-DALI library

CANx-DALI scripting library #

Load the library:

local canxdali = require('applibs.canxdali')

Library functions #

sendcmds(req) #

Sends single or multiple DALI commands to the given gateway. Returns number of bytes sent or nil plus error message. This is completely asynchronous function, it adds commands to gateway queue without waiting for returned results.

req table:

  • lineid - gateway line ID (number, required)
  • nodeid - gateway node ID (number, required)

command table:

  • cmd - command name (string, required)
  • value - command value (number, required for commands with a value)
  • address - DALI address (string or number, required)
  • addrtype - address type (string, required if address is a number)

Address can be a string with the following format: s0..s63 - short address, from 0 to 63 g0..g15 - group, from 0 to 15 b - broadcast

If address is a number then addrtype is required, it can be either:

  • short
  • group
  • broadcast

syncsendcmds(req) #

Similar to canxdali.sendcmds but waits for each command to complete. On success returns Lua table with each command result, nil plus error message otherwise.

sendqueries(req) #

Similar to canxdali.syncsendcmds but checks for each command result. Returns a table of values only for query type commands when all commands were successful. Useful for querying DALI device statuses.

sethandler(type, fn) #

Sets a callback to execute on a specific event. Callback is executed for each command inside data frame separately.

type - event type (string, required):

  • bus - all commands coming from bus side
  • busdata - only "bus data" type commands (from other master devices)
  • all - all commands coming to/from bus

fn - function to execute, or nil to remove callback (function or nil, required)

step() #

Waits for a frame or timeout, whichever happens first. Returns frame or nil plus error message on timeout. Frame can contain multiple commands when sent to bus.

Examples #

Send single command #

Event script that sends arc with value 0 to DALI short address 15 using gateway 0.1.

value = event.getvalue()
value = math.round(value * 2.54) -- 0..100 to 0..254

canxdali = require('applibs.canxdali')

canxdali.sendcmds({
  lineid = 0,
  nodeid = 1,
  cmd = 'arc',
  address = 's15',
  value = value,
})

Send multiple commands #

Event script that sends multiple arc commands using gateway 1.42.

canxdali = require('applibs.canxdali')

canxdali.sendcmds({
  lineid = 1,
  nodeid = 42,
  cmds = {
    { cmd = 'arc', address = 's0', value = 50 },
    { cmd = 'arc', address = 's4', value = 10 },
  }
})

Query ballast status #

Script that checks short address 0 status using gateway 0.1 and logs the results.

canxdali = require('applibs.canxdali')

res, err = canxdali.sendqueries({
  lineid = 0,
  nodeid = 1,
  cmd = 'querystatus',
  address = 's0',
})

bits = {
  'control gear failure',
  'lamp failure',
  'lamp on',
  'limit error',
  'fade running',
  'reset state',
  'missing short address',
  'power cycle seen',
}

if res then
  state = {}

  for i, text in ipairs(bits) do
    mask = bit.lshift(1, i - 1)
    if bit.band(mask, res) ~= 0 then
      state[ #state + 1 ] = text
    end
  end

  log(state)
else
  log('query error', err)
end

Query DALI2 device stauts #

Script that checks DALI2 short address 0, instance 0 status using gateway 0.1 and logs the results.

canxdali = require('applibs.canxdali')

res, err = canxdali.sendqueries({
  lineid = 0,
  nodeid = 1,
  cmd = 'dali2',
  dali2cmd = 'queryinstancestatus',
  address = 's0',
  instance = 0,
})

if res then
  if bit.band(res, 2) == 0 then
    log('instance disabled')
  elseif bit.band(res, 1) ~= 0 then
    log('instance error')
  else
    log('instance ok')
  end
else
  log('query error', err)
end

Bus monitoring #

Resident script with 0 sleep time.

if not canxdali then
  function callback(frame)
    log(frame)
  end

  canxdali = require('applibs.canxdali')
  canxdali.sethandler('bus', callback)
end

canxdali.step()

DALI command list #

Command Description Address Reply Value range
arc direct arc power control ✓ 0..254
off turn off without fading ✓
up dim up using the selected fade rate ✓
down dim down using the selected fade rate ✓
stepup step up ✓
stepdown step down ✓
recallmax recall max level ✓
recallmin recall min level ✓
stepdownoff step down and off ✓
stepupon on and step up ✓
gotolastactlevel go to last active level (DALI2) ✓
gotoscene go to scene ✓ 0..15
reset reset ✓
storeactual store actual level in the dtr ✓
storemax store the dtr as max level ✓
storemin store the dtr as min level ✓
storesystemfailure store the dtr as system failure level ✓
storepoweron store the dtr as power on level ✓
storefadetime store the dtr as fade time ✓
storefaderate store the dtr as fade rate ✓
storescene store the dtr as scene ✓ 0..15
removescene remove from scene ✓ 0..15
addtogroup add to group ✓ 0..15
removefromgroup remove from group ✓ 0..15
storeshortaddress store dtr as short address ✓
querystatus query status ✓ ✓
queryballast query ballast ✓ ✓
querylampfailure query lamp failure ✓ ✓
querylamppoweron query lamp power on ✓ ✓
querylimiterror query limit error ✓ ✓
queryresetstate query reset state ✓ ✓
querymissingshort query missing short address ✓ ✓
queryversion query version number ✓ ✓
querydtr query content of dtr 0 ✓ ✓
querydtr0 alias of querydtr ✓ ✓
querydtr1 query content of dtr 1 ✓ ✓
querydtr2 query content of dtr 2 ✓ ✓
queryoperatingmode query operating mode ✓ ✓
querysourcetype query light source type ✓ ✓
querydevicetype query device type ✓ ✓
queryphysicalmin query physical minimum level ✓ ✓
querypowerfailure query power failure ✓ ✓
queryactual query actual level ✓ ✓
querymax query max level ✓ ✓
querymin query min level ✓ ✓
querypoweron query power on level ✓ ✓
querysystemfailure query system failure level ✓ ✓
queryfadetimerate query fade time / fade rate ✓ ✓
queryscene query scene level (scenes 0-15) ✓ ✓ 0..15
querygroupslow query groups 0-7 ✓ ✓
querygroupshigh query groups 8-15 ✓ ✓
queryrandomaddrh query random address (high) ✓ ✓
queryrandomaddrm query random address (middle) ✓ ✓
queryrandomaddrl query random address (low) ✓ ✓
terminate terminate
setdtr set data transfer register 0 (dtr0) 0..255
setdtr0 alias of setdtr 0..255
setdtr1 set data transfer register 1 (dtr1) 0..255
setdtr2 set data transfer register 2 (dtr2) 0..255
initialise initialise
randomise randomise
compare compare ✓
withdraw withdraw
ping indicate master device presence
searchaddrh set search address (high) 0..255
searchaddrm set search address (middle) 0..255
searchaddrl set search address (low) 0..255
programshortaddr program short address 0..63
verifyshortaddr verify short address ✓ 0..63
queryshortaddr query short address ✓
physicalselection physical selection
enabledevicetype enable device type x 0..255
setx set X color coordinate ✓
sety set Y color coordinate ✓
activate activate current color ✓
stepupx step up X color coordinate ✓
stepdownx step down X color coordinate ✓
stepupy step up Y color coordinate ✓
stepdowny step down Y color coordinate ✓
setcolortemp set color temperature ✓
colortempcooler color temperature cooler ✓
colortempwarmer color temperature warmer ✓
setprimarydimlevel set primary dim level ✓
setrgbdimlevel set RGB dimlevel ✓
setwafdimlevel set WAF dimlevel ✓
setrgbwafcontrol set RGBWAF control ✓
copyreport copy report ✓
storetyprimary store TY primary ✓
storexyprimary store XY primary ✓
storecolortemplimit store color temperature limit ✓
storegearfeatures store gear features ✓
assigncolortolinked assign color to linked ✓
startautocalibration start auto calibration ✓
querygearfeatures query gear features ✓ ✓
querycolorstatus query color status ✓ ✓
querycolorfeatures query color features ✓ ✓
querycolorvalue query color value ✓ ✓
queryrgbwafcontrol query RGBWAF control ✓ ✓
queryassignedcolor query assigned color ✓ ✓
querycontrolgearfailure query control gear failure ✓ ✓
querygeartype query gear type ✓ ✓
querydimmingcurve query dimming curve ✓ ✓
querypossibleoperatingmodes query possible operating modes ✓ ✓
queryfeatures query features ✓ ✓
queryfailurestatus query failure status ✓ ✓
queryshortcircuit query short circuit ✓ ✓
queryopencircuit query open circuit ✓ ✓
queryloaddecrease query load decrease ✓ ✓
queryloadincrease query load increase ✓ ✓
querycurrentprotectoractive query current protector active ✓ ✓
querythermalshutdown query thermal shutdown ✓ ✓
querythermaloverload query thermal overload ✓ ✓
savepersistentvariables save persistent variables ✓
setoperatingmode set operating mode ✓
resetmemorybank reset memory bank ✓
identify stats device identification for 10 s (DALI2) ✓
querymanufacturermode query manufacturer mode (DALI2) ✓ ✓
querynextdevicetype query next device type (DALI2) ✓ ✓
queryextendedfadetime query extended fade time (DALI2) ✓ ✓
querycontrolgearfailure query control gear failure (DALI2) ✓ ✓