Skip to content

Instantly share code, notes, and snippets.

@Jerrylum
Last active November 22, 2023 07:09
Show Gist options
  • Save Jerrylum/cf4d417e3dbce0b7722f06b1311fae12 to your computer and use it in GitHub Desktop.
Save Jerrylum/cf4d417e3dbce0b7722f06b1311fae12 to your computer and use it in GitHub Desktop.
auton selector on VCS
#include "robot-config.h"
void make_button(int x, int y, int tx, int ty, char *title, vex::color c, int w = 80, int h = 40) {
Brain.Screen.drawRectangle(x,y,w,h,c);
Brain.Screen.setFillColor(c);
Brain.Screen.printAt(x+tx,y+ty,title);
}
bool is_clicked(int bx, int by, int w = 80, int h = 40) {
int x = Brain.Screen.xPosition();
int y = Brain.Screen.yPosition();
int rx = x - bx;
int ry = y - by;
return rx > 0 && rx < w && ry > 0 && ry < h;
}
// global variable
bool isBuleSide = false; // true = blue
bool isFarField = false; // true = far field
bool isAutoEnable = true; // true = enable
int main() {
while (true) {
Brain.Screen.setFillColor(color::black);
Brain.Screen.printAt(10,45,"Side:");
Brain.Screen.printAt(10,95,"Field:");
Brain.Screen.printAt(50,140,"Skip auton");
if (isBuleSide) {
make_button(80,20,25,27,"RED",color::red);
} else {
make_button(80,20,20,27,"BLUE",color::blue);
}
if (isFarField) {
make_button(80,70,25,27,"FAR",color::orange);
} else {
make_button(80,70,20,27,"FLAG",color::purple);
}
if (isAutoEnable) {
make_button(10,120,15,15,"",color::black,30,30);
} else {
make_button(10,120,15,15,"",color::green,30,30);
Brain.Screen.setFillColor(color::white);
Brain.Screen.drawLine(80,20,160,60);
Brain.Screen.drawLine(80,70,160,110);
}
while (!Brain.Screen.pressing()) task::sleep(50);
while (Brain.Screen.pressing()) task::sleep(50);
if (is_clicked(10,120,120)) {
isAutoEnable = !isAutoEnable;
}else if (is_clicked(80,20) && isAutoEnable) {
isBuleSide = !isBuleSide;
} else if (is_clicked(80,70) && isAutoEnable) {
isFarField = !isFarField;
}
}
}
#include "robot-config.h"
/*---------------------------------------------------------------------------*/
/* */
/* Description: Competition template for VCS VEX V5 */
/* */
/*---------------------------------------------------------------------------*/
void make_button(int x, int y, int tx, int ty, char *title, vex::color c, int w = 80, int h = 40) {
Brain.Screen.drawRectangle(x,y,w,h,c);
Brain.Screen.setFillColor(c);
Brain.Screen.printAt(x+tx,y+ty,title);
}
bool is_clicked(int bx, int by, int w = 80, int h = 40) {
int x = Brain.Screen.xPosition();
int y = Brain.Screen.yPosition();
int rx = x - bx;
int ry = y - by;
return rx > 0 && rx < w && ry > 0 && ry < h;
}
// global variable
bool isBuleSide = false; // true = blue
bool isFarField = false; // true = far field
bool isAutoEnable = true; // true = enable
void disabled(){
while (!Competition.isEnabled()) {
Brain.Screen.setFillColor(color::black);
Brain.Screen.printAt(10,45,"Side:");
Brain.Screen.printAt(10,95,"Field:");
Brain.Screen.printAt(50,140,"Skip auton");
if (isBuleSide) {
make_button(80,20,25,27,"RED",color::red);
} else {
make_button(80,20,20,27,"BLUE",color::blue);
}
if (isFarField) {
make_button(80,70,25,27,"FAR",color::orange);
} else {
make_button(80,70,20,27,"FLAG",color::purple);
}
if (isAutoEnable) {
make_button(10,120,15,15,"",color::black,30,30);
} else {
make_button(10,120,15,15,"",color::green,30,30);
Brain.Screen.setFillColor(color::white);
Brain.Screen.drawLine(80,20,160,60);
Brain.Screen.drawLine(80,70,160,110);
}
while (!Brain.Screen.pressing() && !Competition.isEnabled()) task::sleep(50);
while (Brain.Screen.pressing() && !Competition.isEnabled()) task::sleep(50);
if (Competition.isEnabled()) break;
if (is_clicked(10,120,120)) {
isAutoEnable = !isAutoEnable;
}else if (is_clicked(80,20) && isAutoEnable) {
isBuleSide = !isBuleSide;
} else if (is_clicked(80,70) && isAutoEnable) {
isFarField = !isFarField;
}
}
}
/*---------------------------------------------------------------------------*/
/* Pre-Autonomous Functions */
/* */
/* You may want to perform some actions before the competition starts. */
/* Do them in the following function. You must return from this function */
/* or the autonomous and usercontrol tasks will not be started. This */
/* function is only called once after the cortex has been powered on and */
/* not every time that the robot is disabled. */
/*---------------------------------------------------------------------------*/
void pre_auton( void ) {
// All activities that occur before the competition starts
// Example: clearing encoders, setting servo positions, ...
}
/*---------------------------------------------------------------------------*/
/* */
/* Autonomous Task */
/* */
/* This task is used to control your robot during the autonomous phase of */
/* a VEX Competition. */
/* */
/* You must modify the code to add your own robot specific commands here. */
/*---------------------------------------------------------------------------*/
void autonomous( void ) {
// ..........................................................................
// Insert autonomous user code here.
// ..........................................................................
}
/*----------------------------------------------------------------------------*/
/* */
/* User Control Task */
/* */
/* This task is used to control your robot during the user control phase of */
/* a VEX Competition. */
/* */
/* You must modify the code to add your own robot specific commands here. */
/*----------------------------------------------------------------------------*/
void usercontrol( void ) {
// User control code here, inside the loop
while (1){
// This is the main execution loop for the user control program.
// Each time through the loop your program should update motor + servo
// values based on feedback from the joysticks.
// ........................................................................
// Insert user code here. This is where you use the joystick values to
// update your motors, etc.
// ........................................................................
vex::task::sleep(20); //Sleep the task for a short amount of time to prevent wasted resources.
}
}
//
// Main will set up the competition functions and callbacks.
//
int main() {
//Run the pre-autonomous function.
pre_auton();
//Set up callbacks for autonomous and driver control periods.
Competition.autonomous( autonomous );
Competition.drivercontrol( usercontrol );
while(1) {
while(Competition.isEnabled()) vex::task::sleep(50);
disabled();
Brain.Screen.clearScreen();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment