触摸 LED#

介绍#

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

类构造函数#

touchled( 
  int32_t index );

类析构函数#

Destroys the touchled object and releases associated resources.

virtual ~touchled();

参数#

范围

类型

描述

index

int32_t

触摸 LED 连接的智能端口,写成 PORTx,其中 x 是端口号(例如, PORT1)。

例子#

// Create a touchled instance in Port 1
touchled TouchLED1 = touchled(PORT1);

成员功能#

touchled 类包含以下成员函数:

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

  • setColor – 设置触摸 LED 的颜色。

  • setFade – 设置颜色之间淡入淡出的速度。

  • setBrightness – 设置触摸 LED 的亮度。

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

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

  • on – 打开触摸 LED。

  • off – 关闭触摸 LED。

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

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

在调用任何 touchled 函数之前,必须先创建一个 touchled ,如下所示:

/* This constructor is required when using VS Code.
Touch LED configuration is generated automatically
in VEXcode using the Device Menu. Replace the values
as needed. */

// Create a Touch LED instance in Port 1
touchled TouchLED1 = touchled(PORT1);

pressing#

返回触摸LED当前是否被按下。

Available Functions
bool pressing();

Parameters

此函数不接受任何参数。

Return Values

返回 bool 布尔值,指示触摸 LED 是否被按下。

  • true — The Touch LED is being pressed.
  • false — The Touch LED is not being pressed.

setColor#

将触摸LED传感器的LED设置为特定颜色。

Available Functions

1 — 使用 vex::color将触摸 LED 设置为某种颜色。

void setColor( 
  vex::color  color );

2 — 使用 colorType设置 LED 颜色。

void setColor( 
  colorType   color );

Parameters

范围

类型

描述

color

colorType

A valid color:

  • blue
  • blue_green
  • blue_violet
  • green
  • orange
  • purple
  • red
  • red_orange
  • red_violet
  • violet
  • white
  • yellow
  • yellow_green
  • yellow_orange
  • colorType::none
  • black
  • transparent

color

vex::color

有效的自定义颜色。

Return Values

此函数不返回值。

setFade#

设置触摸式 LED 传感器的渐变效果。触摸式 LED 传感器将根据所选渐变效果变换颜色。

Available Functions
void setFade(
  fadeType  speed );

Parameters

范围

类型

描述

speed

fadeType

淡入淡出速度可设置为:

  • fast
  • off — 无淡入淡出
  • slow

Return Values

此函数不返回值。

setBrightness#

设置触摸式 LED 的亮度。

Available Functions
void setBrightness(
  uint32_t  value );

Parameters

范围

类型

描述

value

uint32_t

以百分比形式设置触摸 LED 的亮度。

Return Values

此函数不返回值。

pressed#

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

Available Functions
void pressed( void (* callback)(void) );

Parameters

范围

类型

描述

callback

void (*)(void)

指向一个函数的指针,该函数将在触摸 LED 被按下时调用。该函数不能接受任何参数,并且必须返回 void 类型。

Return Values

此函数不返回值。

Examples

定义回调函数(在 int main()之外)

// Display a message when LED is pressed
void LEDPressed() {
  Brain.Screen.print("LED pressed");
}

在 int main()中注册回调函数

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  // Run LEDPressed when the value of the Touch LED is pressed.
  TouchLED1.pressed(LEDPressed);
}

released#

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

Available Functions
void released( void (* callback)(void) );

Parameters

范围

类型

描述

callback

void (*)(void)

指向一个函数的指针,该函数将在触摸 LED 释放时调用。该函数不得接受任何参数,且必须返回 void。

Return Values

此函数不返回值。

Examples

定义回调函数(在 int main()之外)

// Display a message when LED is released
void LEDReleased() {
  Brain.Screen.print("LED released");
}

在 int main()中注册回调函数

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  // Run LEDReleased when the value of the Touch LED is released.
  TouchLED1.released(LEDReleased);
}

on#

使用颜色和设定的亮度打开触摸LED传感器的LED灯。

Available Functions

1 — 使用 vex::color 设置 LED 的颜色。

void on( 
  vex::color    color, 
  uint32_t      brightness = 100 );

2 — 使用色调设置 LED 颜色。

void on( 
  uint32_t hue, 
  uint32_t brightness = 100);

3 — 通过 RGB 值设置 LED 颜色。

void on( 
  uint8_t   red, 
  uint8_t   green, 
  uint8_t   blue, 
  uint32_t  brightness = 100);

Parameters

范围

类型

描述

color

vex::color

A valid color:

  • blue
  • blue_green
  • blue_violet
  • green
  • orange
  • purple
  • red
  • red_orange
  • red_violet
  • violet
  • white
  • yellow
  • yellow_green
  • yellow_orange
  • colorType::none
  • black
  • transparent

brightness

uint32_t

可选。LED 亮度设置百分比,默认值为 100。

hue

uint32_t

LED 的色调。这也可以用十六进制代码值 (0x000000) 表示。

red

uint8_t

表示 LED 红色值的整数。

green

uint8_t

表示 LED 绿色值的整数。

blue

uint8_t

表示 LED 蓝色值的整数。

Return Values

此函数不返回值。

off#

关闭触摸指示灯。

Available Functions
void off( void );

Parameters

此函数不接受任何参数。

Return Values

此函数不返回值。

installed#

返回触摸LED是否已连接。

Available Functions
bool installed();

Parameters

此函数不接受任何参数。

Return Values

返回一个布尔值,指示触摸 LED 是否已连接:

  • true — The sensor is connected and responding.
  • false — The sensor is not connected or not detected.