arduino-mpduino/serialmpcduino.py

235 lines
6.4 KiB
Python
Raw Permalink Normal View History

2017-02-04 23:35:03 +01:00
import serial
import time
class SerialMPCduino(object):
2017-02-26 04:13:16 +01:00
stats = {
'volume' :None,
'title' :None,
'artist' :None,
'album' :None,
'playing' :None,
'repeat' :None,
'shuffle' :None,
'random' :None,
'consume' :None,
'updating' :None,
'single' :None
}
keys = {
'read' :'E',
'write' :'S',
'reset' :'R',
'volume' :'V',
'title' :'T',
'artist' :'A',
'album' :'a',
'playing' :'P',
'repeat' :'r',
'shuffle' :'z',
'random' :'x',
'consume' :'c',
'updating' :'u',
'single' :'s',
'humidity' :'h',
'temperature' :'t',
}
def __init__(self, tty, baud, verbose):
2017-02-04 23:35:03 +01:00
self.tty = tty
self.baud = baud
self.verbose = verbose
2017-02-04 23:35:03 +01:00
self.serial = serial.Serial(tty, baud)
2017-02-26 04:13:16 +01:00
time.sleep(0.2)
2017-02-04 23:35:03 +01:00
2017-02-26 04:13:16 +01:00
if (self.serial.readline().decode('ascii').strip() == self.keys['write']*4):
print("All okay")
time.sleep(0.5)
def __del__(self):
self.close()
def close(self):
self.serial = None
2017-02-04 23:35:03 +01:00
def setup(self):
print("Setup")
print(self.serial)
print("------")
def vprint(self, string):
if self.verbose > 1:
print(string)
2017-02-04 23:35:03 +01:00
def check_serial(self):
2017-02-04 23:35:03 +01:00
if ( self.serial.inWaiting() > 0 ):
#if incoming bytes are waiting to be read from the serial input buffer
#read the bytes and convert from binary array to ASCII
string = self.serial.readline().decode('ascii')
2017-02-26 04:13:16 +01:00
if string != 1 and string != "\n":
print(string)
2017-02-04 23:35:03 +01:00
return string
def write_serial(self, string):
2017-02-26 04:13:16 +01:00
self.vprint(self.keys['write']+string)
wait_time = 0.3 + 20 * (len(string)+2) / float(self.baud)
self.serial.write(self.keys['write']+string+"\n")
2017-02-04 23:35:03 +01:00
time.sleep(wait_time)
string = self.serial.readline().decode('ascii')
time.sleep(0.2)
self.vprint(string)
self.vprint(self.check_serial())
self.vprint("Ready")
2017-02-26 04:13:16 +01:00
2017-02-04 23:35:03 +01:00
def read_serial(self, string):
2017-02-26 04:13:16 +01:00
self.serial.write(self.keys['read']+string+"\n")
2017-02-04 23:35:03 +01:00
return self.serial.readline()
2017-02-26 04:13:16 +01:00
def read_prop(self, prop):
return self.read_serial(prop)
def write_prop(self, prop, new=None, force=False):
if force or new is not None and self.stats[prop] != new:
#write to serial
if force :
new = self.stats[prop]
self.write_serial(prop+new)
2017-02-04 23:35:03 +01:00
def Checker(self, varfrom, varto, string):
if varfrom == varto:
return 0
self.write_serial(string)
return 1
2017-02-26 04:13:16 +01:00
def write_all(self):
for prop in stats:
write_prop(prop, force=True)
return
### Properties
# Ints
2017-02-04 23:35:03 +01:00
@property
def volume(self):
2017-02-26 04:13:16 +01:00
return read_prop('volume')
2017-02-04 23:35:03 +01:00
@volume.setter
def volume(self, new):
2017-02-26 04:13:16 +01:00
if self.Checker(self.stats['volume'], new, self.keys['volume']+str(new)):
2017-02-04 23:35:03 +01:00
print("Volume: {}".format(new))
2017-02-26 04:13:16 +01:00
self.stats['volume'] = new
2017-02-04 23:35:03 +01:00
# Strings
@property
def title(self):
2017-02-26 04:13:16 +01:00
return read_prop('title')
2017-02-04 23:35:03 +01:00
@title.setter
def title(self, new):
2017-02-26 04:13:16 +01:00
if self.Checker(self.stats['title'], new, self.keys['title']+str(new)):
2017-02-04 23:35:03 +01:00
print("Title: {}".format(new))
2017-02-26 04:13:16 +01:00
self.stats['title'] = new
2017-02-04 23:35:03 +01:00
@property
def artist(self):
2017-02-26 04:13:16 +01:00
return read_prop('artist')
2017-02-04 23:35:03 +01:00
@artist.setter
def artist(self, new):
2017-02-26 04:13:16 +01:00
if self.Checker(self.stats['artist'], new, self.keys['artist']+str(new)):
2017-02-04 23:35:03 +01:00
print("Artist: {}".format(new))
2017-02-26 04:13:16 +01:00
self.stats['artist'] = new
2017-02-04 23:35:03 +01:00
@property
def album(self):
2017-02-26 04:13:16 +01:00
return read_prop('album')
2017-02-04 23:35:03 +01:00
@album.setter
def album(self, new):
2017-02-26 04:13:16 +01:00
if self.Checker(self.stats['album'], new, self.keys['album']+str(new)):
2017-02-04 23:35:03 +01:00
print("Album: {}".format(new))
2017-02-26 04:13:16 +01:00
self.stats['album'] = new
2017-02-04 23:35:03 +01:00
# Booleans
@property
def repeat(self):
2017-02-26 04:13:16 +01:00
return read_prop('repeat')
2017-02-04 23:35:03 +01:00
@repeat.setter
def repeat(self, new):
2017-02-26 04:13:16 +01:00
if self.Checker(self.stats['repeat'], new, self.keys['repeat']+str(new*1)):
2017-02-04 23:35:03 +01:00
print("Repeat: {}".format(new))
2017-02-26 04:13:16 +01:00
self.stats['repeat'] = new
2017-02-04 23:35:03 +01:00
@property
def shuffle(self):
2017-02-26 04:13:16 +01:00
return read_prop('shuffle')
2017-02-04 23:35:03 +01:00
@shuffle.setter
def shuffle(self, new):
2017-02-26 04:13:16 +01:00
if self.Checker(self.stats['shuffle'], new, self.keys['shuffle']+str(new*1)):
2017-02-04 23:35:03 +01:00
print("Shuffle: {}".format(new))
2017-02-26 04:13:16 +01:00
self.stats['shuffle'] = new
# Booleans
@property
def repeat(self):
return read_prop('repeat')
@repeat.setter
def repeat(self, new):
if self.Checker(self.stats['repeat'], new, self.keys['repeat']+str(new*1)):
print("Repeat: {}".format(new))
self.stats['repeat'] = new
@property
def shuffle(self):
return read_prop('shuffle')
@shuffle.setter
def shuffle(self, new):
if self.Checker(self.stats['shuffle'], new, self.keys['shuffle']+str(new*1)):
print("Shuffle: {}".format(new))
self.stats['shuffle'] = new
2017-02-04 23:35:03 +01:00
@property
def single(self):
2017-02-26 04:13:16 +01:00
return read_prop('single')
2017-02-04 23:35:03 +01:00
@single.setter
def single(self, single):
2017-02-26 04:13:16 +01:00
if self.Checker(self.stats['single'], new, self.keys['single']+str(new*1)):
2017-02-04 23:35:03 +01:00
print("Single: {}".format(new))
2017-02-26 04:13:16 +01:00
self.stats['single'] = new
2017-02-04 23:35:03 +01:00
@property
def random(self):
2017-02-26 04:13:16 +01:00
return read_prop('random')
2017-02-04 23:35:03 +01:00
@random.setter
def random(self, new):
2017-02-26 04:13:16 +01:00
if self.Checker(self.stats['random'], new, self.keys['random']+str(new*1)):
2017-02-04 23:35:03 +01:00
print("Random: {}".format(new))
2017-02-26 04:13:16 +01:00
self.stats['random'] = new
2017-02-04 23:35:03 +01:00
@property
def consume(self):
2017-02-26 04:13:16 +01:00
return read_prop('random')
2017-02-04 23:35:03 +01:00
@consume.setter
def consume(self, new):
2017-02-26 04:13:16 +01:00
if self.Checker(self.stats['consume'], new, self.keys['consume']+str(new*1)):
2017-02-04 23:35:03 +01:00
print("Consume: {}".format(new))
2017-02-26 04:13:16 +01:00
self.stats['consume'] = new
2017-02-04 23:35:03 +01:00
2017-02-26 04:13:16 +01:00
@property
def update(self):
return read_prop('updating')
@update.setter
def update(self, new):
if self.Checker(self.stats['updating'], new, self.keys['updating']+str(new*1)):
print("Update: {}".format(new))
self.stats['updating'] = new
2017-02-04 23:35:03 +01:00
@property
def playing(self):
2017-02-26 04:13:16 +01:00
return read_prop('playing')
2017-02-04 23:35:03 +01:00
@playing.setter
def playing(self, new):
2017-02-26 04:13:16 +01:00
if self.Checker(self.stats['playing'], new, self.keys['playing']+str(new*1)):
2017-02-04 23:35:03 +01:00
print("Playing: {}".format(new))
2017-02-26 04:13:16 +01:00
self.stats['playing'] = new