Control TV via CEC HDMI from LM4
Example: Control TV via CEC HDMI from LogicMachine4
Task
Consumer Electronics Control (CEC) is an HDMI feature designed to allow the user to command and control up-to 15 CEC-enabled devices, that are connected through HDMI, by using only one of their remote controls (for example by controlling a television set, set-top box, and DVD player using only the remote control of the TV. In comparison with InfraRed, it is possible, for example, to switch off/on the power of TV.
As HDMI has limited cable length, we have developed additional CEC/HDMI adapter which is located near the CEC-compatible control device like TV and wire is put to LogicMachine4 (not limited in length). CEC adapter is available by request when purchasing LM4.
CEC compatibility
The most easiest and quickest way to check if your device is CEC compatible is to use Google Chromecast device.
CEC function
Add the following function in Scripting –> Common Functions
- cec = {}
-
- cec.init = function()
- if not cec.port then
- require('serial')
- cec.port = serial.open('/dev/ttyAPP0')
- cec.port:flush()
- end
- end
-
- cec.send = function(data)
- local cmd, res
-
- if type(data) ~= 'string' then
- return nil, 'invalid data'
- elseif #data < 2 then
- return nil, 'data too short'
- elseif #data > 30 then
- return nil, 'data too long'
- end
-
- cec.init()
-
- cmd = string.char(0x00, 0xA0, #data) .. data
- cec.port:write(cmd)
-
- res = cec.port:read(2, 0.2)
- if type(res) == 'string' and #res == 2 and res:byte(1) == 0x5A then
- if res:byte(2) == 0x10 then
- return true
- elseif res:byte(2) == 0x40 then
- return nil, 'transmitter busy'
- end
- end
-
- return nil, 'reply error'
- end
-
- cec.poll = function()
- local cmd, len, frame, count
-
- cec.init()
-
- cmd = string.char(0x00, 0xA0, 0x01, 0xAA)
- cec.port:write(cmd)
-
- res = cec.port:read(2, 0.2)
- if type(res) == 'string' and #res == 2 and res:byte(1) == 0xA5 then
- len = res:byte(2)
-
- if len == 0 then
- return false, 0
- end
-
- res = cec.port:read(len, 0.2)
-
- if type(res) == 'string' and #res == len then
- count = res:byte(1) - 1
- return res:sub(2), count
- end
- end
-
- return nil, 'reply error'
- end
CEC command generator
CEC command generator is available here.
As Source choose TV. The ID which you get, use in as shown in the example below in form 0xID_nr.
Common commands
- Send stand-by to all devices
- cmd = string.char(0xBF, 0x36)
- cec.send(cmd)
- Returns single telegram from buffer and count of telegrams stored (up to 16). Returns false, 0 if buffer is empty
- cec.poll()