cqueue.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /****************************************************************************
  2. *
  3. * Copyright (c) 2017, Michael Becker (michael.f.becker@gmail.com)
  4. *
  5. * This file is part of the FreeRTOS Add-ons project.
  6. *
  7. * Source Code:
  8. * https://github.com/michaelbecker/freertos-addons
  9. *
  10. * Project Page:
  11. * http://michaelbecker.github.io/freertos-addons/
  12. *
  13. * On-line Documentation:
  14. * http://michaelbecker.github.io/freertos-addons/docs/html/index.html
  15. *
  16. * Permission is hereby granted, free of charge, to any person obtaining a
  17. * copy of this software and associated documentation files
  18. * (the "Software"), to deal in the Software without restriction, including
  19. * without limitation the rights to use, copy, modify, merge, publish,
  20. * distribute, sublicense, and/or sell copies of the Software, and to
  21. * permit persons to whom the Software is furnished to do so,subject to the
  22. * following conditions:
  23. *
  24. * + The above copyright notice and this permission notice shall be included
  25. * in all copies or substantial portions of the Software.
  26. * + Credit is appreciated, but not required, if you find this project
  27. * useful enough to include in your application, product, device, etc.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  30. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  31. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  32. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  33. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  34. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  35. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  36. *
  37. ***************************************************************************/
  38. #include "queue.hpp"
  39. using namespace cpp_freertos;
  40. Queue::Queue(UBaseType_t maxItems, UBaseType_t itemSize)
  41. {
  42. handle = xQueueCreate(maxItems, itemSize);
  43. if (handle == NULL) {
  44. #ifndef CPP_FREERTOS_NO_EXCEPTIONS
  45. throw QueueCreateException();
  46. #else
  47. configASSERT(!"Queue Constructor Failed");
  48. #endif
  49. }
  50. }
  51. Queue::~Queue()
  52. {
  53. vQueueDelete(handle);
  54. }
  55. bool Queue::Enqueue(void *item)
  56. {
  57. BaseType_t success;
  58. success = xQueueSendToBack(handle, item, portMAX_DELAY);
  59. return success == pdTRUE ? true : false;
  60. }
  61. bool Queue::Enqueue(void *item, TickType_t Timeout)
  62. {
  63. BaseType_t success;
  64. success = xQueueSendToBack(handle, item, Timeout);
  65. return success == pdTRUE ? true : false;
  66. }
  67. bool Queue::Dequeue(void *item, TickType_t Timeout)
  68. {
  69. BaseType_t success;
  70. success = xQueueReceive(handle, item, Timeout);
  71. return success == pdTRUE ? true : false;
  72. }
  73. bool Queue::Peek(void *item, TickType_t Timeout)
  74. {
  75. BaseType_t success;
  76. success = xQueuePeek(handle, item, Timeout);
  77. return success == pdTRUE ? true : false;
  78. }
  79. bool Queue::EnqueueFromISR(void *item, BaseType_t *pxHigherPriorityTaskWoken)
  80. {
  81. BaseType_t success;
  82. success = xQueueSendToBackFromISR(handle, item, pxHigherPriorityTaskWoken);
  83. return success == pdTRUE ? true : false;
  84. }
  85. bool Queue::DequeueFromISR(void *item, BaseType_t *pxHigherPriorityTaskWoken)
  86. {
  87. BaseType_t success;
  88. success = xQueueReceiveFromISR(handle, item, pxHigherPriorityTaskWoken);
  89. return success == pdTRUE ? true : false;
  90. }
  91. bool Queue::PeekFromISR(void *item)
  92. {
  93. BaseType_t success;
  94. success = xQueuePeekFromISR(handle, item);
  95. return success == pdTRUE ? true : false;
  96. }
  97. bool Queue::IsEmpty()
  98. {
  99. UBaseType_t cnt = uxQueueMessagesWaiting(handle);
  100. return cnt == 0 ? true : false;
  101. }
  102. bool Queue::IsFull()
  103. {
  104. UBaseType_t cnt = uxQueueSpacesAvailable(handle);
  105. return cnt == 0 ? true : false;
  106. }
  107. void Queue::Flush()
  108. {
  109. xQueueReset(handle);
  110. }
  111. UBaseType_t Queue::NumItems()
  112. {
  113. return uxQueueMessagesWaiting(handle);
  114. }
  115. UBaseType_t Queue::NumSpacesLeft()
  116. {
  117. return uxQueueSpacesAvailable(handle);
  118. }
  119. Deque::Deque(UBaseType_t maxItems, UBaseType_t itemSize)
  120. : Queue(maxItems, itemSize)
  121. {
  122. }
  123. bool Deque::EnqueueToFront(void *item, TickType_t Timeout)
  124. {
  125. BaseType_t success;
  126. success = xQueueSendToFront(handle, item, Timeout);
  127. return success == pdTRUE ? true : false;
  128. }
  129. bool Deque::EnqueueToFrontFromISR(void *item, BaseType_t *pxHigherPriorityTaskWoken)
  130. {
  131. BaseType_t success;
  132. success = xQueueSendToFrontFromISR(handle, item, pxHigherPriorityTaskWoken);
  133. return success == pdTRUE ? true : false;
  134. }
  135. BinaryQueue::BinaryQueue(UBaseType_t itemSize)
  136. : Queue(1, itemSize)
  137. {
  138. }
  139. bool BinaryQueue::Enqueue(void *item)
  140. {
  141. (void)xQueueOverwrite(handle, item);
  142. return true;
  143. }
  144. bool BinaryQueue::EnqueueFromISR(void *item, BaseType_t *pxHigherPriorityTaskWoken)
  145. {
  146. (void)xQueueOverwriteFromISR(handle, item, pxHigherPriorityTaskWoken);
  147. return true;
  148. }