nexus_th.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #include "nexus_th.h"
  2. #define TAG "WSProtocolNexus_TH"
  3. /*
  4. * Help
  5. * https://github.com/merbanan/rtl_433/blob/master/src/devices/nexus.c
  6. *
  7. * Nexus sensor protocol with ID, temperature and optional humidity
  8. * also FreeTec (Pearl) NC-7345 sensors for FreeTec Weatherstation NC-7344,
  9. * also infactory/FreeTec (Pearl) NX-3980 sensors for infactory/FreeTec NX-3974 station,
  10. * also Solight TE82S sensors for Solight TE76/TE82/TE83/TE84 stations,
  11. * also TFA 30.3209.02 temperature/humidity sensor.
  12. * The sensor sends 36 bits 12 times,
  13. * the packets are ppm modulated (distance coding) with a pulse of ~500 us
  14. * followed by a short gap of ~1000 us for a 0 bit or a long ~2000 us gap for a
  15. * 1 bit, the sync gap is ~4000 us.
  16. * The data is grouped in 9 nibbles:
  17. * [id0] [id1] [flags] [temp0] [temp1] [temp2] [const] [humi0] [humi1]
  18. * - The 8-bit id changes when the battery is changed in the sensor.
  19. * - flags are 4 bits B 0 C C, where B is the battery status: 1=OK, 0=LOW
  20. * - and CC is the channel: 0=CH1, 1=CH2, 2=CH3
  21. * - temp is 12 bit signed scaled by 10
  22. * - const is always 1111 (0x0F)
  23. * - humidity is 8 bits
  24. * The sensors can be bought at Clas Ohlsen (Nexus) and Pearl (infactory/FreeTec).
  25. *
  26. */
  27. #define NEXUS_TH_CONST_DATA 0b1111
  28. static const SubGhzBlockConst ws_protocol_nexus_th_const = {
  29. .te_short = 500,
  30. .te_long = 2000,
  31. .te_delta = 150,
  32. .min_count_bit_for_found = 36,
  33. };
  34. struct WSProtocolDecoderNexus_TH {
  35. SubGhzProtocolDecoderBase base;
  36. SubGhzBlockDecoder decoder;
  37. WSBlockGeneric generic;
  38. };
  39. struct WSProtocolEncoderNexus_TH {
  40. SubGhzProtocolEncoderBase base;
  41. SubGhzProtocolBlockEncoder encoder;
  42. WSBlockGeneric generic;
  43. };
  44. typedef enum {
  45. Nexus_THDecoderStepReset = 0,
  46. Nexus_THDecoderStepSaveDuration,
  47. Nexus_THDecoderStepCheckDuration,
  48. } Nexus_THDecoderStep;
  49. const SubGhzProtocolDecoder ws_protocol_nexus_th_decoder = {
  50. .alloc = ws_protocol_decoder_nexus_th_alloc,
  51. .free = ws_protocol_decoder_nexus_th_free,
  52. .feed = ws_protocol_decoder_nexus_th_feed,
  53. .reset = ws_protocol_decoder_nexus_th_reset,
  54. .get_hash_data = ws_protocol_decoder_nexus_th_get_hash_data,
  55. .serialize = ws_protocol_decoder_nexus_th_serialize,
  56. .deserialize = ws_protocol_decoder_nexus_th_deserialize,
  57. .get_string = ws_protocol_decoder_nexus_th_get_string,
  58. };
  59. const SubGhzProtocolEncoder ws_protocol_nexus_th_encoder = {
  60. .alloc = NULL,
  61. .free = NULL,
  62. .deserialize = NULL,
  63. .stop = NULL,
  64. .yield = NULL,
  65. };
  66. const SubGhzProtocol ws_protocol_nexus_th = {
  67. .name = WS_PROTOCOL_NEXUS_TH_NAME,
  68. .type = SubGhzProtocolWeatherStation,
  69. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_868 |
  70. SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
  71. .decoder = &ws_protocol_nexus_th_decoder,
  72. .encoder = &ws_protocol_nexus_th_encoder,
  73. };
  74. void* ws_protocol_decoder_nexus_th_alloc(SubGhzEnvironment* environment) {
  75. UNUSED(environment);
  76. WSProtocolDecoderNexus_TH* instance = malloc(sizeof(WSProtocolDecoderNexus_TH));
  77. instance->base.protocol = &ws_protocol_nexus_th;
  78. instance->generic.protocol_name = instance->base.protocol->name;
  79. return instance;
  80. }
  81. void ws_protocol_decoder_nexus_th_free(void* context) {
  82. furi_assert(context);
  83. WSProtocolDecoderNexus_TH* instance = context;
  84. free(instance);
  85. }
  86. void ws_protocol_decoder_nexus_th_reset(void* context) {
  87. furi_assert(context);
  88. WSProtocolDecoderNexus_TH* instance = context;
  89. instance->decoder.parser_step = Nexus_THDecoderStepReset;
  90. }
  91. static bool ws_protocol_nexus_th_check(WSProtocolDecoderNexus_TH* instance) {
  92. uint8_t type = (instance->decoder.decode_data >> 8) & 0x0F;
  93. if((type == NEXUS_TH_CONST_DATA) && ((instance->decoder.decode_data >> 4) != 0xffffffff)) {
  94. return true;
  95. } else {
  96. return false;
  97. }
  98. return true;
  99. }
  100. /**
  101. * Analysis of received data
  102. * @param instance Pointer to a WSBlockGeneric* instance
  103. */
  104. static void ws_protocol_nexus_th_remote_controller(WSBlockGeneric* instance) {
  105. instance->id = (instance->data >> 28) & 0xFF;
  106. instance->battery_low = !((instance->data >> 27) & 1);
  107. instance->channel = ((instance->data >> 24) & 0x03) + 1;
  108. instance->btn = WS_NO_BTN;
  109. if(!((instance->data >> 23) & 1)) {
  110. instance->temp = (float)((instance->data >> 12) & 0x07FF) / 10.0f;
  111. } else {
  112. instance->temp = (float)((~(instance->data >> 12) & 0x07FF) + 1) / -10.0f;
  113. }
  114. instance->humidity = instance->data & 0xFF;
  115. }
  116. void ws_protocol_decoder_nexus_th_feed(void* context, bool level, uint32_t duration) {
  117. furi_assert(context);
  118. WSProtocolDecoderNexus_TH* instance = context;
  119. switch(instance->decoder.parser_step) {
  120. case Nexus_THDecoderStepReset:
  121. if((!level) && (DURATION_DIFF(duration, ws_protocol_nexus_th_const.te_short * 8) <
  122. ws_protocol_nexus_th_const.te_delta * 4)) {
  123. //Found sync
  124. instance->decoder.parser_step = Nexus_THDecoderStepSaveDuration;
  125. instance->decoder.decode_data = 0;
  126. instance->decoder.decode_count_bit = 0;
  127. }
  128. break;
  129. case Nexus_THDecoderStepSaveDuration:
  130. if(level) {
  131. instance->decoder.te_last = duration;
  132. instance->decoder.parser_step = Nexus_THDecoderStepCheckDuration;
  133. } else {
  134. instance->decoder.parser_step = Nexus_THDecoderStepReset;
  135. }
  136. break;
  137. case Nexus_THDecoderStepCheckDuration:
  138. if(!level) {
  139. if(DURATION_DIFF(duration, ws_protocol_nexus_th_const.te_short * 8) <
  140. ws_protocol_nexus_th_const.te_delta * 4) {
  141. //Found sync
  142. instance->decoder.parser_step = Nexus_THDecoderStepReset;
  143. if((instance->decoder.decode_count_bit ==
  144. ws_protocol_nexus_th_const.min_count_bit_for_found) &&
  145. ws_protocol_nexus_th_check(instance)) {
  146. instance->generic.data = instance->decoder.decode_data;
  147. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  148. ws_protocol_nexus_th_remote_controller(&instance->generic);
  149. if(instance->base.callback)
  150. instance->base.callback(&instance->base, instance->base.context);
  151. instance->decoder.parser_step = Nexus_THDecoderStepCheckDuration;
  152. }
  153. instance->decoder.decode_data = 0;
  154. instance->decoder.decode_count_bit = 0;
  155. break;
  156. } else if(
  157. (DURATION_DIFF(instance->decoder.te_last, ws_protocol_nexus_th_const.te_short) <
  158. ws_protocol_nexus_th_const.te_delta) &&
  159. (DURATION_DIFF(duration, ws_protocol_nexus_th_const.te_short * 2) <
  160. ws_protocol_nexus_th_const.te_delta * 2)) {
  161. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  162. instance->decoder.parser_step = Nexus_THDecoderStepSaveDuration;
  163. } else if(
  164. (DURATION_DIFF(instance->decoder.te_last, ws_protocol_nexus_th_const.te_short) <
  165. ws_protocol_nexus_th_const.te_delta) &&
  166. (DURATION_DIFF(duration, ws_protocol_nexus_th_const.te_short * 4) <
  167. ws_protocol_nexus_th_const.te_delta * 4)) {
  168. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  169. instance->decoder.parser_step = Nexus_THDecoderStepSaveDuration;
  170. } else {
  171. instance->decoder.parser_step = Nexus_THDecoderStepReset;
  172. }
  173. } else {
  174. instance->decoder.parser_step = Nexus_THDecoderStepReset;
  175. }
  176. break;
  177. }
  178. }
  179. uint8_t ws_protocol_decoder_nexus_th_get_hash_data(void* context) {
  180. furi_assert(context);
  181. WSProtocolDecoderNexus_TH* instance = context;
  182. return subghz_protocol_blocks_get_hash_data(
  183. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  184. }
  185. bool ws_protocol_decoder_nexus_th_serialize(
  186. void* context,
  187. FlipperFormat* flipper_format,
  188. SubGhzRadioPreset* preset) {
  189. furi_assert(context);
  190. WSProtocolDecoderNexus_TH* instance = context;
  191. return ws_block_generic_serialize(&instance->generic, flipper_format, preset);
  192. }
  193. bool ws_protocol_decoder_nexus_th_deserialize(void* context, FlipperFormat* flipper_format) {
  194. furi_assert(context);
  195. WSProtocolDecoderNexus_TH* instance = context;
  196. bool ret = false;
  197. do {
  198. if(!ws_block_generic_deserialize(&instance->generic, flipper_format)) {
  199. break;
  200. }
  201. if(instance->generic.data_count_bit !=
  202. ws_protocol_nexus_th_const.min_count_bit_for_found) {
  203. FURI_LOG_E(TAG, "Wrong number of bits in key");
  204. break;
  205. }
  206. ret = true;
  207. } while(false);
  208. return ret;
  209. }
  210. void ws_protocol_decoder_nexus_th_get_string(void* context, FuriString* output) {
  211. furi_assert(context);
  212. WSProtocolDecoderNexus_TH* instance = context;
  213. furi_string_printf(
  214. output,
  215. "%s %dbit\r\n"
  216. "Key:0x%lX%08lX\r\n"
  217. "Sn:0x%lX Ch:%d Bat:%d\r\n"
  218. "Temp:%3.1f C Hum:%d%%",
  219. instance->generic.protocol_name,
  220. instance->generic.data_count_bit,
  221. (uint32_t)(instance->generic.data >> 32),
  222. (uint32_t)(instance->generic.data),
  223. instance->generic.id,
  224. instance->generic.channel,
  225. instance->generic.battery_low,
  226. (double)instance->generic.temp,
  227. instance->generic.humidity);
  228. }