00001
00002
00003 import pigpio
00004
00005 class hasher:
00006
00007 """
00008 This class forms a hash over the IR pulses generated by an
00009 IR remote.
00010
00011 The remote key press is not converted into a code in the manner of
00012 the lirc module. No attempt is made to decode the type of protocol
00013 used by the remote. The hash is likely to be unique for different
00014 keys and different remotes but this is not guaranteed.
00015
00016 This hashing process works for some remotes/protocols but not for
00017 others. The only way to find out if it works for one or more of
00018 your remotes is to try it and see.
00019
00020 EXAMPLE CODE
00021
00022 #!/usr/bin/env python
00023
00024 import time
00025 import pigpio
00026 import ir_hasher
00027
00028 def callback(hash):
00029 print("hash={}".format(hash));
00030
00031 pi = pigpio.pi()
00032
00033 ir = ir_hasher.hasher(pi, 7, callback, 5)
00034
00035 print("ctrl c to exit");
00036
00037 time.sleep(300)
00038
00039 pi.stop()
00040 """
00041
00042 def __init__(self, pi, gpio, callback, timeout=5):
00043
00044 """
00045 Initialises an IR remote hasher on a pi's gpio. A gap of timeout
00046 milliseconds indicates the end of the remote key press.
00047 """
00048
00049 self.pi = pi
00050 self.gpio = gpio
00051 self.code_timeout = timeout
00052 self.callback = callback
00053
00054 self.in_code = False
00055
00056 pi.set_mode(gpio, pigpio.INPUT)
00057
00058 self.cb = pi.callback(gpio, pigpio.EITHER_EDGE, self._cb)
00059
00060 def _hash(self, old_val, new_val):
00061
00062 if new_val < (old_val * 0.60):
00063 val = 13
00064 elif old_val < (new_val * 0.60):
00065 val = 23
00066 else:
00067 val = 2
00068
00069 self.hash_val = self.hash_val ^ val
00070 self.hash_val *= 16777619
00071 self.hash_val = self.hash_val & ((1<<32)-1)
00072
00073 def _cb(self, gpio, level, tick):
00074
00075 if level != pigpio.TIMEOUT:
00076
00077 if self.in_code == False:
00078
00079 self.in_code = True
00080
00081 self.pi.set_watchdog(self.gpio, self.code_timeout)
00082
00083 self.hash_val = 2166136261
00084
00085 self.edges = 1
00086
00087 self.t1 = None
00088 self.t2 = None
00089 self.t3 = None
00090 self.t4 = tick
00091
00092 else:
00093
00094 self.edges += 1
00095
00096 self.t1 = self.t2
00097 self.t2 = self.t3
00098 self.t3 = self.t4
00099 self.t4 = tick
00100
00101 if self.t1 is not None:
00102
00103 d1 = pigpio.tickDiff(self.t1,self.t2)
00104 d2 = pigpio.tickDiff(self.t3,self.t4)
00105
00106 self._hash(d1, d2)
00107
00108 else:
00109
00110 if self.in_code:
00111
00112 self.in_code = False
00113
00114 self.pi.set_watchdog(self.gpio, 0)
00115
00116 if self.edges > 12:
00117
00118 self.callback(self.hash_val)
00119
00120 if __name__ == "__main__":
00121
00122 import time
00123 import pigpio
00124 import ir_hasher
00125
00126 hashes = {
00127 142650387: '2', 244341844: 'menu', 262513468: 'vol-',
00128 272048826: '5', 345069212: '6', 363685443: 'prev.ch',
00129 434191356: '1', 492745084: 'OK', 549497027: 'mute',
00130 603729091: 'text', 646476378: 'chan-', 832916949: 'home',
00131 923778138: 'power', 938165610: 'power', 953243510: 'forward',
00132 1009731980:'1', 1018231875:'TV', 1142888517:'c-up',
00133 1151589683:'chan+', 1344018636:'OK', 1348032067:'chan+',
00134 1367109971:'prev.ch', 1370712102:'c-left', 1438405361:'rewind',
00135 1452589043:'pause', 1518578730:'chan-', 1554432645:'8',
00136 1583569525:'0', 1629745313:'rewind', 1666513749:'record',
00137 1677653754:'c-down', 1825951717:'c-right', 1852412236:'6',
00138 1894279468:'9', 1904895749:'vol+', 1941947509:'ff',
00139 2076573637:'0', 2104823531:'back', 2141641957:'home',
00140 2160787557:'record', 2398525299:'7', 2468117013:'8',
00141 2476712746:'play', 2574308838:'forward', 2577952149:'4',
00142 2706654902:'stop', 2829002741:'c-up', 2956097083:'back',
00143 3112717386:'5', 3263244773:'ff', 3286088195:'pause',
00144 3363767978:'c-down', 3468076364:'vol-', 3491068358:'stop',
00145 3593710134:'c-left', 3708232515:'3', 3734134565:'back',
00146 3766109107:'TV', 3798010010:'play', 3869937700:'menu',
00147 3872715523:'7', 3885097091:'2', 3895301587:'text',
00148 3931058739:'mute', 3983900853:'c-right', 4032250885:'4',
00149 4041913909:'vol+', 4207017660:'9', 4227138677:'back',
00150 4294027955:'3'}
00151
00152 def callback(hash):
00153 if hash in hashes:
00154 print("key={} hash={}".format(hashes[hash], hash));
00155
00156 pi = pigpio.pi()
00157
00158 ir = ir_hasher.hasher(pi, 7, callback, 5)
00159
00160 print("ctrl c to exit");
00161
00162 time.sleep(300)
00163
00164 pi.stop()
00165