触摸LED#
介绍#
触摸 LED 是一种电容式触摸按钮,可以以不同的亮度级别和转换速度显示颜色,并可以检测何时按下。
以下是所有方法的列表:
方法——控制触摸 LED 的行为。
pressing – 返回触摸 LED 是否被按下。
on – 打开触摸 LED。
off – 关闭触摸 LED。
切换 – 打开或关闭触摸 LED。
set_fade – 设置颜色淡入淡出的速度。
set_color – 设置触摸 LED 的颜色。
set_brightness – 设置触摸 LED 的亮度。
set_blink – 无限期交替打开和关闭触摸 LED。
pressed – 当触摸 LED 被按下时调用一个函数。
released – 当保险杠开关被释放时调用一个函数。
installed – 返回触摸 LED 是否连接到大脑。
构造函数——手动初始化和配置触摸 LED。
TouchLED – 创建触摸 LED。
方法#
紧迫#
.pressing
返回一个整数,指示触摸 LED 当前是否被按下。
1
-TouchLED 被按下。0
-TouchLED 未被按下。
用法:
pressing()
参数 |
描述 |
---|---|
该方法没有参数。 |
# Change the LED color when it is
# being pressed.
while True:
if touchled_1.pressing():
touchled_1.set_color(Color.GREEN)
else:
touchled_1.set_color(Color.RED)
在#
“on” 通过将触摸 LED 的亮度设置为 100% 来打开触摸 LED。
用法:
on(value)
参数 |
描述 |
---|---|
|
可选。将 LED 的颜色设置为:
|
# Wait for the LED to be pressed,
# then turn it on.
while True:
if touchled_1.pressing():
touchled_1.on()
break
brain.screen.print("On!")
离开#
“off” 通过将触摸 LED 的亮度设置为 0% 来关闭触摸 LED。
用法:
off()
参数 |
描述 |
---|---|
该方法没有参数。 |
# Flash the LED alternating colors until the Touch LED
# is pressed, then turn it off.
while True:
if touchled_1.pressing():
touchled_1.off()
break
touchled_1.set_color(Color.BLUE_GREEN)
wait(0.5,SECONDS)
touchled_1.set_color(Color.RED_VIOLET)
wait(0.5,SECONDS)
brain.screen.print("Off!")
切换#
toggle
用于打开或关闭触摸 LED。如果 LED 当前处于打开状态,toggle
会将其亮度设置为 0%,即关闭 LED。如果 LED 当前处于关闭状态,toggle
会将其亮度设置为 100%,即打开 LED。
用法:
toggle()
参数 |
描述 |
---|---|
该方法没有参数。 |
# Toggle the Touch LED off and on
touchled_1.set_color(Color.RED)
while True:
touchled_1.toggle()
wait(0.5,SECONDS)
设置淡入淡出#
set_fade
改变触摸 LED 的颜色淡入淡出设置。
用法:
set_fade(type)
参数 |
描述 |
---|---|
类型 |
颜色从一种颜色淡入到另一种颜色
|
# Turn the Touch LED on and off with both
# no fade and a slow fade.
touchled_1.set_color(Color.BLUE_VIOLET)
touchled_1.set_fade(FadeType.OFF)
wait(2,SECONDS)
touchled_1.toggle()
wait(1,SECONDS)
touchled_1.set_color(Color.BLUE_VIOLET)
touchled_1.set_fade(FadeType.SLOW)
wait(2,SECONDS)
touchled_1.toggle()
设置颜色#
set_color
设置触摸 LED 的颜色。如果使用此方法时触摸 LED 尚未亮起,则会将亮度设置为 100% 以打开触摸 LED。
用法:
set_color(color)
参数 |
描述 |
---|---|
|
可选。将 LED 的颜色设置为:
set_color 将使用之前设置的颜色打开触摸 LED。您还可以指定 自定义颜色。 |
# Flash the LED with two different colors continuously
while True:
touchled_1.set_color(Color.BLUE)
wait(0.5,SECONDS)
touchled_1.set_color(Color.GREEN)
wait(0.5,SECONDS)
设置亮度#
set_brightness
设置触摸 LED 的亮度。
用法:
set_brightness(亮度)
参数 |
描述 |
---|---|
亮度 |
触摸 LED 的亮度设置为百分比。 |
# Turn the LED green with different brightnesses
# Default brightness
touchled_1.set_color(Color.GREEN)
wait(2,SECONDS)
# Low brightness
touchled_1.set_brightness(10)
wait(2,SECONDS)
# Max brightness
touchled_1.set_brightness(255)
设置闪烁#
set_blink
会导致触摸 LED 不断交替开启和关闭,直到另一个触摸 LED 方法覆盖它。
**注意:**调用 off
不会停止闪烁行为。
用法:
set_blink(color, ontime, offtime)
参数 |
描述 |
---|---|
颜色 |
可选。将触摸 LED 的颜色设置为:
|
准时 |
LED 亮起的时长(以秒为单位)。 |
休息时间 |
LED 关闭的时间(以秒为单位)。 |
# Blink the Touch LED green off and on every
# 2 seconds
touchled_1.set_blink(Color.GREEN, 2, 2)
按下#
“pressed”注册了一个当触摸 LED 被按下时要调用的函数。
用法:
pressed(callback, arg)
参数 |
描述 |
---|---|
打回来 |
先前定义的 函数 在按下触摸 LED 时执行。 |
arg |
可选。包含要传递给回调函数的参数的元组。更多信息请参阅使用带参数的事件。 |
# Define a function touchLED_pressed()
def touchLED_pressed():
touchled_1.set_color(Color.BLUE)
# Light up the Touch LED when it is pressed
touchled_1.pressed(touchLED_pressed)
发布#
released
注册了一个在触摸 LED 被释放时要调用的函数。
用法:
released(callback,arg)
参数 |
描述 |
---|---|
打回来 |
先前定义的 function 在释放触摸 LED 时执行。 |
arg |
可选。包含要传递给回调函数的参数的元组。更多信息请参阅使用带参数的事件。 |
# Define a function touchLED_released()
def touchLED_released():
touchled_1.set_color(Color.RED)
# Light up the Touch LED red when the Touch LED is released
touchled_1.released(touchLED_released)
已安装#
installed
返回一个布尔值,指示触摸 LED 是否连接到大脑。
“True”——触摸 LED 已连接到大脑。
False
— 触摸 LED 未连接到大脑。
用法:
isntalled()
参数 |
描述 |
---|---|
该方法没有参数。 |
# Display a message if the Touch LED sensor is installed
if touchled_1.installed():
brain.screen.print("Installed!")
构造函数#
构造函数用于手动创建“TouchLED”对象,这对于在 VEXcode 之外配置触摸 LED 是必需的。
触摸LED#
TouchLED
创建一个触摸 LED。
用法:
TouchLED(端口)
范围 |
描述 |
---|---|
|
触摸 LED 连接到哪个智能端口,以“PORT”表示,后跟端口号,范围从 1 到 12。 |
# Construct a Touch LED Sensor "touchled_1" with the
# TouchLED class.
touchled_1 = Touchled(Ports.PORT1)
# Turn the LED green with different brightnesses
# Default brightness
touchled_1.set_color(Color.GREEN)
wait(2,SECONDS)
# Low brightness
touchled_1.set_brightness(10)
wait(2,SECONDS)
# Max brightness
touchled_1.set_brightness(255)