oregon.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. class DecodeOOK {
  2. protected:
  3. byte total_bits, bits, flip, state, pos, data[25];
  4. virtual char decode (word width) =0;
  5. public:
  6. enum { UNKNOWN, T0, T1, T2, T3, OK, DONE };
  7. DecodeOOK () { resetDecoder(); }
  8. bool nextPulse (word width) {
  9. if (state != DONE)
  10. switch (decode(width)) {
  11. case -1: resetDecoder(); break;
  12. case 1: done(); break;
  13. }
  14. return isDone();
  15. }
  16. bool isDone () const { return state == DONE; }
  17. const byte* getData (byte& count) const {
  18. count = pos;
  19. return data;
  20. }
  21. void resetDecoder () {
  22. total_bits = bits = pos = flip = 0;
  23. state = UNKNOWN;
  24. }
  25. // add one bit to the packet data buffer
  26. virtual void gotBit (char value) {
  27. total_bits++;
  28. byte *ptr = data + pos;
  29. *ptr = (*ptr >> 1) | (value << 7);
  30. if (++bits >= 8) {
  31. bits = 0;
  32. if (++pos >= sizeof data) {
  33. resetDecoder();
  34. return;
  35. }
  36. }
  37. state = OK;
  38. }
  39. // store a bit using Manchester encoding
  40. void manchester (char value) {
  41. flip ^= value; // manchester code, long pulse flips the bit
  42. gotBit(flip);
  43. }
  44. // move bits to the front so that all the bits are aligned to the end
  45. void alignTail (byte max =0) {
  46. // align bits
  47. if (bits != 0) {
  48. data[pos] >>= 8 - bits;
  49. for (byte i = 0; i < pos; ++i)
  50. data[i] = (data[i] >> bits) | (data[i+1] << (8 - bits));
  51. bits = 0;
  52. }
  53. // optionally shift bytes down if there are too many of 'em
  54. if (max > 0 && pos > max) {
  55. byte n = pos - max;
  56. pos = max;
  57. for (byte i = 0; i < pos; ++i)
  58. data[i] = data[i+n];
  59. }
  60. }
  61. void reverseBits () {
  62. for (byte i = 0; i < pos; ++i) {
  63. byte b = data[i];
  64. for (byte j = 0; j < 8; ++j) {
  65. data[i] = (data[i] << 1) | (b & 1);
  66. b >>= 1;
  67. }
  68. }
  69. }
  70. void reverseNibbles () {
  71. for (byte i = 0; i < pos; ++i)
  72. data[i] = (data[i] << 4) | (data[i] >> 4);
  73. }
  74. void done () {
  75. while (bits)
  76. gotBit(0); // padding
  77. state = DONE;
  78. }
  79. };
  80. class OregonDecoderV2 : public DecodeOOK {
  81. public:
  82. OregonDecoderV2() {}
  83. // add one bit to the packet data buffer
  84. virtual void gotBit (char value) {
  85. if(!(total_bits & 0x01))
  86. {
  87. data[pos] = (data[pos] >> 1) | (value ? 0x80 : 00);
  88. }
  89. total_bits++;
  90. pos = total_bits >> 4;
  91. if (pos >= sizeof data) {
  92. resetDecoder();
  93. return;
  94. }
  95. state = OK;
  96. }
  97. virtual char decode (word width) {
  98. if (200 <= width && width < 1200) {
  99. byte w = width >= 700;
  100. switch (state) {
  101. case UNKNOWN:
  102. if (w != 0) {
  103. // Long pulse
  104. ++flip;
  105. } else if (32 <= flip) {
  106. // Short pulse, start bit
  107. flip = 0;
  108. state = T0;
  109. } else {
  110. // Reset decoder
  111. return -1;
  112. }
  113. break;
  114. case OK:
  115. if (w == 0) {
  116. // Short pulse
  117. state = T0;
  118. } else {
  119. // Long pulse
  120. manchester(1);
  121. }
  122. break;
  123. case T0:
  124. if (w == 0) {
  125. // Second short pulse
  126. manchester(0);
  127. } else {
  128. // Reset decoder
  129. return -1;
  130. }
  131. break;
  132. }
  133. } else {
  134. return -1;
  135. }
  136. return total_bits == 160 ? 1: 0;
  137. }
  138. };
  139. class OregonDecoderV3 : public DecodeOOK {
  140. public:
  141. OregonDecoderV3() {}
  142. // add one bit to the packet data buffer
  143. virtual void gotBit (char value) {
  144. data[pos] = (data[pos] >> 1) | (value ? 0x80 : 00);
  145. total_bits++;
  146. pos = total_bits >> 3;
  147. if (pos >= sizeof data) {
  148. resetDecoder();
  149. return;
  150. }
  151. state = OK;
  152. }
  153. virtual char decode (word width) {
  154. if (200 <= width && width < 1200) {
  155. byte w = width >= 700;
  156. switch (state) {
  157. case UNKNOWN:
  158. if (w == 0)
  159. ++flip;
  160. else if (32 <= flip) {
  161. flip = 1;
  162. manchester(1);
  163. } else
  164. return -1;
  165. break;
  166. case OK:
  167. if (w == 0)
  168. state = T0;
  169. else
  170. manchester(1);
  171. break;
  172. case T0:
  173. if (w == 0)
  174. manchester(0);
  175. else
  176. return -1;
  177. break;
  178. }
  179. } else {
  180. return -1;
  181. }
  182. return total_bits == 80 ? 1: 0;
  183. }
  184. };