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) | subghz_protocol_blocks_reverse_key(instance->data >> 16, 8);
  104. instance->battery_low = (instance->data >> 14) & 1;
  105. instance->channel = ((instance->data >> 15) & 1) + 1;
  106. temp = subghz_protocol_blocks_reverse_key(instance->data >> 32, 8);
  107. if(temp & 0x80) {
  108. temp = -(temp & 0x7F);
  109. }
  110. instance->temp = locale_fahrenheit_to_celsius((float)temp);
  111. instance->btn = WS_NO_BTN;
  112. instance->humidity = WS_NO_HUMIDITY;
  113. }
  114. void ws_protocol_decoder_acurite_986_feed(void* context, bool level, uint32_t duration) {
  115. furi_assert(context);
  116. WSProtocolDecoderAcurite_986* instance = context;
  117. switch(instance->decoder.parser_step) {
  118. case Acurite_986DecoderStepReset:
  119. if((!level) && (DURATION_DIFF(duration, ws_protocol_acurite_986_const.te_long) <
  120. ws_protocol_acurite_986_const.te_delta * 15)) {
  121. //Found 1st sync bit
  122. instance->decoder.parser_step = Acurite_986DecoderStepSync1;
  123. instance->decoder.decode_data = 0;
  124. instance->decoder.decode_count_bit = 0;
  125. }
  126. break;
  127. case Acurite_986DecoderStepSync1:
  128. if(DURATION_DIFF(duration, ws_protocol_acurite_986_const.te_long) <
  129. ws_protocol_acurite_986_const.te_delta * 15) {
  130. if(!level) {
  131. instance->decoder.parser_step = Acurite_986DecoderStepSync2;
  132. }
  133. } else {
  134. instance->decoder.parser_step = Acurite_986DecoderStepReset;
  135. }
  136. break;
  137. case Acurite_986DecoderStepSync2:
  138. if(DURATION_DIFF(duration, ws_protocol_acurite_986_const.te_long) <
  139. ws_protocol_acurite_986_const.te_delta * 15) {
  140. if(!level) {
  141. instance->decoder.parser_step = Acurite_986DecoderStepSync3;
  142. }
  143. } else {
  144. instance->decoder.parser_step = Acurite_986DecoderStepReset;
  145. }
  146. break;
  147. case Acurite_986DecoderStepSync3:
  148. if(DURATION_DIFF(duration, ws_protocol_acurite_986_const.te_long) <
  149. ws_protocol_acurite_986_const.te_delta * 15) {
  150. if(!level) {
  151. instance->decoder.parser_step = Acurite_986DecoderStepSaveDuration;
  152. }
  153. } else {
  154. instance->decoder.parser_step = Acurite_986DecoderStepReset;
  155. }
  156. break;
  157. case Acurite_986DecoderStepSaveDuration:
  158. if(level) {
  159. instance->decoder.te_last = duration;
  160. instance->decoder.parser_step = Acurite_986DecoderStepCheckDuration;
  161. } else {
  162. instance->decoder.parser_step = Acurite_986DecoderStepReset;
  163. }
  164. break;
  165. case Acurite_986DecoderStepCheckDuration:
  166. if(!level) {
  167. if(DURATION_DIFF(duration, ws_protocol_acurite_986_const.te_short) <
  168. ws_protocol_acurite_986_const.te_delta * 10) {
  169. if(duration < ws_protocol_acurite_986_const.te_short) {
  170. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  171. instance->decoder.parser_step = Acurite_986DecoderStepSaveDuration;
  172. } else {
  173. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  174. instance->decoder.parser_step = Acurite_986DecoderStepSaveDuration;
  175. }
  176. } else {
  177. //Found syncPostfix
  178. instance->decoder.parser_step = Acurite_986DecoderStepReset;
  179. if((instance->decoder.decode_count_bit == ws_protocol_acurite_986_const.min_count_bit_for_found) &&
  180. ws_protocol_acurite_986_check(instance)) {
  181. instance->generic.data = instance->decoder.decode_data;
  182. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  183. ws_protocol_acurite_986_remote_controller(&instance->generic);
  184. if(instance->base.callback)
  185. instance->base.callback(&instance->base, instance->base.context);
  186. }
  187. instance->decoder.decode_data = 0;
  188. instance->decoder.decode_count_bit = 0;
  189. }
  190. } else {
  191. instance->decoder.parser_step = Acurite_986DecoderStepReset;
  192. }
  193. break;
  194. }
  195. }
  196. uint8_t ws_protocol_decoder_acurite_986_get_hash_data(void* context) {
  197. furi_assert(context);
  198. WSProtocolDecoderAcurite_986* instance = context;
  199. return subghz_protocol_blocks_get_hash_data(
  200. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  201. }
  202. SubGhzProtocolStatus ws_protocol_decoder_acurite_986_serialize(
  203. void* context,
  204. FlipperFormat* flipper_format,
  205. SubGhzRadioPreset* preset) {
  206. furi_assert(context);
  207. WSProtocolDecoderAcurite_986* instance = context;
  208. return ws_block_generic_serialize(&instance->generic, flipper_format, preset);
  209. }
  210. SubGhzProtocolStatus
  211. ws_protocol_decoder_acurite_986_deserialize(void* context, FlipperFormat* flipper_format) {
  212. furi_assert(context);
  213. WSProtocolDecoderAcurite_986* instance = context;
  214. return ws_block_generic_deserialize_check_count_bit(
  215. &instance->generic,
  216. flipper_format,
  217. 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. }