[examples] Add snippets for Digital Input article (#7949)

This commit is contained in:
sciencewhiz
2025-05-01 10:40:38 -07:00
committed by GitHub
parent ba37e7eb3c
commit e63899e63a
8 changed files with 221 additions and 0 deletions

View File

@@ -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.
*
* <p>If you change your main robot class, change the parameter type.
*/
public static void main(String... args) {
RobotBase.startRobot(Robot::new);
}
}

View File

@@ -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();
}
}

View File

@@ -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.
*
* <p>If you change your main robot class, change the parameter type.
*/
public static void main(String... args) {
RobotBase.startRobot(Robot::new);
}
}

View File

@@ -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();
}
}

View File

@@ -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"
}
]