類比輸入#

介紹#

analog_in 類別用於讀取連接到 VEX V5 三線連接埠的裝置的類比輸入訊號。它提供對當前模擬值的訪問,並允許註冊回調函數以處理值的變化。

類別建構函數#

analog_in(
    triport::port &port );

類別析構函數#

銷毀 analog_in 物件並釋放相關資源。

~analog_in();

參數#

範圍

類型

描述

port

triport::port&

類比輸入連接的 3 線端口,寫成 Brain.ThreeWirePort.X 或 ExpanderName.X,其中 X 是端口字母(例如, Brain.ThreeWirePort.A 或 Expander1.A)。

例子#

// Create the analog_in instance on 3-Wire Port A
analog_in AnalogInA = analog_in(Brain.ThreeWirePort.A);

成員功能#

analog_in 類別包含以下成員函數:

  • value — 傳回類比輸入設備的值。

  • changed — 註冊模擬輸入值變更時的回呼函數。

在呼叫 analog_in 函數之前,必須先建立 analog_in 實例,如下所示:

/* This constructor is required to use an
Analog Input device. Replace the values
as needed. */

// Create the analog_in instance on 3-Wire Port A
analog_in AnalogInA = analog_in(Brain.ThreeWirePort.A);

value#

傳回類比輸入的當前值。

Available Functions
int32_t value(
  analogUnits units );

Parameters

範圍

類型

描述

units

analogUnits

表示值的單位:

  • range8bit — 8 位元模擬值(具有 256 個可能狀態的值)
  • range10bit — 10 位元模擬值(具有 1024 個可能狀態的值)
  • range12bit — 12 位元模擬值(具有 14096 1409 120 120096 120095 120095 120 120 120095 12009
  • pct )百分比
  • mV — 毫伏

Return Values

此函數傳回模擬輸入的當前值。

Examples
// Get the current value of the Analog Input
int32_t value = AnalogInA.value();

changed#

註冊一個回調函數,當模擬輸入值發生變化時,該函數將被呼叫。

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

Parameters

範圍

類型

描述

callback

void (* callback)(void)

當類比輸入值發生變化時,將呼叫該函數。

Return Values

此函數不傳回值。

Examples

定義回呼函數(在 int main()之外)

// Display a message when the input changes
void AnalogInputChanged() {
  Brain.Screen.print("analog input changed");
}

在 int main()中註冊回呼函數

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

  // Run AnalogInputChanged when the value of the
  // Analog Input changes
  AnalogInA.changed(AnalogInputChanged);
}