Processing Code:
/**
*
* output_1_3
*
* Get inputs from a GUI and displays outputs at bottom of screen.
*
* Enter the appropriate number in the field. Hit enter to add it to the array. The speed is displayed to the right of the
* direction radial buttons. If the string is empty a speed of 0 is displayed.
*
*/
import processing.serial.*;
import controlP5.*;
ControlP5 cp5;
Serial myPort;
Textlabel myTLA, myTLB, myTLC, myTLD;
RadioButton rA, rB, rC, rD;
String[] out = new String[8];
String inputs;
String prevStr;
String prevIn;
int inCount = 0;
int numOut = 8;
boolean freshInput = false;
boolean isClicked = false;
boolean ready = true;
boolean firstSend = true;
PFont f;
void setup() {
size(700, 875);
cp5 = new ControlP5(this);
int c1x = 90;
PFont labels = createFont("arial", 16);
PFont in = createFont("arial", 12);
f = createFont("arial", 20);
myTLA = cp5.addTextlabel("M1").setText("Motor 1").setPosition(20, 50).setColorValue(0xffFFFFFF).setFont(labels);
myTLB = cp5.addTextlabel("M2").setText("Motor 2").setPosition(20, 100).setColorValue(0xffFFFFFF).setFont(labels);
myTLC = cp5.addTextlabel("M3").setText("Motor 3").setPosition(20, 150).setColorValue(0xffFFFFFF).setFont(labels);
myTLD = cp5.addTextlabel("M4").setText("Motor 4").setPosition(20, 200).setColorValue(0xffFFFFFF).setFont(labels);
rA = cp5.addRadioButton("m1D").setPosition(c1x+120, 50).setSize(20, 20).setColorForeground(color(120)).setColorActive(color(255))
.setColorLabel(color(255)).setItemsPerRow(2).setSpacingColumn(60).addItem("M1 Forward", 1).addItem("M1 Reverse", 2);
rB = cp5.addRadioButton("m2D").setPosition(c1x+120, 100).setSize(20, 20).setColorForeground(color(120)).setColorActive(color(255))
.setColorLabel(color(255)).setItemsPerRow(2).setSpacingColumn(60).addItem("M2 Forward", 1).addItem("M2 Reverse", 2);
rC = cp5.addRadioButton("m3D").setPosition(c1x+120, 150).setSize(20, 20).setColorForeground(color(120)).setColorActive(color(255))
.setColorLabel(color(255)).setItemsPerRow(2).setSpacingColumn(60).addItem("M3 Forward", 1).addItem("M3 Reverse", 2);
rD = cp5.addRadioButton("m4D").setPosition(c1x+120, 200).setSize(20, 20).setColorForeground(color(120)).setColorActive(color(255))
.setColorLabel(color(255)).setItemsPerRow(2).setSpacingColumn(60).addItem("M4 Forward", 1).addItem("M4 Reverse", 2);
cp5.addTextfield("m1Sinput").setPosition(c1x, 50).setSize(100, 20).setFont(in).setFocus(true).setColor(color(255, 255, 255));
cp5.addTextfield("m2Sinput").setPosition(c1x, 100).setSize(100, 20).setFont(in).setFocus(false).setColor(color(255, 255, 255));
cp5.addTextfield("m3Sinput").setPosition(c1x, 150).setSize(100, 20).setFont(in).setFocus(false).setColor(color(255, 255, 255));
cp5.addTextfield("m4Sinput").setPosition(c1x, 200).setSize(100, 20).setFont(in).setFocus(false).setColor(color(255, 255, 255));
myPort = new Serial(this, Serial.list()[0], 38400);
}
void draw() {
background(0);
//grid();
myButton();
showSpeeds();
display();
send();
receive();
}
//-----------------------------------------------------------------------------------
// OTHER METHODS
//-----------------------------------------------------------------------------------
//working on what this does
void controlEvent(ControlEvent theEvent) {
if (theEvent.isAssignableFrom(Textfield.class)) {
if (theEvent.getStringValue()!="") {
if (theEvent.getName()=="m1Sinput") {
out[0] = theEvent.getStringValue();
}
else if (theEvent.getName()=="m2Sinput") {
out[2] = theEvent.getStringValue();
}
else if (theEvent.getName()=="m3Sinput") {
out[4] = theEvent.getStringValue();
}
else if (theEvent.getName()=="m4Sinput") {
out[6] = theEvent.getStringValue();
}
}
}
else if (theEvent.isFrom(rA)) {
if (theEvent.getValue() == 1.0) {
out[1] = "F";
}
else if (theEvent.getValue() == 2.0) {
out[1] = "R";
}
}
else if (theEvent.isFrom(rB)) {
if (theEvent.getValue() == 1.0) {
out[3] = "F";
}
else if (theEvent.getValue() == 2.0) {
out[3] = "R";
}
}
else if (theEvent.isFrom(rC)) {
if (theEvent.getValue() == 1.0) {
out[5] = "F";
}
else if (theEvent.getValue() == 2.0) {
out[5] = "R";
}
}
else if (theEvent.isFrom(rD)) {
if (theEvent.getValue() == 1.0) {
out[7] = "F";
}
else if (theEvent.getValue() == 2.0) {
out[7] = "R";
}
inCount++;
freshInput = true;
}
//------------------------------------------------------------------------------
// My Methods
//------------------------------------------------------------------------------
//draws a white grid on the GUI, for helping set up visually
void grid() {
stroke(255, 255, 255);
for (int x = 0; x<700; x+=50) {
line(x, 0, x, 850);
}
for (int y = 0; y<875; y+=50) {
line(0, y, 700, y);
}
}
void myButton() {
fill(color(173, 255, 47));
rect(550, 50, 100, 50);
stroke(0);
fill(255);
textFont(f, 20);
text("START", 568, 82);
if (mouseX>=550&&mouseX<=650&&mouseY>=50&&mouseY<=100&&mousePressed) {
isClicked = true;
}
}
void showSpeeds() {
fill(255);
for (int i = 0; i<numOut; i+=2) {
if (out[i]==null||out[i].isEmpty()) {
text("0", 400, 70+25*i);
}
else {
text(out[i], 400, 70+25*i);
}
}
}
void display() {
if (freshInput) {
for (int i=0; i<numOut; i++) {
print("["+i+"]"+out[i]+" ");
}
println("The current value of inCount is: "+ inCount);
freshInput = false;
}
}
void send() {
if (ready||firstSend){
boolean full = true;
for (int j =0; j<numOut; j++) {
if (out[j]==null||out[j].isEmpty()) {
full = false;
}
}
if ((inCount>=numOut||isClicked)&&full) {
String theStr = "";
for (int i = 0; i<numOut; i++) {
theStr = theStr+out[i];
}
if (theStr != prevStr){
myPort.write("<"+theStr+">");
println(theStr);
prevStr = theStr;
}
inCount = 0;
isClicked = false;
}
ready = false;
}
}
void receive(){
char[] inB = new char[100];
int serialIn = 0;
boolean start = false;
boolean end = false;
while (myPort.available()>0&&serialIn<100){
char inBuffer = myPort.readChar();
if (inBuffer=='['){
start = true;
end = false;
} else if (inBuffer == ']'){
end = true;
break;
} else {
inB[serialIn] = '\0';
inB[serialIn] = inBuffer;
serialIn++;
}
}
if(start&&end){
for (int i = 0; i<100; i++){
inputs = inputs+inB[i];
}
println(inputs);
}
if (inputs == "Next input: "){
ready = true;
firstSend = false;
}
start = false;
end = false;
serialIn = 0;
}