ME507 Utility Library  0.2.1
Mechatronics Course Support Software for ARM/Arduino/FreeRTOS
textqueue.h
Go to the documentation of this file.
1 /** @file textqueue.h
2  * This file contains a class which implements a FreeRTOS queue specifically
3  * for text, allowing a stream insertion operator to be used to put all
4  * sorts of things (strings, numbers, @a etc.) into the queue. The stream
5  * extraction operator has been tested on STM32's and ESP32's only; the
6  * extraction operator @c >> needs somewhat processor specific functions to
7  * determine if it's running in an ISR or not.
8  *
9  * @date 2021-Sep-17 JRR Original file
10  *
11  * License:
12  * This file is copyright 2021 by JR Ridgely and released under the
13  * Lesser GNU Public License, version 2.1. It intended for educational use
14  * only, but its use is not limited thereto. */
15 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIB-
19  * UTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
20  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25  * THE POSSIBILITY OF SUCH DAMAGE. */
26 
27 // This define prevents this .h file from being included more than once
28 #ifndef _TEXTQUEUE_H_
29 #define _TEXTQUEUE_H_
30 
31 #include <Arduino.h>
32 #include "FreeRTOS.h" // Main header for FreeRTOS
33 #include "taskqueue.h"
34 
35 
36 /** @brief Implements a queue to transmit text from one RTOS task to another.
37  * @details Since multithreaded tasks must not use unprotected shared data
38  * items for communication, queues are a primary means of intertask
39  * communication. Other means include shared data items (see
40  * @c taskshare.h) and carrier pigeons. The use of a C++ class
41  * template allows the compiler to check that you're putting the
42  * correct type of data into each queue and getting the correct type
43  * of data out, thus helping to prevent programming mistakes that can
44  * corrupt your data.
45  *
46  * The size of FreeRTOS queues is limited to 255 items in 8-bit
47  * microcontrollers whose @c portBASE_TYPE is an 8-bit number. This
48  * is a FreeRTOS feature.
49  *
50  * @section textqueue_usage Usage
51  * The following bits of code show how to set up and use a queue to
52  * transfer text from one hypothetical task called @c task_A to
53  * another called @c task_B.
54  *
55  * Near the top of the file which contains @c setup() we create a
56  * queue. The constructor of the @c TextQueue class is given the
57  * maximum number of characters which can be stored in the queue (100
58  * in this example) and an optional name for the queue:
59  * @code
60  * #include <PrintStream.h>
61  * #include "textqueue.h"
62  * ...
63  * /// This queue holds angry complaints
64  * TextQueue whiny_queue (100, "Complaints");
65  * @endcode
66  * In a location which is before we use the queue in any other file
67  * than the one in which the queue was created, we re-declare the
68  * queue with the keyword @c extern to make it accessible to any task
69  * within that file:
70  * @code
71  * extern TextQueue whiny_queue;
72  * @endcode
73  * In the sending task, @c task_A, text is put into the queue:
74  * @code
75  * int16_t n_fish = -3; ///< I guess we owe someone
76  * ...
77  * /// Write into the queue the easy way, with stream insertions
78  * whiny_queue << "I only have " << fish << " fish!" << endl;
79  * @endcode
80  * In the receiving task, data is read from the queue one character at
81  * a time using the @c >> operator. In typical usage, the call to
82  * @c >> will block the receiving task until data has been put into
83  * the queue by the sending task:
84  * @code
85  * char recv_ch; ///< Holds received data
86  * ...
87  * for (;;)
88  * {
89  * whiny_queue.get (recv_ch); // Get data from the queue
90  * Serial.print (recv_ch); // Or use << to print
91  * }
92  * @endcode
93  */
94 class TextQueue : public Print, public Queue<char>
95 {
96 // No protected data is needed; the parent classes take care of everything
97 
98 // Public methods can be called from anywhere in the program where there is
99 // a pointer or reference to an object of this class
100 public:
101  /** @brief Construct a queue object, allocating memory for the buffer.
102  * @details This constructor creates the FreeRTOS queue which is wrapped
103  * by the @c Queue class.
104  * @param queue_size The number of chars which can be stored in the queue
105  * @param p_name A name to be shown in the list of task shares (default
106  * empty String)
107  * @param wait_time How long, in RTOS ticks, to wait for a queue to
108  * empty before a character can be sent.
109  * (Default: @c portMAX_DELAY, which causes the sending task to
110  * block until sending occurs.)
111  */
112  TextQueue (BaseType_t queue_size, const char* p_name = NULL,
113  TickType_t wait_time = portMAX_DELAY)
114  : Print (), Queue<char> (queue_size, p_name, wait_time)
115  {
116  }
117 
118  /** @brief Write a character into the queue in the @c Print style.
119  * @details This method is pure virtual in class @c Print, so it must be
120  * specified here. It puts one character into the queue.
121  * @param a_char The character to be queued
122  * @returns The number of characters written, which is one when returning
123  */
124  virtual size_t write (uint8_t a_char)
125  {
126  put (a_char);
127  return 1;
128  }
129 
130 }; // class TextQueue
131 
132 #endif // _TEXTQUEUE_H_
TextQueue
Implements a queue to transmit text from one RTOS task to another.
Definition: textqueue.h:94
TextQueue::write
virtual size_t write(uint8_t a_char)
Write a character into the queue in the Print style.
Definition: textqueue.h:124
taskqueue.h
TextQueue::TextQueue
TextQueue(BaseType_t queue_size, const char *p_name=NULL, TickType_t wait_time=portMAX_DELAY)
Construct a queue object, allocating memory for the buffer.
Definition: textqueue.h:112
Queue
Implements a queue to transmit data from one RTOS task to another.
Definition: taskqueue.h:128
Queue< char >::put
bool put(const char item)
Put an item into the queue behind other items.
Definition: taskqueue.h:504