Skip to content

Instantly share code, notes, and snippets.

@williamhaley
Created December 16, 2020 20:05
Show Gist options
  • Save williamhaley/8b9085716a835ff080cf1066eeed7999 to your computer and use it in GitHub Desktop.
Save williamhaley/8b9085716a835ff080cf1066eeed7999 to your computer and use it in GitHub Desktop.
NCP Java Demo (Commands)
public class MoveLeftCommand extends RobotCommand {
public MoveLeftCommand(String button) {
super(button);
}
protected void logic() {
// Set up the motors
// Move the robot
// Make sure robot is in the right position
System.out.println("move left!");
}
}
public class PickUpBallCommand extends RobotCommand {
public PickUpBallCommand(String button) {
super(button);
}
protected void logic() {
// Set up the motors
// Move the arm
// Open the claw
// Close the claw
System.out.println("pick up ball!");
}
}
class RobotButtonDemo {
public RobotButtonDemo() {
// Nothing to set up.
}
public void run() {
RobotCommand moveLeft = new MoveLeftCommand("X");
moveLeft.runTheCommand();
RobotCommand pickUpBallCommand = new PickUpBallCommand("A");
pickUpBallCommand.runTheCommand();
}
}
public abstract class RobotCommand {
private final String button;
protected abstract void logic();
public RobotCommand(String button) {
this.button = button;
this.setup();
}
private void setup() {
/*
Magic where we configure the button.
*/
}
private void doCommonButtonMagic() {
/*
Lots of magic here
*/
}
public void runTheCommand() {
System.out.printf("'%s' button was pressed!\n", this.button);
this.doCommonButtonMagic();
this.setup();
this.logic();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment