queue.ino 1.4 KB

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