esic.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #include "esic.h"
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include "esp_log.h"
  5. #include "../rxTimer.h"
  6. #include "../led.h"
  7. /**
  8. **********************************************************************************
  9. * TEMP 3
  10. * ESIC WT450H
  11. +---+ +---+ +-------+ + high
  12. | | | | | | |
  13. | | | | | | |
  14. + +---+ +---+ +-------+ low
  15. ^ ^ ^ ^ ^ clock cycle
  16. | 1 | 1 | 0 | 0 | translates as
  17. Each transmission is 36 bits long (i.e. 72 ms)
  18. .short_width = 976, // half-bit width 976 us
  19. .long_width = 1952, // bit width 1952 us
  20. Data is transmitted in pure binary values, NOT BCD-coded.
  21. Example transmission (House 1, Channel 1, RH 59 %, Temperature 23.5 �C)
  22. 1100 00010011001110110100100110011000
  23. b00 - b03 (4 bits): Constant, 1100, probably preamble
  24. b04 - b07 (4 bits): House code (here: 0001 = HC 1)
  25. b08 - b09 (2 bits): Channel code - 1 (here 00 = CC 1)
  26. b10 - b12 (3 bits): Constant, 110
  27. b13 - b19 (7 bits): Relative humidity (here 0111011 = 59 %)
  28. b20 - b34 (15 bits): Temperature (see below)
  29. b35 - b35 (1 bit) : Parity (xor of all bits should give 0)
  30. The temperature is transmitted as (temp + 50.0) * 128,
  31. which equals (temp * 128) + 6400. Adding 50.0 �C makes
  32. all values positive, an unsigned 15 bit integer where the
  33. first 8 bits correspond to the whole part of the temperature
  34. (here 01001001, decimal 73, substract 50 = 23).
  35. Remaining 7 bits correspond to the fractional part.
  36. To avoid floating point calculations I store the raw temperature value
  37. as a signed integer in the variable esicTemp, then transform it to
  38. actual temperature * 10 using "esicTemp = (esicTemp - 6400) * 10 / 128",
  39. where 6400 is the added 50 times 128.
  40. When reporting the temperature I simply print "esicTemp / 10" (integer division,
  41. no fraction), followed by a decimal point and "esicTemp % 10" (remainder, which
  42. equals first fractional decimal digit).
  43. Summary of bit fields:
  44. 1100 0001 00 110 0111011 010010011001100 0
  45. c1 hc cc c2 rh t p
  46. c1, c2 = constant field 1 and 2
  47. hc, cc = house code and channel code
  48. rh, t = relative humidity, temperature
  49. p = parity bit 1111111111111110
  50. **********************************************************************************
  51. */
  52. volatile unsigned long long temp3_x_data;
  53. void ESIC_ResetDecoder() {
  54. temp3_x_data = 0;
  55. }
  56. #define NO_OF_PULSES 60
  57. #define max(a,b) (((a)>(b))?(a):(b))
  58. #define isShortPulse(width) (((long_pulse/2)-maxDiff) <= width && width <= ((long_pulse/2)+maxDiff))
  59. #define isLongPulse(width) ((long_pulse-maxDiff) <= width && width <= (long_pulse+maxDiff))
  60. unsigned int temp3_pulses[NO_OF_PULSES];
  61. static int long_pulse = 1955;
  62. static int maxDiff = 300;
  63. static void storePulses(unsigned int inWidth) {
  64. int i;
  65. // Shift pulses down
  66. for(i=1;i<NO_OF_PULSES;i++) {
  67. temp3_pulses[i-1] = temp3_pulses[i];
  68. }
  69. temp3_pulses[NO_OF_PULSES-1] = inWidth;
  70. }
  71. static void sweepForNoise() {
  72. // If we have a short pulse in position 2
  73. // Then add together pos 1,2 and 3 if they are a valid pulse
  74. // This way we can handle one single noise pulse in a real pulse
  75. if( temp3_pulses[NO_OF_PULSES-2] < ((long_pulse/2)-maxDiff) ) {
  76. int totPulse = temp3_pulses[NO_OF_PULSES-1]+temp3_pulses[NO_OF_PULSES-2]+temp3_pulses[NO_OF_PULSES-3];
  77. if( isShortPulse(totPulse) || isLongPulse(totPulse) ) {
  78. // Store new pulse in last position
  79. temp3_pulses[NO_OF_PULSES-1] = totPulse;
  80. // Move everything up again
  81. for(int i=NO_OF_PULSES-2;i>1;i--) {
  82. temp3_pulses[i] = temp3_pulses[i-2];
  83. }
  84. temp3_pulses[0] = 0;
  85. temp3_pulses[1] = 0;
  86. }
  87. }
  88. }
  89. /*static void adjustTiming() {
  90. int p1 = temp3_pulses[NO_OF_PULSES-8];
  91. int p2 = temp3_pulses[NO_OF_PULSES-7];
  92. int p3 = temp3_pulses[NO_OF_PULSES-6];
  93. int p4 = temp3_pulses[NO_OF_PULSES-5];
  94. int p5 = temp3_pulses[NO_OF_PULSES-4];
  95. int p6 = temp3_pulses[NO_OF_PULSES-3];
  96. int p7 = temp3_pulses[NO_OF_PULSES-2];
  97. int p8 = temp3_pulses[NO_OF_PULSES-1];
  98. // Check max differance between the short pulses
  99. int sh_mean = (p1+p2+p3+p4) / 4;
  100. int sh_max_diff = max(max(abs(p1-sh_mean),abs(p2-sh_mean)),max(abs(p3-sh_mean),abs(p4-sh_mean)));
  101. // Check max differance between the long pulses
  102. int long_mean = (p5+p6+p7+p8) / 4;
  103. int long_max_diff = max(max(abs(p5-long_mean),abs(p6-long_mean)),max(abs(p7-long_mean),abs(p8-long_mean)));
  104. if( sh_max_diff < maxDiff && long_max_diff < maxDiff ) {
  105. int mean = (long_mean + sh_mean)/2;
  106. if( long_mean > (mean+maxDiff) && sh_mean < (mean-maxDiff) ) {
  107. if( abs(long_mean - (sh_mean*2)) < maxDiff ) {
  108. long_pulse = (long_mean + sh_mean*2)/2;
  109. maxDiff = long_mean / 15;
  110. }
  111. }
  112. }
  113. }*/
  114. // The latest/newest pulse is at the end of the array [59]
  115. // This ([59]) is the latest bit in the bit-stream from the transmitter
  116. // This is way we input the bits at a high position and shift them towards lower values
  117. // So..start reading backwards and working towards the first/highest bits
  118. static int checkPulsePattern() {
  119. int i = NO_OF_PULSES-1; // Start reading from the last received (end of array)
  120. int b = 0;
  121. unsigned long long code = 0;
  122. while( i >= 0 ) {
  123. int combWidth = temp3_pulses[i] + temp3_pulses[i-1];
  124. if( isLongPulse(temp3_pulses[i]) ) {
  125. b++;
  126. i-=1;
  127. code = code >> 1;
  128. }
  129. else if( isShortPulse(temp3_pulses[i]) && isShortPulse(temp3_pulses[i-1]) ) {
  130. b++;
  131. i-=2;
  132. code = ((code >> 1) | 0x200000000);
  133. }
  134. else if( isLongPulse( combWidth ) ) {
  135. b++;
  136. i-=2;
  137. code = ((code >> 1) | 0x200000000);
  138. }
  139. else {
  140. return -1;
  141. }
  142. if( b == 34 ) {
  143. /*
  144. 0000 0001 00 110 0101000 010010110101000 1
  145. AND: 0011 1111 00 111 0000000 000000000000000 0 = 001111110011100000000000000000000000 = 0x3F3800000
  146. RES: 0000 0001 00 110 0000000 000000000000000 0 = 000000010011000000000000000000000000 = 0x013000000
  147. Summary of bit fields:
  148. 1100 0001 00 110 0111011 010010011001100 0
  149. c1 hc cc c2 rh t p
  150. */
  151. if( (code & 0x3F3800000) == 0x013000000 ) {
  152. temp3_x_data = (code&0xFFFFFFFF);
  153. // Check parity
  154. int even=0;
  155. unsigned long long data = temp3_x_data;
  156. for(int i=0;i<32;i++) {
  157. if( data & 1 ) even++;
  158. data >>= 1;
  159. }
  160. if( even % 2 != 0 ) {
  161. return -1;
  162. }
  163. // A correct code has been received
  164. //printf("Code received: %llu on row:%d\n",(code&0xFFFFFFFF),row_no);
  165. return 1;
  166. }
  167. else {
  168. return -1;
  169. }
  170. }
  171. }
  172. return 0;
  173. }
  174. static int temp3decode (unsigned int inWidth) {
  175. int width = inWidth; //preProcessPulses(inWidth);
  176. if( width == -1 ) return -1;
  177. if( width == 0 ) return 0;
  178. storePulses(inWidth);
  179. sweepForNoise();
  180. //adjustTiming();
  181. return checkPulsePattern();
  182. }
  183. int64_t nextPulseESICSensor(uint32_t width) {
  184. volatile static int result;
  185. if( width > 0 ) {
  186. if( temp3_x_data == 0 ) {
  187. result = temp3decode(width);
  188. }
  189. }
  190. return (temp3_x_data > 0);
  191. }