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([email protected])

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  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  to finish recording)
KEY_1

Now hold down button "KEY_1".

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

Now hold down button "KEY_2".

Please enter the name for the next button (press  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.结果
raspberry pi (9) 红外遥控器_第1张图片
raspberry pi (9) 红外遥控器_第2张图片
raspberry pi (9) 红外遥控器_第3张图片
raspberry pi (9) 红外遥控器_第4张图片
raspberry pi (9) 红外遥控器_第5张图片
raspberry pi (9) 红外遥控器_第6张图片

你可能感兴趣的:(python3,raspberry,pi)