脑#

介绍#

在 VEX EXP C++ 中,大脑由 brain 表示。您可以创建(或使用项目提供的默认 Brain 对象),然后通过其成员对象访问大脑功能。

派生类#

brain 类作为以下派生类的基类:

  • Screen - 此类提供对 Brain 内置彩色触摸屏的访问,允许程序显示文本、图形并接收触摸输入。

  • Timer - 此类提供高分辨率计时工具,用于测量经过的时间和控制程序中基于时间的行为。

  • Battery - 此类允许程序监控大脑的电池状态,包括电压、电流、容量和温度。

  • Button - 此类允许程序与大脑的按钮进行交互。

  • ThreeWirePort (triport / 三线端口) - 此类提供对大脑三线端口的访问,用于连接和控制传统传感器和设备,例如限位开关、电位器和 LED。

  • SDcard - 此类允许从大脑的 SD 卡读取和写入文件,用于文件存储、数据记录和加载外部资源。

类构造函数#

brain();

参数#

brain 构造函数不使用任何参数。

笔记#

  • 一个项目中只能创建一个 brain 对象。

例子#

// Create the EXP Brain
brain Brain = brain();

成员功能#

brain 类包含以下成员函数:

  • playSound - 播放大脑内置的声音效果之一。

  • playNote - 播放指定时长的音符。

  • programStop - 停止 EXP Brain 上当前正在运行的项目。

  • timer - 返回大脑内部计时器的当前值,单位为指定单位。

  • resetTimer - 将大脑的内部计时器重置为零并重新开始计时。

  • setTimer - 将大脑的内部计时器设置为指定的值和时间单位。

在调用任何 brain 成员函数之前,必须先创建一个 brain 实例,如下所示:

// Create the EXP Brain
brain Brain = brain();

播放声音#

播放大脑内置的声音之一。

Available Functions
void playSound(
    soundtype sound,
    uint32_t volume = 50 );

Parameters

参数

类型

描述

sound

soundtype

大脑要播放的指定声音。

volume

uint32_t

可选。声音播放音量,取值范围为 0% 到 100% 的整数。默认值为 50%。

声音名称

播放声音

alarm

alarm2

doorClose

fillup

headlightsOff

headlightsOn

powerDown

ratchet

ratchet2

siren

siren2

tada

tollBooth

wrench

wrongWay

wrongWaySlow

Return Values

此函数不返回值。

播放音符#

从大脑中发出一个音符。

Available Functions

1 - 演奏一个音符。

void playNote(
    int32_t octave,
    int32_t note );

2 - 以指定的持续时间和音量演奏音符。

void playNote(
    int32_t octave,
    int32_t note,
    int32_t duration,
    int32_t volume = 50 );

Parameters

参数

类型

描述

octave

int32_t

表示音符所在八度的整数:

  • 1 至 3 - 低音八度
  • 4 至 7 - 高音八度

note

int32_t

演奏音符:

  • 0 - C
  • 1 - D
  • 2 - E
  • 3 - F
  • 4 - G
  • 5 - A
  • 6 - B

duration

int32_t

可选。音符的持续时间(以毫秒为单位),最长为 500 毫秒。如果未提供持续时间,则默认持续时间为 500 毫秒。

volume

int32_t

可选。声音播放音量,取值范围为 0% 到 100% 的整数。默认值为 50%。

Return Values

此函数不返回值。

程序停止#

停止 EXP Brain 上当前正在运行的项目。

Available Functions
void programStop();

Parameters

此函数不接受任何参数。

Return Values

此函数不返回值。

Examples
// Stop the project when the Brain screen is pressed
while (true) {
  if (Brain.Screen.pressing()) {
    Brain.programStop();
  }
  wait(20, msec);
}

定时器#

返回大脑内部计时器的当前值,单位为指定单位。

Available Functions
double timer(
  timeUnits units );

Parameters

范围

类型

描述

units

timeUnits

The unit of time to return:

  • seconds / sec - seconds
  • msec - milliseconds

Return Values

返回一个 double 表示自程序启动以来经过的时间,单位为指定值。

Examples
  • 项目开始时,计时器会自动启动。

  • 可以使用 resetTimer 重置计时器。

Examples
// Stop the project after 3 seconds
while (Brain.timer(seconds) < 3) {
  wait(20, msec);
}

Brain.programStop();

重置计时器#

将大脑内部计时器重置为零,之后,计时器立即从零开始重新计数。

Available Functions
void resetTimer();

Parameters

此函数不接受任何参数。

Return Values

此函数不返回值。

Notes
  • 计时器重置后会立即重新开始计时。

  • 调用 resetTimer 会影响后续对 timer 的调用返回的值。

Examples
// Reset the timer and measure elapsed time
Brain.resetTimer();

while (Brain.timer(sec) < 5) {
  wait(20, msec);
}

// 5 seconds have elapsed since reset
Brain.Screen.print("5 seconds passed");

设置定时器#

将大脑的内部计时器设置为指定的值和时间单位。

Available Functions
void setTimer(
  double value,
  timeUnits units );

Parameters

范围

类型

描述

value

double

根据参数 units 中提供的 timeUnits ,设置定时器的时间。

units

timeUnits

The unit used to interpret the value parameter:

  • seconds / sec - seconds
  • msec - milliseconds

Return Values

此函数不返回值。

Notes
  • 计时器设定好后会继续向前计时。

  • 调用 setTimer 立即改变 timer 返回的值。

Examples
// Set the timer to start at 10 seconds
Brain.setTimer(10, sec);

// Wait until the timer is at 3 seconds
while (Brain.timer(sec) < 3) {
  wait(20, msec);
}

// 5 seconds have elapsed since setTimer was called
Brain.Screen.print("5 seconds passed");