RaidZeroLib
Coroutine robot framework for VEX V5
Loading...
Searching...
No Matches
Device.hpp
1#pragma once
2#include <cstdint>
3
4#include "RaidZeroLib/api/Runtime/Component.hpp"
5
6namespace rz {
7
18class Device : public Component {
19 public:
20 [[nodiscard]] std::uint8_t port() const noexcept { return port_; }
21
23 [[nodiscard]] bool deviceConnected() const noexcept { return connected_; }
24
26 [[nodiscard]] std::uint32_t deviceEpoch() const noexcept { return epoch_; }
27
29 static void validatePorts();
30
31 protected:
33 Device(std::uint8_t port, std::string_view kind);
34 ~Device() override;
35
37 virtual bool poll() = 0;
38
40 virtual void onConnect() {}
41
42 private:
43 Task<> loop() final;
44
46 void refresh();
47
48 std::uint8_t port_;
49 bool connected_ = false;
50 bool reportedMissing = false;
51 std::uint32_t epoch_ = 0;
52
53 Device* nextDevice = nullptr; // intrusive list of all devices, for port validation
54};
55
56} // namespace rz
Component(const Component &)=delete
virtual Task loop()=0
Runs for the whole program; never returns.
Base for anything plugged into a V5 smart port. The only layer that talks to hardware.
Definition Device.hpp:18
bool deviceConnected() const noexcept
Whether this tick's read succeeded.
Definition Device.hpp:23
virtual void onConnect()
Applies configuration. Called when the device first connects and after every reconnect.
Definition Device.hpp:40
static void validatePorts()
Throws std::runtime_error if two devices are constructed on the same port.
std::uint8_t port() const noexcept
Definition Device.hpp:20
std::uint32_t deviceEpoch() const noexcept
Increments on every (re)connection.
Definition Device.hpp:26
~Device() override
Device(std::uint8_t port, std::string_view kind)
virtual bool poll()=0
Reads the hardware into cached members. Returns false if the read failed (keep old values).
corral::Task< T > Task
Lazy: nothing runs until it is co_awaited.
Definition Task.hpp:17
Definition DriverControl.hpp:11