Mechatronic Zen Garden  0.1
This webpage contains documentation for the ME 507 term project: Mechatronic Zen Garden.
taskshare.h
Go to the documentation of this file.
1 
28 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
29  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
30  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIB-
32  * UTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
33  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
38  * THE POSSIBILITY OF SUCH DAMAGE. */
39 
40 // This define prevents this .h file from being included more than once
41 #ifndef _TASKSHARE_H_
42 #define _TASKSHARE_H_
43 
44 #include "baseshare.h" // Base class for shared data items
45 #include "FreeRTOS.h" // Main header for FreeRTOS
46 
119 template <class DataType> class Share : public BaseShare
120 {
121 protected:
123  QueueHandle_t queue;
124 
125 public:
133  Share<DataType> (const char* p_name = NULL) : BaseShare (p_name)
134  {
135  queue = xQueueCreate (1, sizeof (DataType));
136  }
137 
142  void put (DataType new_data)
143  {
144  xQueueOverwrite (queue, &new_data);
145  }
146 
153  void ISR_put (DataType new_data)
154  {
155  BaseType_t wake_up;
156  xQueueOverwriteFromISR (queue, &new_data, &wake_up);
157  }
158 
168  void operator << (DataType new_data)
169  {
170  if (CHECK_IF_IN_ISR ())
171  {
172  BaseType_t wake_up;
173  xQueueOverwriteFromISR (queue, &new_data, &wake_up);
174  }
175  else
176  {
177  xQueueOverwrite (queue, &new_data);
178  }
179  }
180 
195  void operator >> (DataType put_here)
196  {
197  if (CHECK_IF_IN_ISR ())
198  {
199  // Copy the data from the queue into the receiving variable
200  xQueuePeekFromISR (queue, &put_here);
201  }
202  else
203  {
204  xQueuePeek (queue, &put_here, portMAX_DELAY);
205  }
206  }
207 
217  void get (DataType& recv_data)
218  {
219  // Copy the data from the queue into the receiving variable
220  xQueuePeek (queue, &recv_data, portMAX_DELAY);
221  }
222 
230  DataType get (void)
231  {
232  DataType return_this;
233 
234  // Copy the data from the queue into the receiving variable
235  xQueuePeek (queue, &return_this, portMAX_DELAY);
236 
237  return return_this;
238  }
239 
247  void ISR_get (DataType& recv_data)
248  {
249  xQueuePeekFromISR (queue, &recv_data);
250  }
251 
260  DataType ISR_get (void)
261  {
262  DataType return_this;
263  xQueuePeekFromISR (queue, &return_this);
264  return return_this;
265  }
266 
267  // Print the share's status within a list of all shares' statuses
268  void print_in_list (Print& printer);
269 
270 }; // class TaskShare<DataType>
271 
272 
281 template <class DataType>
282 void Share<DataType>::print_in_list (Print& printer)
283 {
284  // Print this task's name and pad it to 16 characters
285  printer.printf ("%-16sshare\t", name);
286 
287  // End the line
288  printer << endl;
289 
290  // Call the next item
291  if (p_next != NULL)
292  {
293  p_next->print_in_list (printer);
294  }
295 }
296 
297 #endif // _TASKSHARE_H_
Share::print_in_list
void print_in_list(Print &printer)
Print the name and type (share) of this data item.
Definition: taskshare.h:282
Share::ISR_put
void ISR_put(DataType new_data)
Put data into the shared data item from within an ISR.
Definition: taskshare.h:153
Share::operator<<
void operator<<(DataType new_data)
Operator which inserts data into the share.
Definition: taskshare.h:168
Share::operator>>
void operator>>(DataType put_here)
Read data from the shared data item.
Definition: taskshare.h:195
Share::get
void get(DataType &recv_data)
Read data from the shared data item into a variable.
Definition: taskshare.h:217
Share
Class for data to be shared in a thread-safe manner between tasks.
Definition: taskshare.h:120
Share::queue
QueueHandle_t queue
A queue is used to hold the data, as it's portable to different CPU's.
Definition: taskshare.h:123
baseshare.h
Headers for a base class for type-safe, thread-safe task data exchange classes.
Share::ISR_get
void ISR_get(DataType &recv_data)
Read data from the shared data item, from within an ISR.
Definition: taskshare.h:247
Share::get
DataType get(void)
Read and return data from the shared data item.
Definition: taskshare.h:230
Share::put
void put(DataType new_data)
Put data into the shared data item.
Definition: taskshare.h:142
BaseShare
Base class for classes that share data in a thread-safe manner between tasks.
Definition: baseshare.h:55
Share::ISR_get
DataType ISR_get(void)
Read and return data from the shared data item, from within an ISR.
Definition: taskshare.h:260