# ir_read_nomessage.py
from evdev import InputDevice, list_devices, categorize, ecodes
def get_ir_device():
devices = [InputDevice(path) for path in list_devices()]
for device in devices:
print(device.path, device.name, device.phys)
if 'ir_recv' in device.name:
return device
else:
device.close()
dev = get_ir_device()
try:
for event in dev.read_loop():
if event.type == ecodes.EV_KEY:
if event.value == 1:
if ecodes.KEY[event.code] == 'KEY_1':
print('press 1')
elif ecodes.KEY[event.code] == 'KEY_2':
print('press 2')
elif ecodes.KEY[event.code] == 'KEY_3':
print('press 3')
dev.close()
quit()
except KeyboardInterrupt:
print('press ctrl-c')
dev.close()
quit()
# vlc_id.py
import psutil
# 方法一:先取得所有的process id (PID),再找出vlc的PID,找到就kill離開
# process_ids = psutil.pids()
# for pid in process_ids:
# p = psutil.Process(pid)
# if 'vlc' in p.name():
# print(p.name())
# print(p.pid)
# p.kill()
# break
# 方法二:透過iter的方式,一個個找,找到就kill離開
for proc in psutil.process_iter(['pid', 'name']):
if 'vlc' in proc.info['name']:
print(proc.info['pid'])
p = psutil.Process(proc.info['pid'])
p.kill()
break
from evdev import InputDevice, list_devices, categorize, ecodes
from gpiozero import LED
import subprocess
import psutil
led = LED(27)
led_on = False
vlc_on = False
def get_ir_device():
devices = [InputDevice(path) for path in list_devices()]
for device in devices:
print(device.path, device.name, device.phys)
if 'ir_recv' in device.name:
return device
else:
device.close()
def stop_vlc():
for proc in psutil.process_iter(['pid', 'name']):
if 'vlc' in proc.info['name']:
print(proc.info['pid'])
p = psutil.Process(proc.info['pid'])
p.kill()
return
dev = get_ir_device()
try:
for event in dev.read_loop():
if event.type == ecodes.EV_KEY and event.value == 1:
if ecodes.KEY[event.code] == 'KEY_1':
# 控制VLC播放音樂
if vlc_on:
# 利用PID停止播放
print('停止播放音樂')
stop_vlc()
else:
print('開始播放音樂')
subprocess.run(["cvlc", "-LZ", "/home/pi/Music"])
vlc_on = not vlc_on
elif ecodes.KEY[event.code] == 'KEY_2':
# 控制LED亮與暗
if led_on:
led.off()
else:
led.blink()
led_on = not led_on
elif ecodes.KEY[event.code] == 'KEY_3':
print('離開程式')
dev.close()
quit()
except KeyboardInterrupt:
print('按鍵Ctrl+c中斷程式')
dev.close()
quit()