Queue.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* Copyright (c) 2012 mbed.org */
  2. #ifndef QUEUE_H
  3. #define QUEUE_H
  4. #include <stdint.h>
  5. #include <string.h>
  6. #include "cmsis_os.h"
  7. #include "error.h"
  8. namespace rtos {
  9. /*! The Queue class allow to control, send, receive, or wait for messages.
  10. A message can be a integer or pointer value to a certain type T that is send
  11. to a thread or interrupt service routine.
  12. \tparam T data type of a single message element.
  13. \tparam queue_sz maximum number of messages in queue.
  14. */
  15. template<typename T, uint32_t queue_sz>
  16. class Queue {
  17. public:
  18. /*! Create and initialise a message Queue. */
  19. Queue() {
  20. #ifdef CMSIS_OS_RTX
  21. memset(_queue_q, 0, sizeof(_queue_q));
  22. _queue_def.pool = _queue_q;
  23. _queue_def.queue_sz = queue_sz;
  24. #endif
  25. _queue_id = osMessageCreate(&_queue_def, NULL);
  26. if (_queue_id == NULL) {
  27. error("Error initialising the queue object\n");
  28. }
  29. }
  30. /*! Put a message in a Queue.
  31. \param data message pointer.
  32. \param millisec timeout value or 0 in case of no time-out. (default: 0)
  33. \return status code that indicates the execution status of the function.
  34. */
  35. osStatus put(T* data, uint32_t millisec=0) {
  36. return osMessagePut(_queue_id, (uint32_t)data, millisec);
  37. }
  38. /*! Get a message or Wait for a message from a Queue.
  39. \param millisec timeout value or 0 in case of no time-out. (default: osWaitForever).
  40. \return event information that includes the message and the status code.
  41. */
  42. osEvent get(uint32_t millisec=osWaitForever) {
  43. return osMessageGet(_queue_id, millisec);
  44. }
  45. private:
  46. osMessageQId _queue_id;
  47. osMessageQDef_t _queue_def;
  48. #ifdef CMSIS_OS_RTX
  49. uint32_t _queue_q[4+(queue_sz)];
  50. #endif
  51. };
  52. }
  53. #endif