Sylib
A C++ Library For V5
Loading...
Searching...
No Matches
system.hpp
1
12#pragma once
13
14#include "sylib/env.hpp"
15
16namespace sylib {
17#ifdef SYLIB_ENV_PROS
18typedef pros::Mutex PlatformMutex;
19typedef pros::Task PlatformTask;
20#elif defined(SYLIB_ENV_VEXCODE)
21#define V5_MAX_DEVICE_PORTS 32
22typedef vex::mutex PlatformMutex;
23typedef vex::task PlatformTask;
24#endif
25
30
47void delay_until(std::uint32_t* const prev_time, const std::uint32_t delta);
48
58void delay(std::uint32_t delay);
59
68uint32_t millis();
69
77uint64_t micros();
78
85class Mutex : public PlatformMutex {
86 private:
87 bool try_lock_for();
88 bool try_lock_until();
89
90 public:
94 bool take();
95
99 bool give();
100};
101
108class Task : public PlatformTask {
109 public:
110 template <class F>
111 Task(F&& func) : PlatformTask(func) {}
112};
113
114extern Mutex sylib_port_mutexes[V5_MAX_DEVICE_PORTS];
115extern Mutex sylib_controller_mutexes[2];
116using mutex_lock = const std::lock_guard<sylib::Mutex>;
117
118class Device;
119
128 private:
129 SylibDaemon();
130 ~SylibDaemon();
131 SylibDaemon(const SylibDaemon&) = delete;
132 const SylibDaemon& operator=(const SylibDaemon&) = delete;
133 std::unique_ptr<sylib::Task> managerTask = nullptr;
134 static int subTasksCreated;
135 static std::vector<sylib::Device*>& getLivingSubtasks();
136 static int frameCount;
137
138 public:
144
152
158
169
178 [[noreturn]] static int managerTaskFunction();
179
189 static int createSubTask(sylib::Device* objectPointerToSchedule);
190
203 static int createSubTaskUnsafe(sylib::Device* objectPointerToSchedule);
204
212 static void removeSubTask(sylib::Device* objectPointerToSchedule);
213
221 static void removeSubTaskByID(int idToKill);
222
230 static uint64_t getFrameCount();
231};
232
238class Device {
239 private:
240 bool subTaskPaused = false;
241 int updateFrequency;
242 int updateOffset;
243 int subTaskID;
244 void startSubTask();
245 bool idSet = false;
246
247 public:
253
266 Device(int interval = 2, int offset = 0);
267
269
274
279
284
291
298
305
312 void setUpdateFrequency(int interval);
313
321 void setUpdateOffset(int offset);
322
329
334 virtual void update() = 0;
335
342};
343} // namespace sylib
Device Parent Class.
Definition: system.hpp:238
int getSubTaskID()
Gets the subtask ID of the device's update function.
int getUpdateFrequency()
Gets the number of frames delayed between updates.
void setUpdateFrequency(int interval)
Sets the number of frames to wait between updates.
void setUpdateOffset(int offset)
Sets the number of frames to shift the excecution of the update function.
sylib::Mutex mutex
Mutex to prevent multiple threads from performing operations on important system things at once.
Definition: system.hpp:252
bool getSubTaskPaused()
Gets whether or not the device update function is paused.
bool isSubTaskRunning()
Gets whether or not the device update function is running.
void resumeSubTask()
Resumes the device update function.
Device(int interval=2, int offset=0)
Creates a Device object.
void pauseSubTask()
Pauses the device update function.
virtual void update()=0
Default device update function. This is overridden by classes that inherit from Device for their own ...
void killSubTask()
Permanently ends the device update function.
int getUpdateOffset()
Gets the number of frames the update function is offset by.
Mutex.
Definition: system.hpp:85
bool give()
Unlocks the mutex.
bool take()
Locks the mutex.
Sylib System Daemon.
Definition: system.hpp:127
static sylib::Mutex mutex
Mutex to prevent multiple threads from performing operations on important system things at once.
Definition: system.hpp:143
static int managerTaskFunction()
The sole function that runs inside a task made specifically for the Sylib daemon. Contains an infinit...
static int createSubTask(sylib::Device *objectPointerToSchedule)
Adds a Device object or one of its child classes to the daemon's list of objects which need periodic ...
static SylibDaemon & getInstanceUnsafe()
Gets the single existing SylibDaemon object, or creates it if it doesn't exist yet.
void startSylibDaemon()
Starts the Sylib daemon. This should be called in initialize() or pre_auton()
static int createSubTaskUnsafe(sylib::Device *objectPointerToSchedule)
Adds a Device object or one of its child classes to the daemon's list of objects which need periodic ...
static SylibDaemon & getInstance()
Gets the single existing SylibDaemon object, or creates it if it doesn't exist yet.
static void removeSubTask(sylib::Device *objectPointerToSchedule)
Removes a Device object or one of its child classes from the daemon's list of objects which need peri...
static uint64_t getFrameCount()
Gets the number of ticks of the Sylib daemon update loop.
static void removeSubTaskByID(int idToKill)
Removes a subtask from the daemon's list of objects which need periodic updating.
Task.
Definition: system.hpp:108
Task(F &&func)
Definition: system.hpp:111
Definition: addrled.hpp:20
uint32_t millis()
The current system time.
uint64_t micros()
The current system time in microseconds.
void delay(std::uint32_t delay)
Delays the current task for a set number of milliseconds.
const std::lock_guard< sylib::Mutex > mutex_lock
Definition: system.hpp:116
Mutex sylib_controller_mutexes[2]
void initialize()
Starts Sylib background processes. Called by the user in initialize()
void delay_until(std::uint32_t *const prev_time, const std::uint32_t delta)
Delays the current task until a set time.
Mutex sylib_port_mutexes[V5_MAX_DEVICE_PORTS]