nexa.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include "nexa.h"
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include "../rxTimer.h"
  5. #include "../led.h"
  6. typedef struct {
  7. uint32_t max;
  8. uint32_t min;
  9. uint32_t cnt;
  10. uint32_t total;
  11. } debug_data_t;
  12. static debug_data_t debug_data = { 0,99999,0,0 };
  13. static debug_data_t debug_data_tot = { 0,99999,0,0 };
  14. #define DEBUG_WIDTH_FUNC() { \
  15. if( width > debug_data.max ) debug_data.max = width; \
  16. if( width < debug_data.min ) debug_data.min = width; \
  17. debug_data.total += width; \
  18. debug_data.cnt++; \
  19. }
  20. #define FACTOR 9
  21. #define CONV(a) ((((a)*FACTOR) / 100) + (a))
  22. struct nexaData_t {
  23. unsigned char bitNo;
  24. unsigned short width;
  25. } ;
  26. enum { UNKNOWN, T0, T1, T2, T3, OK_Sensor, DONE };
  27. static uint8_t rx_state = UNKNOWN;
  28. static uint64_t sensor_data;
  29. static uint32_t rx_numBits;
  30. static uint8_t x_2ndbit;
  31. void NEXA_resetDecoder () {
  32. sensor_data = 0;
  33. rx_numBits = 0;
  34. x_2ndbit = false;
  35. rx_state = UNKNOWN;
  36. debug_data.max = 0;
  37. debug_data.min = 99999;
  38. }
  39. static void addBit (uint8_t value) {
  40. // X
  41. rx_numBits++;
  42. sensor_data = (sensor_data << 1) + (value & 0x01);
  43. if( x_2ndbit == false ) {
  44. x_2ndbit = true;
  45. }
  46. else {
  47. x_2ndbit = false;
  48. unsigned int bits = (sensor_data & 0x03);
  49. if( bits == 2 ) {
  50. // Bit is 1
  51. sensor_data = (sensor_data >> 1) | 0x00000001;
  52. }
  53. else {
  54. // Bit is 0
  55. sensor_data = (sensor_data >> 1) & 0xFFFFFFFE;
  56. }
  57. }
  58. rx_state = OK_Sensor;
  59. }
  60. static int rx_decode(uint32_t width) {
  61. if( rx_numBits == 3 ) { // end of frame
  62. //state = DONE;
  63. //return 1;
  64. }
  65. switch (rx_state) {
  66. case UNKNOWN: // Start of frame
  67. if ( 2240 <= width && width <= 3540 ) {
  68. rx_state = T0;
  69. }
  70. else {
  71. return -1; // error, reset
  72. }
  73. break;
  74. case T0: // First half of pulse
  75. if ( 155 <= width && width <= 305 ) {
  76. rx_state = T1;
  77. }
  78. else {
  79. if( rx_numBits == 0 && 2240 <= width && width <= 3540 ) {
  80. rx_state = T0;
  81. }
  82. else {
  83. return -1; // error, reset
  84. }
  85. }
  86. break;
  87. case T1:
  88. if ( 240 <= width && width <= 440 ) {
  89. addBit(0);
  90. } else if ( (1410-300) <= width && width <= (1410+300) ) {
  91. DEBUG_WIDTH_FUNC();
  92. addBit(1);
  93. } else if( rx_numBits == 64 ) { // end of frame
  94. rx_state = DONE;
  95. return 1;
  96. } else {
  97. return -1; // error, reset
  98. }
  99. rx_state = T0;
  100. break;
  101. }
  102. return 0;
  103. }
  104. #define TOO_SHORT 100
  105. int64_t nextPulseNEXA(uint32_t width)
  106. {
  107. static int64_t previous_data = 0;
  108. static uint32_t old_time=0;
  109. static uint32_t now;
  110. int64_t retVal = -1;
  111. if (width > 0)
  112. {
  113. if (rx_state != DONE)
  114. {
  115. switch (rx_decode(width))
  116. {
  117. case -1:
  118. NEXA_resetDecoder();
  119. break;
  120. case 1:
  121. rx_state = DONE;
  122. //printf("%d\n",debug_width);
  123. break;
  124. }
  125. }
  126. }
  127. if (rx_state == DONE) {
  128. now = millis();
  129. if( debug_data.max > debug_data_tot.max ) debug_data_tot.max = debug_data.max;
  130. if( debug_data.min < debug_data_tot.min ) debug_data_tot.min = debug_data.min;
  131. //printf("OREGON DEbug min,max:%u %u\n",debug_data_tot.min,debug_data_tot.max);
  132. if( sensor_data != previous_data || (now > (old_time+1000)) ) {
  133. previous_data = sensor_data;
  134. retVal = sensor_data;
  135. blinkTheLED();
  136. }
  137. old_time = now;
  138. NEXA_resetDecoder();
  139. }
  140. return retVal;
  141. }