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_45CEC-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

Google Unveils Updated Nexus 7 in Push Against Apple, Microsoft
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

Source code    
  1. cec = {}
  2.  
  3. cec.init = function()
  4. if not cec.port then
  5. require('serial')
  6. cec.port = serial.open('/dev/ttyAPP0')
  7. cec.port:flush()
  8. end
  9. end
  10.  
  11. cec.send = function(data)
  12. local cmd, res
  13.  
  14. if type(data) ~= 'string' then
  15. return nil, 'invalid data'
  16. elseif #data < 2 then
  17. return nil, 'data too short'
  18. elseif #data > 30 then
  19. return nil, 'data too long'
  20. end
  21.  
  22. cec.init()
  23.  
  24. cmd = string.char(0x00, 0xA0, #data) .. data
  25. cec.port:write(cmd)
  26.  
  27. res = cec.port:read(2, 0.2)
  28. if type(res) == 'string' and #res == 2 and res:byte(1) == 0x5A then
  29. if res:byte(2) == 0x10 then
  30. return true
  31. elseif res:byte(2) == 0x40 then
  32. return nil, 'transmitter busy'
  33. end
  34. end
  35.  
  36. return nil, 'reply error'
  37. end
  38.  
  39. cec.poll = function()
  40. local cmd, len, frame, count
  41.  
  42. cec.init()
  43.  
  44. cmd = string.char(0x00, 0xA0, 0x01, 0xAA)
  45. cec.port:write(cmd)
  46.  
  47. res = cec.port:read(2, 0.2)
  48. if type(res) == 'string' and #res == 2 and res:byte(1) == 0xA5 then
  49. len = res:byte(2)
  50.  
  51. if len == 0 then
  52. return false, 0
  53. end
  54.  
  55. res = cec.port:read(len, 0.2)
  56.  
  57. if type(res) == 'string' and #res == len then
  58. count = res:byte(1) - 1
  59. return res:sub(2), count
  60. end
  61. end
  62.  
  63. return nil, 'reply error'
  64. 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.

cec_example

 

Common commands

  • Send stand-by to all devices
Source code    
  1. cmd = string.char(0xBF, 0x36)
  2. cec.send(cmd)
  • Returns single telegram from buffer and count of telegrams stored (up to 16). Returns false, 0 if buffer is empty
Source code    
  1. cec.poll()