acurite_609txc.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #include "acurite_609txc.h"
  2. #define TAG "WSProtocolAcurite_609TXC"
  3. /*
  4. * Help
  5. * https://github.com/merbanan/rtl_433/blob/5bef4e43133ac4c0e2d18d36f87c52b4f9458453/src/devices/acurite.c#L216
  6. *
  7. * 0000 1111 | 0011 0000 | 0101 1100 | 0000 0000 | 1110 0111
  8. * iiii iiii | buuu tttt | tttt tttt | hhhh hhhh | cccc cccc
  9. * - i: identification; changes on battery switch
  10. * - c: checksum (sum of previous by bytes)
  11. * - u: unknown
  12. * - b: battery low; flag to indicate low battery voltage
  13. * - t: temperature; in °C * 10, 12 bit with complement
  14. * - h: humidity
  15. *
  16. */
  17. static const SubGhzBlockConst ws_protocol_acurite_609txc_const = {
  18. .te_short = 500,
  19. .te_long = 1000,
  20. .te_delta = 150,
  21. .min_count_bit_for_found = 40,
  22. };
  23. struct WSProtocolDecoderAcurite_609TXC {
  24. SubGhzProtocolDecoderBase base;
  25. SubGhzBlockDecoder decoder;
  26. WSBlockGeneric generic;
  27. };
  28. struct WSProtocolEncoderAcurite_609TXC {
  29. SubGhzProtocolEncoderBase base;
  30. SubGhzProtocolBlockEncoder encoder;
  31. WSBlockGeneric generic;
  32. };
  33. typedef enum {
  34. Acurite_609TXCDecoderStepReset = 0,
  35. Acurite_609TXCDecoderStepSaveDuration,
  36. Acurite_609TXCDecoderStepCheckDuration,
  37. } Acurite_609TXCDecoderStep;
  38. const SubGhzProtocolDecoder ws_protocol_acurite_609txc_decoder = {
  39. .alloc = ws_protocol_decoder_acurite_609txc_alloc,
  40. .free = ws_protocol_decoder_acurite_609txc_free,
  41. .feed = ws_protocol_decoder_acurite_609txc_feed,
  42. .reset = ws_protocol_decoder_acurite_609txc_reset,
  43. .get_hash_data = ws_protocol_decoder_acurite_609txc_get_hash_data,
  44. .serialize = ws_protocol_decoder_acurite_609txc_serialize,
  45. .deserialize = ws_protocol_decoder_acurite_609txc_deserialize,
  46. .get_string = ws_protocol_decoder_acurite_609txc_get_string,
  47. };
  48. const SubGhzProtocolEncoder ws_protocol_acurite_609txc_encoder = {
  49. .alloc = NULL,
  50. .free = NULL,
  51. .deserialize = NULL,
  52. .stop = NULL,
  53. .yield = NULL,
  54. };
  55. const SubGhzProtocol ws_protocol_acurite_609txc = {
  56. .name = WS_PROTOCOL_ACURITE_609TXC_NAME,
  57. .type = SubGhzProtocolWeatherStation,
  58. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_868 |
  59. SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
  60. .decoder = &ws_protocol_acurite_609txc_decoder,
  61. .encoder = &ws_protocol_acurite_609txc_encoder,
  62. };
  63. void* ws_protocol_decoder_acurite_609txc_alloc(SubGhzEnvironment* environment) {
  64. UNUSED(environment);
  65. WSProtocolDecoderAcurite_609TXC* instance = malloc(sizeof(WSProtocolDecoderAcurite_609TXC));
  66. instance->base.protocol = &ws_protocol_acurite_609txc;
  67. instance->generic.protocol_name = instance->base.protocol->name;
  68. return instance;
  69. }
  70. void ws_protocol_decoder_acurite_609txc_free(void* context) {
  71. furi_assert(context);
  72. WSProtocolDecoderAcurite_609TXC* instance = context;
  73. free(instance);
  74. }
  75. void ws_protocol_decoder_acurite_609txc_reset(void* context) {
  76. furi_assert(context);
  77. WSProtocolDecoderAcurite_609TXC* instance = context;
  78. instance->decoder.parser_step = Acurite_609TXCDecoderStepReset;
  79. }
  80. static bool ws_protocol_acurite_609txc_check(WSProtocolDecoderAcurite_609TXC* instance) {
  81. if(!instance->decoder.decode_data) return false;
  82. uint8_t crc = (uint8_t)(instance->decoder.decode_data >> 32) +
  83. (uint8_t)(instance->decoder.decode_data >> 24) +
  84. (uint8_t)(instance->decoder.decode_data >> 16) +
  85. (uint8_t)(instance->decoder.decode_data >> 8);
  86. return (crc == (instance->decoder.decode_data & 0xFF));
  87. }
  88. /**
  89. * Analysis of received data
  90. * @param instance Pointer to a WSBlockGeneric* instance
  91. */
  92. static void ws_protocol_acurite_609txc_remote_controller(WSBlockGeneric* instance) {
  93. instance->id = (instance->data >> 32) & 0xFF;
  94. instance->battery_low = (instance->data >> 31) & 1;
  95. instance->channel = WS_NO_CHANNEL;
  96. // Temperature in Celsius is encoded as a 12 bit integer value
  97. // multiplied by 10 using the 4th - 6th nybbles (bytes 1 & 2)
  98. // negative values are recovered by sign extend from int16_t.
  99. int16_t temp_raw =
  100. (int16_t)(((instance->data >> 12) & 0xf000) | ((instance->data >> 16) << 4));
  101. instance->temp = (temp_raw >> 4) * 0.1f;
  102. instance->humidity = (instance->data >> 8) & 0xff;
  103. instance->btn = WS_NO_BTN;
  104. }
  105. void ws_protocol_decoder_acurite_609txc_feed(void* context, bool level, uint32_t duration) {
  106. furi_assert(context);
  107. WSProtocolDecoderAcurite_609TXC* instance = context;
  108. switch(instance->decoder.parser_step) {
  109. case Acurite_609TXCDecoderStepReset:
  110. if((!level) && (DURATION_DIFF(duration, ws_protocol_acurite_609txc_const.te_short * 17) <
  111. ws_protocol_acurite_609txc_const.te_delta * 8)) {
  112. //Found syncPrefix
  113. instance->decoder.parser_step = Acurite_609TXCDecoderStepSaveDuration;
  114. instance->decoder.decode_data = 0;
  115. instance->decoder.decode_count_bit = 0;
  116. }
  117. break;
  118. case Acurite_609TXCDecoderStepSaveDuration:
  119. if(level) {
  120. instance->decoder.te_last = duration;
  121. instance->decoder.parser_step = Acurite_609TXCDecoderStepCheckDuration;
  122. } else {
  123. instance->decoder.parser_step = Acurite_609TXCDecoderStepReset;
  124. }
  125. break;
  126. case Acurite_609TXCDecoderStepCheckDuration:
  127. if(!level) {
  128. if(DURATION_DIFF(instance->decoder.te_last, ws_protocol_acurite_609txc_const.te_short) <
  129. ws_protocol_acurite_609txc_const.te_delta) {
  130. if((DURATION_DIFF(duration, ws_protocol_acurite_609txc_const.te_short) <
  131. ws_protocol_acurite_609txc_const.te_delta) ||
  132. (duration > ws_protocol_acurite_609txc_const.te_long * 3)) {
  133. //Found syncPostfix
  134. instance->decoder.parser_step = Acurite_609TXCDecoderStepReset;
  135. if((instance->decoder.decode_count_bit ==
  136. ws_protocol_acurite_609txc_const.min_count_bit_for_found) &&
  137. ws_protocol_acurite_609txc_check(instance)) {
  138. instance->generic.data = instance->decoder.decode_data;
  139. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  140. ws_protocol_acurite_609txc_remote_controller(&instance->generic);
  141. if(instance->base.callback)
  142. instance->base.callback(&instance->base, instance->base.context);
  143. }
  144. instance->decoder.decode_data = 0;
  145. instance->decoder.decode_count_bit = 0;
  146. } else if(
  147. DURATION_DIFF(duration, ws_protocol_acurite_609txc_const.te_long) <
  148. ws_protocol_acurite_609txc_const.te_delta * 2) {
  149. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  150. instance->decoder.parser_step = Acurite_609TXCDecoderStepSaveDuration;
  151. } else if(
  152. DURATION_DIFF(duration, ws_protocol_acurite_609txc_const.te_long * 2) <
  153. ws_protocol_acurite_609txc_const.te_delta * 4) {
  154. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  155. instance->decoder.parser_step = Acurite_609TXCDecoderStepSaveDuration;
  156. } else {
  157. instance->decoder.parser_step = Acurite_609TXCDecoderStepReset;
  158. }
  159. } else {
  160. instance->decoder.parser_step = Acurite_609TXCDecoderStepReset;
  161. }
  162. } else {
  163. instance->decoder.parser_step = Acurite_609TXCDecoderStepReset;
  164. }
  165. break;
  166. }
  167. }
  168. uint8_t ws_protocol_decoder_acurite_609txc_get_hash_data(void* context) {
  169. furi_assert(context);
  170. WSProtocolDecoderAcurite_609TXC* instance = context;
  171. return subghz_protocol_blocks_get_hash_data(
  172. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  173. }
  174. SubGhzProtocolStatus ws_protocol_decoder_acurite_609txc_serialize(
  175. void* context,
  176. FlipperFormat* flipper_format,
  177. SubGhzRadioPreset* preset) {
  178. furi_assert(context);
  179. WSProtocolDecoderAcurite_609TXC* instance = context;
  180. return ws_block_generic_serialize(&instance->generic, flipper_format, preset);
  181. }
  182. SubGhzProtocolStatus
  183. ws_protocol_decoder_acurite_609txc_deserialize(void* context, FlipperFormat* flipper_format) {
  184. furi_assert(context);
  185. WSProtocolDecoderAcurite_609TXC* instance = context;
  186. return ws_block_generic_deserialize_check_count_bit(
  187. &instance->generic,
  188. flipper_format,
  189. ws_protocol_acurite_609txc_const.min_count_bit_for_found);
  190. }
  191. void ws_protocol_decoder_acurite_609txc_get_string(void* context, FuriString* output) {
  192. furi_assert(context);
  193. WSProtocolDecoderAcurite_609TXC* instance = context;
  194. furi_string_printf(
  195. output,
  196. "%s %dbit\r\n"
  197. "Key:0x%lX%08lX\r\n"
  198. "Sn:0x%lX Ch:%d Bat:%d\r\n"
  199. "Temp:%3.1f C Hum:%d%%",
  200. instance->generic.protocol_name,
  201. instance->generic.data_count_bit,
  202. (uint32_t)(instance->generic.data >> 40),
  203. (uint32_t)(instance->generic.data),
  204. instance->generic.id,
  205. instance->generic.channel,
  206. instance->generic.battery_low,
  207. (double)instance->generic.temp,
  208. instance->generic.humidity);
  209. }