Good afternoon, I am using the switch construction, but I don’t know how to write correctly for other values after the case operators.
In the DisplayData code, in the switch construction, I assign a command number to the fields, for example: Vin = cp5.addTextlabel, so that later, when connecting via a com port, display or change the data by sending this command "60",123 in the Vin = cp5.addTextlabel field.
Only works in (minutess = cp5.addButton) - 10, (Vin = cp5.addTextlabel) - 60, (inputPULI = cp5.addNumberbox) - 40, (inputNapryzenieKV = cp5.addNumberbox) - 70
The main error is below (function "setText (String)" is undefined. See screenshot.
How do I fix this for variables and write it correctly?
ImageButton button;
RadioButton CheckBoxuvum; Documentation
CheckBox P4; Documentation
See the GUI file!
A task:
ImageButton (button) - 20, the goal is to get a picture change in the window by sending a command through a terminal or microcontroller.
P4 = cp5.addCheckBox - 30 CHECK, the goal is to get a change in the state of the checkbox on and off in the window by sending a command through a terminal or microcontroller.
CheckBoxuvum = cp5.addRadioButton - 50 +/-, the goal is to get a change in the state of the checkbox on and off (+/-) in the window by sending a command through a terminal or microcontroller.
In the Displaydata code, I marked this with a question.
In the future, I plan to send commands from the terminal via the com port to receive a change in the state of the data in the window for these values.
I will have to upload all the test project files so you can test this.
Com_Interface1:
import processing.serial.*;
import controlP5.*;
ControlP5 cp5;
DropdownList serialPortsList;
Serial serialPort;
final int BAUD_RATE = 115200;
char parity = 'N';
int dataBits = 8;
float stopBits = 1.0;
public void setup() {
size(700, 420, JAVA2D);
surface.setTitle("TEST");
surface.setResizable(false);
setupUI();
smooth();
frameRate(30);
writeOutgioing[lengthmas-1]=1;
String[] portNames = Serial.list();
//serialPort.clear(); // Why does not it work?
for (int i = 0; i < portNames.length; i++) serialPortsList.addItem(portNames[i], i);
}
public void toplug (int theValue) { // Start button on click sends a commad 1.
println("Button click events: "+theValue);
strata =!strata;
if (!strata) {
connection.setLabel("Пуск");
} else {
connection.setLabel("СТОП");
serialports((int)serialPortsList.getValue());
writeOutgioing[0]=1;
writeOut();
}
}
public void serialports(int theValue) {
try {
serialPort = new Serial(this, Serial.list()[theValue], BAUD_RATE, parity, dataBits, stopBits);
serialPort.bufferUntil('\n');
println("COM connected: "+ Serial.list()[theValue] );
/*Send.unlock();
connection.unlock();*/ //locking buttons in applications if not connected via rs-232.
}
catch(Exception e) {
System.err.println("Error opening serial port" + Serial.list()[theValue]);
e.printStackTrace();
}
}
Displaydata:
void Displaydata() {
switch(readIncome[0]) {
case 10:
minutess.setCaptionLabel(readIncome[1]+" Мин1.");
min1=readIncome[1];
break;
case 20:
// What to write?
// label(readIncome[1]+" Мин."); // ImageButton
// min=readIncome[1];
break;
case 30:
// What to write?
// P4.setText("std2"+readIncome[1] ); // CheckBox
break;
case 40:
inputPULI.setLabel("Bullet - "+readIncome[1] );
break;
case 70:
inputNapryzenieKV.setLabel("Voltage - "+readIncome[1] );
break;
case 60:
Vin.setText("Voltage K.V - "+readIncome[1] );
break;
case 50:
// What to write?
// CheckBoxuvum.setText("UV/UM - "+readIncome[1] ); // RadioButton
break;
default:
println("DisplayData(): no case selected.");
break; // technically not necessary, but I like my switches tidy
}
}
GUI:
int min1 = 0;
int PUL;
float NapryzenieKV;
boolean strata=false;
Button minutess;
ImageButton button;
Numberbox inputPULI;
Numberbox inputNapryzenieKV;
RadioButton CheckBoxuvum;
CheckBox P4;
Textlabel Vin;
Button connection;
Button Send;
public void setupUI()
{
cp5 = new ControlP5(this);
PFont fontn = createFont("Times New Roman", 18);
PFont p = createFont("Times New Roman", 18);
ControlFont font=new
ControlFont(p);
cp5.setFont(font);
connection = cp5.addButton("toplug")
.setCaptionLabel("ПУСК")
.setPosition(387, 30)
.setSize(150, 30);
serialPortsList = cp5.addDropdownList("Порт")
.setPosition(130, 30)
.setSize(150, 200)
.setItemHeight(30)
.setBarHeight(30);
minutess = cp5.addButton("minutesss")
.setCaptionLabel("ВЫКЛ")
.setPosition(145, 100)
.setSize(90, 25);
Send = cp5.addButton("toapply")
.setCaptionLabel("Apply")
.setPosition(510, 370)
.setSize(150, 30);
Vin = cp5.addTextlabel("naprazhenie kondencatora")
.setText("Voltage K.V")
.setFont(p)
.setColor(color(#00ffff))
.setPosition(45, 320);
CheckBoxuvum = cp5.addRadioButton("UV/UM")
.setPosition(155, 360)
.setSize(15, 15)
.setColorActive(color(255))
.setItemsPerRow(2)
.setSpacingColumn(85)
.addItem("+", 1)
.addItem("-", 2);
P4 = cp5.addCheckBox("std2")
.setPosition(150, 190)
.setSize(15, 15)
.setItemsPerRow(1)
.setSpacingColumn(30)
.setSpacingRow(20)
.addItem("Check", 2);
inputPULI = cp5.addNumberbox("PUL")
.setLabel("Bullet")
.setPosition(150, 220)
.setSize(80, 30)
.setColorValue(0xffffff00)
.setFont(p)
.setScrollSensitivity(1.1)
.setDirection(Controller.HORIZONTAL)
.setRange(1, 199)
.setValue(3);
Label labelinputPULI = inputPULI.getCaptionLabel();
labelinputPULI.setFont(font);
labelinputPULI.setColor(color(#00ffff));
labelinputPULI.toUpperCase(false);
labelinputPULI.setText("Пуль");
labelinputPULI.align(ControlP5.LEFT_OUTSIDE, CENTER);
labelinputPULI.getStyle().setPaddingLeft(-55);
inputNapryzenieKV = cp5.addNumberbox("NapryzenieKV")
.setLabel("Voltage")
.setPosition(150, 270)
.setSize(80, 30)
.setColorValue(0xffffff00)
.setFont(p)
.setScrollSensitivity(1.1)
.setMin(25)
.setMax(99)
.setMultiplier(0.01)
.setDirection(Controller.HORIZONTAL)
.setValue(25);
Label labelinputNapryzenieKV = inputNapryzenieKV.getCaptionLabel();
labelinputNapryzenieKV.setFont(font);
labelinputNapryzenieKV.setColor(color(#00ffff));
labelinputNapryzenieKV.toUpperCase(false);
labelinputNapryzenieKV.setText("Напряжение");
labelinputNapryzenieKV.align(ControlP5.LEFT_OUTSIDE, CENTER);
labelinputNapryzenieKV.getStyle().setPaddingLeft(-45);
textFont(fontn);
{
// button dimensions
int w = 99;
int h = 25;
// test with generated images
button = new ImageButton(140, 140, w, h,
new PImage[]{
getImage(w, h, color(192, 0, 32 * 2)), // off
getImage(w, h, color(0, 0, 32 * 3)), // 10
getImage(w, h, color(0, 0, 32 * 4)), // 20
getImage(w, h, color(0, 0, 32 * 5)), // 30
getImage(w, h, color(0, 0, 32 * 6)), // 40
getImage(w, h, color(0, 0, 32 * 7)), // 50
getImage(w, h, color(0, 0, 32 * 8)), // 60
});
}
}
void mousePressed() {
button.mousePressed(mouseX, mouseY);
println(button.min);
}
// test images to represent loaded state images
PImage getImage(int w, int h, int c) {
PImage img = createImage(w, h, RGB);
java.util.Arrays.fill(img.pixels, c);
img.updatePixels();
return img;
}
// make a custom image button class
class ImageButton {
// minutes is the data it stores
int min = 0;
// images for each state
PImage[] stateImages;
// which image to display
int stateIndex;
// position
int x, y;
// dimensions: width , height
int w, h;
// text to display
String label = "ВЫКЛ";
ImageButton(int x, int y, int w, int h, PImage[] stateImages) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.stateImages = stateImages;
}
void mousePressed(int mx, int my) {
// check the cursor is within the button bounds
boolean isOver = ((mx >= x && mx <= x + w) && // check horizontal
(my >= y && my <= y + h) ); // check vertical
if (isOver) {
min += 10;
stateIndex++;
if (min>60) {
min = 0;
stateIndex = 0;
label = "ВЫКЛ";
} else {
label = (str(min) + "Мин");
}
}
}
void draw() {
// if the images and index are valid
if (stateImages != null && stateIndex < stateImages.length) {
image(stateImages[stateIndex], x, y, w, h);
} else {
println("error displaying button state image");
println("stateImages: ");
printArray(stateImages);
println("stateIndex: " + stateIndex);
}
// display text
text(label, x + 17, y + h - 8);
}
}
public void minutesss() {
min1 += 10;
if (min1>60) {
min1 = 0;
minutess.setCaptionLabel("ВЫКЛ");
} else {
minutess.setCaptionLabel(str(min1)+" Мин1");
}
}
void controlEvent(ControlEvent theEvent) {
if (theEvent.isFrom(CheckBoxuvum)) {
//myColorBackground = 0;
print("got an event from "+CheckBoxuvum.getName()+"\t\n");
// checkbox uses arrayValue to store the state of
// individual checkbox-items. usage:
println(CheckBoxuvum.getArrayValue());
int col = 0;
for (int i=0; i<CheckBoxuvum.getArrayValue().length; i++) {
int n = (int)CheckBoxuvum.getArrayValue()[i];
print(n);
if (n==1) {
//myColorBackground += CheckBoxuvum.getItem(i).internalValue();
}
}
println();
}
if (theEvent.isGroup()) {
// check if the Event was triggered from a ControlGroup
println("event from group : "+theEvent.getGroup().getValue()+" from "+theEvent.getGroup());
} else if (theEvent.isController()) {
println("event from controller : "+theEvent.getController().getValue()+" from "+theEvent.getController());
}
}
Protocol:
int lengthmas = 7;
int RC = -128; // -128 separating byte в java.
int[] readIncome = new int[lengthmas];
int[] writeOutgioing = new int[lengthmas];
String incomingData= null;
String outgoingData=null;
void readInc() {
while ( serialPort.available() > 0) {
incomingData = serialPort.readStringUntil(RC);
readIncome = int(split (incomingData, ',' )); // the received data is comma separated.
}
}
void writeOut() {
outgoingData=str(writeOutgioing[0])+str(writeOutgioing[1])+str(writeOutgioing[2])+str(writeOutgioing[3])+str(writeOutgioing[4])+str(writeOutgioing[5])+str(writeOutgioing[6]); // sending data as a string.
serialPort.write(outgoingData);
}
/*Protocol:
Protocol composition - an array of seven elements, 0 - command, 1-4 data, 5 - remainder of packets for transmission, 6 (-128) separating byte.
The controller responds by sending the same command and data.
Example: 10(Command),235,2353,564,5(packages left),77777(CRC),-128(RC)
Commands:
Modifier:
10 - minutess = cp5.addButton
20 - ImageButton button
30 - CheckBox P4
40 - Numberbox inputPULI
70 - Numberbox inputNapryzenieKV
60 - Textlabel Vin
50 - RadioButton CheckBoxuvum
*/
Senddata:
public void toapply()
{
}
loop:
void draw() {
background(50);
button.draw();
}
{
if (strata) {
readInc();
//readIncome[]
Displaydata();
}
}

The problem is
default:.The switch run like this: it finds the case which corresponds to the key and starts executing code from this point on until it hits a
break;. Thedefault:case is a catch-all: it'll like a condition which would always be true.To illustrate, I like this example: here when the user enters a number, it prints to the console the names of all the month from that point up to December.
Notice how
defaultis at the end. That's because since it's always "true" and since the switch will select only one case and then run until abreakstatement, if I put it anywhere else it will always be selected when it's read. See by modifying the example like this:This is exactly what happens in your program. In
DisplayData(), right at tthe beginning there's adefaultcase which overrides all the other cases:You can re-write with the correct syntax like this:
Have fun!