我正在为使用arduino UNO作为处理器的战斗机器人创建一个遥控器。
我在Processing IDE中使用G4P工具创建了一个简单的LED控件来绘制GUI,但是COM端口是在代码中设置的。但是我希望用户能够使用GP4 GUI工具创建的下拉列表来更改COM端口。
这是主要草图:
import processing.serial.*;
import java.awt.Dimension;
// Need G4P library
import g4p_controls.*;
Serial myPort;
String myText="";
int keyboard;
Dimension minimumSize = new Dimension(500, 50); //Change values for limit
public void setup(){
createGUI();
customGUI();
// Place your setup code here
myPort = new Serial(this, "COM3", 9600);
myPort.bufferUntil('\n');
keyboard = 0;
frame.setTitle("RG LED CONTROL");
PImage titlebaricon = loadImage("myicon.png");
surface.setIcon(titlebaricon);
size(500, 500);
frame.setResizable(true);
frame.setMinimumSize(minimumSize);
frame.setSize(500, 500);
}
public void draw(){
background(255,255,255);
if (keyboard == 1){
if (keyPressed){
if (key=='a' || key=='A'){
myPort.write('1');
}
if (key=='d' || key=='D'){
myPort.write('2');
}
if (key=='s' || key=='S'){
myPort.write('0');
}
}
if (keyPressed == false){
myPort.write('0');
}
}
}
// Use this method to add additional statements
// to customise the GUI controls
public void customGUI(){
}
这是G4P GUI工具生成的GUI代码:
public void button4_click1(GButton source, GEvent event) { //_CODE_:button4:739741:
println("button4 - GButton >> GEvent." + event + " @ " + millis());
myPort.write('1');
} //_CODE_:button4:739741:
public void button5_click1(GButton source, GEvent event) { //_CODE_:button5:355171:
println("button5 - GButton >> GEvent." + event + " @ " + millis());
myPort.write('2');
} //_CODE_:button5:355171:
public void button6_click1(GButton source, GEvent event) { //_CODE_:button6:770875:
println("button6 - GButton >> GEvent." + event + " @ " + millis());
myPort.write('0');
} //_CODE_:button6:770875:
public void button7_click1(GButton source, GEvent event) { //_CODE_:button7:243410:
println("button7 - GButton >> GEvent." + event + " @ " + millis());
if (keyboard == 0){
keyboard = 1;
button4.setVisible(false);
button5.setVisible(false);
button6.setVisible(false);
surface.setSize(500, 50);
} else {
keyboard = 0;
surface.setSize(500, 500);
button4.setVisible(true);
button5.setVisible(true);
button6.setVisible(true);
}
} //_CODE_:button7:243410:
public void imgButton1_click1(GImageButton source, GEvent event) { //_CODE_:imgButton1:580878:
println("imgButton1 - GImageButton >> GEvent." + event + " @ " + millis());
} //_CODE_:imgButton1:580878:
// Create all the GUI controls.
// autogenerated do not edit
public void createGUI(){
G4P.messagesEnabled(false);
G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
G4P.setCursor(ARROW);
surface.setTitle("Sketch Window");
button4 = new GButton(this, 20, 70, 140, 140);
button4.setText("RED");
button4.setTextBold();
button4.setLocalColorScheme(GCScheme.RED_SCHEME);
button4.addEventHandler(this, "button4_click1");
label2 = new GLabel(this, 20, 20, 300, 30);
label2.setTextAlign(GAlign.CENTER, GAlign.MIDDLE);
label2.setText("RG LED CONTROL");
label2.setTextBold();
label2.setOpaque(false);
button5 = new GButton(this, 340, 70, 140, 140);
button5.setText("GREEN");
button5.setTextBold();
button5.setLocalColorScheme(GCScheme.GREEN_SCHEME);
button5.addEventHandler(this, "button5_click1");
button6 = new GButton(this, 180, 70, 140, 140);
button6.setText("OFF");
button6.setTextBold();
button6.addEventHandler(this, "button6_click1");
button7 = new GButton(this, 340, 10, 140, 30);
button7.setText("KEYBOARD");
button7.setTextBold();
button7.setLocalColorScheme(GCScheme.CYAN_SCHEME);
button7.addEventHandler(this, "button7_click1");
imgButton1 = new GImageButton(this, 130, 230, 250, 250, new String[] { "logo.png", "logo.png", "logo.png" } );
imgButton1.addEventHandler(this, "imgButton1_click1");
}
// Variable declarations
// autogenerated do not edit
GButton button4;
GLabel label2;
GButton button5;
GButton button6;
GButton button7;
GImageButton imgButton1;
答案 0 :(得分:0)
将问题分解为更小的步骤。
第1步:重新开始使用基本草图,只需构建一个下拉菜单,而无需担心com端口。创建一个函数,根据参数数组或ArrayList
构建一个下拉菜单。让它先工作。这样,只要用户从菜单中选择内容,就可以将选择内容打印到控制台。
第2步:另外,创建另一个基本草图,只构建一个数组或可用com端口中的ArrayList
。创建一个返回数组或ArrayList
的函数,然后将该数据结构的内容打印到控制台。不要担心这里的下拉菜单。
第3步:然后当你们两个分开工作时,将它们组合成一个草图,构建一个下拉菜单(使用步骤1中的功能)应该更容易从步骤2的函数返回的数据结构。
如果您遇到某个特定步骤,那么您将拥有MCVE和特定问题,并且它会更容易为您提供帮助。祝你好运。