#!/bin/sh
import os.path
import time
import subprocess
import RPi.GPIO as GPIO
from Adafruit_CharLCD import Adafruit_CharLCD
lcd = Adafruit_CharLCD()
GPIO.setmode(GPIO.BCM)
#def. GPIO2 (pin3) a GPIO3 (pin 5) jako vstupy, odpor na 3.3V
GPIO.setup(2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(4, GPIO.OUT)
GPIO.setup(7, GPIO.OUT)
# vypneme blikajici LED a podsvitime displej
GPIO.output(4, 1)
# nahodime pridrzovaci rele zdroje
GPIO.output(7, 1)
#####################################################
############ TADY MENIT A PRIDAVAT STANICE ############
#####################################################
channels = [\
("http://icecast7.play.cz/casradioolomouc128.mp3", "Radio Cas", " Olomouc"), \
("http://icecast4.play.cz/country128.mp3", "Country", "radio"), \
("http://icecast3.play.cz/frekvence1-128.mp3", "Frekvence 1", "") \
]
####################################################
####################################################
f = open('/home/pi/posledniStaniceCislo', 'r')
idx =int(f.readline())
f.close()
text1 = channels[idx][1]
text2 = channels[idx][2]
lcd.clear()
lcd.message('P' + str(idx) + ' ' + text1 + '\n' + text2)
while True:
pocitadlo = 0
v = int(GPIO.input(2))
# Button pressed: zobrazime dalsi kanal
if v <> 1:
print "Tlacitko zmacknuto zacatek"
test = os.path.exists('/home/pi/lock')
# pokud jsem zacal znova mackat a radio
# jeste neprevzalo nastaveni, smazu lock
if test == 1:
os.remove('/home/pi/lock')
while pocitadlo < 1:
idx = idx + 1
if idx >= len(channels):
idx = 0
url = channels[idx][0]
text1 = channels[idx][1]
text2 = channels[idx][2]
lcd.clear()
lcd.message('P' + str(idx) + ' ' + text1 + '\n' + text2)
pocitadlo = pocitadlo + 1
time.sleep(1)
v2 = int(GPIO.input(2))
#dokud drzime tlacitko, listujeme
if v2 <> 1:
pocitadlo = 0
print "Tlacitko stale stisknuto, pokracuji v listovani"
#skoncili jsme listovani, tedy ulozime posledni hodnotu
f3 = open('/home/pi/posledniStaniceCislo', 'w')
f3.write(str(idx))
f3.close()
f4 = open('/home/pi/posledniStanice', 'w')
f4.write(url)
f4.close()
time.sleep(0.2)
#a vytvorime lock soubor, aby si to nacetlo radio
open('/home/pi/lock', 'a').close()
#vyskocime z cyklu
v = 1
time.sleep(0.2)
#otestujeme stisk vypinaciho kontaktu
v3 = int(GPIO.input(3))
if v3 <> 1:
print "Stisknuto tlacitko pro vypnuti"
# zapneme blikajici LED
GPIO.output(4, 0)
# vypneme Raspberry
os.system("sudo shutdown -h now")
#!/usr/bin/python
import time
import subprocess
import os.path
def play_channel(url):
global radio_process
if radio_process <> None:
radio_process.terminate()
print "Radio: Shazuji radio"
f7=open('/home/pi/posledniStaniceCislo', 'r')
cislo=f7.readline()
f7.close()
znelka='/home/pi/P' + cislo + '.wav'
time.sleep(2)
subprocess.call(["/usr/bin/aplay", znelka])
radio_process = subprocess.Popen(["/usr/bin/cvlc",
/"-A", "alsa,none", "--alsa-audio-device", "default", url])
####
radio_process = None
# pro jistotu smazeme lock soubor, kdyby tam byl
test = os.path.exists('/home/pi/lock')
if test == 1:
os.remove('/home/pi/lock')
f3 = open('/home/pi/posledniStanice', 'r')
url = f3.readline()
f3.close()
print "Radio: poustim stanici " + url
play_channel(url)
time.sleep(8)
try:
while True:
test = os.path.exists('/home/pi/lock')
if test == 1:
print "Radio: Nalezen lock"
f5 = open('/home/pi/posledniStanice', 'r')
url = f5.readline()
f5.close()
print "Radio: Nova stanice: " + url
os.remove('/home/pi/lock')
play_channel(url)
#dlouha pauza, protoze pri rychlem prepinani havaruje VLC
time.sleep(7)
# Poll interval.
time.sleep(0.5)
except:
# Keyboard interrupt or other stuff that causes termination -
# clean up any running child process.
if radio_process <> None:
radio_process.terminate()