腦#
介紹#
在 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 Functionsvoid playSound(
soundtype sound,
uint32_t volume = 50 );
參數 |
類型 |
描述 |
|---|---|---|
|
|
大腦要播放的指定聲音。 |
|
|
可選。聲音播放音量,取值範圍為 0% 到 100% 的整數。預設值為 50%。 |
聲音名稱 |
播放聲音 |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
此函數不傳回值。
播放音符#
從大腦中發出一個音符。
Available Functions1 - 彈奏一個音符。
void playNote( int32_t octave, int32_t note );
Parameters2 - 以指定的持續時間和音量演奏音符。
void playNote( int32_t octave, int32_t note, int32_t duration, int32_t volume = 50 );
參數 |
類型 |
描述 |
|---|---|---|
|
|
表示音符所在八度的整數:
|
|
|
演奏音符:
6 - G |
|
|
可選。音符的持續時間(以毫秒為單位),最長為 500 毫秒。如果未提供持續時間,則預設持續時間為 500 毫秒。 |
|
|
可選。聲音播放音量,取值範圍為 0% 到 100% 的整數。預設值為 50%。 |
此函數不傳回值。
程式停止#
停止 EXP Brain 上目前正在執行的項目。
Available Functionsvoid programStop();
此函數不接受任何參數。
Return Values此函數不傳回值。
Examples// Stop the project when the Brain screen is pressed
while (true) {
if (Brain.Screen.pressing()) {
Brain.programStop();
}
wait(20, msec);
}
定時器#
傳回大腦內部計時器的目前值,單位為指定單位。
Available Functionsdouble timer(
timeUnits units );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
The unit of time to return:
|
傳回一個 double 表示自程式啟動以來經過的時間,單位為指定值。
專案開始時,計時器會自動啟動。
可以使用
resetTimer重設計時器。
// Stop the project after 3 seconds
while (Brain.timer(seconds) < 3) {
wait(20, msec);
}
Brain.programStop();
重置計時器#
將大腦內部計時器重置為零,之後,計時器立即從零開始重新計數。
Available Functionsvoid resetTimer();
此函數不接受任何參數。
Return Values此函數不傳回值。
Notes計時器重置後會立即重新開始計時。
呼叫
resetTimer會影響後續呼叫timer傳回的值。
// 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 Functionsvoid setTimer(
double value,
timeUnits units );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
根據參數 |
|
|
The unit used to interpret the
|
此函數不傳回值。
Notes計時器設定好後會繼續向前計時。
呼叫
setTimer立即改變timer傳回的值。
// 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");