nexus_th.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. if(instance->humidity > 95)
  116. instance->humidity = 95;
  117. else if(instance->humidity < 20)
  118. instance->humidity = 20;
  119. }
  120. void ws_protocol_decoder_nexus_th_feed(void* context, bool level, uint32_t duration) {
  121. furi_assert(context);
  122. WSProtocolDecoderNexus_TH* instance = context;
  123. switch(instance->decoder.parser_step) {
  124. case Nexus_THDecoderStepReset:
  125. if((!level) && (DURATION_DIFF(duration, ws_protocol_nexus_th_const.te_short * 8) <
  126. ws_protocol_nexus_th_const.te_delta * 4)) {
  127. //Found sync
  128. instance->decoder.parser_step = Nexus_THDecoderStepSaveDuration;
  129. instance->decoder.decode_data = 0;
  130. instance->decoder.decode_count_bit = 0;
  131. }
  132. break;
  133. case Nexus_THDecoderStepSaveDuration:
  134. if(level) {
  135. instance->decoder.te_last = duration;
  136. instance->decoder.parser_step = Nexus_THDecoderStepCheckDuration;
  137. } else {
  138. instance->decoder.parser_step = Nexus_THDecoderStepReset;
  139. }
  140. break;
  141. case Nexus_THDecoderStepCheckDuration:
  142. if(!level) {
  143. if(DURATION_DIFF(duration, ws_protocol_nexus_th_const.te_short * 8) <
  144. ws_protocol_nexus_th_const.te_delta * 4) {
  145. //Found sync
  146. instance->decoder.parser_step = Nexus_THDecoderStepReset;
  147. if((instance->decoder.decode_count_bit ==
  148. ws_protocol_nexus_th_const.min_count_bit_for_found) &&
  149. ws_protocol_nexus_th_check(instance)) {
  150. instance->generic.data = instance->decoder.decode_data;
  151. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  152. ws_protocol_nexus_th_remote_controller(&instance->generic);
  153. if(instance->base.callback)
  154. instance->base.callback(&instance->base, instance->base.context);
  155. instance->decoder.parser_step = Nexus_THDecoderStepCheckDuration;
  156. }
  157. instance->decoder.decode_data = 0;
  158. instance->decoder.decode_count_bit = 0;
  159. break;
  160. } else if(
  161. (DURATION_DIFF(instance->decoder.te_last, ws_protocol_nexus_th_const.te_short) <
  162. ws_protocol_nexus_th_const.te_delta) &&
  163. (DURATION_DIFF(duration, ws_protocol_nexus_th_const.te_short * 2) <
  164. ws_protocol_nexus_th_const.te_delta * 2)) {
  165. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  166. instance->decoder.parser_step = Nexus_THDecoderStepSaveDuration;
  167. } else if(
  168. (DURATION_DIFF(instance->decoder.te_last, ws_protocol_nexus_th_const.te_short) <
  169. ws_protocol_nexus_th_const.te_delta) &&
  170. (DURATION_DIFF(duration, ws_protocol_nexus_th_const.te_short * 4) <
  171. ws_protocol_nexus_th_const.te_delta * 4)) {
  172. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  173. instance->decoder.parser_step = Nexus_THDecoderStepSaveDuration;
  174. } else {
  175. instance->decoder.parser_step = Nexus_THDecoderStepReset;
  176. }
  177. } else {
  178. instance->decoder.parser_step = Nexus_THDecoderStepReset;
  179. }
  180. break;
  181. }
  182. }
  183. uint8_t ws_protocol_decoder_nexus_th_get_hash_data(void* context) {
  184. furi_assert(context);
  185. WSProtocolDecoderNexus_TH* instance = context;
  186. return subghz_protocol_blocks_get_hash_data(
  187. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  188. }
  189. SubGhzProtocolStatus ws_protocol_decoder_nexus_th_serialize(
  190. void* context,
  191. FlipperFormat* flipper_format,
  192. SubGhzRadioPreset* preset) {
  193. furi_assert(context);
  194. WSProtocolDecoderNexus_TH* instance = context;
  195. return ws_block_generic_serialize(&instance->generic, flipper_format, preset);
  196. }
  197. SubGhzProtocolStatus
  198. ws_protocol_decoder_nexus_th_deserialize(void* context, FlipperFormat* flipper_format) {
  199. furi_assert(context);
  200. WSProtocolDecoderNexus_TH* instance = context;
  201. return ws_block_generic_deserialize_check_count_bit(
  202. &instance->generic, flipper_format, ws_protocol_nexus_th_const.min_count_bit_for_found);
  203. }
  204. void ws_protocol_decoder_nexus_th_get_string(void* context, FuriString* output) {
  205. furi_assert(context);
  206. WSProtocolDecoderNexus_TH* instance = context;
  207. furi_string_printf(
  208. output,
  209. "%s %dbit\r\n"
  210. "Key:0x%lX%08lX\r\n"
  211. "Sn:0x%lX Ch:%d Bat:%d\r\n"
  212. "Temp:%3.1f C Hum:%d%%",
  213. instance->generic.protocol_name,
  214. instance->generic.data_count_bit,
  215. (uint32_t)(instance->generic.data >> 32),
  216. (uint32_t)(instance->generic.data),
  217. instance->generic.id,
  218. instance->generic.channel,
  219. instance->generic.battery_low,
  220. (double)instance->generic.temp,
  221. instance->generic.humidity);
  222. }