From e63899e63af065eb326bdbb2cf1457cf27433bd9 Mon Sep 17 00:00:00 2001 From: sciencewhiz Date: Thu, 1 May 2025 10:40:38 -0700 Subject: [PATCH] [examples] Add snippets for Digital Input article (#7949) --- .../cpp/snippets/AnalogTrigger/cpp/Robot.cpp | 40 +++++++++++++++++++ .../cpp/snippets/DigitalInput/cpp/Robot.cpp | 29 ++++++++++++++ .../src/main/cpp/snippets/snippets.json | 21 ++++++++++ .../wpilibj/snippets/analogtrigger/Main.java | 25 ++++++++++++ .../wpilibj/snippets/analogtrigger/Robot.java | 35 ++++++++++++++++ .../wpilibj/snippets/digitalinput/Main.java | 25 ++++++++++++ .../wpilibj/snippets/digitalinput/Robot.java | 23 +++++++++++ .../wpi/first/wpilibj/snippets/snippets.json | 23 +++++++++++ 8 files changed, 221 insertions(+) create mode 100644 wpilibcExamples/src/main/cpp/snippets/AnalogTrigger/cpp/Robot.cpp create mode 100644 wpilibcExamples/src/main/cpp/snippets/DigitalInput/cpp/Robot.cpp create mode 100644 wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analogtrigger/Main.java create mode 100644 wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analogtrigger/Robot.java create mode 100644 wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/digitalinput/Main.java create mode 100644 wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/digitalinput/Robot.java diff --git a/wpilibcExamples/src/main/cpp/snippets/AnalogTrigger/cpp/Robot.cpp b/wpilibcExamples/src/main/cpp/snippets/AnalogTrigger/cpp/Robot.cpp new file mode 100644 index 0000000000..5270ac3146 --- /dev/null +++ b/wpilibcExamples/src/main/cpp/snippets/AnalogTrigger/cpp/Robot.cpp @@ -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 +#include +#include + +/** + * 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(); +} +#endif diff --git a/wpilibcExamples/src/main/cpp/snippets/DigitalInput/cpp/Robot.cpp b/wpilibcExamples/src/main/cpp/snippets/DigitalInput/cpp/Robot.cpp new file mode 100644 index 0000000000..4a3b648c63 --- /dev/null +++ b/wpilibcExamples/src/main/cpp/snippets/DigitalInput/cpp/Robot.cpp @@ -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 +#include + +/** + * 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(); +} +#endif diff --git a/wpilibcExamples/src/main/cpp/snippets/snippets.json b/wpilibcExamples/src/main/cpp/snippets/snippets.json index 27fd0e46a1..4ccb059c0c 100644 --- a/wpilibcExamples/src/main/cpp/snippets/snippets.json +++ b/wpilibcExamples/src/main/cpp/snippets/snippets.json @@ -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" } ] diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analogtrigger/Main.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analogtrigger/Main.java new file mode 100644 index 0000000000..a9f312defa --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analogtrigger/Main.java @@ -0,0 +1,25 @@ +// 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. + +package edu.wpi.first.wpilibj.snippets.analogtrigger; + +import edu.wpi.first.wpilibj.RobotBase; + +/** + * Do NOT add any static variables to this class, or any initialization at all. Unless you know what + * you are doing, do not modify this file except to change the parameter class to the startRobot + * call. + */ +public final class Main { + private Main() {} + + /** + * Main initialization function. Do not perform any initialization here. + * + *

If you change your main robot class, change the parameter type. + */ + public static void main(String... args) { + RobotBase.startRobot(Robot::new); + } +} diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analogtrigger/Robot.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analogtrigger/Robot.java new file mode 100644 index 0000000000..893db80acd --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/analogtrigger/Robot.java @@ -0,0 +1,35 @@ +// 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. + +package edu.wpi.first.wpilibj.snippets.analogtrigger; + +import edu.wpi.first.wpilibj.AnalogInput; +import edu.wpi.first.wpilibj.AnalogTrigger; +import edu.wpi.first.wpilibj.TimedRobot; + +/** + * DigitalInput snippets for frc-docs. + * https://docs.wpilib.org/en/stable/docs/software/hardware-apis/sensors/digital-inputs-software.html + */ +public class Robot extends TimedRobot { + // Initializes an AnalogTrigger on port 0 + AnalogTrigger m_trigger0 = new AnalogTrigger(0); + // Initializes an AnalogInput on port 1 and enables 2-bit oversampling + AnalogInput m_input = new AnalogInput(1); + + // Initializes an AnalogTrigger using the above input + AnalogTrigger m_trigger1 = new AnalogTrigger(m_input); + + /** Called once at the beginning of the robot program. */ + public Robot() { + // Enables 2-bit oversampling + m_input.setAverageBits(2); + // Sets the trigger to enable at a raw value of 3500, and disable at a value of 1000 + m_trigger0.setLimitsRaw(1000, 3500); + // Sets the trigger to enable at a voltage of 4 volts, and disable at a value of 1.5 volts + m_trigger0.setLimitsVoltage(1.5, 4); + + m_trigger1.getTriggerState(); + } +} diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/digitalinput/Main.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/digitalinput/Main.java new file mode 100644 index 0000000000..fb52ad8ae6 --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/digitalinput/Main.java @@ -0,0 +1,25 @@ +// 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. + +package edu.wpi.first.wpilibj.snippets.digitalinput; + +import edu.wpi.first.wpilibj.RobotBase; + +/** + * Do NOT add any static variables to this class, or any initialization at all. Unless you know what + * you are doing, do not modify this file except to change the parameter class to the startRobot + * call. + */ +public final class Main { + private Main() {} + + /** + * Main initialization function. Do not perform any initialization here. + * + *

If you change your main robot class, change the parameter type. + */ + public static void main(String... args) { + RobotBase.startRobot(Robot::new); + } +} diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/digitalinput/Robot.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/digitalinput/Robot.java new file mode 100644 index 0000000000..999c72821f --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/digitalinput/Robot.java @@ -0,0 +1,23 @@ +// 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. + +package edu.wpi.first.wpilibj.snippets.digitalinput; + +import edu.wpi.first.wpilibj.DigitalInput; +import edu.wpi.first.wpilibj.TimedRobot; + +/** + * DigitalInput snippets for frc-docs. + * https://docs.wpilib.org/en/stable/docs/software/hardware-apis/sensors/digital-inputs-software.html + */ +public class Robot extends TimedRobot { + // Initializes a DigitalInput on DIO 0 + DigitalInput m_input = new DigitalInput(0); + + @Override + public void teleopPeriodic() { + // Gets the value of the digital input. Returns true if the circuit is open. + m_input.get(); + } +} diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/snippets.json b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/snippets.json index 75d78b3284..4fada01e0b 100644 --- a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/snippets.json +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/snippets/snippets.json @@ -68,5 +68,28 @@ "foldername": "limitswitch", "gradlebase": "java", "mainclass": "Main" + }, + { + "name": "DigitalInput", + "description": "Snippets of Digital Input for frc-docs.", + "tags": [ + "Hardware", + "Digital Input" + ], + "foldername": "digitalinput", + "gradlebase": "java", + "mainclass": "Main" + }, + { + "name": "AnalogTrigger", + "description": "Snippets of Analog Trigger for frc-docs.", + "tags": [ + "Hardware", + "Analog", + "Digital Input" + ], + "foldername": "analogtrigger", + "gradlebase": "java", + "mainclass": "Main" } ]