数字输入#

介绍#

The digital_in class allows the V5 Brain to read a digital signal from an external source, such as a switch or sensor. It detects whether the input line is high, receiving 5 volts, or low, receiving 0 volts. This can be used to determine on/off states or trigger events in a project.

类构造函数#

digital_in(
  triport::port &port );

类析构函数#

Destroys the digital_in object and releases associated resources.

~digital_in();

参数#

范围

类型

描述

port

triport::port &

The 3-Wire Port that the Digital Input device is connected to, written as Brain.ThreeWirePort.X or ExpanderName.X, where X is the port letter (for example, Brain.ThreeWirePort.A or Expander1.A).

例子#

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

成员功能#

The digital_in class includes the following member functions:

  • value — Returns whether the Digital In port is receiving a high or low signal.

  • high — Registers a function to be called whenever the Digital In device sends a high signal.

  • low — Registers a function to be called whenever the Digital In device sends a low signal.

Before calling any digital_in member functions, a digital_in instance must be created, as shown below:

/* This constructor is required when using VS Code.
Digital input configuration is generated automatically
in VEXcode using the Device Menu. Replace the values
as needed. */

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

value#

返回数字输入端口接收到的是高电平信号还是低电平信号。

可用功能

int32_t value();

参数

此函数不接受任何参数。

返回值

Returns an int32_t representing the current digital signal state:

  • 1 – The Digital In port is receiving a high signal (approximately 5V).

  • 0 – The Digital In port is receiving a low signal (approximately 0V).

high#

注册一个函数,当数字输入设备发送高电平信号(约 5V)时,该函数将被调用。

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

Parameters

范围

类型

描述

callback

void (* callback)(void)

当输入为高电平时将调用的函数。

Return Values

此函数不返回值。

Examples

Define the callback function (outside of int main())

// Display that the input is high
void inputHigh() {
  Brain.Screen.print("digital input high");
}

Register the callback inside int main()

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

  // Run inputHigh when the Digital Input is high
  DigitalInA.high(inputHigh);
}

low#

注册一个函数,当数字输入设备发送低信号(约 0V)时,该函数将被调用。

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

Parameters

范围

类型

描述

callback

void (* callback)(void)

当输入为低电平时将调用的函数。

Return Values

此函数不返回值。

Examples

Define the callback function (outside of int main())

// Display that the input is low
void inputLow() {
  Brain.Screen.print("digital input low");
}

Register the callback inside int main()

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

  // Run inputLow when the Digital Input is low
  DigitalInA.low(inputLow);
}