腦#

介紹#

在 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 - G
  • 22

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");