ir_hasher.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import pigpio
4 
5 class hasher:
6 
7  """
8  This class forms a hash over the IR pulses generated by an
9  IR remote.
10 
11  The remote key press is not converted into a code in the manner of
12  the lirc module. No attempt is made to decode the type of protocol
13  used by the remote. The hash is likely to be unique for different
14  keys and different remotes but this is not guaranteed.
15 
16  This hashing process works for some remotes/protocols but not for
17  others. The only way to find out if it works for one or more of
18  your remotes is to try it and see.
19 
20  EXAMPLE CODE
21 
22  #!/usr/bin/env python
23 
24  import time
25  import pigpio
26  import ir_hasher
27 
28  def callback(hash):
29  print("hash={}".format(hash));
30 
31  pi = pigpio.pi()
32 
33  ir = ir_hasher.hasher(pi, 7, callback, 5)
34 
35  print("ctrl c to exit");
36 
37  time.sleep(300)
38 
39  pi.stop()
40  """
41 
42  def __init__(self, pi, gpio, callback, timeout=5):
43 
44  """
45  Initialises an IR remote hasher on a pi's gpio. A gap of timeout
46  milliseconds indicates the end of the remote key press.
47  """
48 
49  self.pi = pi
50  self.gpio = gpio
51  self.code_timeout = timeout
52  self.callback = callback
53 
54  self.in_code = False
55 
56  pi.set_mode(gpio, pigpio.INPUT)
57 
58  self.cb = pi.callback(gpio, pigpio.EITHER_EDGE, self._cb)
59 
60  def _hash(self, old_val, new_val):
61 
62  if new_val < (old_val * 0.60):
63  val = 13
64  elif old_val < (new_val * 0.60):
65  val = 23
66  else:
67  val = 2
68 
69  self.hash_val = self.hash_val ^ val
70  self.hash_val *= 16777619 # FNV_PRIME_32
71  self.hash_val = self.hash_val & ((1<<32)-1)
72 
73  def _cb(self, gpio, level, tick):
74 
75  if level != pigpio.TIMEOUT:
76 
77  if self.in_code == False:
78 
79  self.in_code = True
80 
81  self.pi.set_watchdog(self.gpio, self.code_timeout)
82 
83  self.hash_val = 2166136261 # FNV_BASIS_32
84 
85  self.edges = 1
86 
87  self.t1 = None
88  self.t2 = None
89  self.t3 = None
90  self.t4 = tick
91 
92  else:
93 
94  self.edges += 1
95 
96  self.t1 = self.t2
97  self.t2 = self.t3
98  self.t3 = self.t4
99  self.t4 = tick
100 
101  if self.t1 is not None:
102 
103  d1 = pigpio.tickDiff(self.t1,self.t2)
104  d2 = pigpio.tickDiff(self.t3,self.t4)
105 
106  self._hash(d1, d2)
107 
108  else:
109 
110  if self.in_code:
111 
112  self.in_code = False
113 
114  self.pi.set_watchdog(self.gpio, 0)
115 
116  if self.edges > 12:
117 
118  self.callback(self.hash_val)
119 
120 if __name__ == "__main__":
121 
122  import time
123  import pigpio
124  import ir_hasher
125 
126  hashes = {
127  142650387: '2', 244341844: 'menu', 262513468: 'vol-',
128  272048826: '5', 345069212: '6', 363685443: 'prev.ch',
129  434191356: '1', 492745084: 'OK', 549497027: 'mute',
130  603729091: 'text', 646476378: 'chan-', 832916949: 'home',
131  923778138: 'power', 938165610: 'power', 953243510: 'forward',
132  1009731980:'1', 1018231875:'TV', 1142888517:'c-up',
133  1151589683:'chan+', 1344018636:'OK', 1348032067:'chan+',
134  1367109971:'prev.ch', 1370712102:'c-left', 1438405361:'rewind',
135  1452589043:'pause', 1518578730:'chan-', 1554432645:'8',
136  1583569525:'0', 1629745313:'rewind', 1666513749:'record',
137  1677653754:'c-down', 1825951717:'c-right', 1852412236:'6',
138  1894279468:'9', 1904895749:'vol+', 1941947509:'ff',
139  2076573637:'0', 2104823531:'back', 2141641957:'home',
140  2160787557:'record', 2398525299:'7', 2468117013:'8',
141  2476712746:'play', 2574308838:'forward', 2577952149:'4',
142  2706654902:'stop', 2829002741:'c-up', 2956097083:'back',
143  3112717386:'5', 3263244773:'ff', 3286088195:'pause',
144  3363767978:'c-down', 3468076364:'vol-', 3491068358:'stop',
145  3593710134:'c-left', 3708232515:'3', 3734134565:'back',
146  3766109107:'TV', 3798010010:'play', 3869937700:'menu',
147  3872715523:'7', 3885097091:'2', 3895301587:'text',
148  3931058739:'mute', 3983900853:'c-right', 4032250885:'4',
149  4041913909:'vol+', 4207017660:'9', 4227138677:'back',
150  4294027955:'3'}
151 
152  def callback(hash):
153  if hash in hashes:
154  print("key={} hash={}".format(hashes[hash], hash));
155 
156  pi = pigpio.pi()
157 
158  ir = ir_hasher.hasher(pi, 7, callback, 5)
159 
160  print("ctrl c to exit");
161 
162  time.sleep(300)
163 
164  pi.stop()
165 
def _cb(self, gpio, level, tick)
Definition: ir_hasher.py:73
def tickDiff(t1, t2)
Definition: pigpio.py:911
def _hash(self, old_val, new_val)
Definition: ir_hasher.py:60
def __init__(self, pi, gpio, callback, timeout=5)
Definition: ir_hasher.py:42
def callback(hash)
Definition: ir_hasher.py:152


cob_hand_bridge
Author(s): Mathias Lüdtke
autogenerated on Tue Oct 20 2020 03:35:57