Skip to content

Instantly share code, notes, and snippets.

@alexBaizeau
Last active December 18, 2015 07:19
Show Gist options
  • Save alexBaizeau/5745705 to your computer and use it in GitHub Desktop.
Save alexBaizeau/5745705 to your computer and use it in GitHub Desktop.
#######################
# Author : Alex Baizeau
#
# The Credit for the analog input part goes to adafruit : http://bit.ly/VHTEmS
########################
from i2clibraries import i2c_lcd
from time import *
import time
import os
from mplayer import Player
import RPi.GPIO as GPIO
# Configuration parameters
# I2C Address, Port, Enable pin, RW pin, RS pin, Data 4 pin, Data 5 pin, Data
# 6 pin, Data 7 pin, Backlight pin (optional)
lcd = i2c_lcd.i2c_lcd(0x3f,1, 2, 1, 0, 4, 5, 6, 7, 3)
# If you want to disable the cursor, uncomment the following line
# lcd.command(lcd.CMD_Display_Control | lcd.OPT_Enable_Display)
lcd.backLightOn()
lcd.writeString("Alex Web Radio")
lcd.setPosition(2, 3)
lcd.writeString("For the Pi")
DEBUG = 0
p =Player()
radio_urls = [
"http://1671.live.streamtheworld.com/CBC_R1_OTT_H",
'http://hd2.lagrosseradio.info:8300/',
'http://95.81.147.3/nostalgie/all/nos_113812.mp3',
]
radio_names = [
"CBC Radio 1",
"Grosse Radio Reggae",
"Nostalgie",
]
count_radios = len(radio_urls);
current_radio_index = 0
radio_index = 0
GPIO.setmode(GPIO.BCM)
DEBUG = 1
# remap the a range of number to another
def translate(value, leftMin, leftMax, rightMin, rightMax):
# Figure out how 'wide' each range is
leftSpan = leftMax - leftMin
rightSpan = rightMax - rightMin
# Convert the left range into a 0-1 range (float)
valueScaled = float(value - leftMin) / float(leftSpan)
print ("value", value)
# Convert the 0-1 range into a value in the right
# range.
return round(rightMin + (valueScaled * rightSpan))
# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
def readadc(adcnum, clockpin, mosipin, misopin, cspin):
if ((adcnum > 7) or (adcnum < 0)):
return -1
GPIO.output(cspin, True)
GPIO.output(clockpin, False) # start clock low
GPIO.output(cspin, False) # bring CS low
commandout = adcnum
commandout |= 0x18 # start bit + single-ended bit
commandout <<= 3 # we only need to send 5 bits here
for i in range(5):
if (commandout & 0x80):
GPIO.output(mosipin, True)
else:
GPIO.output(mosipin, False)
commandout <<= 1
GPIO.output(clockpin, True)
GPIO.output(clockpin, False)
adcout = 0
# read in one empty bit, one null bit and 10 ADC bits
for i in range(12):
GPIO.output(clockpin, True)
GPIO.output(clockpin, False)
adcout <<= 1
if (GPIO.input(misopin)):
adcout |= 0x1
GPIO.output(cspin, True)
adcout >>= 1 # first bit is 'null' so drop it
return adcout
# change these as desired - they're the pins connected from the
# SPI port on the ADC to the Cobbler
SPICLK = 18
SPIMISO = 23
SPIMOSI = 24
SPICS = 25
# set up the SPI interface pins
GPIO.setup(SPIMOSI, GPIO.OUT)
GPIO.setup(SPIMISO, GPIO.IN)
GPIO.setup(SPICLK, GPIO.OUT)
GPIO.setup(SPICS, GPIO.OUT)
# 10k trim pot connected to adc #0
potentiometer_adc = 0;
last_read = 0 # this keeps track of the last potentiometer value
tolerance = 5 # A little bit of hysteresis
while True:
# we'll assume that the pot didn't move
# we'll assume that the pot didn't move
trim_pot_changed = False
# read the analog pin
trim_pot = readadc(potentiometer_adc, SPICLK, SPIMOSI, SPIMISO, SPICS)
# how much has it changed since the last read?
pot_adjust = abs(trim_pot - last_read)
if ( pot_adjust > tolerance ):
trim_pot_changed = True
#if trim pot changed check if we need to chasnge the radio
#find the index of the radio
radio_index = translate(trim_pot,0,1024,0,count_radios - 1 )
print ("new radio index:",radio_index)
print ("current radio index" , current_radio_index)
if ( radio_index != current_radio_index ):
print (" different new radio index:",radio_index)
print ("different current radio index" , current_radio_index)
lcd.clear()
lcd.setPosition(1, 0)
lcd.writeString(radio_names[radio_index])
p.loadfile(radio_urls[radio_index])
current_radio_index = radio_index
# save the potentiometer reading for the next loop
last_read = trim_pot
@alexBaizeau
Copy link
Author

Alex Python Webradio

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment