美文网首页
【树莓派+arduino实验记录8 】红外遥控器

【树莓派+arduino实验记录8 】红外遥控器

作者: Geekero | 来源:发表于2020-08-11 12:36 被阅读0次

一、Arduino

/***************************************
 * name:Infrared-Receiver
 * function:press the Power key of a remote control,
 * and both the LED attached and that connected to pin 13 on the Arduino Uno board will light up
 * Then press any other key, and the LEDs will go out.
 **************************************/
 # include <IRremote.h>
 const int irReceiverPin = 7;
 const int ledPin = 13;
 IRrecv irrecv(irReceiverPin); //Creates a variable of type IRecv
 decode_results results;

 void setup()
 {
  pinMode(ledPin,OUTPUT);
  Serial.begin(9600); //initialize serial
  irrecv.enableIRIn(); //enable ir receiver module
 }

 void loop()
 {
  if (irrecv.decode(&results)) //if the ir receiver module receive data
  {
    Serial.print("irCode: ");
    Serial.print(results.value, HEX); //print the value in hexdeimal
    Serial.print(", bits: ");
    Serial.print(results.bits); //print the bits
    irrecv.resume();//Receive the next value
  }
  
  delay(600); //600ms
  
  if (results.value == 0xFFA25D)
  {
    digitalWrite(ledPin, HIGH); //turn on the led
  }
  else
  {
    digitalWrite(ledPin, LOW); //turn off the led
  }
  
 }

二、树莓派

2.1 树莓派环境配置

sudo su -c "grep '^deb' /etc/apt/sources.list |sed 's/^deb/deb-src/g' >/etc/apt/sources.list.d/deb-src.list"
sudo apt update
sudo apt install devscripts
sudo apt install dh-exec doxygen expect libasound2-dev libftdi1-dev libsystemd-dev libudev-dev libusb-1.0-0-dev libusb-dev man2html-base portaudio19-dev socat xsltproc python3-yaml
mkdir build
cd build
apt source lirc
wget https://raw.githubusercontent.com/neuralassembly/raspi/master/lirc-gpio-ir-0.10.patch
patch -p0 -i lirc-gpio-ir-0.10.patch
cd lirc-0.10.1
debuild -uc -us -b
cd ..

这里会失败报错

sudo apt install ./liblirc0_0.10.1-5.2_armhf.deb ./liblircclient0_0.10.1-5.2_armhf.deb ./lirc_0.10.1-5.2_armhf.deb 

需要配置:/boot/config.txt和lirc_options.conf

sudo vi  /boot/config.txt

然后在文件末尾添加如下:

dtoverlay=gpio-ir,gpio_pin=23  #红外输入口
dtoverlay=gpio-ir-tx,gpio_pin=22  #红外输出口

编辑/etc/lirc/lirc_options.conf

sudo cp /etc/lirc/lirc_options.conf.dist /etc/lirc/lirc_options.conf 

通过XFTP将lircd.conf文件复制到到pi目录

sudo cp lircd.conf /etc/lirc/lircd.conf

lircd.conf的代码:

# Please make this file available to others
# by sending it to <lirc@bartelmus.de>
#
# this config file was automatically generated
# using lirc-0.9.0-pre1(default) on Mon Jan  4 07:04:19 2016
#
# contributed by 
#
# brand:                       ./lircd.conf
# model no. of remote control: 
# devices being controlled by this remote:
#

