Commit 40a5168a authored by Evan Raskob's avatar Evan Raskob

fixed bug with bad loop when adding data to commands list in OSC in...

fixed bug with bad loop when adding data to commands list in OSC in p5SerialSendReceive and added ring buffer to AnalogSerialAPI for stability
parent fbc68190
......@@ -8,13 +8,16 @@ void setup() {
while (!Serial);
}
unsigned int startIndex = 0; // start index for buffer
unsigned int endIndex = 0; // end index for buffer
const unsigned int bufferSize = 256; // number of bytes we can read
char buffer[bufferSize];
char stringBuffer[bufferSize]; // or copying parsed strings into
int charsRead = 0; // amount of bytes we've read
void loop() {
// we need this while loop to fully empty the serial buffer
while (Serial.available())
{
......@@ -27,18 +30,27 @@ void loop() {
if (charsRead < bufferSize )
{
if (charRead != '\n') {
buffer[charsRead] = charRead;
buffer[endIndex] = charRead;
charsRead = charsRead + 1;
endIndex = (endIndex + 1) % bufferSize; // ring buffer
// for testing:
// Serial.print(charsRead);
}
else
{
//Serial.println("process command[");
//Serial.print(charsRead);
//Serial.print("]\n");
buffer[charsRead] = '\0'; // terminate string
// Serial.println("process command[");
// Serial.print(charsRead);
// Serial.println("]");
buffer[endIndex] = '\0'; // terminate string
// copy to string buffer for processing
for (int i = 0; i <= charsRead; i++) {
stringBuffer[i] = buffer[(startIndex + i) % bufferSize];
//Serial.print(stringBuffer[i]);
}
//Serial.println();
// process results
//Serial.print(':');
......@@ -50,10 +62,10 @@ void loop() {
// sscanf reads the values and stores them in variables we just declared. %d represents a number of digits to read
// the "n" returned by this function is the number of matches. Whitespace is ignored, by default.
// our protocol - 1 character (means an 'action' we perform)
// followed by up to 3 int numbers separated by ,'s
// we do NOT expect to get negative numbers
// we do not expect a cmd character of '-'
// our protocol - 1 character (means an 'action' we perform)
// followed by up to 3 int numbers separated by ,'s
// we do NOT expect to get negative numbers
// we do not expect a cmd character of '-'
const int INVALID = -1;
char cmd = '-'; // the command character: don't expect to read
......@@ -61,42 +73,67 @@ void loop() {
int b = INVALID; // number 2
int c = INVALID; // number 3
// define a protocol: call and response for analogue values
// cmd character 'a' means read an analogue port,
// then we expect a port number for variable a
// for example, "a0" means read and return port A0
// that also means "a0,1,2" because we ignore everything after the 1st argument
// define a protocol: call and response for analogue values
// cmd character 'a' means read an analogue port,
// then we expect a port number for variable a
// for example, "a0" means read and return port A0
// that also means "a0,1,2" because we ignore everything after the 1st argument
int n = sscanf(buffer, "/%c,%d,%d,%d", &cmd, &a, &b, &c);
int n = sscanf(stringBuffer, "/%c,%d,%d,%d", &cmd, &a, &b, &c);
const int ANALOGUE_PORTS = 6;
const int portMappings[] = {
A0, A1, A2, A3, A4, A5
A0, A1, A2, A3, A4, A5
};
int port = INVALID;
if (cmd == 'a')
{
if (a != INVALID && a < ANALOGUE_PORTS && a >= 0) {
port = portMappings[a];
int sensorValue = analogRead(port);
// echo back command in same format
Serial.print('/');
Serial.print(cmd);
Serial.print(",");
Serial.print(a);
Serial.print(",");
Serial.println(sensorValue); // send value, end command
}
else {
Serial.println("ERROR MESSAGE HERE");
}
// make sure to check we have valid data! There's no guarantee...
if (a != INVALID && a < ANALOGUE_PORTS && a >= 0) {
port = portMappings[a];
int sensorValue = analogRead(port);
// echo back command in same format
Serial.print('/');
Serial.print(cmd);
Serial.print(",");
Serial.print(a);
Serial.print(",");
Serial.println(sensorValue); // send value, end command
}
else {
Serial.println("ERROR MESSAGE HERE");
}
}
else if (cmd == 'l') //LEDs?
{
if (a != INVALID && b != INVALID && c != INVALID)
{
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
if (a > 1)
digitalWrite(13,HIGH);
else
digitalWrite(13,LOW);
}
// echo back command in same format -- no really needed here, though
// Serial.print('/');
// Serial.print(cmd);
// Serial.print(",");
// Serial.print(a);
// Serial.print(",");
// Serial.print(b);
// Serial.print(",");
// Serial.println(c); // send value, end command
}
charsRead = 0; // go back to beginning of buffer for next command
startIndex = endIndex; //restart ring buffer with current index
}
}
// otherwise too many bytes!
......
......@@ -26,21 +26,29 @@ NetAddress myRemoteLocation;
OscP5 oscP5;
ControlP5 gui;
int sliderValue = 0; // value of the slider, auto updated
void setup()
{
size(800, 600);
frameRate(10);
frameRate(20);
// start oscP5, listening for incoming messages at wekinator port
oscP5 = new OscP5(this, inPort);
myRemoteLocation = new NetAddress(ipAddress, outPort);
gui = new ControlP5(this);
// add a horizontal sliders, the value of this slider will be linked
// to variable 'sliderValue'
gui.addSlider("sliderValue")
.setPosition(20,20)
.setRange(0,5)
;
}
void draw()
{
background(0);
......@@ -52,10 +60,14 @@ void draw()
oscP5.send(oscMsg, myRemoteLocation);
}
OscMessage mosc = new OscMessage("/l");
int[] vals = {sliderValue,1,2};
mosc.add(vals);
oscP5.send(mosc, myRemoteLocation);
//this next line is because things might happen out of order
synchronized(oscMessagesReceived) {
// handle incoming messages from oscEvent()
for (int i=0; i < oscMessagesReceived.size(); i++)
{
......
......@@ -26,6 +26,10 @@ void stopOSC()
*/
void handleSerialCommands() {
synchronized(commandsReceived) {
//int s = commandsReceived.size();
//if (s>0) {
//println("OSC--------");
// handle all commands in buffer
while (commandsReceived.size() > 0)
{
......@@ -54,21 +58,23 @@ void handleSerialCommands() {
// OR, you could just route it to OSC without touching it:
OscMessage oscMsg = new OscMessage(cmd);
print(cmd + " ");
//print(cmd + " ");
for (int i=1; i<command.length; i++)
{
int arg = int(command[i]); // parse as number
// check it's valid!
if (arg != INVALID)
{
print(arg + " ");
// print(arg + " ");
oscMsg.add(arg); // add as integer
}
}
println();
//println();
// send the message
oscP5.send(oscMsg, myRemoteLocation);
}
//println("---------OSC");
//}
}
// done with all received commands
}
......@@ -121,10 +127,9 @@ void oscEvent(OscMessage theOscMessage) {
default:
println("ERR: bad OSC type:" + type);
} // end switch
// finally, send the serial. Should be the command character followed by list of args separated by ,'s
String cmdOut = command + "," + String.join(",", args);
//println(cmdOut);
serial.write(cmdOut + stopChar);
}
// finally, send the serial. Should be the command character followed by list of args separated by ,'s
String cmdOut = command + "," + String.join(",", args);
//println(cmdOut);
serial.write(cmdOut + stopChar);
}
......@@ -69,7 +69,9 @@ void serialAvailable(Serial serial) {
// too quickly!
//String input = trim( serial.readStringUntil(stopChar) );
String serialIn = serial.readString();
println("serial received: " + serialIn);
// Uncomment this for debugging:
//println("serial received: " + serialIn);
String serialLines[] = split(serialIn, stopChar); // we might have received a few lines of serial so we
// split them by the stop character (newline)
......@@ -86,7 +88,6 @@ void serialAvailable(Serial serial) {
if (msgs.length > 0 && msgs[0] != "-") {
synchronized(commandsReceived)
{
commandsReceived.add(msgs);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment