Hey there,
I haven’t posted here in a while, but here’s what I want to do.
There is no API call that returns all buttons being pressed, and I was wondering if there is a much better way of doing it then this:
std::map<std::string, std::vector<int>> controllerButtonsPressed(const vex::controller &controller)
{
if (Competition.isEnabled())
{
logHandler("controllerButtonsPressed", "Robot is IN competition mode!", Log::Level::Error, 3);
}
std::map<std::string, std::vector<int>> buttonPressTimes;
vex::timer pressTimer;
std::array<ControllerButtonInfo, 12> AllControllerButtons = createControllerButtonArray(controller);
auto checkButtonPress = [&](const ControllerButtonInfo &info)
{
if (info.button->pressing())
{
pressTimer.clear();
while (info.button->pressing())
{
vex::this_thread::sleep_for(ConfigManager.getCtrlr1PollingRate());
}
buttonPressTimes[info.name].push_back(pressTimer.time());
}
};
while (!Competition.isEnabled())
{
for (const auto &btn : AllControllerButtons)
{
checkButtonPress(btn);
}
vex::this_thread::sleep_for(ConfigManager.getCtrlr1PollingRate());
}
for (const auto &[button, durations] : buttonPressTimes)
{
for (auto duration : durations)
{
std::string message = std::format("Button: {}, Duration: {} ms", button, duration);
logHandler("controllerButtonsPressed", message, Log::Level::Debug);
}
}
return buttonPressTimes;
}
class:
struct ControllerButtonInfo
{
const vex::controller::button *button;
std::string name;
};
std::array<ControllerButtonInfo, 12> createControllerButtonArray(const vex::controller &controller)
{
return {
ControllerButtonInfo{&controller.ButtonA, "A"},
ControllerButtonInfo{&controller.ButtonB, "B"},
ControllerButtonInfo{&controller.ButtonX, "X"},
ControllerButtonInfo{&controller.ButtonY, "Y"},
ControllerButtonInfo{&controller.ButtonUp, "Up"},
ControllerButtonInfo{&controller.ButtonDown, "Down"},
ControllerButtonInfo{&controller.ButtonLeft, "Left"},
ControllerButtonInfo{&controller.ButtonRight, "Right"},
ControllerButtonInfo{&controller.ButtonL1, "L1"},
ControllerButtonInfo{&controller.ButtonL2, "L2"},
ControllerButtonInfo{&controller.ButtonR1, "R1"},
ControllerButtonInfo{&controller.ButtonR2, "R2"}};
}
std::array<ControllerButtonInfo, 12> getControllerButtonArray(const vex::controller &controller)
{
return createControllerButtonArray(controller);
}
Here’s design:
the controllerButtonsPressed gets passed the controller you want to check. Then, get’s all the buttons from the class, and checks if they are being pressed and if so how long.
(I am aware this would only take the time of 1 button at a time, so if your holding X and A, and ket go of A, and then X right after, A would have the correct value, and X would not. )