queue.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "queue.h"
  2. //==========================================================================================================
  3. // QUEUE THAT SENDS PULSES FROM INT TO MAIN-LOOP
  4. //==========================================================================================================
  5. static int read_d; /*!< Read index points to the place in the queue that has already been read */
  6. static int write_d; /*!< The write index points to the place where the next write in the queue will take place. */
  7. static unsigned int nexaQueueBuffer[NEXA_QUEUE_SIZE];
  8. void rcvInitQueue( ) {
  9. read_d = 0;
  10. write_d = 1;
  11. }
  12. unsigned int getQueueUsed() {
  13. return (write_d-read_d);
  14. }
  15. unsigned char rcvEnQueue( unsigned int value ) {
  16. // Is the queue full ?
  17. if( write_d == read_d )
  18. {
  19. return 0u;
  20. }
  21. // Make the data copy
  22. nexaQueueBuffer[write_d] = value;
  23. // Increment the write position
  24. write_d++;
  25. if( write_d == NEXA_QUEUE_SIZE ) write_d=0;
  26. return 1u;
  27. }
  28. unsigned char rcvDeQueue( unsigned int *value ) {
  29. int writePos = write_d;
  30. // Pseudo writepos
  31. if( writePos < read_d || writePos == read_d ) writePos += NEXA_QUEUE_SIZE;
  32. // The queue is empty ?
  33. if( writePos == ( read_d + 1 ) )
  34. {
  35. return 0u;
  36. }
  37. // Increment the read position
  38. read_d++;
  39. if( read_d == NEXA_QUEUE_SIZE ) read_d=0;
  40. // Make the data copy
  41. *value = nexaQueueBuffer[read_d];
  42. return 1u;
  43. }
  44. //==========================================================================================================
  45. // QUEUE THAT SENDS CODES FROM MAIN-LOOP TO WIFI
  46. //==========================================================================================================
  47. static int wifi_read_d; /*!< Read index points to the place in the queue that has already been read */
  48. static int wifi_write_d; /*!< The write index points to the place where the next write in the queue will take place. */
  49. static unsigned long long wifiQueueBufferValue[WIFI_QUEUE_SIZE];
  50. static unsigned int wifiQueueBufferType[WIFI_QUEUE_SIZE];
  51. void wifi_rcvInitQueue( ) {
  52. wifi_read_d = 0;
  53. wifi_write_d = 1;
  54. }
  55. unsigned char wifi_rcvEnQueue( unsigned int type, unsigned long long value ) {
  56. // Is the queue full ?
  57. if( wifi_write_d == wifi_read_d )
  58. {
  59. return 0u;
  60. }
  61. // Make the data copy
  62. wifiQueueBufferType[wifi_write_d] = type;
  63. wifiQueueBufferValue[wifi_write_d] = value;
  64. // Increment the write position
  65. wifi_write_d++;
  66. if( wifi_write_d == WIFI_QUEUE_SIZE ) wifi_write_d=0;
  67. return 1u;
  68. }
  69. unsigned char wifi_rcvDeQueue( unsigned int *type, unsigned int *value ) {
  70. int wifi_writePos = wifi_write_d;
  71. // Pseudo writepos
  72. if( wifi_writePos < wifi_read_d || wifi_writePos == wifi_read_d ) wifi_writePos += WIFI_QUEUE_SIZE;
  73. // The queue is empty ?
  74. if( wifi_writePos == ( wifi_read_d + 1 ) )
  75. {
  76. return 0u;
  77. }
  78. // Increment the read position
  79. wifi_read_d++;
  80. if( wifi_read_d == WIFI_QUEUE_SIZE ) wifi_read_d=0;
  81. // Make the data copy
  82. *type = wifiQueueBufferType[wifi_read_d];
  83. *value = wifiQueueBufferValue[wifi_read_d];
  84. return 1u;
  85. }