触摸 LED#

介绍#

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

类构造函数#

touchled( 
  int32_t index );

类析构函数#

Destroys the touchled object and releases associated resources.

virtual ~touchled();

参数#

范围

类型

描述

index

int32_t

The Smart Port that the Touch LED is connected to, written as PORTx, where x is the port number (for example, PORT1).

例子#

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

成员功能#

The touchled class includes the following member functions:

  • pressing – Returns whether the Touch LED is being pressed.

  • setColor – Sets the color of the Touch LED.

  • setFade – Sets how fast colors will fade to one another.

  • setBrightness – Sets the brightness of the Touch LED.

  • pressed – Registers a function to be called when the Touch LED is pressed.

  • released – Registers a function to be called when the Touch LED is released.

  • on – Turns the Touch LED on.

  • off – Turns the Touch LED off.

  • setBlink – Alternate the Touch LED on and off indefinitely.

  • installed – Returns whether the Touch LED is connected to the Brain.

Before calling any touchled member functions, a touchled instance must be created, as shown below:

/* 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

Returns an bool indicating whether the Touch LED is being pressed.

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

setColor#

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

Available Functions

1 Sets the Touch LED to a color using vex::color.

void setColor( 
  vex::color  color );

2 Sets LED colors using a colorType.

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

The speed the fade will be set to:

  • fast
  • off — no fade
  • 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

Define the callback function (outside of int main())

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

Register the callback inside 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

Define the callback function (outside of int main())

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

Register the callback inside 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) 表示。

r

uint8_t

表示 LED 红色值的整数。

g

uint8_t

表示 LED 绿色值的整数。

b

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.