nexa.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. uint32_t startPulseWidth = 0;
  32. void NEXA_resetDecoder () {
  33. sensor_data = 0;
  34. rx_numBits = 0;
  35. x_2ndbit = false;
  36. rx_state = UNKNOWN;
  37. //debug_data.max = 0;
  38. //debug_data.min = 99999;
  39. }
  40. static void addBit (uint8_t value) {
  41. // X
  42. rx_numBits++;
  43. sensor_data = (sensor_data << 1) + (value & 0x01);
  44. if( x_2ndbit == false ) {
  45. x_2ndbit = true;
  46. }
  47. else {
  48. x_2ndbit = false;
  49. unsigned int bits = (sensor_data & 0x03);
  50. if( bits == 2 ) {
  51. // Bit is 1
  52. sensor_data = (sensor_data >> 1) | 0x00000001;
  53. }
  54. else {
  55. // Bit is 0
  56. sensor_data = (sensor_data >> 1) & 0xFFFFFFFE;
  57. }
  58. }
  59. rx_state = OK_Sensor;
  60. }
  61. static int rx_decode(uint32_t width) {
  62. if( rx_numBits == 3 ) { // end of frame
  63. //state = DONE;
  64. //return 1;
  65. }
  66. switch (rx_state) {
  67. case UNKNOWN: // Start of frame
  68. if ( 2240 <= width && width <= 3540 ) { // Hallway-button: 2760
  69. //DEBUG_WIDTH_FUNC();
  70. startPulseWidth = width;
  71. rx_state = T0;
  72. }
  73. else {
  74. return -1; // error, reset
  75. }
  76. break;
  77. case T0: // First half of pulse
  78. if ( 155 <= width && width <= 305 ) { // Hallway-button: 230-250
  79. rx_state = T1;
  80. }
  81. else {
  82. if( rx_numBits == 0 && 2240 <= width && width <= 3540 ) {
  83. rx_state = T0;
  84. }
  85. else {
  86. return -1; // error, reset
  87. }
  88. }
  89. break;
  90. case T1:
  91. if ( 200 <= width && width <= 440 ) { // Hallway-button: 250-270
  92. addBit(0);
  93. }
  94. else if ( 950 <= width && width <= 1700 ) { // Hallway-button: 1250-1270
  95. addBit(1);
  96. } else if( rx_numBits == 64 ) { // end of frame
  97. rx_state = DONE;
  98. return 1;
  99. } else {
  100. return -1; // error, reset
  101. }
  102. rx_state = T0;
  103. break;
  104. }
  105. return 0;
  106. }
  107. #define TOO_SHORT 100
  108. int64_t nextPulseNEXA(uint32_t width)
  109. {
  110. static int64_t previous_data = 0;
  111. static uint32_t old_time=0;
  112. static uint32_t now;
  113. int64_t retVal = -1;
  114. if (width > 0)
  115. {
  116. if (rx_state != DONE)
  117. {
  118. switch (rx_decode(width))
  119. {
  120. case -1:
  121. NEXA_resetDecoder();
  122. break;
  123. case 1:
  124. rx_state = DONE;
  125. //printf("%d\n",debug_width);
  126. break;
  127. }
  128. }
  129. }
  130. if (rx_state == DONE) {
  131. now = millis();
  132. //if( debug_data.max > debug_data_tot.max ) debug_data_tot.max = debug_data.max;
  133. //if( debug_data.min < debug_data_tot.min ) debug_data_tot.min = debug_data.min;
  134. //printf("NEXA Debug min,max:%u %u\n",debug_data_tot.min,debug_data_tot.max);
  135. if( sensor_data != previous_data || (now > (old_time+1000)) ) {
  136. previous_data = sensor_data;
  137. retVal = sensor_data;
  138. blinkTheLED();
  139. }
  140. old_time = now;
  141. NEXA_resetDecoder();
  142. }
  143. return retVal;
  144. }