oregon.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "app.h"
  2. #include "nexa.h"
  3. #include "oregon.h"
  4. // Constructor must run resetOV3Decoder()
  5. static int decode(int width);
  6. static void done ();
  7. static boolean isDone();
  8. static unsigned char total_bits, bits, flip, state, pos, data[25];
  9. enum { UNKNOWN, T0, T1, T2, T3, OK, DONE };
  10. boolean nextPulseoregonV3(unsigned int width) {
  11. if (state != DONE)
  12. switch (decode(width)) {
  13. case -1: resetOV3Decoder(); break;
  14. case 1: done(); break;
  15. }
  16. return isDone();
  17. }
  18. static boolean isDone() {
  19. return state == DONE;
  20. }
  21. const unsigned char* getOV3Data (unsigned char *count) {
  22. *count = pos;
  23. return data;
  24. }
  25. void resetOV3Decoder () {
  26. total_bits = bits = pos = flip = 0;
  27. state = UNKNOWN;
  28. }
  29. // V3-Version of the gitBit-function
  30. void gotBit (char value) {
  31. data[pos] = (data[pos] >> 1) | (value ? 0x80 : 00);
  32. total_bits++;
  33. pos = total_bits >> 3;
  34. if (pos >= sizeof data) {
  35. resetOV3Decoder();
  36. return;
  37. }
  38. state = OK;
  39. }
  40. // store a bit using Manchester encoding
  41. void manchester (char value) {
  42. flip ^= value; // manchester code, long pulse flips the bit
  43. gotBit(flip);
  44. }
  45. static void done () {
  46. while (bits)
  47. gotBit(0); // padding
  48. state = DONE;
  49. }
  50. static int decode (int width) {
  51. if (200 <= width && width < 1200) {
  52. char w = width >= 700;
  53. switch (state) {
  54. case UNKNOWN:
  55. if (w == 0)
  56. ++flip;
  57. else if (32 <= flip) {
  58. flip = 1;
  59. manchester(1);
  60. } else
  61. return -1;
  62. break;
  63. case OK:
  64. if (w == 0)
  65. state = T0;
  66. else
  67. manchester(1);
  68. break;
  69. case T0:
  70. if (w == 0)
  71. manchester(0);
  72. else
  73. return -1;
  74. break;
  75. }
  76. } else {
  77. return -1;
  78. }
  79. return total_bits == 80 ? 1: 0;
  80. }
  81. /*
  82. // move bits to the front so that all the bits are aligned to the end
  83. static void alignTail(char max) {
  84. // align bits
  85. if (bits != 0) {
  86. data[pos] >>= 8 - bits;
  87. for (char i = 0; i < pos; ++i)
  88. data[i] = (data[i] >> bits) | (data[i+1] << (8 - bits));
  89. bits = 0;
  90. }
  91. // optionally shift bytes down if there are too many of 'em
  92. if (max > 0 && pos > max) {
  93. char n = pos - max;
  94. pos = max;
  95. for (char i = 0; i < pos; ++i)
  96. data[i] = data[i+n];
  97. }
  98. }
  99. */
  100. /*
  101. static void reverseBits () {
  102. for (char i = 0; i < pos; ++i) {
  103. char b = data[i];
  104. for (char j = 0; j < 8; ++j) {
  105. data[i] = (data[i] << 1) | (b & 1);
  106. b >>= 1;
  107. }
  108. }
  109. }
  110. */
  111. /*
  112. static void reverseNibbles () {
  113. for (char i = 0; i < pos; ++i)
  114. data[i] = (data[i] << 4) | (data[i] >> 4);
  115. }
  116. */