nexa.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 ) { // Hallway-button: 2760
  68. //DEBUG_WIDTH_FUNC();
  69. rx_state = T0;
  70. }
  71. else {
  72. return -1; // error, reset
  73. }
  74. break;
  75. case T0: // First half of pulse
  76. if ( 155 <= width && width <= 305 ) { // Hallway-button: 230-250
  77. rx_state = T1;
  78. }
  79. else {
  80. if( rx_numBits == 0 && 2240 <= width && width <= 3540 ) {
  81. rx_state = T0;
  82. }
  83. else {
  84. return -1; // error, reset
  85. }
  86. }
  87. break;
  88. case T1:
  89. if ( 200 <= width && width <= 440 ) { // Hallway-button: 250-270
  90. addBit(0);
  91. }
  92. else if ( 950 <= width && width <= 1700 ) { // Hallway-button: 1250-1270
  93. addBit(1);
  94. } else if( rx_numBits == 64 ) { // end of frame
  95. rx_state = DONE;
  96. return 1;
  97. } else {
  98. return -1; // error, reset
  99. }
  100. rx_state = T0;
  101. break;
  102. }
  103. return 0;
  104. }
  105. #define TOO_SHORT 100
  106. int64_t nextPulseNEXA(uint32_t width)
  107. {
  108. static int64_t previous_data = 0;
  109. static uint32_t old_time=0;
  110. static uint32_t now;
  111. int64_t retVal = -1;
  112. if (width > 0)
  113. {
  114. if (rx_state != DONE)
  115. {
  116. switch (rx_decode(width))
  117. {
  118. case -1:
  119. NEXA_resetDecoder();
  120. break;
  121. case 1:
  122. rx_state = DONE;
  123. //printf("%d\n",debug_width);
  124. break;
  125. }
  126. }
  127. }
  128. if (rx_state == DONE) {
  129. now = millis();
  130. if( debug_data.max > debug_data_tot.max ) debug_data_tot.max = debug_data.max;
  131. if( debug_data.min < debug_data_tot.min ) debug_data_tot.min = debug_data.min;
  132. printf("NEXA Debug min,max:%u %u\n",debug_data_tot.min,debug_data_tot.max);
  133. if( sensor_data != previous_data || (now > (old_time+1000)) ) {
  134. previous_data = sensor_data;
  135. retVal = sensor_data;
  136. blinkTheLED();
  137. }
  138. old_time = now;
  139. NEXA_resetDecoder();
  140. }
  141. return retVal;
  142. }