begin remote

  name  ./lircd.conf
  flags RAW_CODES
  eps            30
  aeps          100

  gap          96776

      begin raw_codes

          name KEY_CHANNELDOWN
             8967    4540     604     591     569     592
              567     593     569     591     567     592
              566     594     567     600     560     594
              567    1652     597    1652     601    1650
              588    1672     579    1664     597    1654
              588    1664     599    1653     587    1664
              598     594     583    1634     596     599
              565     595     563     597     563    1672
              570     605     565     596     563    1655
              593     600     560    1657     587    1672
              591    1654     585     606     559    1658
              587   39914    8971    2249     592

          name KEY_CHANNEL
             8907    4604     523     639     520     640
              520     640     521     640     520     639
              521     640     519     640     520     639
              521    1728     524    1735     577    1668
              584    1666     582    1671     522    1728
              584    1668     523    1728     584     578
              520    1727     587    1666     584     579
              520     639     521     639     521    1727
              584     578     521    1728     583     579
              526     635     520    1727     524    1727
              584    1667     584     579     520    1728
              584   39927    8965    2252     522

          name KEY_CHANNELUP
             8964    4539     587     577     520     640
              521     639     520     640     520     640
              520     639     521     638     522     652
              507    1729     582    1679     513    1725
              585    1668     522    1728     585    1666
              584    1667     546    1704     585    1666
              523    1728     523    1726     587     607
              490     669     491     644     515    1728
              523     638     544     616     521     640
              519     639     521    1728     584    1666
              522    1727     524     639     521    1728
              522   39974    8969    2249     524

          name KEY_PREVIOUS
             8905    4601     523     641     519     640
              520     640     520     651     508     640
              520     640     520     640     520     640
              519    1727     585    1665     585    1666
              585    1672     578    1668     583    1667
              522    1730     521    1728     522     640
              520     640     520    1727     524     639
              520     640     519     640     519    1728
              523     670     490    1728     583    1667
              522     639     520    1728     523    1727
              523    1726     586     577     520    1727
              586   39903    8968    2251     588

          name KEY_NEXT
             8964    4540     549     644     517     644
              517     643     516     616     544     644
              516     644     515     644     521     640
              516    1703     586    1666     585    1665
              586    1667     549    1703     585    1672
              579    1666     586    1667     585     607
              518     643     516     643     516     644
              515     616     546     615     545    1704
              523     669     517    1702     547    1704
              551    1702     548    1705     548    1702
              583    1668     586     613     510    1703
              586   39932    8935    2287     555

          name KEY_PLAYPAUSE
             8966    4520     586     607     569     590
              571     588     568     590     568     591
              568     591     568     591     569     590
              567    1648     587    1670     578    1666
              584    1666     582    1665     587    1664
              601    1650     583    1665     583    1666
              585    1665     585     608     564     594
              565     593     565     594     567    1650
              585     606     567     592     565     594
              569    1647     583    1666     586    1663
              585    1664     586     605     570    1649
              584   39896    8977    2232     594

          name KEY_VOLUMEUP
             8889    4608     521     643     515     643
              518     644     515     643     516     642
              519     642     517     643     517     643
              516    1734     518    1732     608    1645
              518    1740     511    1733     518    1733
              519    1734     608    1644     608    1643
              518     662     588    1645     608     553
              516    1735     517     642     517     643
              518     643     517     644     516    1734
              608     553     516    1734     519     644
              613    1636     609    1664     498    1734
              518   40014    8997    2228     610

          name KEY_VOLUMEDOWN
             8977    4517     549     642     575     586
              573     586     574     584     574     585
              574     586     573     585     574     586
              573    1645     548    1699     553    1696
              553    1697     608    1645     554    1694
              552    1698     552    1699     551    1697
              553    1698     606    1644     607     586
              573     585     575     584     574     586
              573     587     573     586     573     586
              573     585     574    1644     605    1644
              605    1645     605    1644     552    1697
              607   39879    8934    2281     609

          name KEY_EQUAL
             8960    4542     608     588     571     589
              579     580     571     589     574     586
              573     588     572     588     573     587
              572    1644     580    1671     582    1670
              580    1670     582    1668     583    1666
              609    1645     605    1645     580    1669
              584     609     574     587     573    1652
              598     587     573     587     572     589
              572     587     572     587     573    1644
              606    1643     581     612     575    1645
              605    1644     582    1668     587    1664
              582   39901    8996    2230     606

          name KEY_NUMERIC_0
             8974    4534     588     605     560     600
              568     591     587     573     562     599
              559     600     559     602     563     597
              556    1660     586    1665     586    1663
              590    1661     590    1661     594    1658
              593    1656     587    1663     587     605
              563    1655     592    1665     589     597
              560    1657     587     607     561     598
              559     601     559    1676     574     600
              560     599     562    1655     595     598
              561    1656     587    1663     599    1651
              596   39902    8964    2240     595

          name KEY_NUMERIC_1
             8922    4595     585     579     520     640
              519     641     519     640     521     641
              520     640     519     640     521     640
              518    1728     584    1667     524    1728
              522    1727     584    1668     522    1728
              523    1727     524    1726     584     581
              519     640     520    1727     523    1728
              522     641     520     640     520     641
              519     640     520    1727     524    1726
              584     580     519     641     520    1726
              524    1728     522    1728     582    1668
              588   39890    8974    2253     521

          name KEY_NUMERIC_2
             8902    4600     584     580     519     647
              513     641     519     641     519     640
              520     640     518     641     520     640
              518    1730     522    1729     580    1668
              582    1669     521    1729     583    1673
              577    1666     583    1668     583     580
              519     641     519     640     519    1728
              582    1669     522     641     519     640
              519     641     519    1727     583    1673
              576    1668     522     640     519     639
              520    1726     524    1732     577    1668
              583   39905    8960    2253     521

          name KEY_NUMERIC_3
             8922    4599     584     581     519     642
              519     641     519     641     520     640
              519     641     519     641     520     641
              520    1728     522    1727     584    1668
              582    1669     522    1730     582    1669
              527    1722     524    1727     524     641
              519    1728     523    1728     587    1665
              582    1669     582     580     520    1728
              523     641     520    1733     578     580
              519     641     520     641     520     641
              519    1728     523     640     520    1728
              582   39890    8919    2311     524

          name KEY_NUMERIC_4
             8902    4600     583     579     520     640
              519     641     518     641     518     641
              520     639     519     639     520     640
              518    1727     585    1668     521    1728
              521    1729     582    1676     515    1727
              582    1669     520    1729     523     640
              519     641     525     635     517    1729
              581     580     520     640     518     642
              518     641     518    1726     583    1669
              521    1729     520     642     518    1727
              581    1676     576    1667     582    1668
              582   39890    8966    2250     582

          name KEY_NUMERIC_5
             8967    4539     584     580     518     641
              518     641     518     641     518     641
              518     641     518     649     511     641
              519    1727     582    1667     583    1668
              581    1677     512    1728     520    1727
              582    1667     584    1669     580     580
              518     640     518    1727     523    1728
              581    1669     581     579     527     633
              518     640     519    1728     581    1669
              580     581     519     639     518     642
              518    1728     582    1669     520    1726
              583   39880    8893    2315     580

          name KEY_NUMERIC_6
             8976    4540     545     617     518     643
              518     642     541     619     523     637
              519     641     520     641     519     641
              518    1728     527    1726     521    1729
              522    1729     521    1731     545    1704
              547    1704     546    1705     546     617
              518    1729     522     642     519    1728
              522    1729     522     641     519    1729
              545     617     519    1730     521     641
              519    1730     520     642     519     641
              519    1728     523     641     518    1729
              546   39928    8979    2251     546

          name KEY_NUMERIC_7
             8964    4536     549     614     552     607
              547     612     549     611     551     609
              580     579     582     578     582     578
              581    1667     584    1664     584    1668
              583    1668     582    1667     583    1666
              586    1666     584    1666     590     573
              581    1667     586     581     578     578
              582     577     582     579     581    1666
              583     580     580    1667     585     578
              581    1665     585    1665     585    1666
              582    1668     588     573     582    1664
              587   39879    8978    2250     583

          name KEY_NUMERIC_8
             8969    4540     582     581     519     640
              520     641     519     641     518     641
              520     639     520     640     519     640
              520    1729     522    1728     521    1726
              524    1726     586    1668     521    1727
              523    1727     523    1734     518     640
              520    1728     522     641     518     641
              519    1727     524     647     513    1726
              524     640     530    1716     524     640
              521    1726     523    1733     516     640
              520    1727     524     640     519    1727
              525   39943    8978    2257     517

          name KEY_NUMERIC_9
             8904    4602     581     580     520     640
              519     641     519     641     519     641
              518     641     519     641     519     639
              520    1727     523    1727     583    1669
              521    1727     581    1678     514    1727
              523    1727     522    1726     524     641
              519    1734     516     641     519    1727
              583     579     522     640     519    1727
              582     587     513    1727     583     580
              520    1727     523     640     521    1727
              528    1720     584     581     519    1728
              582   39886    8919    2310     584

          name BTN_0
             8913    4601     589     576     520     641
              520     641     520     641     520     641
              521     639     520     640     521     640
              521    1729     522    1729     584    1667
              582    1668     584    1668     583    1668
              582    1669     523    1735     577    1669
              522     640     519     641     520    1727
              585    1675     576     581     520     640
              520     640     519     641     521    1731
              520    1728     523     648     512     641
              520    1728     523    1728     522    1731
              521   39963    8923    2318     518

          name BTN_1
             8904    4599     583     581     519     641
              519     641     519     641     519     640
              519     641     518     647     513     641
              518    1729     583    1669     521    1727
              582    1674     517    1728     582    1669
              538    1710     581    1672     585    1665
              581     581     519    1727     583    1673
              582     580     521     639     520     644
              513     639     519     640     520    1727
              583     581     520     640     519    1729
              520    1728     583    1670     581    1668
              521   39952    8915    2313     522

      end raw_codes

