oregon.c 2.5 KB

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