fifo.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2015 Daniel Campora
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdint.h>
  27. #include <stdbool.h>
  28. #include "fifo.h"
  29. /******************************************************************************
  30. DEFINE PUBLIC FUNCTIONS
  31. ******************************************************************************/
  32. void FIFO_Init (FIFO_t *fifo, unsigned int uiElementsMax,
  33. void (*pfElmentPush)(void * const pvFifo, const void * const pvElement),
  34. void (*pfElementPop)(void * const pvFifo, void * const pvElement)) {
  35. if (fifo) {
  36. fifo->uiFirst = 0;
  37. fifo->uiLast = uiElementsMax - 1;
  38. fifo->uiElementCount = 0;
  39. fifo->uiElementsMax = uiElementsMax;
  40. fifo->pfElementPush = pfElmentPush;
  41. fifo->pfElementPop = pfElementPop;
  42. }
  43. }
  44. bool FIFO_bPushElement (FIFO_t *fifo, const void * const pvElement) {
  45. if (!fifo) {
  46. return false;
  47. }
  48. // Check if the queue is full
  49. if (true == FIFO_IsFull (fifo)) {
  50. return false;
  51. }
  52. // Increment the element count
  53. if (fifo->uiElementsMax > fifo->uiElementCount) {
  54. fifo->uiElementCount++;
  55. }
  56. fifo->uiLast++;
  57. if (fifo->uiLast == fifo->uiElementsMax) {
  58. fifo->uiLast = 0;
  59. }
  60. // Insert the element into the queue
  61. fifo->pfElementPush(fifo, pvElement);
  62. return true;
  63. }
  64. bool FIFO_bPopElement (FIFO_t *fifo, void * const pvElement) {
  65. if (!fifo) {
  66. return false;
  67. }
  68. // Check if the queue is empty
  69. if (true == FIFO_IsEmpty (fifo)) {
  70. return false;
  71. }
  72. // Get the element from the queue
  73. fifo->pfElementPop(fifo, pvElement);
  74. // Decrement the element count
  75. if (fifo->uiElementCount > 0) {
  76. fifo->uiElementCount--;
  77. }
  78. fifo->uiFirst++;
  79. if (fifo->uiFirst == fifo->uiElementsMax) {
  80. fifo->uiFirst = 0;
  81. }
  82. return true;
  83. }
  84. bool FIFO_bPeekElement (FIFO_t *fifo, void * const pvElement) {
  85. if (!fifo) {
  86. return false;
  87. }
  88. // Check if the queue is empty
  89. if (true == FIFO_IsEmpty (fifo)) {
  90. return false;
  91. }
  92. // Get the element from the queue
  93. fifo->pfElementPop(fifo, pvElement);
  94. return true;
  95. }
  96. bool FIFO_IsEmpty (FIFO_t *fifo) {
  97. if (fifo) {
  98. return ((fifo->uiElementCount == 0) ? true : false);
  99. }
  100. return false;
  101. }
  102. bool FIFO_IsFull (FIFO_t *fifo) {
  103. if (fifo) {
  104. return ((fifo->uiElementCount < fifo->uiElementsMax) ? false : true);
  105. }
  106. return false;
  107. }
  108. void FIFO_Flush (FIFO_t *fifo) {
  109. if (fifo) {
  110. fifo->uiElementCount = 0;
  111. fifo->uiFirst = 0;
  112. fifo->uiLast = fifo->uiElementsMax - 1;
  113. }
  114. }