模拟输入#

介绍#

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 位模拟值(具有 4096 个可能状态的值)
  • 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);
}