import serial import time class SerialMPCduino(object): 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): self.tty = tty self.baud = baud self.verbose = verbose self.serial = serial.Serial(tty, baud) time.sleep(0.2) 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 def setup(self): print("Setup") print(self.serial) print("------") def vprint(self, string): if self.verbose > 1: print(string) def check_serial(self): 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') if string != 1 and string != "\n": print(string) return string def write_serial(self, string): 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") 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") def read_serial(self, string): self.serial.write(self.keys['read']+string+"\n") return self.serial.readline() 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) def Checker(self, varfrom, varto, string): if varfrom == varto: return 0 self.write_serial(string) return 1 def write_all(self): for prop in stats: write_prop(prop, force=True) return ### Properties # Ints @property def volume(self): return read_prop('volume') @volume.setter def volume(self, new): if self.Checker(self.stats['volume'], new, self.keys['volume']+str(new)): print("Volume: {}".format(new)) self.stats['volume'] = new # Strings @property def title(self): return read_prop('title') @title.setter def title(self, new): if self.Checker(self.stats['title'], new, self.keys['title']+str(new)): print("Title: {}".format(new)) self.stats['title'] = new @property def artist(self): return read_prop('artist') @artist.setter def artist(self, new): if self.Checker(self.stats['artist'], new, self.keys['artist']+str(new)): print("Artist: {}".format(new)) self.stats['artist'] = new @property def album(self): return read_prop('album') @album.setter def album(self, new): if self.Checker(self.stats['album'], new, self.keys['album']+str(new)): print("Album: {}".format(new)) self.stats['album'] = 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 # 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 @property def single(self): return read_prop('single') @single.setter def single(self, single): if self.Checker(self.stats['single'], new, self.keys['single']+str(new*1)): print("Single: {}".format(new)) self.stats['single'] = new @property def random(self): return read_prop('random') @random.setter def random(self, new): if self.Checker(self.stats['random'], new, self.keys['random']+str(new*1)): print("Random: {}".format(new)) self.stats['random'] = new @property def consume(self): return read_prop('random') @consume.setter def consume(self, new): if self.Checker(self.stats['consume'], new, self.keys['consume']+str(new*1)): print("Consume: {}".format(new)) self.stats['consume'] = new @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 @property def playing(self): return read_prop('playing') @playing.setter def playing(self, new): if self.Checker(self.stats['playing'], new, self.keys['playing']+str(new*1)): print("Playing: {}".format(new)) self.stats['playing'] = new