queue.ino 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // This file constructs a FIFO-queue that can be filled with data from one function and drained from somewhere else without data curruption
  2. #define RX_QUEUE_SIZE 500 // Queue size for pulses
  3. static int read_d; /*!< Read index points to the place in the queue that has already been read */
  4. static int write_d; /*!< The write index points to the place where the next write in the queue will take place. */
  5. static uint16_t nexaQueueBuffer[RX_QUEUE_SIZE];
  6. void rcvInitQueue( ) {
  7. read_d = 0;
  8. write_d = 1;
  9. }
  10. unsigned int getQueueUsed() {
  11. return (write_d-read_d);
  12. }
  13. unsigned char rcvEnQueue( uint16_t value ) {
  14. // Is the queue full ?
  15. if( write_d == read_d )
  16. {
  17. return 0u;
  18. }
  19. // Make the data copy
  20. nexaQueueBuffer[write_d] = value;
  21. // Increment the write position
  22. write_d++;
  23. if( write_d == RX_QUEUE_SIZE ) write_d=0;
  24. return 1u;
  25. }
  26. unsigned char rcvDeQueue( uint16_t *value ) {
  27. int writePos = write_d;
  28. // Pseudo writepos
  29. if( writePos < read_d || writePos == read_d ) writePos += RX_QUEUE_SIZE;
  30. // The queue is empty ?
  31. if( writePos == ( read_d + 1 ) )
  32. {
  33. return 0u;
  34. }
  35. // Increment the read position
  36. read_d++;
  37. if( read_d == RX_QUEUE_SIZE ) read_d=0;
  38. // Make the data copy
  39. *value = nexaQueueBuffer[read_d];
  40. return 1u;
  41. }
  42. // This function rolls back the latest inserted entry
  43. unsigned char rcvRollbackQueue() {
  44. // Is the queue empty ?
  45. if( write_d == ( read_d + 1 ) )
  46. {
  47. return 0u; // No rollback
  48. }
  49. if( write_d == 0 ) {
  50. write_d = RX_QUEUE_SIZE - 1;
  51. }
  52. else {
  53. write_d--;
  54. }
  55. return 1u;
  56. }