参考树莓派+HX1838红外遥控器(-09-12)
参考红外协议之NEC协议
参考hx1838 红外遥控(1):接收时序的解码
1 树莓派管脚
1 红外遥控器和接收头
1.1 红外遥控器
1.2 1838红外接收头
1838红外接收头,是一种用于接收信号的电器元件。
下图为红外遥控系统(主要分为调制、发射和接收三部分)。
红外接收电路通常被厂家集成在一个元件中,成为一体化红外接收头。如下图所示。
3 编程
nohup jupyter notebook &ps -aux | grep jupyter
3.1 原始代码
#!/usr/bin/python# -*- coding:utf-8 -*- HX1838import RPi.GPIO as GPIOimport timePIN = 14;GPIO.setmode(GPIO.BCM)GPIO.setup(PIN,GPIO.IN,GPIO.PUD_UP)print("等待中,请按下遥控器按钮...")def ir_1838():ir_key=""if GPIO.input(PIN) == 0:count = 0while GPIO.input(PIN) == 0 and count < 200:count += 1time.sleep(0.00006)count = 0while GPIO.input(PIN) == 1 and count < 80:count += 1time.sleep(0.00006)idx = 0cnt = 0data = [0,0,0,0]for i in range(0,32):count = 0while GPIO.input(PIN) == 0 and count < 15:count += 1time.sleep(0.00006)count = 0while GPIO.input(PIN) == 1 and count < 40:count += 1time.sleep(0.00006)if count > 8:data[idx] |= 1<<cntif cnt == 7:cnt = 0idx += 1else:cnt += 1if data[0]+data[1] == 0xFF and data[2]+data[3] == 0xFF:#print("Get the key: 0x%02x" %data[2])if (data[2]==0x45):ir_key="1"elif(data[2]==0x46):ir_key="2"elif(data[2]==0x47):ir_key="3"elif(data[2]==0x44):ir_key="4"elif(data[2]==0x40):ir_key="5"elif(data[2]==0x43):ir_key="6"elif(data[2]==0x07):ir_key="7"elif(data[2]==0x15):ir_key="8"elif(data[2]==0x09):ir_key="9"elif(data[2]==0x16):ir_key="*"elif(data[2]==0x19):ir_key="0"elif(data[2]==0x0d):ir_key="#"elif(data[2]==0x18):ir_key="上"elif(data[2]==0x52):ir_key="下"elif(data[2]==0x08):ir_key="左"elif(data[2]==0x5a):ir_key="右"elif(data[2]==0x1c):ir_key="OK"#print("检测到按键: "+ir_key)return ir_keytry:while True:ir_key=ir_1838()if ir_key != "":print("检测到遥控按键: "+ir_key)time.sleep(0.01)except KeyboardInterrupt:GPIO.cleanup();
3.2 优化代码
#!/usr/bin/python# -*- coding:utf-8 -*- HX1838import RPi.GPIO as GPIOimport timePIN = 14;GPIO.setmode(GPIO.BCM)GPIO.setup(PIN,GPIO.IN,GPIO.PUD_UP)print("等待中,请按下遥控器按钮...")def ir_1838():ir_key=""if GPIO.input(PIN) == 0:count = 0while GPIO.input(PIN) == 0 and count < 200:count += 1time.sleep(0.00006)count = 0while GPIO.input(PIN) == 1 and count < 80:count += 1time.sleep(0.00006)idx = 0cnt = 0data = [0,0,0,0]for i in range(0,32):count = 0while GPIO.input(PIN) == 0 and count < 15:count += 1time.sleep(0.00006)count = 0while GPIO.input(PIN) == 1 and count < 40:count += 1time.sleep(0.00006)if count > 8:data[idx] |= 1<<cntif cnt == 7:cnt = 0idx += 1else:cnt += 1if data[0]+data[1] == 0xFF and data[2]+data[3] == 0xFF:#print("Get the key: 0x%02x" %data[2])if (data[2]==0x45):ir_key="1"elif(data[2]==0x46):ir_key="2"elif(data[2]==0x47):ir_key="3"elif(data[2]==0x44):ir_key="4"elif(data[2]==0x40):ir_key="5"elif(data[2]==0x43):ir_key="6"elif(data[2]==0x07):ir_key="7"elif(data[2]==0x15):ir_key="8"elif(data[2]==0x09):ir_key="9"elif(data[2]==0x16):ir_key="*"elif(data[2]==0x19):ir_key="0"elif(data[2]==0x0d):ir_key="#"elif(data[2]==0x18):ir_key="上"elif(data[2]==0x52):ir_key="下"elif(data[2]==0x08):ir_key="左"elif(data[2]==0x5a):ir_key="右"elif(data[2]==0x1c):ir_key="OK"#print("检测到按键: "+ir_key)return ir_keytry:while True:ir_key=ir_1838()if ir_key != "":print("检测到遥控按键: "+ir_key)time.sleep(0.01)except KeyboardInterrupt:GPIO.cleanup();