RaidZeroLib
Coroutine robot framework for VEX V5
Loading...
Searching...
No Matches
Controller.hpp
1#pragma once
2#include <array>
3#include <cstddef>
4#include <cstdint>
5#include <string_view>
6
7#include "RaidZeroLib/api/Runtime/Component.hpp"
8#include "RaidZeroLib/api/Runtime/Task.hpp"
9
10namespace rz {
11
19class Controller : public Component {
20 public:
21 enum class Stick : std::uint8_t { LeftX, LeftY, RightX, RightY };
22 enum class Button : std::uint8_t { L1, L2, R1, R2, Up, Down, Left, Right, X, B, Y, A };
23
24 static constexpr std::size_t STICK_COUNT = 4;
25 static constexpr std::size_t BUTTON_COUNT = 12;
26
28 [[nodiscard]] double stick(Stick stick) const { return current.sticks[static_cast<std::size_t>(stick)]; }
29
31 [[nodiscard]] bool held(Button button) const { return current.buttons[static_cast<std::size_t>(button)]; }
32
34 [[nodiscard]] bool pressed(Button button) const {
35 const auto index = static_cast<std::size_t>(button);
36 return current.buttons[index] && !previous.buttons[index];
37 }
38
39 [[nodiscard]] bool connected() const { return current.connected; }
40
42 [[nodiscard]] Task<> nextPress(Button button);
43
44 protected:
46 struct State {
47 bool connected = false;
48 std::array<double, STICK_COUNT> sticks{};
49 std::array<bool, BUTTON_COUNT> buttons{};
50 };
51
52 explicit Controller(std::string_view name) : Component(name) {}
53
55 [[nodiscard]] virtual State read() = 0;
56
57 private:
58 Task<> loop() final;
59
60 State current;
61 State previous;
62};
63
64} // namespace rz
std::string_view name() const noexcept
Used in log and error messages.
Definition Component.hpp:30
Component(const Component &)=delete
bool held(Button button) const
Whether the button is down.
Definition Controller.hpp:31
Task nextPress(Button button)
Waits for the next press of button, in a later tick than this one. Checked at estimatorsUpdated.
double stick(Stick stick) const
How far the stick is pushed, from -1 to 1. Right and up are positive.
Definition Controller.hpp:28
virtual State read()=0
Reads the controller now. Sticks from -1 to 1.
Button
Definition Controller.hpp:22
Stick
Definition Controller.hpp:21
bool connected() const
Definition Controller.hpp:39
static constexpr std::size_t BUTTON_COUNT
Definition Controller.hpp:25
bool pressed(Button button) const
Whether the button went down this tick.
Definition Controller.hpp:34
static constexpr std::size_t STICK_COUNT
Definition Controller.hpp:24
Controller(std::string_view name)
Definition Controller.hpp:52
corral::Task< T > Task
Lazy: nothing runs until it is co_awaited.
Definition Task.hpp:17
Definition DriverControl.hpp:11
Everything read from the controller in one tick.
Definition Controller.hpp:46
bool connected
Definition Controller.hpp:47
std::array< bool, BUTTON_COUNT > buttons
Definition Controller.hpp:49
std::array< double, STICK_COUNT > sticks
Definition Controller.hpp:48