觸摸 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)