-
Notifications
You must be signed in to change notification settings - Fork 5
/
necir-esp.py
87 lines (78 loc) · 2.57 KB
/
necir-esp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# NEC Infrared capture module for MicroPython ESP32 or ESP8266 board
#
# Connect HX1838B receiver to 35
# (That's a 38kHz IR receiver, as shipped on taobao)
#
#
# Created by Zhao Tang 2018-03-13 Initial draft
# Matt Page / tju.tangzhao@gmail.com
# Difference with the version for pyboard(Nano):
# time module (utime module on pyboard)
# pin=35(pyboard can only use the form like: pin=pyb.Pin.board.Y10 and number is not supported)
# Usage:
# def nec_cb(nec, a, c, r)
# print(a, c, r) # Address, Command, Repeat
#
# from necir import NecIr
# nec = NecIr()
# nec.callback(nec_cb)
from machine import Pin
import time
class NecIr:
def __init__(self, pin=35):
self._ic_start = 0
self._ic_last = time.ticks_us()
self._ic_width = 0
self._sr = [0, 0, 0, 0]
self._rst()
self._address = 0
self._command = 0
self._cb = None
self._ic_pin = Pin(pin, Pin.IN)
self._ic_pin.irq(trigger=Pin.IRQ_RISING, handler=self._ic_cb)
self._id = 0
def _rst(self):
self._sr[0] = 0
self._sr[1] = 0
self._sr[2] = 0
self._sr[3] = 0
self._sc = 0
self._sb = 0
def _bit(self, v):
self._sr[self._sb] = (self._sr[self._sb] >> 1) + v
self._sc = self._sc + 1
if (self._sc > 7):
self._sc = 0
self._sb = self._sb + 1
if (self._sb > 3):
if ((self._sr[0] ^ self._sr[1] ^ self._sr[2] ^ self._sr[3]) == 0):
self._address = self._sr[0]
self._command = self._sr[2]
if (self._cb):
self._cb(self, self._address, self._command, False) # Contains the address & command
self._rst()
def _ic_cb(self, pin):
self._ic_start = time.ticks_us()
icw = time.ticks_diff(self._ic_start, self._ic_last)
self._ic_last = self._ic_start
self._ic_width = icw
self._id += 1
if (icw > 5500):
# print('[gap]') # gap in transmission
pass
elif (icw > 4000):
# print('IR start')
self._rst()
elif (icw > 2500): # Repeat command
# print('IR repeat')
if (self._cb):
self._cb(self, self._address, self._command, True)
elif (icw > 1500):
self._bit(0x80) # High bit
# print('High bit')
else:
self._bit(0x00) # Low bit
# print('Low bit')
# print(self._id, self._ic_width)
def callback(self, fn):
self._cb = fn