oregon.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "oregon.h"
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include "../rxTimer.h"
  5. #include <string.h>
  6. #include "esp_log.h"
  7. #include "config.h"
  8. #ifdef TRANCEIVER_ENABLED
  9. static void done ();
  10. static unsigned char total_bits, bits, flip, state, pos, data[25];
  11. char result_str[25];
  12. enum { UNKNOWN, T0, T1, T2, T3, SENSOR_OK, DONE };
  13. void Oregon_ResetDecoder () {
  14. total_bits = bits = pos = flip = 0;
  15. state = UNKNOWN;
  16. memset(result_str,'\0',sizeof(result_str));
  17. }
  18. // V3-Version of the gitBit-function
  19. static void gotBit (char value) {
  20. data[pos] = (data[pos] >> 1) | (value ? 0x80 : 00);
  21. total_bits++;
  22. pos = total_bits >> 3;
  23. if (pos >= sizeof data) {
  24. Oregon_ResetDecoder();
  25. return;
  26. }
  27. state = SENSOR_OK;
  28. }
  29. // store a bit using Manchester encoding
  30. static void manchester (char value) {
  31. flip ^= value; // manchester code, long pulse flips the bit
  32. gotBit(flip);
  33. }
  34. static void done () {
  35. while (bits)
  36. gotBit(0); // padding
  37. state = DONE;
  38. }
  39. static int rx_decode( int width ) {
  40. if( (480-200) < width && width < (980+200) ) {
  41. char w = width >= 730;
  42. switch (state) {
  43. case UNKNOWN:
  44. if (w == 0)
  45. ++flip;
  46. else if (32 <= flip) {
  47. flip = 1;
  48. manchester(1); // Width 950
  49. } else
  50. return -1;
  51. break;
  52. case SENSOR_OK:
  53. if (w == 0)
  54. state = T0;
  55. else
  56. manchester(1); // Width 980
  57. break;
  58. case T0:
  59. if (w == 0)
  60. manchester(0); // Width 480
  61. else
  62. return -1;
  63. break;
  64. }
  65. } else {
  66. return -1;
  67. }
  68. return total_bits == 80 ? 1: 0;
  69. }
  70. //10 bytes data: 2A 19 04 CE 00 00 60 12 00 30
  71. char * nextPulseOregonSensor(uint32_t width) {
  72. if( state != DONE ) {
  73. switch (rx_decode(width)) {
  74. case -1:
  75. Oregon_ResetDecoder();
  76. break;
  77. case 1:
  78. done();
  79. break;
  80. }
  81. }
  82. if( state == DONE ) {
  83. if( pos == 10 ) {
  84. for( int i=0; i<pos; i++ ) {
  85. sprintf(result_str+(i*2),"%02X",data[i] );
  86. }
  87. return result_str;
  88. }
  89. else {
  90. Oregon_ResetDecoder();
  91. return (char *)NULL;
  92. }
  93. }
  94. else
  95. return (char *)NULL;
  96. }
  97. #endif