Advertisement
Javi

IoT: Pronto to broadlink

Nov 24th, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. #Pronto IR Code to broadlink_cli
  2. https://gist.github.com/appden/42d5272bf128125b019c45bc2ed3311f
  3. https://repl.it/repls/UsedRobustTranslation
  4.  
  5. import binascii
  6. import struct
  7.  
  8. def pronto2lirc(pronto):
  9. codes = [long(binascii.hexlify(pronto[i:i+2]), 16) for i in xrange(0, len(pronto), 2)]
  10.  
  11. if codes[0]:
  12. raise ValueError('Pronto code should start with 0000')
  13. if len(codes) != 4 + 2 * (codes[2] + codes[3]):
  14. raise ValueError('Number of pulse widths does not match the preamble')
  15.  
  16. frequency = 1 / (codes[1] * 0.241246)
  17. return [int(round(code / frequency)) for code in codes[4:]]
  18.  
  19. def lirc2broadlink(pulses):
  20. array = bytearray()
  21.  
  22. for pulse in pulses:
  23. pulse = pulse * 269 / 8192 # 32.84ms units
  24.  
  25. if pulse < 256:
  26. array += bytearray(struct.pack('>B', pulse)) # big endian (1-byte)
  27. else:
  28. array += bytearray([0x00]) # indicate next number is 2-bytes
  29. array += bytearray(struct.pack('>H', pulse)) # big endian (2-bytes)
  30.  
  31. packet = bytearray([0x26, 0x00]) # 0x26 = IR, 0x00 = no repeats
  32. packet += bytearray(struct.pack('<H', len(array))) # little endian byte count
  33. packet += array
  34. packet += bytearray([0x0d, 0x05]) # IR terminator
  35.  
  36. # Add 0s to make ultimate packet size a multiple of 16 for 128-bit AES encryption.
  37. remainder = (len(packet) + 4) % 16 # rm.send_data() adds 4-byte header (02 00 00 00)
  38. if remainder:
  39. packet += bytearray(16 - remainder)
  40.  
  41. return packet
  42.  
  43. if __name__ == '__main__':
  44. import sys
  45.  
  46.  
  47. pronto = bytearray.fromhex('0000 006d 0026 0000 0155 00aa 0016 0015 0016 0015 0016 0040 0016 0015 0016 0015 0016 0014 0016 0015 0016 0015 0016 0040 0016 0040 0016 0015 0016 0040 0016 0040 0016 0040 0016 0040 0016 0040 0016 0015 0016 0015 0016 0040 0016 0015 0016 0015 0016 0014 0016 0040 0016 0040 0016 0040 0016 0040 0016 0015 0016 0040 0016 0040 0016 0040 0016 0014 0016 0015 0016 060b 0155 0055 0016 0e58 0155 0055 0016 00aa')
  48. pulses = pronto2lirc(pronto)
  49. packet = lirc2broadlink(pulses)
  50.  
  51. print
  52. print binascii.hexlify(packet)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement