lacrosse_tx141thbv2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #include "lacrosse_tx141thbv2.h"
  2. #define TAG "WSProtocolLaCrosse_TX141THBv2"
  3. #define LACROSSE_TX141TH_BV2_BIT_COUNT 41
  4. /*
  5. * Help
  6. * https://github.com/merbanan/rtl_433/blob/master/src/devices/lacrosse_tx141x.c
  7. *
  8. * iiii iiii | bkcc tttt | tttt tttt | hhhh hhhh | cccc cccc | u - 41 bit
  9. * or
  10. * iiii iiii | bkcc tttt | tttt tttt | hhhh hhhh | cccc cccc | -40 bit
  11. * - i: identification; changes on battery switch
  12. * - c: lfsr_digest8_reflect;
  13. * - u: unknown;
  14. * - b: battery low; flag to indicate low battery voltage
  15. * - h: Humidity;
  16. * - t: Temperature; in °F as binary number with one decimal place + 50 °F offset
  17. * - n: Channel; Channel number 1 - 3
  18. */
  19. static const SubGhzBlockConst ws_protocol_lacrosse_tx141thbv2_const = {
  20. .te_short = 208,
  21. .te_long = 417,
  22. .te_delta = 120,
  23. .min_count_bit_for_found = 40,
  24. };
  25. struct WSProtocolDecoderLaCrosse_TX141THBv2 {
  26. SubGhzProtocolDecoderBase base;
  27. SubGhzBlockDecoder decoder;
  28. WSBlockGeneric generic;
  29. uint16_t header_count;
  30. };
  31. struct WSProtocolEncoderLaCrosse_TX141THBv2 {
  32. SubGhzProtocolEncoderBase base;
  33. SubGhzProtocolBlockEncoder encoder;
  34. WSBlockGeneric generic;
  35. };
  36. typedef enum {
  37. LaCrosse_TX141THBv2DecoderStepReset = 0,
  38. LaCrosse_TX141THBv2DecoderStepCheckPreambule,
  39. LaCrosse_TX141THBv2DecoderStepSaveDuration,
  40. LaCrosse_TX141THBv2DecoderStepCheckDuration,
  41. } LaCrosse_TX141THBv2DecoderStep;
  42. const SubGhzProtocolDecoder ws_protocol_lacrosse_tx141thbv2_decoder = {
  43. .alloc = ws_protocol_decoder_lacrosse_tx141thbv2_alloc,
  44. .free = ws_protocol_decoder_lacrosse_tx141thbv2_free,
  45. .feed = ws_protocol_decoder_lacrosse_tx141thbv2_feed,
  46. .reset = ws_protocol_decoder_lacrosse_tx141thbv2_reset,
  47. .get_hash_data = ws_protocol_decoder_lacrosse_tx141thbv2_get_hash_data,
  48. .serialize = ws_protocol_decoder_lacrosse_tx141thbv2_serialize,
  49. .deserialize = ws_protocol_decoder_lacrosse_tx141thbv2_deserialize,
  50. .get_string = ws_protocol_decoder_lacrosse_tx141thbv2_get_string,
  51. };
  52. const SubGhzProtocolEncoder ws_protocol_lacrosse_tx141thbv2_encoder = {
  53. .alloc = NULL,
  54. .free = NULL,
  55. .deserialize = NULL,
  56. .stop = NULL,
  57. .yield = NULL,
  58. };
  59. const SubGhzProtocol ws_protocol_lacrosse_tx141thbv2 = {
  60. .name = WS_PROTOCOL_LACROSSE_TX141THBV2_NAME,
  61. .type = SubGhzProtocolWeatherStation,
  62. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_868 |
  63. SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
  64. .decoder = &ws_protocol_lacrosse_tx141thbv2_decoder,
  65. .encoder = &ws_protocol_lacrosse_tx141thbv2_encoder,
  66. };
  67. void* ws_protocol_decoder_lacrosse_tx141thbv2_alloc(SubGhzEnvironment* environment) {
  68. UNUSED(environment);
  69. WSProtocolDecoderLaCrosse_TX141THBv2* instance =
  70. malloc(sizeof(WSProtocolDecoderLaCrosse_TX141THBv2));
  71. instance->base.protocol = &ws_protocol_lacrosse_tx141thbv2;
  72. instance->generic.protocol_name = instance->base.protocol->name;
  73. return instance;
  74. }
  75. void ws_protocol_decoder_lacrosse_tx141thbv2_free(void* context) {
  76. furi_assert(context);
  77. WSProtocolDecoderLaCrosse_TX141THBv2* instance = context;
  78. free(instance);
  79. }
  80. void ws_protocol_decoder_lacrosse_tx141thbv2_reset(void* context) {
  81. furi_assert(context);
  82. WSProtocolDecoderLaCrosse_TX141THBv2* instance = context;
  83. instance->decoder.parser_step = LaCrosse_TX141THBv2DecoderStepReset;
  84. }
  85. static bool
  86. ws_protocol_lacrosse_tx141thbv2_check_crc(WSProtocolDecoderLaCrosse_TX141THBv2* instance) {
  87. if(!instance->decoder.decode_data) return false;
  88. uint64_t data = instance->decoder.decode_data;
  89. if(instance->decoder.decode_count_bit == LACROSSE_TX141TH_BV2_BIT_COUNT) {
  90. data >>= 1;
  91. }
  92. uint8_t msg[] = {data >> 32, data >> 24, data >> 16, data >> 8};
  93. uint8_t crc = subghz_protocol_blocks_lfsr_digest8_reflect(msg, 4, 0x31, 0xF4);
  94. return (crc == (data & 0xFF));
  95. }
  96. /**
  97. * Analysis of received data
  98. * @param instance Pointer to a WSBlockGeneric* instance
  99. */
  100. static void ws_protocol_lacrosse_tx141thbv2_remote_controller(WSBlockGeneric* instance) {
  101. uint64_t data = instance->data;
  102. if(instance->data_count_bit == LACROSSE_TX141TH_BV2_BIT_COUNT) {
  103. data >>= 1;
  104. }
  105. instance->id = data >> 32;
  106. instance->battery_low = (data >> 31) & 1;
  107. instance->btn = (data >> 30) & 1;
  108. instance->channel = ((data >> 28) & 0x03) + 1;
  109. instance->temp = ((float)((data >> 16) & 0x0FFF) - 500.0f) / 10.0f;
  110. instance->humidity = (data >> 8) & 0xFF;
  111. }
  112. /**
  113. * Analysis of received data
  114. * @param instance Pointer to a WSBlockGeneric* instance
  115. */
  116. static bool ws_protocol_decoder_lacrosse_tx141thbv2_add_bit(
  117. WSProtocolDecoderLaCrosse_TX141THBv2* instance,
  118. uint32_t te_last,
  119. uint32_t te_current) {
  120. furi_assert(instance);
  121. bool ret = false;
  122. if(DURATION_DIFF(
  123. te_last + te_current,
  124. ws_protocol_lacrosse_tx141thbv2_const.te_short +
  125. ws_protocol_lacrosse_tx141thbv2_const.te_long) <
  126. ws_protocol_lacrosse_tx141thbv2_const.te_delta * 2) {
  127. if(te_last > te_current) {
  128. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  129. } else {
  130. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  131. }
  132. ret = true;
  133. }
  134. return ret;
  135. }
  136. void ws_protocol_decoder_lacrosse_tx141thbv2_feed(void* context, bool level, uint32_t duration) {
  137. furi_assert(context);
  138. WSProtocolDecoderLaCrosse_TX141THBv2* instance = context;
  139. switch(instance->decoder.parser_step) {
  140. case LaCrosse_TX141THBv2DecoderStepReset:
  141. if((level) &&
  142. (DURATION_DIFF(duration, ws_protocol_lacrosse_tx141thbv2_const.te_short * 4) <
  143. ws_protocol_lacrosse_tx141thbv2_const.te_delta * 2)) {
  144. instance->decoder.parser_step = LaCrosse_TX141THBv2DecoderStepCheckPreambule;
  145. instance->decoder.te_last = duration;
  146. instance->header_count = 0;
  147. }
  148. break;
  149. case LaCrosse_TX141THBv2DecoderStepCheckPreambule:
  150. if(level) {
  151. instance->decoder.te_last = duration;
  152. } else {
  153. if((DURATION_DIFF(
  154. instance->decoder.te_last,
  155. ws_protocol_lacrosse_tx141thbv2_const.te_short * 4) <
  156. ws_protocol_lacrosse_tx141thbv2_const.te_delta * 2) &&
  157. (DURATION_DIFF(duration, ws_protocol_lacrosse_tx141thbv2_const.te_short * 4) <
  158. ws_protocol_lacrosse_tx141thbv2_const.te_delta * 2)) {
  159. //Found preambule
  160. instance->header_count++;
  161. } else if(instance->header_count == 4) {
  162. if(ws_protocol_decoder_lacrosse_tx141thbv2_add_bit(
  163. instance, instance->decoder.te_last, duration)) {
  164. instance->decoder.decode_data = instance->decoder.decode_data & 1;
  165. instance->decoder.decode_count_bit = 1;
  166. instance->decoder.parser_step = LaCrosse_TX141THBv2DecoderStepSaveDuration;
  167. } else {
  168. instance->decoder.parser_step = LaCrosse_TX141THBv2DecoderStepReset;
  169. }
  170. } else {
  171. instance->decoder.parser_step = LaCrosse_TX141THBv2DecoderStepReset;
  172. }
  173. }
  174. break;
  175. case LaCrosse_TX141THBv2DecoderStepSaveDuration:
  176. if(level) {
  177. instance->decoder.te_last = duration;
  178. instance->decoder.parser_step = LaCrosse_TX141THBv2DecoderStepCheckDuration;
  179. } else {
  180. instance->decoder.parser_step = LaCrosse_TX141THBv2DecoderStepReset;
  181. }
  182. break;
  183. case LaCrosse_TX141THBv2DecoderStepCheckDuration:
  184. if(!level) {
  185. if(((DURATION_DIFF(
  186. instance->decoder.te_last,
  187. ws_protocol_lacrosse_tx141thbv2_const.te_short * 3) <
  188. ws_protocol_lacrosse_tx141thbv2_const.te_delta * 2) &&
  189. (DURATION_DIFF(duration, ws_protocol_lacrosse_tx141thbv2_const.te_short * 4) <
  190. ws_protocol_lacrosse_tx141thbv2_const.te_delta * 2))) {
  191. if((instance->decoder.decode_count_bit ==
  192. ws_protocol_lacrosse_tx141thbv2_const.min_count_bit_for_found) ||
  193. (instance->decoder.decode_count_bit == LACROSSE_TX141TH_BV2_BIT_COUNT)) {
  194. if(ws_protocol_lacrosse_tx141thbv2_check_crc(instance)) {
  195. instance->generic.data = instance->decoder.decode_data;
  196. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  197. ws_protocol_lacrosse_tx141thbv2_remote_controller(&instance->generic);
  198. if(instance->base.callback)
  199. instance->base.callback(&instance->base, instance->base.context);
  200. }
  201. instance->decoder.decode_data = 0;
  202. instance->decoder.decode_count_bit = 0;
  203. instance->header_count = 1;
  204. instance->decoder.parser_step = LaCrosse_TX141THBv2DecoderStepCheckPreambule;
  205. break;
  206. }
  207. } else if(ws_protocol_decoder_lacrosse_tx141thbv2_add_bit(
  208. instance, instance->decoder.te_last, duration)) {
  209. instance->decoder.parser_step = LaCrosse_TX141THBv2DecoderStepSaveDuration;
  210. } else {
  211. instance->decoder.parser_step = LaCrosse_TX141THBv2DecoderStepReset;
  212. }
  213. } else {
  214. instance->decoder.parser_step = LaCrosse_TX141THBv2DecoderStepReset;
  215. }
  216. break;
  217. }
  218. }
  219. uint8_t ws_protocol_decoder_lacrosse_tx141thbv2_get_hash_data(void* context) {
  220. furi_assert(context);
  221. WSProtocolDecoderLaCrosse_TX141THBv2* instance = context;
  222. return subghz_protocol_blocks_get_hash_data(
  223. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  224. }
  225. SubGhzProtocolStatus ws_protocol_decoder_lacrosse_tx141thbv2_serialize(
  226. void* context,
  227. FlipperFormat* flipper_format,
  228. SubGhzRadioPreset* preset) {
  229. furi_assert(context);
  230. WSProtocolDecoderLaCrosse_TX141THBv2* instance = context;
  231. return ws_block_generic_serialize(&instance->generic, flipper_format, preset);
  232. }
  233. SubGhzProtocolStatus ws_protocol_decoder_lacrosse_tx141thbv2_deserialize(
  234. void* context,
  235. FlipperFormat* flipper_format) {
  236. furi_assert(context);
  237. WSProtocolDecoderLaCrosse_TX141THBv2* instance = context;
  238. return ws_block_generic_deserialize_check_count_bit(
  239. &instance->generic,
  240. flipper_format,
  241. ws_protocol_lacrosse_tx141thbv2_const.min_count_bit_for_found);
  242. }
  243. void ws_protocol_decoder_lacrosse_tx141thbv2_get_string(void* context, FuriString* output) {
  244. furi_assert(context);
  245. WSProtocolDecoderLaCrosse_TX141THBv2* instance = context;
  246. furi_string_printf(
  247. output,
  248. "%s %dbit\r\n"
  249. "Key:0x%lX%08lX\r\n"
  250. "Sn:0x%lX Ch:%d Bat:%d\r\n"
  251. "Temp:%3.1f C Hum:%d%%",
  252. instance->generic.protocol_name,
  253. instance->generic.data_count_bit,
  254. (uint32_t)(instance->generic.data >> 32),
  255. (uint32_t)(instance->generic.data),
  256. instance->generic.id,
  257. instance->generic.channel,
  258. instance->generic.battery_low,
  259. (double)instance->generic.temp,
  260. instance->generic.humidity);
  261. }