receiver.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include <stdio.h>
  2. #include "sw_fifo_1.h"
  3. #include "sw_fifo_2.h"
  4. #include "app.h"
  5. #include "led_blink.h"
  6. #include "main.h"
  7. #include "queue.h"
  8. #include "temp1.h"
  9. #include "temp2.h"
  10. #include "temp3.h"
  11. #include "nexa.h"
  12. #include "stm32f4xx_hal.h"
  13. #include "stm32f4xx_hal_gpio.h"
  14. #include "wifi.h"
  15. #include "receiver.h"
  16. #include "interrupts.h"
  17. int receiverEnabled = 0;
  18. unsigned long startLogging=0; // Keep the time for next lo gevent
  19. int startLoggingState=0; // 0=Not active, 1=Wait for next event, 2=Sample-state
  20. int noLogsSent=0; // Number of logs that have been sent
  21. int startLoggingPos=0;
  22. int stopLoggingPos=0;
  23. int loggingReady=0;
  24. // This receiver task is called every 1mS, with a rather low priority.
  25. void receiver() {
  26. static unsigned int width;
  27. static unsigned long previous_temp0 = 0; // Get the current time
  28. static unsigned long previous_temp1 = 0; // Get the current time
  29. static unsigned long previous_temp2 = 0; // Get the current time
  30. static unsigned long previous_temp3 = 0; // Get the current time
  31. static unsigned long previous_now = 0; // Get the current time
  32. unsigned long now = millis(); // Get the current time
  33. if( receiverEnabled == 0 ) return;
  34. // Check millis() for wrapping (after 49 days)
  35. if( previous_now > now ) {
  36. previous_temp3 = 0;
  37. }
  38. while( rcvDeQueue(&width) ) {
  39. // ***************** NEXA *********************
  40. /* if( nextPulseNexa(width) ) {
  41. if( now > (previous_temp0+1000) ) {
  42. wifi_rcvEnQueue( 0, (unsigned int)(x_data & 0xFFFFFFFF)); // Send to queue to be sent to WiFi (in WiFi.c via main-loop in app.c)
  43. previous_temp0 = now;
  44. }
  45. resetDecoder();
  46. blinkTheLED();
  47. }
  48. */
  49. /*
  50. // TEMP 1
  51. if( nextPulseTemp1(width) ) {
  52. if( now > (previous_temp1+1000) ) {
  53. wifi_rcvEnQueue( 1, (unsigned int)(temp1_x_data & 0xFFFFFFFF)); // Send to queue to be sent to WiFi (in WiFi.c via main-loop in app.c)
  54. previous_temp1 = now;
  55. }
  56. temp1ResetDecoder();
  57. blinkTheLED();
  58. }
  59. */
  60. /*
  61. // TEMP 2
  62. if( nextPulseTemp2(width) ) {
  63. if( now > (previous_temp2+1000) ) {
  64. wifi_rcvEnQueue( 2, temp2_x_data ); // Send to queue to be sent to WiFi (in WiFi.c via main-loop in app.c)
  65. previous_temp2 = now;
  66. }
  67. temp2ResetDecoder();
  68. blinkTheLED();
  69. } */
  70. // TEMP 3
  71. if( nextPulseTemp3(width) ) {
  72. if( ((temp3_x_data & 0xFC000000) >> 26) == 4 ) {
  73. startLogging = millis()+59500; // Reset the timer for the next sample-event
  74. startLoggingState = 1;
  75. }
  76. if( now > (previous_temp3+1000) ) {
  77. wifi_rcvEnQueue( 3, (unsigned int)(temp3_x_data & 0xFFFFFFFF)); // Send to queue to be sent to WiFi (in WiFi.c via main-loop in app.c)
  78. previous_temp3 = now;
  79. }
  80. temp3ResetDecoder();
  81. blinkTheLED();
  82. }
  83. // Handle logging
  84. if( startLoggingState == 1 ) {
  85. if( now >= startLogging ) {
  86. // Start sampling
  87. startLoggingPos = logCounter; // Save start-log-position
  88. startLogging = now + 1000; // Set timer for next event
  89. startLoggingState = 2;
  90. }
  91. }
  92. if( startLoggingState == 2 ) {
  93. // Logging is occuring....
  94. if( now >= startLogging ) {
  95. // Stop sampling
  96. stopLoggingPos = logCounter; // Save end-log-position
  97. startLogging = now + 59500; // Set timer for next event
  98. startLoggingState = 1;
  99. loggingReady = 1;
  100. noLogsSent++;
  101. if( noLogsSent == 3 ) {
  102. startLoggingState=0; // Turn off log-send after 3 missed messages
  103. }
  104. }
  105. }
  106. }
  107. previous_now = now;
  108. }