Skip to content

Commit

Permalink
Multiple improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
fsahli committed Feb 7, 2016
1 parent f584ee4 commit 36a6c34
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 15 deletions.
39 changes: 39 additions & 0 deletions CardList.py
@@ -0,0 +1,39 @@
import csv
import os.path
import sys

class CardList:
def __init__(self):
self.path = os.path.dirname(os.path.realpath(__file__))
self.cardList = self.readList()

def readList(self):
with open(self.path + '/cardList.csv', mode='r') as infile:
reader = csv.reader(infile)
cardList = {rows[0]:rows[1] for rows in reader}
infile.close()
return cardList

def getPlaylist(self,card):
self.cardList = self.readList()
try:
return self.cardList[card]
except:
print 'Card %s is not card list' % card
return ''

def addPlaylist(self, card, plist):
try:
if card not in self.cardList.keys():
f = open(self.path + '/cardList.csv', 'a')
f.write(card + ',' + plist + '\n')
self.cardList[card] = plist
else:
print 'Card %s is already used' % card
except:
print 'Could not write file'
if not os.path.isfile(self.path + '/cardList.csv'):
print 'File cardList.csv does not exist'



38 changes: 38 additions & 0 deletions Reader.py
@@ -0,0 +1,38 @@
import string
import csv
import os.path
import sys

from evdev import InputDevice, categorize, ecodes, list_devices
from select import select
class Reader:
def __init__(self):
path = os.path.dirname(os.path.realpath(__file__))
self.keys = "X^1234567890XXXXqwertzuiopXXXXasdfghjklXXXXXyxcvbnmXXXXXXXXXXXXXXXXXXXXXXX"
if not os.path.isfile(path + '/deviceName.txt'):
sys.exit('Please run config.py first')
else:
with open(path + '/deviceName.txt','r') as f:
deviceName = f.read()
devices = [InputDevice(fn) for fn in list_devices()]
for device in devices:
if device.name == deviceName:
self.dev = device
break
try:
self.dev
except:
sys.exit('Could not find the device %s\n. Make sure is connected' % deviceName)

def readCard(self):
stri=''
key = ''
while key != 'KEY_ENTER':
r,w,x = select([self.dev], [], [])
for event in self.dev.read():
if event.type==1 and event.value==1:
stri+=self.keys[ event.code ]
#print( keys[ event.code ] )
key = ecodes.KEY[ event.code ]
return stri[:-1]

56 changes: 41 additions & 15 deletions box.py
Expand Up @@ -2,27 +2,53 @@
from mpd import MPDClient
from readtest import *
import re
from CardList import CardList
from Reader import Reader
import sys

while True:

def connectMPD():
try:
card = readCard()
cardList = readList()
plist=cardList[card]
client = MPDClient() # create client object
client.timeout = 10 # network timeout in seconds (floats allowed), default: None
client.timeout = 200 # network timeout in seconds (floats allowed), default: None
client.idletimeout = None
print "Connecting..."
client.connect("localhost", 6600)
print "Connected!"
if plist=='pause':
client.pause()
else:
client.stop()
client.clear()
client.add(plist)
if re.search('playlist',plist):
client.shuffle()
client.play()
client.close()
return client
except:
print 'Could not connect to MPD server'

def play(client, plist):
try:
client.stop()
client.clear()
client.add(plist)
if re.search('playlist',plist):
client.shuffle()
client.play()
except:
print 'Could not play playlist %s' % plist

reader = Reader()
cardList = CardList()

print 'Ready: place a card on top of the reader'

while True:
try:
card = reader.readCard()
print 'Read card', card
plist = cardList.getPlaylist(card)
print 'Playlist', plist
if plist != '':
client = connectMPD()
if plist=='pause':
client.pause()
else:
play(client, plist)
client.close()
except KeyboardInterrupt:
sys.exit(0)
except:
pass
18 changes: 18 additions & 0 deletions config.py
@@ -0,0 +1,18 @@
import os.path
from evdev import InputDevice, list_devices

devices = [InputDevice(fn) for fn in list_devices()]
path = os.path.dirname(os.path.realpath(__file__))
i = 0
print "Choose the reader from list"
for dev in devices:
print i, dev.name
i += 1

dev_id = int(raw_input('Device Number: '))

with open(path + '/deviceName.txt','w') as f:
f.write(devices[dev_id].name)
f.close()


0 comments on commit 36a6c34

Please sign in to comment.