Changed protocol, tty not yet adapted
This commit is contained in:
parent
c1e6c76ca2
commit
5926538616
2 changed files with 157 additions and 56 deletions
92
serial.ino
92
serial.ino
|
@ -22,36 +22,66 @@ void serialEvent() {
|
|||
void setFromSerial (String inputString) {
|
||||
//remove newline
|
||||
inputString = inputString.substring(0, inputString.length() -1);
|
||||
switch ( inputString.charAt(0) ) {
|
||||
case 'V':
|
||||
volume = inputString.substring(1).toInt();
|
||||
Serial.println("Setting volume to " + String(volume));
|
||||
break;
|
||||
case 'T':
|
||||
title = inputString.substring(1);
|
||||
Serial.println("Setting title to " + String(title));
|
||||
break;
|
||||
case 'A':
|
||||
artist = inputString.substring(1);
|
||||
Serial.println("Setting artist to " + String(artist));
|
||||
break;
|
||||
case 'a':
|
||||
album = inputString.substring(1);
|
||||
Serial.println("Setting album to " + String(album));
|
||||
break;
|
||||
case 'r':
|
||||
repeat = inputString.substring(1) != 0;
|
||||
Serial.println("Setting repeat to " + String(repeat));
|
||||
break;
|
||||
case 'z':
|
||||
shuffle = inputString.substring(1) != 0;
|
||||
Serial.println("Setting shuffle to " + String(shuffle));
|
||||
break;
|
||||
default:
|
||||
Serial.println(F("!Invalid line:"));
|
||||
Serial.print(F("!"));
|
||||
Serial.println(inputString);
|
||||
break;
|
||||
char operation1 = inputString.charAt(0);
|
||||
|
||||
if ( operation1 = 'E' ){
|
||||
String string = "";
|
||||
//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;
|
||||
}
|
||||
Serial.println(string);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
|
121
ttycontrol.py
121
ttycontrol.py
|
@ -5,17 +5,95 @@ baud = 9600
|
|||
import serial
|
||||
import time
|
||||
|
||||
ser = serial.Serial(tty, baud)
|
||||
|
||||
# Setup
|
||||
print("Setup")
|
||||
print(ser)
|
||||
print("------")
|
||||
|
||||
ser.write("!Booya\n")
|
||||
ser.readline()
|
||||
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")
|
||||
|
||||
|
||||
|
||||
# Final Loop
|
||||
volume = 0
|
||||
title = "Blub"
|
||||
artist = "ee"
|
||||
|
@ -23,23 +101,16 @@ album = "dd"
|
|||
repeat = 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:
|
||||
print(volume)
|
||||
volume = (volume + 1)
|
||||
output("V" + str(volume))
|
||||
output("T" + title)
|
||||
output("A" + artist)
|
||||
output("a" + album)
|
||||
output("r" + str(repeat))
|
||||
output("z" + str(shuffle))
|
||||
print("--- output ---")
|
||||
check_serial()
|
||||
print("--- output ---")
|
||||
ser.setVolume(volume)
|
||||
ser.setTitle(title)
|
||||
ser.setArtist(artist)
|
||||
ser.setAlbum(album)
|
||||
ser.setRepeat(repeat)
|
||||
ser.setShuffle(shuffle)
|
||||
ser.readArtist()
|
||||
|
|
Loading…
Reference in a new issue