Processing 如何将函数添加到下拉列表中?
我在使用controlp5库进行处理时添加了一个下拉列表。我想在列表中选择某个项目时,将数据串行传输到Arduino。如何为此目的添加控件 这是我的密码:Processing 如何将函数添加到下拉列表中?,processing,Processing,我在使用controlp5库进行处理时添加了一个下拉列表。我想在列表中选择某个项目时,将数据串行传输到Arduino。如何为此目的添加控件 这是我的密码: void设置{ d1=cp5.addDropdownList(“颜色”) .设置位置(((宽度/4)-50),((高度/2)-40)) .setBackgroundColor(颜色(37126214)) .setFont(font1) .setItemHeight(25) .立根高度(20) .addItem(“红色”,d1) .addIte
void设置{
d1=cp5.addDropdownList(“颜色”)
.设置位置(((宽度/4)-50),((高度/2)-40))
.setBackgroundColor(颜色(37126214))
.setFont(font1)
.setItemHeight(25)
.立根高度(20)
.addItem(“红色”,d1)
.addItem(“蓝色”,d1)
.addItem(“绿色”,d1)
}
当分别选择每个项目时,我想向Arduino发送一个字符'r'、'b'、'g'。建议我如何编写代码。正如作者在示例中所说,下拉列表不推荐使用,我认为最简单的方法是实现更灵活的ScrollableList,并通过串行端口将数据发送到Arduino
// Import java utils, for the list to add to the scrollable list
import java.util.*;
// Import ControlP5 library and declare it
import controlP5.*;
ControlP5 cp5;
// Import the Serial port and declare it
import processing.serial.*;
Serial myPort; // Create object from Serial class
void setup() {
size(400, 400);
// Initialize the Serial port
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);-
// Initialize the dropdown list
cp5 = new ControlP5(this);
List l = Arrays.asList("red", "green", "blue");
/* add a ScrollableList, by default it behaves like a DropdownList */
cp5.addScrollableList("dropdown")
.setPosition(100, 100)
.setSize(200, 100)
.setBarHeight(20)
.setItemHeight(20)
.addItems(l)
;
}
void draw() {
background(255);
}
// Dropdown callback function fromthe ScrollableList, triggered when you select an item
void dropdown(int n) {
/* request the selected item based on index n and store in a char */
String string = cp5.get(ScrollableList.class, "dropdown").getItem(n).get("name").toString();
char c = string.charAt(0);
// Write the char to the serial port
myPort.write(c);
}
在Arduino方面
char val; // Data received from the serial port
void setup() {
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
while (Serial.available()) { // If data is available to read,
val = Serial.read(); // read it and store it in val
}
}
非常感谢。我试试你的程序。