-
Notifications
You must be signed in to change notification settings - Fork 0
/
IR_Command.py
148 lines (124 loc) · 4.25 KB
/
IR_Command.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# USING THE REMOTE CONTROL
# TO TURN AN LED ON AND OFF
import pyb
from pyb import Pin, Timer
import machine
import sys
import gc
class IrRemote:
# Limits for the BIT START
_strbit1 = 8000
_strbit2 = 10000
_strbit3 = 4000
_strbit4 = 5000
def __init__(self,irpin):
self._irpin = irpin
def decode_ir(self):
# the IR remote control signal comes
# sent to PIN_X10
ingresso = Pin(self._irpin, Pin.IN, Pin.PULL_UP)
lettura_tot = []
while True:
read1 = machine.time_pulse_us(ingresso,0)
read2 = machine.time_pulse_us(ingresso,1)
read3 = machine.time_pulse_us(ingresso,0)
read4 = machine.time_pulse_us(ingresso,1)
read5 = machine.time_pulse_us(ingresso,0)
read6 = machine.time_pulse_us(ingresso,1)
if read1 > 0 :
lettura_tot.append(read1)
if read2 > 0 :
lettura_tot.append(read2)
if read3 > 0 :
lettura_tot.append(read3)
if read4 > 0 :
lettura_tot.append(read4)
if read5 > 0 :
lettura_tot.append(read5)
if read6> 0 :
lettura_tot.append(read6)
if len(lettura_tot) > 64 :
return lettura_tot
def trovaflag(self,lista,start,end,start1,end1):
for count in range(len(lista)-1):
b1 = lista[count]
b2 = lista[count+1]
if (start < b1 < end) and (start1 < b2 < end1) :
return count+1
# Converts a number from binary to decimal
def bin2dec(self,binario):
lung = len(binario)
dec = 0
for i in range(lung):
indice = lung - i - 1
cifra = int(binario[indice]) * pow(2, i)
dec = dec + cifra
return dec
def prot_nec(self,lista):
contabit = 0
allbit = []
endbit = False
first = self.trovaflag(lista, IrRemote._strbit1,
IrRemote._strbit2,
IrRemote._strbit3,
IrRemote._strbit4) - 1
last = first + 64
lista = lista[first:last]
# versione protocollo in bit
bitcode = int(((last - first) - 3) / 2)
for count in range(0, len(lista), 2):
b1 = lista[count]
b2 = lista[count + 1]
if (IrRemote._strbit1 < b1 < IrRemote._strbit2) and (IrRemote._strbit3 < b2 < IrRemote._strbit4):
pass
elif (b1 < 700) and (b2 < 700): # BIT 0
allbit.append('0')
contabit += 1
elif (b1 < 700) and (1500 < b2 < 1700): # BIT 1
allbit.append('1')
contabit += 1
elif (b1 < 700) and (30000 < b2): # END BIT
endbit = True
c = allbit[16:24]
c.reverse()
c1 = "".join(c)
return(self.bin2dec(c1))
# Check that the associated remote is the one
# ELEGOO with NEC protocol. In the presence of others
# IR rays do not react
def identify_prot(self,lista):
tmp1 = (self.trovaflag(lista, 8000, 10000, 4300, 4700))
print("Pos. StartBit",tmp1)
#FREE MEMORY
gc.collect()
mem = gc.mem_free()
heap = gc.mem_alloc()
if tmp1 is not None:
if tmp1 > 0:
code = self.prot_nec(lista)
return code
# I create an IrRemote object
# and I pass him the pin it is to
# connected the sensor
irremote = IrRemote("X5")
led = Pin("X1",Pin.OUT)
led.off()
while True:
# I take the list generated by the remote control
rawcode = irremote.decode_ir()
print(rawcode)
print("N° letture",len(rawcode))
# pass the list to the decode function
# code = is the code of the key typed
code = irremote.identify_prot(rawcode)
print("Codice",code)
if code == 94: # key 3 led on
led.on()
print("Led acceso\n")
elif code == 12: # key 1 led off
led.off()
print("Led spento\n")
elif code == 22: # key 0 exit
led.off()
print("Exit from control\n")
sys.exit()