树莓派-13-红外遥控器HX1838

参考树莓派+HX1838红外遥控器(2020-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 -*-  HX1838
import RPi.GPIO as GPIO
import time

PIN = 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 = 0
        while GPIO.input(PIN) == 0 and count < 200:
            count += 1
            time.sleep(0.00006)
        count = 0

        while GPIO.input(PIN) == 1 and count < 80:
            count += 1
            time.sleep(0.00006)
        
        idx = 0
        cnt = 0
        data = [0,0,0,0]
        for i in range(0,32):
            count = 0
            while GPIO.input(PIN) == 0 and count < 15:
                count += 1
                time.sleep(0.00006)

            count = 0
            while GPIO.input(PIN) == 1 and count < 40:
                count += 1
                time.sleep(0.00006)

            if count > 8:
                data[idx] |= 1<<cnt
            if cnt == 7:
                cnt = 0
                idx += 1
            else:
                cnt += 1

        if 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_key


try:
    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 -*-  HX1838
import RPi.GPIO as GPIO
import time

PIN = 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 = 0
        while GPIO.input(PIN) == 0 and count < 200:
            count += 1
            time.sleep(0.00006)
        count = 0

        while GPIO.input(PIN) == 1 and count < 80:
            count += 1
            time.sleep(0.00006)
        
        idx = 0
        cnt = 0
        data = [0,0,0,0]
        for i in range(0,32):
            count = 0
            while GPIO.input(PIN) == 0 and count < 15:
                count += 1
                time.sleep(0.00006)

            count = 0
            while GPIO.input(PIN) == 1 and count < 40:
                count += 1
                time.sleep(0.00006)

            if count > 8:
                data[idx] |= 1<<cnt
            if cnt == 7:
                cnt = 0
                idx += 1
            else:
                cnt += 1

        if 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_key


try:
    while True:
        
        ir_key=ir_1838()
        if ir_key != "":
            print("检测到遥控按键:  "+ir_key)
        time.sleep(0.01)
except KeyboardInterrupt:
    GPIO.cleanup();
  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皮皮冰燃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值