telldus_sensor.ino 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //#define TELLDUS_DEBUG
  2. //#define TELLDUS_RECORDER
  3. namespace telldus {
  4. enum { T0, T1, T2, OK, DONE };
  5. static unsigned char state = T0;
  6. uint64_t x_data;
  7. uint8_t x_numBits;
  8. uint32_t meanPulseWidth = 0;
  9. uint8_t meanPulseWidthCnt = 0;
  10. void resetDecoder () {
  11. x_data = 0;
  12. x_numBits = 0;
  13. state = T0;
  14. meanPulseWidth = 0;
  15. meanPulseWidthCnt = 0;
  16. }
  17. static boolean isDone() {
  18. return state == DONE;
  19. }
  20. static void done () {
  21. state = DONE;
  22. }
  23. static void gotBit (uint8_t value) {
  24. x_numBits++;
  25. x_data = (x_data << 1) + (value & 0x01);
  26. state = OK;
  27. }
  28. static int16_t decode( uint16_t width ) {
  29. static uint16_t firstPulse=0;
  30. #ifdef TELLDUS_RECORDER
  31. static uint8_t recTrigger=0;
  32. static uint16_t recorder[200];
  33. static uint8_t recIdx = 0;
  34. recorder[recIdx] = width;
  35. recIdx++;
  36. if(recIdx==200) recIdx=0;
  37. if( recTrigger > 1 ) recTrigger--;
  38. if( recTrigger == 1 ) {
  39. recTrigger=0;
  40. for(uint8_t i=0; i<200;i++) {
  41. Serial.print("-");
  42. Serial.print(recorder[recIdx]);
  43. recIdx++;
  44. if(recIdx==200) recIdx=0;
  45. }
  46. Serial.println("");
  47. }
  48. #endif
  49. switch( state ) {
  50. case T0: // First half of pulse : HIGH around 910
  51. if( x_numBits == 48 || (width > 5000 && x_numBits>=42) ) { // end of frame
  52. #ifdef TELLDUS_RECORDER
  53. recTrigger = 6;
  54. #endif
  55. meanPulseWidth /= meanPulseWidthCnt;
  56. state = DONE;
  57. return 1;
  58. }
  59. else if ( 780 <= width && width <= 1100 ) {
  60. firstPulse = width;
  61. state = T1;
  62. }
  63. else {
  64. #ifdef TELLDUS_DEBUG
  65. if( x_numBits > 0 ) {
  66. Serial.print(" E ");
  67. Serial.print(x_numBits);
  68. Serial.print(" (");
  69. Serial.print(width);
  70. Serial.println("- )");
  71. }
  72. #endif
  73. return -1; // error, reset
  74. }
  75. break;
  76. case T1:
  77. if( 360 <= width && width <= 640 ) { // 410
  78. gotBit(1);
  79. #ifdef TELLDUS_DEBUG
  80. Serial.print(" (");
  81. Serial.print(firstPulse);
  82. Serial.print("-");
  83. Serial.print(width);
  84. Serial.print(") ");
  85. #endif
  86. } else if ( 1280 <= width && width <= 1560 ) { // 1290
  87. #ifdef TELLDUS_DEBUG
  88. Serial.print(" (");
  89. Serial.print(firstPulse);
  90. Serial.print("-");
  91. Serial.print(width);
  92. Serial.print(") ");
  93. #endif
  94. gotBit(0);
  95. meanPulseWidth += width;
  96. meanPulseWidthCnt++;
  97. } else {
  98. #ifdef TELLDUS_DEBUG
  99. if( x_numBits > 0 ) {
  100. Serial.print(" E ");
  101. Serial.print(x_numBits);
  102. Serial.print(" (");
  103. Serial.print(firstPulse);
  104. Serial.print("-");
  105. Serial.print(width);
  106. Serial.println(")");
  107. }
  108. #endif
  109. return -1; // error, reset
  110. }
  111. state = T0;
  112. break;
  113. }
  114. return 0;
  115. }
  116. boolean nextPulse( uint16_t width ) {
  117. if ( width > 0 ) {
  118. if (state != DONE)
  119. switch( decode(width) ) {
  120. case -1:
  121. resetDecoder();
  122. break;
  123. case 1: done(); break;
  124. }
  125. }
  126. return isDone();
  127. }
  128. }