nexa.c 3.0 KB

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