nexa.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "app.h"
  2. #include "nexa.h"
  3. struct nexaData_t {
  4. unsigned char bitNo;
  5. unsigned short width;
  6. } ;
  7. //#define LOG(wd) { if(logCounter<LOG_SIZE){/*nexaData[logcounter].bitNo=x_numBits;*/ nexaData[logCounter]=wd;logCounter++;if(logCounter==LOG_SIZE)logCounter=0;}}
  8. #define LOG(x) {}
  9. enum { UNKNOWN, T0, T1, T2, T3, OK, DONE };
  10. unsigned char state = UNKNOWN;
  11. unsigned long x_data;
  12. static unsigned int x_numBits;
  13. static boolean x_2ndbit;
  14. unsigned int dbg_periodTime; // For debugging
  15. void resetDecoder () {
  16. x_data = 0;
  17. x_numBits = 0;
  18. x_2ndbit = false;
  19. state = UNKNOWN;
  20. }
  21. static boolean isDone() { return state == DONE; }
  22. static void done () { state = DONE; }
  23. static void gotBit (char value) {
  24. // X
  25. x_numBits++;
  26. x_data = (x_data << 1) + (value & 0x01);
  27. if( x_2ndbit == false ) {
  28. x_2ndbit = true;
  29. }
  30. else {
  31. x_2ndbit = false;
  32. unsigned int bits = (x_data & 0x03);
  33. if( bits == 2 ) {
  34. // Bit is 1
  35. x_data = (x_data >> 1) | 0x00000001;
  36. }
  37. else {
  38. // Bit is 0
  39. x_data = (x_data >> 1) & 0xFFFFFFFE;
  40. }
  41. }
  42. state = OK;
  43. }
  44. // Detta är den äldre versionen med bredare siffror
  45. static int decode (unsigned int width) {
  46. if( x_numBits == 3 ) { // end of frame
  47. //state = DONE;
  48. //return 1;
  49. }
  50. switch (state) {
  51. case UNKNOWN: // Start of frame
  52. if ( 2200 <= width && width <= 3500 ) {
  53. state = T0;
  54. dbg_periodTime = width;
  55. LOG(width);
  56. }
  57. else {
  58. LOG(width);LOG(55555);
  59. return -1; // error, reset
  60. }
  61. break;
  62. case T0: // First half of pulse : HIGH around 230us
  63. if ( 150 <= width && width <= 300 ) {
  64. state = T1;
  65. LOG(width);
  66. }
  67. else {
  68. if( x_numBits == 0 && 2200 <= width && width <= 3500 ) {
  69. LOG(44444);
  70. state = T0;
  71. }
  72. else {
  73. LOG(width);LOG(55555);
  74. return -1; // error, reset
  75. }
  76. }
  77. break;
  78. case T1:
  79. if ( 200 <= width && width <= 400) {
  80. gotBit(0);
  81. LOG(width);
  82. } else if ( 900 <= width && width <= 1500 ) {
  83. gotBit(1);
  84. LOG(width);
  85. } else if( x_numBits == 64 ) { // end of frame
  86. state = DONE;
  87. return 1;
  88. } else {
  89. LOG(width);LOG(55555);
  90. return -1; // error, reset
  91. }
  92. state = T0;
  93. break;
  94. }
  95. return 0;
  96. }
  97. #define TOO_SHORT 100
  98. boolean nextPulseNexa (unsigned int width) {
  99. unsigned int decodew = 0;
  100. //static unsigned int totw = 0;
  101. //static int mode=0;
  102. //if( width < 100 ) LOG(width);
  103. /*
  104. if( state != T0 ) {
  105. switch( mode ) {
  106. case 0:
  107. totw = width;
  108. mode = 1;
  109. break;
  110. case 1:
  111. if( width < TOO_SHORT ) {
  112. totw += width;
  113. mode = 2;
  114. }
  115. else {
  116. decodew = totw;
  117. totw = width;
  118. }
  119. break;
  120. case 2:
  121. totw += width;
  122. //if( width < TOO_SHORT ) { // This change caused less reception
  123. // mode = 2;
  124. //}
  125. //else {
  126. mode = 1;
  127. //}
  128. break;
  129. }
  130. }
  131. else {
  132. // T0-State, send right through
  133. decodew = totw;
  134. totw = width;
  135. if( width < TOO_SHORT ) {
  136. mode = 2;
  137. }
  138. else {
  139. mode = 1;
  140. }
  141. }*/
  142. decodew = width;
  143. if( decodew > 0 ) {
  144. if (state != DONE)
  145. switch (decode(decodew)) {
  146. case -1:
  147. resetDecoder();
  148. break;
  149. case 1: done(); break;
  150. }
  151. }
  152. return isDone();
  153. }