From c0ad31810fbad9d2a6075b6fa99d294d99c430c4 Mon Sep 17 00:00:00 2001 From: Eric Teunis de Boone Date: Sat, 4 Feb 2017 23:35:03 +0100 Subject: [PATCH] Working for data transfer from process --- __init__.py | 0 layout.ino | 63 ++++++++-------- mpcArduino.ino | 17 +++-- serial.ino | 85 +++++++++------------ serialmpcduino.py | 173 +++++++++++++++++++++++++++++++++++++++++++ ttycontrol.py | 184 ++++++++++++++++++++-------------------------- 6 files changed, 328 insertions(+), 194 deletions(-) create mode 100644 __init__.py create mode 100644 serialmpcduino.py diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/layout.ino b/layout.ino index 7459b64..fab7a92 100644 --- a/layout.ino +++ b/layout.ino @@ -11,51 +11,46 @@ void layout(unsigned int tick) { display.setTextColor(WHITE); display.setTextWrap(false); - updateLine1(tick); - updateLine2(tick); - updateLine3(tick); - updateLine4(tick); + if ( playing || tick % 3 ) { + display.setCursor(0, 0); + // First line (20) + if (volume < 100 ) display.print(' '); + display.print(volume); + display.print(F("% || ")); + if (repeat_bool) display.print('r'); + else display.print(' '); + if (random_bool) display.print('x'); + else display.print(' '); + if (single_bool) display.print('s'); + else display.print(' '); + if (consume_bool) display.print('c'); + else display.print(' '); + if (shuffle_bool) display.print('z'); + else display.print(' '); + if (updating_bool) display.print('u'); + else display.print(' '); + display.print(" || "); + display.print(progress); + display.print("% "); + } - display.display(); -} -void updateLine1(unsigned int tick) { - //reset line - display.drawRect(0, 0, display.width(), 8, BLACK); - display.setCursor(0, 0); - // First line (20) - if (volume < 100 ) display.print(' '); - display.print(volume); - display.print(F("% ")); - display.print(F("|mm:ss/mm:ss|")); - display.print(F(" ")); - if (repeat) display.print('r'); - else display.print('-'); - if (shuffle) display.print('z'); - else display.print('-'); - -} -void updateLine2(unsigned int tick) { - //reset line - display.drawRect(0, 8, display.width(), 16, BLACK); + //line 2 display.setCursor(0, 8); display.print(textscroll(title,tick)); display.startscrollleft(0x06, 0x0F); -} -void updateLine3(unsigned int tick) { - //reset line - display.drawRect(0, 16, display.width(), 24, BLACK); + //line 3 display.setCursor(0, 16); display.print(textscroll(artist, tick)); -} -void updateLine4(unsigned int tick) { - //reset line - display.drawRect(0, 24, display.width(), 32, BLACK); + //line 4 display.setCursor(0, 24); display.print(textscroll(album, tick)); + + display.display(); } + String textscroll(String text, unsigned int tick) { int chars = 21; int len = text.length(); @@ -72,5 +67,7 @@ String textscroll(String text, unsigned int tick) { int calcMaxTicks() { maxticks = title.length() * artist.length() * album.length(); + Serial.print("MT:"); + Serial.println(maxticks); return maxticks; } diff --git a/mpcArduino.ino b/mpcArduino.ino index a0fbf57..dd86c6d 100644 --- a/mpcArduino.ino +++ b/mpcArduino.ino @@ -46,12 +46,19 @@ static const unsigned char PROGMEM logo16_glcd_bmp[] = String inputString = ""; // a string to hold incoming data boolean stringComplete = false; // whether the string is complete -boolean repeat = false; -boolean shuffle = false; +boolean repeat_bool = true; +boolean shuffle_bool = true; +boolean consume_bool = true; +boolean random_bool = true; +boolean updating_bool = true; +boolean single_bool = true; +boolean playing = false; + int volume = 100; -String title = "Yesterdays"; -String artist = "Guns 'n Roses"; -String album = "Appetite for Destruction"; +int progress = 51; +String title = " << << Title >> >> "; +String artist = " << << Artist >> >> "; +String album = " << << Album >> >> "; unsigned int tick = 0; unsigned int maxticks = 0; diff --git a/serial.ino b/serial.ino index 60cdaa4..53589fd 100644 --- a/serial.ino +++ b/serial.ino @@ -23,65 +23,50 @@ void setFromSerial (String inputString) { //remove newline inputString = inputString.substring(0, inputString.length() -1); char operation1 = inputString.charAt(0); + String string = ""; - if ( operation1 = 'E' ){ - String string = ""; + if ( operation1 == 'E' ){ //Echo string like: EV means echo volume switch ( inputString.charAt(1) ) { - case 'V': - string = volume; - break; - case 'T': - string = title; - break; - case 'A': - string = artist; - break; - case 'a': - string = album; - break; - case 'r': - string = repeat; - break; - case 'z': - string = shuffle; - break; - default: - string = ""; - break; + case 'V': string = volume; break; + case 'T': string = title; break; + case 'A': string = artist; break; + case 'a': string = album; break; + case 'r': string = repeat_bool; break; + case 'z': string = shuffle_bool; break; + case 'x': string = random_bool; break; + case 'c': string = consume_bool; break; + case 'u': string = updating_bool; break; + case 's': string = single_bool; break; + case 'p': string = progress; break; + case 'P': string = playing; break; + default: string = "!"; break; } Serial.println(string); } - else if ( operation1 = 'S' ){ + else if ( operation1 == 'S' ){ bool success = true; + string = inputString.substring(2); + switch ( inputString.charAt(1) ) { - case '!'://Echo - Serial.println(inputString.substring(1)); - break; - case 'V': - volume = inputString.substring(1).toInt(); - break; - case 'T': - title = inputString.substring(1); - break; - case 'A': - artist = inputString.substring(1); - break; - case 'a': - album = inputString.substring(1); - break; - case 'r': - repeat = inputString.substring(1) != 0; - break; - case 'z': - shuffle = inputString.substring(1) != 0; - break; - default: - success = false; - break; + case 'V': volume = string.toInt(); break; + case 'T': title = string; break; + case 'A': artist = string; break; + case 'a': album = string; break; + case 'p': progress = string.toInt(); break; + case 'P': playing = (string != 0); break; + case 'r': repeat_bool = (string != 0); break; + case 'z': shuffle_bool = (string != 0); break; + case 'x': random_bool = (string != 0); break; + case 'c': consume_bool = (string != 0); break; + case 'u': updating_bool = (string != 0); break; + case 's': single_bool = (string != 0); break; + //Echo + case '!': Serial.println(string); break; + default: success = false; break; } - Serial.println(int(success)); calcMaxTicks(); + Serial.println(int(success), DEC); } - else Serial.println(-1); + else Serial.println(-1, DEC); } diff --git a/serialmpcduino.py b/serialmpcduino.py new file mode 100644 index 0000000..6106a2c --- /dev/null +++ b/serialmpcduino.py @@ -0,0 +1,173 @@ +import serial +import time + +class SerialMPCduino(object): + __volume = None + __progress = None + + __title = None + __artist = None + __album = None + + __playing = None + __repeat = None + __shuffle = None + __random = None + __single = None + __consume = None + + def __init__(self, tty, baud): + self.tty = tty + self.baud = baud + self.serial = serial.Serial(tty, baud) + + + def setup(self): + print("Setup") + print(self.serial) + print("------") + + def check_serial(self): + while not self.serial.inWaiting(): + pass + + 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') + print(string) + return string + + def write_serial(self, string): + print("S"+string) + wait_time = 0.3 + 10 * len(string) / float(self.baud) + self.serial.write("S"+string+"\n") + time.sleep(wait_time) + + string = self.serial.readline().decode('ascii') + time.sleep(0.2) + print(string) + print("Ready") + + def read_serial(self, string): + self.serial.write("E"+string+"\n") + return self.serial.readline() + + def Checker(self, varfrom, varto, string): + if varfrom == varto: + return 0 + + self.write_serial(string) + return 1 + + + # Percentages + @property + def progress(self): + return self.read_serial("p") + @progress.setter + def progress(self, new): + if self.Checker(self.__progress, new, "p"+str(new)): + print("Progress: {}".format(new)) + self.__progress = new + + @property + def volume(self): + return self.read_serial("V") + @volume.setter + def volume(self, new): + if self.Checker(self.__volume, new, "V"+str(new)): + print("Volume: {}".format(new)) + self.__volume = new + + # Strings + @property + def title(self): + return self.write_serial("T") + @title.setter + def title(self, new): + if self.Checker(self.__title, new, "T"+str(new)): + print("Title: {}".format(new)) + self.__title = new + + @property + def artist(self): + return self.write_serial("A") + @artist.setter + def artist(self, new): + if self.Checker(self.__artist, new, "A"+str(new)): + print("Artist: {}".format(new)) + self.__artist = new + + @property + def album(self): + return self.write_serial("a") + @album.setter + def album(self, new): + if self.Checker(self.__album, new, "a"+str(new)): + print("Album: {}".format(new)) + self.__album = new + + # Booleans + @property + def repeat(self): + return self.write_serial("r") + @repeat.setter + def repeat(self, new): + if self.Checker(self.__repeat, new, "r"+str(new)): + print("Repeat: {}".format(new)) + self.__repeat = new + + @property + def shuffle(self): + return self.write_serial("z") + @shuffle.setter + def shuffle(self, new): + if self.Checker(self.__shuffle, new, "z"+str(new)): + print("Shuffle: {}".format(new)) + self.__shuffle = new + + @property + def single(self): + return self.read_serial("s") + @single.setter + def single(self, single): + if self.Checker(self.__single, new, "s"+str(new)): + print("Single: {}".format(new)) + self.__single = new + + @property + def random(self): + return self.read_serial("x") + @random.setter + def random(self, new): + if self.Checker(self.__random, new, "x"+str(new)): + print("Random: {}".format(new)) + self.__random = new + + @property + def consume(self): + return self.read_serial("x") + @consume.setter + def consume(self, new): + if self.Checker(self.__consume, new, "c"+str(new)): + print("Consume: {}".format(new)) + self.__consume = new + + @property + def update(self): + return self.read_serial("u") + @update.setter + def update(self, new): + if self.Checker(self.__update, new, "u"+str(new)): + print("Update: {}".format(new)) + self.__update = new + + @property + def playing(self): + return self.read_serial("P") + @playing.setter + def playing(self, new): + if self.Checker(self.__playing, new, "P"+str(new)): + print("Playing: {}".format(new)) + self.__playing = new diff --git a/ttycontrol.py b/ttycontrol.py index b82921f..7b9c205 100755 --- a/ttycontrol.py +++ b/ttycontrol.py @@ -1,116 +1,88 @@ #!/usr/bin/env python -tty = '/dev/ttyACM1' -baud = 9600 - -import serial +from serialmpcduino import SerialMPCduino +import subprocess import time +tty = '/dev/ttyACM0' +baud = 9600 +mpdhost = 'fatserf.thuis' +mpcformat = "+%title%\t%artist%\t%album%" - -class SerialMPCduino(object): - volume = None - title = None - artist = None - album = None - repeat = None - shuffle = None - can_receive = True - - def __init__(self, tty, baud): - self.serial = serial.Serial(tty, baud) - - # Setup - print("Setup") - print(self.serial) - print("------") - - 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 - print(self.serial.readline().decode('ascii')) - # Make sure buffer is empty - self.check_serial() - - def write_serial(self, string): - self.serial.write("S"+string+"\n") - if not self.serial.read(1): - print("Error") - self.check_serial() - - def read_serial(self, string): - self.serial.write("E"+string+"\n") - return self.serial.readline() - - - - def setVolume(self, volume): - if self.volume != volume: - self.volume = volume - self.write_serial("V" + str(volume)) - - def setTitle(self, title): - if self.title != title: - self.title = title - self.write_serial("T" + str(title)) - - def setArtist(self, artist): - if self.artist != artist: - self.artist = artist - self.write_serial("A" + str(artist)) - - def setAlbum(self, album): - if self.album != album: - self.album = album - self.write_serial("a" + str(album)) - - def setRepeat(self, repeat): - if self.repeat != repeat: - self.repeat = repeat - self.write_serial("r" + str(repeat)) - - def setShuffle(self, shuffle): - if self.shuffle != shuffle: - self.shuffle = shuffle - self.write_serial("z" + str(shuffle)) - - def readVolume(self): - return self.read_serial("V") - - def readTitle(self): - return self.read_serial("T") - - def readArtist(self): - return self.read_serial("A") - - def readAlbum(self): - return self.read_serial("a") - - def readRepeat(self): - return self.read_serial("r") - - def readShuffle(self): - return self.read_serial("z") - - +title = None +artist = None +album = None volume = 0 -title = "Blub" -artist = "ee" -album = "dd" +#progress = 0 repeat = True shuffle = True +random = True +single = True +consume = True +playing = True -ser = SerialMPCduino(tty, baud) -print("loop") -while True: - print(volume) - volume = (volume + 1) - ser.setVolume(volume) - ser.setTitle(title) - ser.setArtist(artist) - ser.setAlbum(album) - ser.setRepeat(repeat) - ser.setShuffle(shuffle) - ser.readArtist() + #mpctext = subprocess.check_output(['mpc', 'current', '--wait', '-h', mpdhost, '-f', mpcformat]) + +if True: + ser = SerialMPCduino(tty, baud) + time.sleep(0.5) + while True: + mpctext = subprocess.check_output(['mpc', '-h', mpdhost, '-f', mpcformat]) + # Parse output + i = 0 + for line in mpctext.rstrip().split("\n"): + if i == 0: + # Make sure it still works + if not line.startswith('+'): + playing = False + break + + line = line[1:] + + # Parse it + line = line.split("\t") + title = line[0].strip() + artist = line[1].strip() + album = line[2].strip() + elif i == 1: + line = line.replace(" "," ") + line = line.replace(" ","\t") + line = line.split("\t") + + playing = line[0][1:-1] == "playing" + + #progress = line[-1][1:-2] + elif i == 2: + line = line.replace(" ","\t") + line = line.replace(" ","") + line = line.split("\t") + volume = line[0].split(':')[1] + repeat = line[1].split(':')[1] == 'on' + random = line[2].split(':')[1] == 'on' + single = line[3].split(':')[1] == 'on' + consume = line[4].split(':')[1] == 'on' + i = i +1 + + #print("Title: {}".format(title)) + #print("Artist: {}".format(artist)) + #print("Album: {}".format(album)) + #print("Volume: {}".format(volume)) + #print("Progress: {}".format(progress)) + #print("Repeat: {}".format(repeat)) + #print("Shuffle: {}".format(shuffle)) + #print("Random: {}".format(random)) + #print("Single: {}".format(single)) + #print("Consume: {}".format(consume)) + #print("Playing: {}".format(playing)) + #raw_input("") + + ser.playing = playing + ser.volume = volume + #ser.progress = progress + ser.title = title + ser.artist = artist + ser.album = album + ser.repeat =repeat + ser.shuffle = shuffle + time.sleep(0.8)