Skip to content

Instantly share code, notes, and snippets.

@satanowski
Last active December 16, 2015 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satanowski/5476742 to your computer and use it in GitHub Desktop.
Save satanowski/5476742 to your computer and use it in GitHub Desktop.

Prosty skrypt do obsługi kilku fizycznych guzików, ułatwiający używanie RaspberryPi jako komputera do codziennej pracy.

Hardware

Podłączenie guzików jest banalne:

schemat

Software

Programowa obsługa jest równie "skomplikowana" co schemat: w pętli sprawdzamy, czy aby któryś port nie zmienił stanu, jeśli zmienił uruchamiamy przypisaną procedurę.

2 guziki przeznaczone są do ściszania i podgłaszania muzyki stąd obecność procedur get_mixer i set_mixer. Z nieznanych mi przyczyn amixer zwraca poziom głośności w formie 3 liczb (minimum, maximum i wartość bieżąca):

numid=1,iface=MIXER,name='Capture Volume'

; type=INTEGER,access=rw------,values=2,min=0,max=65536,step=1

: values=56210,56210

natomiast przy ustawianiu przyjmuje jedną wartość wyrażoną w procentach. Dlatego właśnie get_mixer parsuje output z amixer i przelicza go na procenty.

Efekt końcowy

pix1

pix2

pix3

#!/usr/bin/env python
import RPi.GPIO as gpio
import time,re
import subprocess as sub
# config
DELAY = 0.10
B_GREEN = 25
B_BLUE = 22
B_BLACK1 = 4
B_BLACK2 = 17
buttons = [B_GREEN, B_BLUE, B_BLACK1, B_BLACK2]
def get_mixer():
mix_state = sub.Popen(['amixer', 'cget', 'numid=1'], stdin=sub.PIPE, stdout=sub.PIPE).communicate()[0]
mix_state = mix_state.replace('\n','')
vmin, vmax, vval = re.findall('min=(-*\d+),max=(-*\d+),.*values=(-*\d+)', mix_state)[0]
vmin = 1.0 * int(vmin)
vmax = 1.0 * int(vmax)
vval = 1.0 * int(vval)
return int((vval - vmin)/(vmax - vmin)*100)
def set_mixer(vol):
cmd = ('amixer cset numid=1 %d' % vol or 50) + "%"
sub.Popen(cmd.split(), stdin=sub.PIPE, stdout=sub.PIPE).communicate()
def volumeUp():
vol = get_mixer()
if vol < 100:
set_mixer(vol + 1)
def volumeDown():
vol = get_mixer()
if vol>0:
set_mixer(vol - 1)
def Trojka():
sub.call(['trojka'])
def Blue():
sub.call(['xscreensaver-command','-lock'])
actions = {
B_GREEN : Trojka,
B_BLUE : Blue,
B_BLACK1 : volumeUp,
B_BLACK2 : volumeDown
}
gpio.setmode(gpio.BCM)
for pin in buttons:
gpio.setup(pin, gpio.IN)
while True:
prv_state = {}
cur_state = {}
for pin in buttons: prv_state[pin] = gpio.input(pin)
time.sleep(0.05) # debounce
for pin in buttons: cur_state[pin] = gpio.input(pin)
for pin in buttons:
if cur_state[pin] == 1 and prv_state[pin] == cur_state[pin]:
actions[pin]()
time.sleep(DELAY)
#!/bin/bash
PR3="program3_wma10"
PID=`ps -aef | grep $PR3 | grep -v grep | sort | head -1 | awk '{print $2}'`
if [[ $PID ]]
then
echo "No to nie gramy ..."
kill -15 $PID
else
echo "No to gramy ..."
/usr/bin/mplayer mms://stream.polskieradio.pl/$PR3 < /dev/null > /dev/null 2>&1 &
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment