You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.4 KiB

  1. #pragma once
  2. #include "basic/zlog.h"
  3. #include "cmsis_os.h"
  4. namespace iflytop {
  5. template <typename T>
  6. class ZQueue {
  7. private:
  8. int32_t m_num = 0;
  9. int32_t m_eachsize = 0;
  10. QueueHandle_t xQueue;
  11. public:
  12. void initialize(int32_t num, int32_t eachsize) {
  13. xQueue = xQueueCreate(num, eachsize);
  14. ZASSERT(xQueue);
  15. }
  16. void clear() { xQueueReset(xQueue); }
  17. bool send(T *data, int32_t overtime) { return _send((uint8_t *)data, sizeof(T), overtime); }
  18. bool receive(T *data, int32_t overtime) { return _receive((uint8_t *)data, overtime); }
  19. bool isFull() {
  20. BaseType_t xStatus = xQueueIsQueueFullFromISR(xQueue);
  21. return xStatus == pdTRUE;
  22. }
  23. bool isEmpty() {
  24. BaseType_t xStatus = xQueueIsQueueEmptyFromISR(xQueue);
  25. return xStatus == pdTRUE;
  26. }
  27. private:
  28. bool _send(uint8_t *data, int32_t len, int32_t overtime) {
  29. if (xPortIsInsideInterrupt()) {
  30. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  31. BaseType_t xStatus = xQueueSendToFrontFromISR(xQueue, data, &xHigherPriorityTaskWoken);
  32. return xStatus == pdPASS;
  33. } else {
  34. BaseType_t xStatus = xQueueSend(xQueue, data, overtime);
  35. return xStatus == pdPASS;
  36. }
  37. }
  38. bool _receive(uint8_t *data, int32_t overtime) {
  39. BaseType_t xStatus = xQueueReceive(xQueue, data, overtime);
  40. return xStatus == pdPASS;
  41. }
  42. };
  43. } // namespace iflytop