1 /* 2 * Copyright (c) 2006-2018, RT-Thread Development Team 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Change Logs: 7 * Date Author Notes 8 * 2016/10/1 Bernard The first version 9 */ 10 11 #pragma once 12 13 #include <stdint.h> 14 #include <string.h> 15 16 #include <rtthread.h> 17 18 namespace rtthread { 19 20 /** 21 * The Queue class allow to control, send, receive, or wait for messages. 22 * A message can be a integer or pointer value to a certain type T that is send 23 * to a thread or interrupt service routine. 24 * @param T data type of a single message element. 25 * @param queue_sz maximum number of messages in queue. 26 */ 27 template<typename T, uint32_t queue_sz> 28 class Queue 29 { 30 public: 31 /** Create and initialise a message Queue. */ 32 Queue() 33 { 34 rt_mq_init(&mID, "mq", mPool, sizeof(T), sizeof(mPool), RT_IPC_FLAG_FIFO); 35 }; 36 37 ~Queue() 38 { 39 rt_mq_detach(&mID); 40 }; 41 42 /** Put a message in a Queue. 43 @param data message pointer. 44 @param millisec timeout value or 0 in case of no time-out. (default: 0) 45 @return status code that indicates the execution status of the function. 46 */ 47 rt_err_t put(T& data, int32_t millisec = 0) 48 { 49 return rt_mq_send(&mID, &data, sizeof(data)); 50 } 51 52 /** Get a message or Wait for a message from a Queue. 53 @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever). 54 @return bool . 55 */ 56 bool get(T& data, int32_t millisec = WAIT_FOREVER) 57 { 58 rt_int32_t tick; 59 60 if (millisec < 0) 61 tick = -1; 62 else 63 tick = rt_tick_from_millisecond(millisec); 64 65 return rt_mq_recv(&mID, &data, sizeof(data), tick) == RT_EOK; 66 } 67 68 private: 69 struct rt_messagequeue mID; 70 71 char mPool[(sizeof(struct rt_messagequeue)+sizeof(T)) * queue_sz]; 72 }; 73 74 } 75