触摸LED#

介绍#

VEX IQ 触摸 LED 既是指示灯又是按钮。它可以发出不同颜色的光来提供反馈,例如指示机器人已准备就绪、显示所选模式或匹配其他传感器检测到的颜色,它还可以检测到触摸,让您只需轻触一下即可启动或更改机器人的行为。

本页面使用 touchled_1 作为触摸 LED 的示例名称。您可以根据需要将其替换为您自己配置的名称。

以下是所有方法的列表:

操作——与触摸式LED互动。

  • pressed — 注册一个在触摸 LED 被按下时要调用的函数。

  • released — 注册一个在触摸 LED 释放时要调用的函数。

  • on — 打开触摸 LED。

  • off — 关闭触摸 LED。

  • toggle — 打开或关闭触摸 LED。

自定义设置——更改触摸式 LED 的颜色、亮度和设置。

  • set_color — 设置触摸 LED 的颜色。

  • set_fade — 设置颜色之间淡入淡出的速度。

  • set_brightness — 设置触摸 LED 的亮度。

  • set_blink — 无限循环地交替打开和关闭触摸 LED。

获取器 — 返回触摸 LED 的数据。

  • pressing — 返回触摸 LED 是否被按下。

  • installed — 返回触摸 LED 是否连接到大脑。

构造函数 — 手动初始化和配置触摸 LED。

  • Touchled — 创建一个触摸 LED。

行动#

pressed#

pressed 注册一个在触摸 LED 被按下时要调用的函数。

Usage:
touchled_1.pressed(callback, arg)

参数

描述

callback

一个预先定义好的函数,当触摸 LED 被按下时执行。

arg

可选。包含要传递给回调函数的参数的元组。有关更多信息,请参阅带参数的函数。

# Turn blue while the Touch LED is pressed
def light_blue():
    touchled_1.set_color(Color.BLUE)

touchled_1.pressed(light_blue)

released#

released 注册一个在触摸 LED 释放时要调用的函数。

Usage:
touchled_1.released(callback, arg)

参数

描述

callback

一个预先定义好的函数,当触摸 LED 被释放时执行。

arg

可选。包含要传递给回调函数的参数的元组。有关更多信息,请参阅带参数的函数。

# Turn red when the Touch LED is released
def light_red():
    touchled_1.set_color(Color.RED)

touchled_1.released(light_red)

on#

on 表示将触摸 LED 的亮度设置为 100%,从而打开触摸 LED。

Usage:
touchled_1.on(value)

参数

描述

value

选修的。 设置 LED 灯颜色:

  • Color.RED
  • Color.GREEN
  • Color.BLUE
  • Color.WHITE (默认)
  • Color.YELLOW
  • Color.ORANGE
  • Color.PURPLE
  • Color.RED_VIOLET
  • Color.VIOLET
  • Color.BLUE_VIOLET
  • Color.BLUE_GREEN
  • Color.YELLOW_GREEN
  • Color.YELLOW_ORANGE
  • Color.RED_ORANGE
您还可以指定自定义颜色。

# Turn green when the robot is moving
touchled_1.on(Color.GREEN)
drivetrain.drive_for(FORWARD, 5)
touchled_1.off()

off#

off 将触摸 LED 的亮度设置为 0%,从而关闭触摸 LED。

Usage:
touchled_1.off()

参数

描述

该方法没有参数。

# Turn off Touch LED when movement is over
touchled_1.on(Color.GREEN)
drivetrain.drive_for(FORWARD, 5)
touchled_1.off()

toggle#

toggle 可打开或关闭触摸 LED 灯。如果 LED 灯当前亮着, toggle 可将其亮度设置为 0%,即关闭 LED 灯。如果 LED 灯已关闭, toggle 可将其亮度设置为 100%,即打开 LED 灯。

Usage:
touchled_1.toggle()

参数

描述

该方法没有参数。

# Turn the Touch LED off and on
touchled_1.set_color(Color.RED)
while True:
    touchled_1.toggle()
    wait(0.5,SECONDS)

修改器#

set_color#

set_color 设置触摸 LED 的颜色。如果使用此方法时触摸 LED 尚未点亮,则会将亮度设置为 100% 以打开触摸 LED。

Usage:
touchled_1.set_color(color)

参数

描述

color

Optional. Sets the LED’s color to:

  • Color.RED
  • Color.GREEN
  • Color.BLUE
  • Color.WHITE (default)
  • Color.YELLOW
  • Color.ORANGE
  • Color.PURPLE
  • Color.RED_VIOLET
  • Color.VIOLET
  • Color.BLUE_VIOLET
  • Color.BLUE_GREEN
  • Color.YELLOW_GREEN
  • Color.YELLOW_ORANGE
  • Color.RED_ORANGE
If unspecified, set_color will turn the Touch LED on with the previously set color.
You can also specify a custom color.

# Change colors between blue and green
while True:
    touchled_1.set_color(Color.BLUE)
    wait(0.5, SECONDS)
    touchled_1.set_color(Color.GREEN)
    wait(0.5, SECONDS)

set_fade#

set_fade 改变触摸 LED 过渡到新颜色的方式。

Usage:
touchled_1.set_fade(type)

参数

描述

type

How fast a color will fade from one to the next:

  • FadeType.FAST — Colors will transition quickly.
  • FadeType.OFF — Colors will instantly transition.
  • FadeType.SLOW — Colors will transition slowly.

# No fade to red, then slow fade to purple
touchled_1.set_fade(FadeType.OFF)
touchled_1.set_color(Color.RED)
wait(2, SECONDS)
touchled_1.set_fade(FadeType.SLOW)
touchled_1.set_color(Color.PURPLE)

set_brightness#

set_brightness 设置触摸 LED 的亮度。

Usage:
touchled_1.set_brightness(brightness)

参数

描述

brightness

触摸 LED 的亮度可以设置为 0% 到 100% 之间的整数。

# Show green at different brightness levels
touchled_1.set_color(Color.GREEN)
wait(1, SECONDS)
touchled_1.set_brightness(10)
wait(1, SECONDS)
touchled_1.set_brightness(100)

吸气剂#

pressing#

pressing 返回一个整数,指示触摸 LED 当前是否被按下。

  • 1 — 触摸 LED 正在被按下。

  • 0 — 触摸 LED 未被按下。

Usage:
touchled_1.pressing()

参数

描述

该方法没有参数。

# Turn TouchLED green when touched
while True:
    if touchled_1.pressing():
        touchled_1.set_color(Color.GREEN)
    else:
        touchled_1.set_color(Color.RED)
    wait(50, MSEC)

installed#

installed 返回一个布尔值,指示触摸 LED 是否连接到大脑。

  • True — 触摸 LED 已连接到大脑。

  • False ​​— 触摸 LED 未连接到大脑。

Usage:
touchled_1.installed()

参数

描述

该方法没有参数。

构造函数#

构造函数用于手动创建 Touchled 对象,这是在 VEXcode 之外配置触摸 LED 所必需的。

Touchled#

Touchled 创建一个触摸 LED。

Usage:
touchled_1 = Touchled(smartport)

范围

描述

smartport

触摸 LED 连接的智能端口,记为 PORTx,其中 x 是端口号。

# Create a Touchled in Port 1
touchled_1 = Touchled(Ports.PORT1)