nexa.c 4.0 KB

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