end remote

配置/etc/lirc/lirc_options.conf文件

driver = default
device = /dev/lirc1

再执行最终的安装命令

sudo apt install ./liblirc0_0.10.1-5.2_armhf.deb ./liblircclient0_0.10.1-5.2_armhf.deb ./lirc_0.10.1-5.2_armhf.deb 

重启树莓派

sudo reboot

检查lirc模块是否加载完成:

pi@raspberrypi:~ $ ls /dev/li*
/dev/lirc0  /dev/lirc1
pi@raspberrypi:~ $ irw  #然后按遥控器按键
0000000000000001 00 KEY_CHANNELDOWN ./lircd.conf
0000000000000002 00 KEY_CHANNEL ./lircd.conf
0000000000000003 00 KEY_CHANNELUP ./lircd.conf
0000000000000004 00 KEY_PREVIOUS ./lircd.conf
0000000000000005 00 KEY_NEXT ./lircd.conf
0000000000000006 00 KEY_PLAYPAUSE ./lircd.conf
0000000000000008 00 KEY_VOLUMEDOWN ./lircd.conf
0000000000000007 00 KEY_VOLUMEUP ./lircd.conf
0000000000000009 00 KEY_EQUAL ./lircd.conf

C/C++安装LIRC客户端

sudo apt-get install liblircclient-dev

