raspberry pi (9) 红外遥控器

1.配置

1)
sudo apt update

2)
sudo apt-get install lirc

3)
sudo vi /etc/lirc/hardware.conf
LIRCD_ATGS="--uinput"
DRIVER="default"
DEVICE="/dev/lirc0"
MODULES="lirc-rpi

4)
sudo vi /etc/modules
最后面加入:
lirc_dev
lirc_rpi gpio_in_pin=23 gpio_out_pin=22

5)
sudo reboot

6)红外接收功能配置检测
首先关闭lirc软件
sudo /etc/init.d/lirc stop

然后执行如下命令
mode2 -d /dev/lirc0

按下遥控器的任意按键会返回如下图所示的内容,说明红外接收配置成功了
pi@raspberrypi:~ $ mode2 -d /dev/lirc0
space 4899669
pulse 9123
space 4507
pulse 595
space 569
pulse 596
space 570
pulse 595
space 570
pulse 594
space 571
pulse 595
space 570
pulse 595

7)录制红外编码
关闭lirc软件,然后查看可用的按键名
使用命令:
sudo /etc/init.d/lirc stop
irrecord -list-namespace

返回为目前可用的按键名称
pi@raspberrypi:~ $ sudo /etc/init.d/lirc stop
[ ok ] Stopping lirc (via systemctl): lirc.service.
pi@raspberrypi:~ $ irrecord -list-namespace
KEY_0
KEY_102ND
KEY_1
KEY_2
KEY_3
KEY_4
KEY_5
KEY_6
KEY_7
KEY_8
KEY_9
KEY_A
KEY_AB
KEY_ADDRESSBOOK
KEY_AGAIN
KEY_ALTERASE
KEY_ANGLE
KEY_APOSTROPHE

pi@raspberrypi:~ $ irrecord -d /dev/lirc0 ~/lircd.conf

irrecord -  application for recording IR-codes for usage with lirc

Copyright (C) 1998,1999 Christoph Bartelmus(lirc@bartelmus.de)

This program will record the signals from your remote control
and create a config file for lircd.


A proper config file for lircd is maybe the most vital part of this
package, so you should invest some time to create a working config
file. Although I put a good deal of effort in this program it is often
not possible to automatically recognize all features of a remote
control. Often short-comings of the receiver hardware make it nearly
impossible. If you have problems to create a config file READ THE
DOCUMENTATION of this package, especially section "Adding new remote
controls" for how to get help.

If there already is a remote control of the same brand available at
http://www.lirc.org/remotes/ you might also want to try using such a
remote as a template. The config files already contain all
parameters of the protocol used by remotes of a certain brand and
knowing these parameters makes the job of this program much
easier. There are also template files for the most common protocols
available in the remotes/generic/ directory of the source
distribution of this package. You can use a template files by
providing the path of the file as command line parameter.

Please send the finished config files to <lirc@bartelmus.de> so that I
can make them available to others. Don't forget to put all information
that you can get about the remote control in the header of the file.

Press RETURN to continue.


Now start pressing buttons on your remote control.

It is very important that you press many different buttons and hold them
down for approximately one second. Each button should generate at least one
dot but in no case more than ten dots of output.
Don't stop pressing buttons until two lines of dots (2x80) have been
generated.

Press RETURN now to start recording.
................................................................................
Found const length: 108440
Please keep on pressing buttons like described above.
................................................................................
Space/pulse encoded remote control found.
Signal length is 67.
Found possible header: 9099 4508
Found trail pulse: 592
No repeat code found.
Signals are space encoded.
Signal length is 32
Now enter the names for the buttons.

Please enter the name for the next button (press <ENTER> to finish recording)
KEY_1

Now hold down button "KEY_1".

Please enter the name for the next button (press <ENTER> to finish recording)
KEY_2

Now hold down button "KEY_2".

Please enter the name for the next button (press <ENTER> to finish recording)
KEY_3

Now hold down button "KEY_3".

Please enter the name for the next button (press <ENTER> to finish recording)

Checking for toggle bit mask.
Please press an arbitrary button repeatedly as fast as possible.
Make sure you keep pressing the SAME button and that you DON'T HOLD
the button down!.
If you can't see any dots appear, then wait a bit between button presses.

