tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
queue.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3
4#ifndef TETL_FREERTOS_QUEUE_HPP
5#define TETL_FREERTOS_QUEUE_HPP
6
7#include <etl/version.hpp>
8
9#include <etl/cstdint.hpp>
10#include <etl/utility.hpp>
11
12#if defined(TETL_FREERTOS_USE_STUBS)
13 #include <etl/experimental/freertos/stubs.hpp>
14#endif
15
16namespace etl::experimental::freertos {
17
18/// \brief Wrapper around a FreeRTOS queue.
19///
20/// https://www.freertos.org/Embedded-RTOS-Queues.html
21///
22/// \tparam T The type that's being stored inside the queue.
23/// \tparam Size The maximum capacity of the queue.
24template <typename T, etl::uint32_t Size>
25struct queue {
26 using value_type = T;
27 using size_type = etl::uint32_t;
28
29 /// \brief Creates a new queue. RAM is automatically allocated from the
30 /// FreeRTOS heap.
31 ///
32 /// https://www.freertos.org/a00116.html
33 queue();
34
35 /// \brief Delete a queue - freeing all the memory allocated for storing of
36 /// items placed on the queue.
37 ///
38 /// https://www.freertos.org/a00018.html#vQueueDelete
39 ~queue();
40
41 queue(queue const&) = delete;
42 auto operator=(queue const&) -> queue& = delete;
43
44 queue(queue&&) = delete;
45 auto operator=(queue&&) -> queue& = delete;
46
47 /// Returns the capacity of the internal buffer
48 [[nodiscard]] auto capacity() const -> size_type;
49
50 /// Push an element on to the queue.
51 [[nodiscard]] auto send(T const& data, TickType_t ticksToWait = 0) const -> bool;
52
53 /// Pop an element of the queue.
54 auto receive(T& data, TickType_t ticksToWait = 0) const -> bool;
55
56 /// Pop an element of the queue.
57 [[nodiscard]] auto receive(TickType_t ticksToWait = 0) const -> pair<bool, T>;
58
59 [[nodiscard]] auto reset() const -> bool;
60 [[nodiscard]] auto messages_waiting() const -> etl::uint32_t;
61
62private:
63 QueueHandle_t _handle = nullptr;
64};
65
66template <typename T, etl::uint32_t Size>
67inline queue<T, Size>::queue()
68 : _handle{[]() { return xQueueCreate(Size, sizeof(T)); }()}
69{
70}
71
72template <typename T, etl::uint32_t Size>
73inline queue<T, Size>::~queue()
74{
75 if (_handle != nullptr) {
76 vQueueDelete(_handle);
77 }
78}
79
80template <typename T, etl::uint32_t Size>
81inline auto queue<T, Size>::capacity() const -> size_type
82{
83 return Size;
84}
85
86template <typename T, etl::uint32_t Size>
87inline auto queue<T, Size>::send(T const& data, TickType_t ticksToWait) const -> bool
88{
89 auto const* const rawData = static_cast<void const*>(&data);
90 auto const success = xQueueSend(_handle, rawData, ticksToWait);
91 return static_cast<bool>(success);
92}
93
94template <typename T, etl::uint32_t Size>
95inline auto queue<T, Size>::receive(T& data, TickType_t ticksToWait) const -> bool
96{
97 auto* const rawData = static_cast<void*>(&data);
98 auto const success = xQueueReceive(_handle, rawData, ticksToWait);
99 return static_cast<bool>(success);
100}
101
102template <typename T, etl::uint32_t Size>
103inline auto queue<T, Size>::receive(TickType_t ticksToWait) const -> pair<bool, T>
104{
105 auto value = T{};
106 auto* const rawData = static_cast<void*>(&value);
107 auto const success = xQueueReceive(_handle, rawData, ticksToWait);
108 return {static_cast<bool>(success), value};
109}
110
111template <typename T, etl::uint32_t Size>
112inline auto queue<T, Size>::reset() const -> bool
113{
114 auto const result = xQueueReset(_handle);
115 return static_cast<bool>(result);
116}
117
118template <typename T, etl::uint32_t Size>
119inline auto queue<T, Size>::messages_waiting() const -> etl::uint32_t
120{
121 auto const result = uxQueueMessagesWaiting(_handle);
122 return static_cast<etl::uint32_t>(result);
123}
124} // namespace etl::experimental::freertos
125
126#endif // TETL_FREERTOS_QUEUE_HPP
Definition queue.hpp:16
Definition adjacent_find.hpp:9
Wrapper around a FreeRTOS queue.
Definition queue.hpp:25
auto send(T const &data, TickType_t ticksToWait=0) const -> bool
Push an element on to the queue.
Definition queue.hpp:87
~queue()
Delete a queue - freeing all the memory allocated for storing of items placed on the queue.
Definition queue.hpp:73
auto receive(T &data, TickType_t ticksToWait=0) const -> bool
Pop an element of the queue.
Definition queue.hpp:95
auto receive(TickType_t ticksToWait=0) const -> pair< bool, T >
Pop an element of the queue.
Definition queue.hpp:103
auto operator=(queue const &) -> queue &=delete
auto messages_waiting() const -> etl::uint32_t
Definition queue.hpp:119
auto capacity() const -> size_type
Returns the capacity of the internal buffer.
Definition queue.hpp:81
auto operator=(queue &&) -> queue &=delete
auto reset() const -> bool
Definition queue.hpp:112
queue()
Creates a new queue. RAM is automatically allocated from the FreeRTOS heap.
Definition queue.hpp:67
etl::pair is a class template that provides a way to store two heterogeneous objects as a single unit...
Definition pair.hpp:37
auto vQueueDelete(QueueHandle_t xQueue) -> void
Definition stubs.hpp:78
auto xQueueReset(QueueHandle_t xQueue) -> BaseType_t
Definition stubs.hpp:95
auto xQueueCreate(UBaseType_t uxQueueLength, UBaseType_t uxItemSize) -> QueueHandle_t
Definition stubs.hpp:72
auto xQueueReceive(QueueHandle_t xQueue, void *pvBuffer, TickType_t xTicksToWait) -> BaseType_t
Definition stubs.hpp:89
auto uxQueueMessagesWaiting(QueueHandle_t xQueue) -> UBaseType_t
Definition stubs.hpp:101
auto xQueueSend(QueueHandle_t xQueue, void const *pvItemToQueue, TickType_t xTicksToWait) -> BaseType_t
Definition stubs.hpp:83