将lircrc 库文件上传到/home目录,然后移动到/etc/lirc/

sudo mv lircrc /etc/lirc/

Python安装pylirc库

sudo apt-get install python-pylirc

2.2、树莓派接线

2.3 C代码

#include <wiringPi.h>
#include <softPwm.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <lirc/lirc_client.h>
#include <time.h>

#define uchar unsigned char

#define LedPinRed   0
#define LedPinGreen 1
#define LedPinBlue  2

uchar color[3] = {0x00, 0x00, 0x00};
uchar Lv[3] = {0x00, 0x44, 0xff};

char *keymap[21] = {
                   " KEY_CHANNELDOWN ",
                   " KEY_CHANNEL ",
                   " KEY_CHANNELUP ",
                   " KEY_PREVIOUS ",
                   " KEY_NEXT ",
                   " KEY_PLAYPAUSE ",
                   " KEY_VOLUMEDOWN ",
                   " KEY_VOLUMEUP ",
                   " KEY_EQUAL ",
                   " KEY_NUMERIC_0 ",
                   " BTN_0 ",
                   " BTN_1 ",
                   " KEY_NUMERIC_1 ",
                   " KEY_NUMERIC_2 ",
                   " KEY_NUMERIC_3 ",
                   " KEY_NUMERIC_4 ",
                   " KEY_NUMERIC_5 ",
                   " KEY_NUMERIC_6 ",
                   " KEY_NUMERIC_7 ",
                   " KEY_NUMERIC_8 ",
                   " KEY_NUMERIC_9 "};
                     
void ledInit(void)
{
    softPwmCreate(LedPinRed,  0, 100);
    softPwmCreate(LedPinGreen,0, 100);
    softPwmCreate(LedPinBlue, 0, 100);
}

void ledColorSet()
{
    softPwmWrite(LedPinRed, color[0]);
    softPwmWrite(LedPinGreen, color[1]);
    softPwmWrite(LedPinBlue, color[2]);
}

int key(char *code){
    int i;
    int num;
    for (i=0;i<21;i++){
        if (strstr(code, keymap[i])){
            num = i;
        }
    }
    return num + 1;
}

