mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[examples] Add snippets for Digital Input article (#7949)
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#include <frc/AnalogInput.h>
|
||||
#include <frc/AnalogTrigger.h>
|
||||
#include <frc/TimedRobot.h>
|
||||
|
||||
/**
|
||||
* Analog Trigger snippets for frc-docs.
|
||||
* https://docs.wpilib.org/en/stable/docs/software/hardware-apis/sensors/digital-input-software.html
|
||||
*/
|
||||
class Robot : public frc::TimedRobot {
|
||||
public:
|
||||
Robot() {
|
||||
// Enables 2-bit oversampling
|
||||
input.SetAverageBits(2);
|
||||
// Sets the trigger to enable at a raw value of 3500, and disable at a value
|
||||
// of 1000
|
||||
trigger0.SetLimitsRaw(1000, 3500);
|
||||
// Sets the trigger to enable at a voltage of 4 volts, and disable at a
|
||||
// value of 1.5 volts
|
||||
trigger0.SetLimitsVoltage(1.5, 4);
|
||||
}
|
||||
|
||||
private:
|
||||
// Initializes an AnalogTrigger on port 0
|
||||
frc::AnalogTrigger trigger0{0};
|
||||
// Initializes an AnalogInput on port 1
|
||||
frc::AnalogInput input{1};
|
||||
|
||||
// Initializes an AnalogTrigger using the above input
|
||||
frc::AnalogTrigger trigger1{input};
|
||||
};
|
||||
|
||||
#ifndef RUNNING_FRC_TESTS
|
||||
int main() {
|
||||
return frc::StartRobot<Robot>();
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,29 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#include <frc/DigitalInput.h>
|
||||
#include <frc/TimedRobot.h>
|
||||
|
||||
/**
|
||||
* Digital Input snippets for frc-docs.
|
||||
* https://docs.wpilib.org/en/stable/docs/software/hardware-apis/sensors/digital-input-software.html
|
||||
*/
|
||||
class Robot : public frc::TimedRobot {
|
||||
public:
|
||||
void TeleopPeriodic() override {
|
||||
// Gets the value of the digital input. Returns true if the circuit is
|
||||
// open.
|
||||
m_input.Get();
|
||||
}
|
||||
|
||||
private:
|
||||
// Initializes a DigitalInput on DIO 0
|
||||
frc::DigitalInput m_input{0};
|
||||
};
|
||||
|
||||
#ifndef RUNNING_FRC_TESTS
|
||||
int main() {
|
||||
return frc::StartRobot<Robot>();
|
||||
}
|
||||
#endif
|
||||
@@ -62,5 +62,26 @@
|
||||
],
|
||||
"foldername": "LimitSwitch",
|
||||
"gradlebase": "cpp"
|
||||
},
|
||||
{
|
||||
"name": "DigitalInput",
|
||||
"description": "Snippets of Digital Input for frc-docs.",
|
||||
"tags": [
|
||||
"Hardware",
|
||||
"Digital Input"
|
||||
],
|
||||
"foldername": "DigitalInput",
|
||||
"gradlebase": "cpp"
|
||||
},
|
||||
{
|
||||
"name": "AnalogTrigger",
|
||||
"description": "Snippets of Analog Trigger for frc-docs.",
|
||||
"tags": [
|
||||
"Hardware",
|
||||
"Analog",
|
||||
"Digital Input"
|
||||
],
|
||||
"foldername": "AnalogTrigger",
|
||||
"gradlebase": "cpp"
|
||||
}
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user