触碰过#
介绍#
VEX IQ(第二代)的 Touch LED API 提供了控制 IQ Touch LED 的方法。这些模块允许您设置颜色、亮度和淡入淡出速度,以及检测 TouchLED 何时被按下。
以下是所有方法的列表:
方法——控制触摸 LED 的行为。
pressing – 返回触摸 LED 是否被按下。
setColor – 设置触摸 LED 的颜色。
setFade – 设置颜色淡入淡出的速度。
setBrightness – 设置触摸 LED 的亮度。
pressed – 当触摸 LED 被按下时调用一个函数。
released – 当保险杠开关被释放时调用一个函数。
on – 打开触摸 LED。
off – 关闭触摸 LED。
setBlink – 无限期交替打开和关闭触摸 LED。
installed – 返回触摸 LED 是否连接到大脑。
构造函数——手动初始化和配置触摸 LED。
touchled – 创建触摸 LED。
方法#
紧迫#
pressing
返回 TouchLED 传感器当前是否被按下。
1
– TouchLED 当前处于按下状态。0
– TouchLED 当前未被按下。
用法:
pressing()
参数 |
描述 |
---|---|
该方法没有参数。 |
while (true){
// Change LED color when it is being pressed
if (TouchLED1.pressing()){
TouchLED1.setColor(colorType::green);
}
else{
TouchLED1.setColor(colorType::red);
}
}
设置颜色#
setColor
用于打开 TouchLED 传感器的 LED。此方法的功能与 on()
方法类似,但该方法将使用之前的亮度级别,而不是指定的值。
用法:
setColor(color)
参数 |
描述 |
---|---|
颜色 |
有效的颜色类型: |
// Alternate colors forever
while (true){
TouchLED1.setColor(colorType::blue_violet);
wait(0.5, seconds);
TouchLED1.setColor(colorType::blue_green);
wait(0.5, seconds);
}
设置淡入淡出#
setFade
会更改 TouchLED 传感器的淡入淡出设置。TouchLED 传感器将使用淡入淡出类型切换为新的颜色。
用法:
setFade(speed)
参数 |
描述 |
---|---|
速度 |
有效的
|
// Turn of LED with no fade and slow fade
TouchLED1.setColor(colorType::blue_violet);
TouchLED1.setFade(off);
wait(2,seconds);
TouchLED1.off();
wait(1,seconds);
TouchLED1.setColor(colorType::blue_violet);
TouchLED1.setFade(slow);
wait(2,seconds);
TouchLED1.off();
设置亮度#
setBrightness
设置触摸 LED 的亮度。
用法:
setBrightness(亮度)
参数 |
描述 |
---|---|
亮度 |
触摸 LED 的亮度设置为百分比。 |
// Turn the LED green with different brightnesses
// Default brightness
TouchLED1.setColor(colorType::green);
wait(2,seconds);
// Low brightness
TouchLED1.setBrightness(10);
wait(2,seconds);
// Max brightness
TouchLED1.setBrightness(100);
按下#
“pressed”注册了一个当触摸 LED 被按下时要调用的函数。
用法:
按下(回调)
参数 |
描述 |
---|---|
打回来 |
先前定义的当触摸 LED 被按下时执行的功能。 |
void touchLEDPressed(){
TouchLED1.setColor(colorType::red);
}
int main(){
vexcodeInit();
// Turn LED on when pressed
TouchLED1.pressed(touchLEDPressed);
}
发布#
released
注册了一个在触摸 LED 被释放时要调用的函数。
用法:
released(回调)
参数 |
描述 |
---|---|
打回来 |
先前定义的在释放触摸 LED 时执行的功能。 |
void touchLEDReleased(){
TouchLED1.off();
}
int main(){
vexcodeInit();
TouchLED1.setColor(colorType::red);
// Turn LED off when released
TouchLED1.released(touchLEDReleased);
}
在#
on
方法使用预定义的颜色和设置的亮度打开 TouchLED 传感器的 LED。
默认用法:
on(color, brightenness)
参数 |
描述 |
---|---|
颜色 |
有效的颜色类型:
|
亮度 |
以百分比形式设置 LED 的亮度。 |
while (true){
// Turn on the LED with a colorType
if (TouchLED1.pressing()){
TouchLED1.on(colorType::red_orange, 100);
}
else{
TouchLED1.off();
}
}
重载
on
还可以使用色调值、rgb 值或 vex::color
设置 TouchLED 的颜色。
重载用法:
on(hue, brighten)
on(r, g, b, brighten)
on(color, brighten)
参数 |
描述 |
---|---|
色调 |
LED 的色调。这也可以表示为十六进制代码值。 |
r |
表示 LED 红色值的无符号 8 位整数。 |
克 |
表示 LED 绿色值的无符号 8 位整数。 |
b |
表示 LED 蓝色值的无符号 8 位整数。 |
颜色 |
有效颜色:
|
亮度 |
设置 LED 的亮度。 |
// Turn the LED on with different parameters
TouchLED1.on(0xF3DFC1, 100);
wait(2,seconds);
TouchLED1.on(255, 161, 159, 100);
wait(2, seconds);
TouchLED1.on(color::blue, 100);
离开#
“off”关闭 TouchLED 传感器的 LED。
用法:
off()
参数 |
描述 |
---|---|
该方法没有参数。 |
// Turn off the LED
TouchLED1.setColor(colorType::green);
wait(5, seconds);
TouchLED1.off();
设置闪烁#
setBlink
使 TouchLED 以开启和关闭的模式反复闪烁。
**注意:**调用 off
不会停止闪烁行为。要停止闪烁行为,请使用 setBrightness
将 LED 亮度设置为 0。
默认用法:
setBlink(color, ontime, offtime)
参数 |
描述 |
---|---|
颜色 |
有效的颜色类型:
|
准时 |
LED 闪烁的持续时间(以秒为单位)。 |
休息时间 |
LED 闪烁关闭的时间间隔(以秒为单位)。 |
// Blink the LED
while (true){
if (TouchLED1.pressing()){
TouchLED1.setBlink(colorType::green, 0.25, 0.25);
}
else{
TouchLED1.setBlink(colorType::red, 0.25, 0.25);
}
}
重载
setBlink
还可以使用色调值或有效的 vex::color
来设置颜色。
重载用法:
setBlink(hue, ontime, offtime)
setBlink(color, ontime, offtime)
参数 |
描述 |
---|---|
色调 |
LED 的色调。也可以用十六进制代码值表示。 |
颜色 |
有效颜色:
|
准时 |
LED 亮起的时间(以秒为单位)。 |
休息时间 |
LED 关闭的时间(以秒为单位)。 |
// Blink the LED
while (true){
if (TouchLED1.pressing()){
// Using vex::color
TouchLED1.setBlink(color::green, 0.25, 0.25);
}
else{
// Using a hue value
TouchLED1.setBlink(0, 0.25, 0.25);
}
}
已安装#
如果 TouchLED 已连接到 Brain,则返回“installed”。
1
– TouchLED 连接到 IQ(第二代)大脑。0
– TouchLED 未连接到 IQ(第二代)Brain。
用法:
installed()
参数 |
描述 |
---|---|
该方法没有参数。 |
// Display a message if the Bumper Switch is detected
if (TouchLED1.installed()){
Brain.Screen.print("Installed!");
}
构造函数#
构造函数用于手动创建“touchled”对象,这对于在 VEXcode 之外配置 TouchLED 是必需的。
对于下面的示例,配置的 TouchLDE 将被命名为“TouchLED1”,并且在整个 API 文档的所有后续示例中引用“touchled”类方法时将使用它。
触摸LED#
touchled
创建一个 Bumper Switch。
用法:
touchled(port)
范围 |
描述 |
---|---|
|
颜色传感器连接到哪个智能端口,以“PORT”表示,后跟端口号,范围从 1 到 12。 |
// Construct the Brain.
brain Brain;
// Construct a TouchLED Sensor "touch" with the
// TouchLED class.
touchled touch = touchled(PORT1);
// Default brightness
TouchLED1.setColor(colorType::green);
wait(2,seconds);
// Low brightness
TouchLED1.setBrightness(10);
wait(2,seconds);
// Max brightness
TouchLED1.setBrightness(100);