--Next section is for slider control of lights local state5={"OFF","1","2","3","4","5","6","7","8","9","10","11","12","13","14","FULL"} function newHorizSliderWithFeedback(opt) local textFeedback = display.newText{ text = string.format(opt.msg, "?"), x=opt.width / 2, y=opt.top + 30, fontSize=10 } function updateFeedback(desc) textFeedback.text = string.format(opt.msg, desc) end local controller = levelController{ states = opt.states, listener = updateFeedback, xapTarget = opt.xapTarget } local horizontalSlider = widget.newSlider { top=opt.top, left=opt.left, width = opt.width, orientation = "horizontal", listener = controller } group:insert ( horizontalSlider ) group:insert ( textFeedback ) -- Monitor so a change will update the slider local f = xap.Filter() f:add("xap-header","source",opt.xapTarget) f:add("xap-header","class","xAPBSC.event") f:callback(function(frame) local level = tonumber(frame:getValue("output.state.1","level")) horizontalSlider:setValue( level ) -- Synthetic event to update internal state and fire text feedback. controller{value=level, synthetic=true} end ) xap_server:addFilter(f) end function levelController(opt) local previousValue = 0 return function(event) -- A level device can be set to a specific level at its native resolution using -- Level=45 for example (no % sign) or Level= 64/1023 -- We will use discrete values of the range 0-100 -- Quantize the level into the ranges available local idx = 1 --print("Event.phase1 ",event.phase) if event.value > 0 then idx = math.ceil(event.value / (100/#opt.states)) end -- Only if the slider has moved into the next quantile --if previousValue ~= idx then --previousValue = idx opt.listener(opt.states[idx]) local lvl = (idx-1)*(150/(#opt.states-1)) print(string.format("bsc.sendLevel(%s, %s)", opt.xapTarget, lvl)) --print("Event.phase2 ",event.phase) if event.phase == "ended" then if event.synthetic ~= true then bscSend{target=opt.xapTarget, level=lvl} end end --end end end newHorizSliderWithFeedback { top=300, left=0, width = 200, states = state5, msg = "Lounge light 1 at %s", xapTarget="dbzoo.livebox.LightingControl:LoungeLights1" } newHorizSliderWithFeedback { top=330, left=0, width = 200, states = state5, msg = "Lounge light 2 at %s", xapTarget="dbzoo.livebox.LightingControl:LoungeLights2" } -- End of slider control of lights