Press RETURN to continue.
......
Toggle bit mask is 0xf0f0.
Successfully written config file.
pi@raspberrypi:~ $ sudo cp ~/lircd.conf /etc/lirc/lircd.conf
pi@raspberrypi:~ $ sudo /etc/init.d/lirc start
[ ok ] Starting lirc (via systemctl): lirc.service.
pi@raspberrypi:~ $ irw
0000000000ff30cf 00 KEY_1 /home/pi/lircd.conf
0000000000ff18e7 00 KEY_2 /home/pi/lircd.conf
0000000000ff7a85 00 KEY_3 /home/pi/lircd.conf
0000000000ff7a85 00 KEY_3 /home/pi/lircd.conf
0000000000ff18e7 00 KEY_2 /home/pi/lircd.conf
0000000000ff30cf 00 KEY_1 /home/pi/lircd.conf
^C

2.conf

begin
  remote = *
  button = KEY_1
  prog = pylirc
  config = KEY_1
end

begin
  remote = *
  button = KEY_2
  prog = pylirc
  config = KEY_2
end

begin
  remote = *
  button = KEY_3
  prog = pylirc
  config = KEY_3
end

3.代码:

#!/usr/bin/python

import pylirc, time
import RPi.GPIO as GPIO

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

color = [100, 100, 100]

def setColor(color):
#	global p_R, p_G, p_B
	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, 200)
	p_G = GPIO.PWM(Gpin, 200)
	p_B = GPIO.PWM(Bpin, 200)
	
	p_R.start(0)
	p_G.start(0)
	p_B.start(0)
	pylirc.init("pylirc", "./conf", blocking)

def RGB(config):
	global color
	if config == 'KEY_1':
		color[0] = 100 - color[0]
		print 'Red'

	if config == 'KEY_2':
		color[1] = 100 - color[1]
		print 'Green'

	if config == 'KEY_3':
		color[2] = 100 - color[2]
		print 'Blue'

def loop():
	while True:
		s = pylirc.nextcode(1)
		
		while(s):
			for (code) in s:
				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)    # Turn off all leds
	GPIO.output(Gpin, GPIO.HIGH)
	GPIO.output(Bpin, GPIO.HIGH)
	GPIO.cleanup()
	pylirc.exit()

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

4.结果
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
2022 / 01/ 30: 新版esptool 刷micropython固件指令不是 esptool.py cmd... 而是 esptool cmd... 即可;另外rshell 在 >= python 3.10 的时候出错解决方法可以查看:  已于2022年发布的: 第二章:修复rshell在python3.10出错 免费内容: https://edu.csdn.net/course/detail/29666 micropython语法和python3一样,编写起来非常方便。如果你快速入门单片机玩物联网而且像轻松实现各种功能,那绝力推荐使用micropython。方便易懂易学。 同时如果你懂C语音,也可以用C写好函数并编译进micropython固件里然后进入micropython调用(非必须)。 能通过WIFI联网(2.1章),也能通过sim卡使用2G/3G/4G/5G联网(4.5章)。 为实现语音控制,本教程会教大家使用tensorflow利用神经网络训练自己的语音模型并应用。为实现通过网页控制,本教程会教大家linux(debian10 nginx->uwsgi->python3->postgresql)网站前后台入门。为记录单片机传输过来的数据, 本教程会教大家入门数据库。  本教程会通过通俗易懂的比喻来讲解各种原理与思路,并手把手编写程序来实现各项功能。 本教程micropython版本是 2019年6月发布的1.11; 更多内容请看视频列表。  学习这门课程之前你需要至少掌握: 1: python3基础(变量, 循环, 函数, 常用库, 常用方法)。 本视频使用到的零件与淘宝上大致价格:     1: 超声波传感(3)     2: MAX9814麦克风放大模块(8)     3: DHT22(15)     4: LED(0.1)     5: 8路5V低电平触发继电(12)     6: HX1838红外接收模块(2)     7:红外发射管(0.1),HX1838红外接收板(1)     other: 电表, 排线, 面包板(2)*2,ESP32(28)  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值