int RGB(int i){
    switch(i){
        case 1: color[0] = Lv[0]; printf("Red OFF\n"); break;
        case 2: color[0] = Lv[1]; printf("Light Red\n"); break;
        case 3: color[0] = Lv[2]; printf("Dark Red\n"); break;
        case 4: color[1] = Lv[0]; printf("Green OFF\n"); break;
        case 5: color[1] = Lv[1]; printf("Light Green\n"); break;
        case 6: color[1] = Lv[2]; printf("Dark Green\n"); break;
        case 7: color[2] = Lv[0]; printf("Blue OFF\n"); break;
        case 8: color[2] = Lv[1]; printf("Light Blue\n"); break;
        case 9: color[2] = Lv[2]; printf("Dark Blue\n"); break;
    }
}


int main(void)
{
    struct lirc_config *config;
    int buttonTimer = millis();
    char *code;
    char *c;
    if (wiringPiSetup() == -1){
        printf("setup wiringPi failed !");
        return 1;
    }

    if (lirc_init("lirc", 1) == -1)
            exit(EXIT_FAILURE);

    ledInit();
    ledColorSet();

    if (lirc_readconfig(NULL, &config, NULL)==0)
    {
        while (lirc_nextcode(&code)==0)
        {
            if (code==NULL) continue;{
                if (millis() - buttonTimer > 400){
                    RGB(key(code));
                    ledColorSet(color);
                }
            }
            free(code);
        }
        lirc_freeconfig(config);
    }
    lirc_deinit();
    exit(EXIT_SUCCESS);
    return 0;
}

Python代码

#!/usr/bin/python

import pylirc, time
import RPi.GPIO as GPIO

Rpin = 17
Gpin = 18
Bpin = 27
blocking = 0;

Lv = [100, 20, 0] #Light Level
color = [100, 100, 100]

def setColor(color):
    p_R.ChangeDutyCycle(100 - color[0])  #Change duty cycle
    p_G.ChangeDutyCycle(100 - color[1])
    p_B.ChangeDutyCycle(100 - color[2])

def setup():
    global p_R, p_G, p_B
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(Rpin, GPIO.OUT)
    GPIO.setup(Gpin, GPIO.OUT)
    GPIO.setup(Bpin, GPIO.OUT)

    p_R = GPIO.PWM(Rpin, 2000) #Set Frequence to 2KHz
    p_G = GPIO.PWM(Gpin, 2000)
    p_B = GPIO.PWM(Bpin, 2000)

    p_R.start(0)
    p_G.start(0)
    p_B.start(0)
    pylirc.init("pylirc", "./conf", blocking)

def map(x, in_min, in_max, out_min, out_max):
    return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min

def RGB(config):
    global color
    if config == 'KEY_CHANNELDOWN':
        color[0] = Lv[0]
        print('Red OFF')
    if config == 'KEY_CHANNEL':
        color[0] = Lv[1]
        print('Light Red')
    if config == 'KEY_CHANNELUP':
        color[0] = Lv[2]
        print("Red")

    if config == 'KEY_PREVIOUS':
        color[1] = Lv[0]
        print('Green OFF')
    if config == 'KEY_NEXT':
        color[1] = Lv[1]
        print('light Green')
    if config == 'KEY_PLAYPAUSE':
        color[1] = Lv[2]
        print('Green')

    if config == 'KEY_VOLUMEDOWN':
        color[2] = Lv[0]
        print('Blue OFF')
    if config == 'KEY_VOLUMEUP':
        color[2] = Lv[1]
        print('light Blue')
    if config == 'KEY_EQUAL':
        color[2] = Lv[2]
        print('Blue')

def loop():
    while True:
        s = pylirc.nextcode(1)
        
        while(s):
            for (code) in s:
                print('Command: ', code["config"]) #For debug: Uncommnet this line to see the return value of buttons
                RGB(code["config"])
                setColor(color)
            if (not blocking):
                s = pylirc.nextcode(1)
            else:
                s = []

def destroy():
    p_R.stop()
    p_G.stop()
    p_B.stop()
    GPIO.output(Rpin, GPIO.HIGH)
    GPIO.output(Gpin, GPIO.HIGH)
    GPIO.output(Bpin, GPIO.HIGH)
    GPIO.cleanup()
    pylirc.exit()


if __name__ == '__main__':
    try:
        setup()
        loop()
    except KeyboardInterrupt:
        destroy()

相关文章

网友评论

      本文标题:【树莓派+arduino实验记录8 】红外遥控器

      本文链接:https://www.haomeiwen.com/subject/tanykktx.html