Changed protocol, tty not yet adapted

This commit is contained in:
Eric Teunis de Boone 2017-01-30 23:59:09 +01:00
parent c1e6c76ca2
commit 5926538616
2 changed files with 157 additions and 56 deletions

View file

@ -22,36 +22,66 @@ void serialEvent() {
void setFromSerial (String inputString) { void setFromSerial (String inputString) {
//remove newline //remove newline
inputString = inputString.substring(0, inputString.length() -1); inputString = inputString.substring(0, inputString.length() -1);
switch ( inputString.charAt(0) ) { char operation1 = inputString.charAt(0);
case 'V':
volume = inputString.substring(1).toInt(); if ( operation1 = 'E' ){
Serial.println("Setting volume to " + String(volume)); String string = "";
break; //Echo string like: EV means echo volume
case 'T': switch ( inputString.charAt(1) ) {
title = inputString.substring(1); case 'V':
Serial.println("Setting title to " + String(title)); string = volume;
break; break;
case 'A': case 'T':
artist = inputString.substring(1); string = title;
Serial.println("Setting artist to " + String(artist)); break;
break; case 'A':
case 'a': string = artist;
album = inputString.substring(1); break;
Serial.println("Setting album to " + String(album)); case 'a':
break; string = album;
case 'r': break;
repeat = inputString.substring(1) != 0; case 'r':
Serial.println("Setting repeat to " + String(repeat)); string = repeat;
break; break;
case 'z': case 'z':
shuffle = inputString.substring(1) != 0; string = shuffle;
Serial.println("Setting shuffle to " + String(shuffle)); break;
break; default:
default: string = "";
Serial.println(F("!Invalid line:")); break;
Serial.print(F("!")); }
Serial.println(inputString); Serial.println(string);
break;
} }
calcMaxTicks(); else if ( operation1 = 'S' ){
bool success = true;
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;
}
Serial.println(int(success));
calcMaxTicks();
}
else Serial.println(-1);
} }

View file

@ -5,17 +5,95 @@ baud = 9600
import serial import serial
import time import time
ser = serial.Serial(tty, baud)
# Setup
print("Setup")
print(ser)
print("------")
ser.write("!Booya\n") class SerialMPCduino(object):
ser.readline() 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")
# Final Loop
volume = 0 volume = 0
title = "Blub" title = "Blub"
artist = "ee" artist = "ee"
@ -23,23 +101,16 @@ album = "dd"
repeat = True repeat = True
shuffle = True shuffle = True
ser = SerialMPCduino(tty, baud)
print("loop")
def output(string):
ser.write(string+"\n")
time.sleep(0.4)
def check_serial():
if (ser.inWaiting()>0): #if incoming bytes are waiting to be read from the serial input buffer
data_str = ser.read(ser.inWaiting()).decode('ascii') #read the bytes and convert from binary array to ASCII
print(data_str)
while True: while True:
print(volume)
volume = (volume + 1) volume = (volume + 1)
output("V" + str(volume)) ser.setVolume(volume)
output("T" + title) ser.setTitle(title)
output("A" + artist) ser.setArtist(artist)
output("a" + album) ser.setAlbum(album)
output("r" + str(repeat)) ser.setRepeat(repeat)
output("z" + str(shuffle)) ser.setShuffle(shuffle)
print("--- output ---") ser.readArtist()
check_serial()
print("--- output ---")