LightLib
PROS library for VEX V5: EKF/MCL localization, RAMSETE path following, high-level chassis API
Loading...
Searching...
No Matches
coreProsAPI.hpp
Go to the documentation of this file.
1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 */
6#pragma once
7
8#include <cmath>
9#include <cstdbool>
10#include <cstddef>
11#include <cstdint>
12#include <cstdio>
13#include <cstdlib>
14#include <functional>
15#include <sstream>
16
17#ifdef THREADS_STD
18#include <thread>
19#define CROSSPLATFORM_THREAD_T std::thread
20
21#include <mutex>
22#define CROSSPLATFORM_MUTEX_T std::mutex
23#else
24#include "api.h"
25#include "pros/apix.h"
26#define CROSSPLATFORM_THREAD_T pros::task_t
27#define CROSSPLATFORM_MUTEX_T pros::Mutex
28#endif
29
30#define NOT_INITIALIZE_TASK \
31 (strcmp(pros::c::task_get_name(pros::c::task_get_current()), "User Initialization (PROS)") != 0)
32
33#define NOT_COMP_INITIALIZE_TASK \
34 (strcmp(pros::c::task_get_name(pros::c::task_get_current()), "User Comp. Init. (PROS)") != 0)
35
37 public:
38#ifdef THREADS_STD
39 CrossplatformThread(void (*ptr)(void *),
40 void *params,
41 const char *const = "OkapiLibCrossplatformTask")
42#else
43 CrossplatformThread(void (*ptr)(void *),
44 void *params,
45 const char *const name = "OkapiLibCrossplatformTask")
46#endif
47 :
48#ifdef THREADS_STD
49 thread(ptr, params)
50#else
51 thread(
52 pros::c::task_create(ptr, params, TASK_PRIORITY_DEFAULT, TASK_STACK_DEPTH_DEFAULT, name))
53#endif
54 {
55 }
56
58#ifdef THREADS_STD
59 thread.join();
60#else
61 if (pros::c::task_get_state(thread) != pros::E_TASK_STATE_DELETED) {
62 pros::c::task_delete(thread);
63 }
64#endif
65 }
66
67#ifdef THREADS_STD
69 }
70#else
72 pros::c::task_notify_when_deleting(parent->thread, thread, 1, pros::E_NOTIFY_ACTION_INCR);
73 }
74#endif
75
76#ifdef THREADS_STD
78 }
79#else
81 pros::c::task_notify_when_deleting(parent, thread, 1, pros::E_NOTIFY_ACTION_INCR);
82 }
83#endif
84
85#ifdef THREADS_STD
86 std::uint32_t notifyTake(const std::uint32_t) {
87 return 0;
88 }
89#else
90 std::uint32_t notifyTake(const std::uint32_t itimeout) {
91 return pros::c::task_notify_take(true, itimeout);
92 }
93#endif
94
95 static std::string getName() {
96#ifdef THREADS_STD
97 std::ostringstream ss;
98 ss << std::this_thread::get_id();
99 return ss.str();
100#else
101 return std::string(pros::c::task_get_name(NULL));
102#endif
103 }
104
106};
107
109 public:
111
112 void lock() {
113#ifdef THREADS_STD
114 mutex.lock();
115#else
116 while (!mutex.take(1)) {
117 }
118#endif
119 }
120
121 void unlock() {
122#ifdef THREADS_STD
123 mutex.unlock();
124#else
125 mutex.give();
126#endif
127 }
128
129 protected:
131};
PROS API header provides high-level user functionality.
CrossplatformMutex()=default
CROSSPLATFORM_MUTEX_T mutex
std::uint32_t notifyTake(const std::uint32_t itimeout)
CrossplatformThread(void(*ptr)(void *), void *params, const char *const name="OkapiLibCrossplatformTask")
void notifyWhenDeletingRaw(CROSSPLATFORM_THREAD_T parent)
CROSSPLATFORM_THREAD_T thread
void notifyWhenDeleting(CrossplatformThread *parent)
static std::string getName()
#define CROSSPLATFORM_THREAD_T
#define CROSSPLATFORM_MUTEX_T