nexa.c 3.4 KB

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