acurite_986.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include "acurite_986.h"
  2. #define TAG "WSProtocolAcurite_986"
  3. /*
  4. * Help
  5. * https://github.com/merbanan/rtl_433/blob/5bef4e43133ac4c0e2d18d36f87c52b4f9458453/src/devices/acurite.c#L1644
  6. *
  7. * 0110 0100 | 1010 1011 | 0110 0010 | 0000 0000 | 0111 0110
  8. * tttt tttt | IIII IIII | iiii iiii | nbuu uuuu | cccc cccc
  9. * - t: temperature in °F
  10. * - I: identification (high byte)
  11. * - i: identification (low byte)
  12. * - n: sensor number
  13. * - b: battery low flag to indicate low battery voltage
  14. * - u: unknown
  15. * - c: CRC (CRC-8 poly 0x07, little-endian)
  16. *
  17. * bits are sent and shown above LSB first
  18. * identification changes on battery switch
  19. */
  20. static const SubGhzBlockConst ws_protocol_acurite_986_const = {
  21. .te_short = 800,
  22. .te_long = 1750,
  23. .te_delta = 50,
  24. .min_count_bit_for_found = 40,
  25. };
  26. struct WSProtocolDecoderAcurite_986 {
  27. SubGhzProtocolDecoderBase base;
  28. SubGhzBlockDecoder decoder;
  29. WSBlockGeneric generic;
  30. };
  31. struct WSProtocolEncoderAcurite_986 {
  32. SubGhzProtocolEncoderBase base;
  33. SubGhzProtocolBlockEncoder encoder;
  34. WSBlockGeneric generic;
  35. };
  36. typedef enum {
  37. Acurite_986DecoderStepReset = 0,
  38. Acurite_986DecoderStepSync1,
  39. Acurite_986DecoderStepSync2,
  40. Acurite_986DecoderStepSync3,
  41. Acurite_986DecoderStepSaveDuration,
  42. Acurite_986DecoderStepCheckDuration,
  43. } Acurite_986DecoderStep;
  44. const SubGhzProtocolDecoder ws_protocol_acurite_986_decoder = {
  45. .alloc = ws_protocol_decoder_acurite_986_alloc,
  46. .free = ws_protocol_decoder_acurite_986_free,
  47. .feed = ws_protocol_decoder_acurite_986_feed,
  48. .reset = ws_protocol_decoder_acurite_986_reset,
  49. .get_hash_data = ws_protocol_decoder_acurite_986_get_hash_data,
  50. .serialize = ws_protocol_decoder_acurite_986_serialize,
  51. .deserialize = ws_protocol_decoder_acurite_986_deserialize,
  52. .get_string = ws_protocol_decoder_acurite_986_get_string,
  53. };
  54. const SubGhzProtocolEncoder ws_protocol_acurite_986_encoder = {
  55. .alloc = NULL,
  56. .free = NULL,
  57. .deserialize = NULL,
  58. .stop = NULL,
  59. .yield = NULL,
  60. };
  61. const SubGhzProtocol ws_protocol_acurite_986 = {
  62. .name = WS_PROTOCOL_ACURITE_986_NAME,
  63. .type = SubGhzProtocolWeatherStation,
  64. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_868 |
  65. SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
  66. .decoder = &ws_protocol_acurite_986_decoder,
  67. .encoder = &ws_protocol_acurite_986_encoder,
  68. };
  69. void* ws_protocol_decoder_acurite_986_alloc(SubGhzEnvironment* environment) {
  70. UNUSED(environment);
  71. WSProtocolDecoderAcurite_986* instance = malloc(sizeof(WSProtocolDecoderAcurite_986));
  72. instance->base.protocol = &ws_protocol_acurite_986;
  73. instance->generic.protocol_name = instance->base.protocol->name;
  74. return instance;
  75. }
  76. void ws_protocol_decoder_acurite_986_free(void* context) {
  77. furi_assert(context);
  78. WSProtocolDecoderAcurite_986* instance = context;
  79. free(instance);
  80. }
  81. void ws_protocol_decoder_acurite_986_reset(void* context) {
  82. furi_assert(context);
  83. WSProtocolDecoderAcurite_986* instance = context;
  84. instance->decoder.parser_step = Acurite_986DecoderStepReset;
  85. }
  86. static bool ws_protocol_acurite_986_check(WSProtocolDecoderAcurite_986* instance) {
  87. if(!instance->decoder.decode_data) return false;
  88. uint8_t msg[] = {
  89. instance->decoder.decode_data >> 32,
  90. instance->decoder.decode_data >> 24,
  91. instance->decoder.decode_data >> 16,
  92. instance->decoder.decode_data >> 8};
  93. uint8_t crc = subghz_protocol_blocks_crc8(msg, 4, 0x07, 0x00);
  94. return (crc == (instance->decoder.decode_data & 0xFF));
  95. }
  96. /**
  97. * Analysis of received data
  98. * @param instance Pointer to a WSBlockGeneric* instance
  99. */
  100. static void ws_protocol_acurite_986_remote_controller(WSBlockGeneric* instance) {
  101. int temp;
  102. instance->id = subghz_protocol_blocks_reverse_key(instance->data >> 24, 8);
  103. instance->id = (instance->id << 8) |
  104. subghz_protocol_blocks_reverse_key(instance->data >> 16, 8);
  105. instance->battery_low = (instance->data >> 14) & 1;
  106. instance->channel = ((instance->data >> 15) & 1) + 1;
  107. temp = subghz_protocol_blocks_reverse_key(instance->data >> 32, 8);
  108. if(temp & 0x80) {
  109. temp = -(temp & 0x7F);
  110. }
  111. instance->temp = locale_fahrenheit_to_celsius((float)temp);
  112. instance->btn = WS_NO_BTN;
  113. instance->humidity = WS_NO_HUMIDITY;
  114. }
  115. void ws_protocol_decoder_acurite_986_feed(void* context, bool level, uint32_t duration) {
  116. furi_assert(context);
  117. WSProtocolDecoderAcurite_986* instance = context;
  118. switch(instance->decoder.parser_step) {
  119. case Acurite_986DecoderStepReset:
  120. if((!level) && (DURATION_DIFF(duration, ws_protocol_acurite_986_const.te_long) <
  121. ws_protocol_acurite_986_const.te_delta * 15)) {
  122. //Found 1st sync bit
  123. instance->decoder.parser_step = Acurite_986DecoderStepSync1;
  124. instance->decoder.decode_data = 0;
  125. instance->decoder.decode_count_bit = 0;
  126. }
  127. break;
  128. case Acurite_986DecoderStepSync1:
  129. if(DURATION_DIFF(duration, ws_protocol_acurite_986_const.te_long) <
  130. ws_protocol_acurite_986_const.te_delta * 15) {
  131. if(!level) {
  132. instance->decoder.parser_step = Acurite_986DecoderStepSync2;
  133. }
  134. } else {
  135. instance->decoder.parser_step = Acurite_986DecoderStepReset;
  136. }
  137. break;
  138. case Acurite_986DecoderStepSync2:
  139. if(DURATION_DIFF(duration, ws_protocol_acurite_986_const.te_long) <
  140. ws_protocol_acurite_986_const.te_delta * 15) {
  141. if(!level) {
  142. instance->decoder.parser_step = Acurite_986DecoderStepSync3;
  143. }
  144. } else {
  145. instance->decoder.parser_step = Acurite_986DecoderStepReset;
  146. }
  147. break;
  148. case Acurite_986DecoderStepSync3:
  149. if(DURATION_DIFF(duration, ws_protocol_acurite_986_const.te_long) <
  150. ws_protocol_acurite_986_const.te_delta * 15) {
  151. if(!level) {
  152. instance->decoder.parser_step = Acurite_986DecoderStepSaveDuration;
  153. }
  154. } else {
  155. instance->decoder.parser_step = Acurite_986DecoderStepReset;
  156. }
  157. break;
  158. case Acurite_986DecoderStepSaveDuration:
  159. if(level) {
  160. instance->decoder.te_last = duration;
  161. instance->decoder.parser_step = Acurite_986DecoderStepCheckDuration;
  162. } else {
  163. instance->decoder.parser_step = Acurite_986DecoderStepReset;
  164. }
  165. break;
  166. case Acurite_986DecoderStepCheckDuration:
  167. if(!level) {
  168. if(DURATION_DIFF(duration, ws_protocol_acurite_986_const.te_short) <
  169. ws_protocol_acurite_986_const.te_delta * 10) {
  170. if(duration < ws_protocol_acurite_986_const.te_short) {
  171. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  172. instance->decoder.parser_step = Acurite_986DecoderStepSaveDuration;
  173. } else {
  174. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  175. instance->decoder.parser_step = Acurite_986DecoderStepSaveDuration;
  176. }
  177. } else {
  178. //Found syncPostfix
  179. instance->decoder.parser_step = Acurite_986DecoderStepReset;
  180. if((instance->decoder.decode_count_bit ==
  181. ws_protocol_acurite_986_const.min_count_bit_for_found) &&
  182. ws_protocol_acurite_986_check(instance)) {
  183. instance->generic.data = instance->decoder.decode_data;
  184. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  185. ws_protocol_acurite_986_remote_controller(&instance->generic);
  186. if(instance->base.callback)
  187. instance->base.callback(&instance->base, instance->base.context);
  188. }
  189. instance->decoder.decode_data = 0;
  190. instance->decoder.decode_count_bit = 0;
  191. }
  192. } else {
  193. instance->decoder.parser_step = Acurite_986DecoderStepReset;
  194. }
  195. break;
  196. }
  197. }
  198. uint8_t ws_protocol_decoder_acurite_986_get_hash_data(void* context) {
  199. furi_assert(context);
  200. WSProtocolDecoderAcurite_986* instance = context;
  201. return subghz_protocol_blocks_get_hash_data(
  202. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  203. }
  204. SubGhzProtocolStatus ws_protocol_decoder_acurite_986_serialize(
  205. void* context,
  206. FlipperFormat* flipper_format,
  207. SubGhzRadioPreset* preset) {
  208. furi_assert(context);
  209. WSProtocolDecoderAcurite_986* instance = context;
  210. return ws_block_generic_serialize(&instance->generic, flipper_format, preset);
  211. }
  212. SubGhzProtocolStatus
  213. ws_protocol_decoder_acurite_986_deserialize(void* context, FlipperFormat* flipper_format) {
  214. furi_assert(context);
  215. WSProtocolDecoderAcurite_986* instance = context;
  216. return ws_block_generic_deserialize_check_count_bit(
  217. &instance->generic, flipper_format, ws_protocol_acurite_986_const.min_count_bit_for_found);
  218. }
  219. void ws_protocol_decoder_acurite_986_get_string(void* context, FuriString* output) {
  220. furi_assert(context);
  221. WSProtocolDecoderAcurite_986* instance = context;
  222. furi_string_printf(
  223. output,
  224. "%s %dbit\r\n"
  225. "Key:0x%lX%08lX\r\n"
  226. "Sn:0x%lX Ch:%d Bat:%d\r\n"
  227. "Temp:%3.1f C Hum:%d%%",
  228. instance->generic.protocol_name,
  229. instance->generic.data_count_bit,
  230. (uint32_t)(instance->generic.data >> 32),
  231. (uint32_t)(instance->generic.data),
  232. instance->generic.id,
  233. instance->generic.channel,
  234. instance->generic.battery_low,
  235. (double)instance->generic.temp,
  236. instance->generic.humidity);
  237. }