Processing: TAB 1 GUI
//SerialGUI_Processing_v3 13.02.2019
/*ToDo:
*total cleanup!!!
*private/public not needed for TAB
*only transmit data if values changed?
*reduce draw() calculations
*update serialTimeout procedure
*timeout calc: lastSerialRequestMs - ...
*/
/*Description:
*processing acts as serial "master".
*arduino is only sending data if requestet from master
*
*/
//import serialHandler; //?
//final variables:
final byte arduinoId = 45, processingId = 25;
final byte X = 0, Y = 1;
final byte DIPin1 = 2, DIPin2 = 3;
final byte AIPin1 = 0, AIPin2 = 1;
final byte DOPin1 = 4, DOPin2 = 13;
final byte AOPin1 = 9, AOPin2 = 10;
interface Button {
byte
Name1 = 0,
Name2 = 1;
};
interface Output {
byte
Name1 = 0,
Name2 = 1;
};
interface Slider {
byte
Name1 = 0,
Name2 = 1;
};
final int normalFontSize = 12, titleFontSize = 14;
final int generalSpace = 2;
final int buttonSize = 25; //diameter of rect's X=Y
final int buttonStartPos = buttonSize;
final int circleSize = buttonSize;
final int circleStartPos = circleSize + circleSize/2 + 1;
final int circleSpace = generalSpace;
final int sliderStartPos = 1;
final int sliderSize = 15;
final int sliderSpace = generalSpace;
//colors:
final color wndColor = #F0F0F0; //window color
final color normalFontCol = #000000, titleFontCol = #A50202;
final color colorOFF = #FF0000, colorON = #00FF0A;
final color rectColBorder = #DBB837;
final color circColBorder = #0D26FA;
final color sliderBorder = #03FA10;
//variables:
//private long lastUpdateMs = 0;
byte arduinoDIByte1 = 0; //or as array? , arduinoDIByte2 = 0;
byte DOByte1 = 0, lastDOByte1 = 1;
//public int arduinoAOVal1 = 0, arduinoAOVal2 = 0;
int arduinoAIVal1 = 0, arduinoAIVal2 = 0;
int sliderEndPos = 0;
boolean[] buttonState = {false, false};
boolean[] sliderState = {false, false};
int[] AOVal = {0, 0}, lastAOVal = {1, 1};
int textXPos = buttonSize + 5; //define space from button
int[] textYPos = {0, 0};
int[][] rectPos = { {0,0}, {0,0} };
int[][] circPos = { {0,0}, {0,0} };
int[][] sliderPos = { {0,0}, {0,0} };
void setup()
{
size(250, 200);
serialStart();
serialConnect(); //is blocking!
//position of Button.Name1 [rect]
rectPos[Button.Name1][X] = 1;
rectPos[Button.Name1][Y] = buttonStartPos;
//position of Button.Name2
rectPos[Button.Name2][X] = rectPos[Button.Name1][X];
rectPos[Button.Name2][Y] = rectPos[Button.Name1][Y] + buttonSize + generalSpace;
//position of Output.Name1 (circle)
circPos[Output.Name1][X] = width/2 + circleSize/2 + 3;
circPos[Output.Name1][Y] = circleStartPos;
//position of Output.Name2 (circle)
circPos[Output.Name2][X] = circPos[Output.Name1][X];
circPos[Output.Name2][Y] = circPos[Output.Name1][Y] + circleSize + circleSpace;
//position of Slider.Name1 [moveable rect]
sliderPos[Slider.Name1][X] = sliderStartPos;
sliderPos[Slider.Name1][Y] = rectPos[Button.Name2][Y] + 45 + normalFontSize;
//position of Slider.Name1 [moveable rect]
sliderPos[Slider.Name2][X] = sliderStartPos;
sliderPos[Slider.Name2][Y] = sliderPos[Slider.Name1][Y] + sliderSize + sliderSpace + normalFontSize;
//calculate sliderEndPos
sliderEndPos = width/2 - sliderSpace - sliderSize;
//calculate text pos
for (int t = 0; t < 2; t++) textYPos[t] = rectPos[t][Y] + buttonSize - normalFontSize / 2;
}//void setup() END
void draw() //like void loop()
{
// !!! reduce calculations !!!
//window:
background(wndColor); //wndBackground color
//text: titles
fill(titleFontCol);
textSize(titleFontSize);
text("Digital OUT", 1, titleFontSize);
text("Digital IN", width/2 + generalSpace, titleFontSize);
text("Analog OUT", 1, rectPos[Button.Name2][Y] + buttonSize + generalSpace + titleFontSize);
text("Analog IN", width/2 + generalSpace, rectPos[Button.Name2][Y] + buttonSize + generalSpace + titleFontSize);
//text: normal
fill(normalFontCol);
textSize(normalFontSize);
text("Button Pin: " + DOPin1, textXPos, textYPos[0]);
text("Button Pin: " + DOPin2, textXPos, textYPos[1]);
text("State Pin: " + DIPin1, textXPos+width/2, textYPos[0]);
text("State Pin: " + DIPin2, textXPos+width/2, textYPos[1]);
text("PWM Pin: " + AOPin1 + " = " + AOVal[0], sliderStartPos, sliderPos[Slider.Name1][Y] - sliderSpace);
text("PWM Pin: " + AOPin2 + " = " + AOVal[1], sliderStartPos, sliderPos[Slider.Name2][Y] - sliderSpace);
text("Pin: A" + AIPin1 + " = " + arduinoAIVal1, width/2 + 2, sliderPos[Slider.Name1][Y] - sliderSpace);
text("Pin: A" + AIPin2 + " = " + arduinoAIVal2, width/2 + 2, sliderPos[Slider.Name1][Y] + 1 + normalFontSize);
//text: COMPort
fill(#764401);
text("Port: " + portName + " State:", 1, height - normalFontSize/2);
//text: ArId + PrId
fill(#0014B9);
text("ArId: " + arduinoId, width/2 + generalSpace, height - normalFontSize/2);
text("PrId: " + processingId, width - 55, height - normalFontSize/2);
//(under)line:
stroke(0);
//underline titles:
//line (width/2+1, normalFontSize + 1, width/2 +textWidth(InputTitle) + 9, normalFontSize + 1);
//lines: splitframe
line(0, rectPos[Button.Name2][Y] + buttonSize + generalSpace,width,rectPos[Button.Name2][Y] + buttonSize + generalSpace);
line(0,height - normalFontSize * 2,width,height - normalFontSize * 2);
line(width/2, 0, width/2, height /*- normalFontSize * 2*/); //split screen vertical
//buttons:
for (int b = 0; b < 2; b++)
{
stroke(rectColBorder);
if (buttonState[b]) fill(colorON);
else fill(colorOFF);
rect(rectPos[b][X], rectPos[b][Y], buttonSize, buttonSize);
}
//inputs:
for (byte i = 0; i < 2; i++)
{
stroke(circColBorder);
if (bitRead(arduinoDIByte1, i)) fill(colorON);
else fill(colorOFF);
//println("arDIb:" + bitRead(arduinoDIByte1, i));
circle(circPos[i][X], circPos[i][Y], circleSize);
}
//sliders:
for (int s = 0; s < 2; s++)
{
if (sliderState[s])
{
sliderPos[s][X] = constrain(mouseX - sliderSize/2, sliderStartPos, sliderEndPos);
AOVal[s] = int(map(sliderPos[s][X], sliderStartPos, sliderEndPos, 0, 255));
}
stroke(0);
fill(#F7FAA4);
rect(sliderStartPos, sliderPos[s][Y], sliderEndPos + sliderSize - 1, sliderSize);
//stroke(sliderBorder); //sliderBorder color
fill(#000FAD);
rect(sliderPos[s][X], sliderPos[s][Y], sliderSize, sliderSize);
}
//COMStatus rect
if (serialTimeoutState) fill(colorOFF);
else fill(colorON);
rect(width/2 - 18, height - 20, 16, 16);
//update Arduino values
updateArduinoValues();
serialUpdate();
}//void draw() END
void mousePressed()
{
for (int b = 0; b < 2; b++)
{
if (overRect(rectPos[b][X], rectPos[b][Y], buttonSize, buttonSize))
{
buttonState[b] = !buttonState[b];
}
}
for (int s = 0; s < 2; s++)
{
sliderState[s] = overRect(sliderPos[s][X], sliderPos[s][Y], sliderSize, sliderSize);
}
}//void mousePressed() END
void mouseReleased()
{
for (int s = 0; s < 2; s++)
{
sliderState[s] = false;
}
}//void mouseReleased() END
boolean overRect(final int x,final int y,final int wid,final int hei)
{
if (mouseX >= x && mouseX <= x + wid &&
mouseY >= y && mouseY <= y + hei)
{
return true;
} else {
return false;
}
}//boolean overRect(.) END
void updateArduinoValues()
{
for (byte b = 0; b < 2/*7*/; b++) DOByte1 = bitWrite(DOByte1, b, buttonState[b]);
//for (byte b = 0; b < 7; b++) arduinoDOByte2 = bitWrite(arduinoDOByte2, b, buttonState[b+7]);
}//void updateArduinoValues() END
//bitwise functions:
boolean bitRead(final byte val, final byte pos)
{
return boolean(val & 1 << pos);
}//boolean bitRead(.) END
//would be better with usage of reference/pointer.
byte bitWrite(byte val, final byte pos, final boolean state)
{
if (!bitRead(val, pos) && state) val |= 1 << pos;
else if (bitRead(val, pos) && !state) val ^= 1 << pos;
return val;
}//byte bitWrite(.) END