-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.hpp
62 lines (52 loc) · 1.1 KB
/
queue.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#pragma once
#include <mutex.hpp>
#include "bsp_time.h"
#include "om.hpp"
namespace System {
template <typename Data>
class Queue {
public:
Queue(uint16_t length) {
om_fifo_create(&fifo_, malloc(length * sizeof(Data)), length, sizeof(Data));
}
bool Send(const Data& data) {
mutex_.Lock();
if (om_fifo_write(&fifo_, &data) == OM_OK) {
mutex_.Unlock();
return true;
}
mutex_.Unlock();
return false;
}
bool Receive(Data& data) {
mutex_.Lock();
if (om_fifo_read(&fifo_, &data) == OM_OK) {
mutex_.Unlock();
return true;
} else {
mutex_.Unlock();
return false;
}
}
bool Overwrite(const Data& data) {
mutex_.Lock();
bool ans = om_fifo_overwrite(&fifo_, &data) == OM_OK;
mutex_.Unlock();
return ans;
}
bool Reset() {
mutex_.Lock();
bool ans = om_fifo_reset(&fifo_) == OM_OK;
mutex_.Unlock();
return ans;
}
uint32_t Size() {
mutex_.Lock();
return om_fifo_readable_item_count(&fifo_);
mutex_.Unlock();
}
private:
om_fifo_t fifo_;
System::Mutex mutex_;
};
} // namespace System