Skip to content

Commit c8c668d

Browse files
committed
add ESP32 RMT IR
1 parent 7713326 commit c8c668d

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# ESP32 RMT IR for MicroPython
2+
# MIT license; Copyright (c) 2022-2024 WEMOS.CC
3+
4+
import esp32
5+
from machine import Pin
6+
import struct
7+
import time
8+
import gc
9+
10+
11+
IR_TYPE=[{'tag':'NEC','start':(9000, 4500),'high':( 560, 1690 ),'low':( 560, 560 ),'end':( 560,0 ) },
12+
{'tag':'SAMSUNG','start':(4500, 4450),'high':( 560, 1600 ),'low':( 560, 560 ),'end':( 8950,0 )},
13+
{'tag':'LG32','start':(4500, 4500),'high':( 500, 1750 ),'low':( 500, 560 ),'end':( 8950,0 )}]
14+
15+
16+
17+
class ESP32_RMT_IR:
18+
19+
def __init__(self,pin):
20+
try:
21+
self.ir = esp32.RMT(0, pin=Pin(pin), clock_div=80, tx_carrier=(38000, 33, 1))#1us, 38khz
22+
except:
23+
print("Error")
24+
self.available=0
25+
else:
26+
self.available=1
27+
28+
def find_type(self,type):
29+
for i, dic in enumerate(IR_TYPE):
30+
if(dic['tag']==type):
31+
return i
32+
return -1
33+
34+
def build_data(self,cmd_data,type):
35+
36+
i=self.find_type(type)
37+
38+
if(i!=-1):
39+
data_START=IR_TYPE[i]['start']
40+
data_HIGH=IR_TYPE[i]['high']
41+
data_LOW=IR_TYPE[i]['low']
42+
data_END=IR_TYPE[i]['end']
43+
44+
tmp=data_START
45+
46+
for b in ('{:032b}'.format(cmd_data)):
47+
if (b=='1'):
48+
tmp+=data_HIGH
49+
50+
else:
51+
tmp+=data_LOW
52+
53+
tmp+=data_END
54+
55+
return tmp
56+
57+
else:
58+
return -1
59+
60+
def send_INT32(self,data,type):
61+
if(self.available):
62+
63+
tmp=self.build_data(data,type)
64+
65+
if(tmp!=-1):
66+
if(self.ir.wait_done()):
67+
self.ir.write_pulses(tmp, 1)
68+
69+
70+
def send_NEC(self,address,cmd):
71+
72+
if(self.available):
73+
tmp=struct.pack(">4b",address,~address,cmd,~cmd)
74+
data=struct.unpack('>i',tmp)[0]
75+
rmt_data=self.send_INT32(data,'NEC')
76+
return rmt_data
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
metadata(description="ESP32 RMT IR.", version="1.0.0")
2+
module("esp32_rmt_ir.py", opt=3)

0 commit comments

Comments
 (0)