nexus_th.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #include "nexus_th.h"
  2. #define TAG "WSProtocolNexus_TH"
  3. /*
  4. * Help
  5. * https://github.com/merbanan/rtl_433/blob/ef2d37cf51e3264d11cde9149ef87de2f0a4d37a/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. if(!((instance->data >> 23) & 1)) {
  109. instance->temp = (float)((instance->data >> 12) & 0x07FF) / 10.0f;
  110. } else {
  111. instance->temp = (float)((~(instance->data >> 12) & 0x07FF) + 1) / -10.0f;
  112. }
  113. instance->humidity = instance->data & 0xFF;
  114. }
  115. void ws_protocol_decoder_nexus_th_feed(void* context, bool level, uint32_t duration) {
  116. furi_assert(context);
  117. WSProtocolDecoderNexus_TH* instance = context;
  118. switch(instance->decoder.parser_step) {
  119. case Nexus_THDecoderStepReset:
  120. if((!level) && (DURATION_DIFF(duration, ws_protocol_nexus_th_const.te_short * 8) <
  121. ws_protocol_nexus_th_const.te_delta * 4)) {
  122. //Found sync
  123. instance->decoder.parser_step = Nexus_THDecoderStepSaveDuration;
  124. instance->decoder.decode_data = 0;
  125. instance->decoder.decode_count_bit = 0;
  126. }
  127. break;
  128. case Nexus_THDecoderStepSaveDuration:
  129. if(level) {
  130. instance->decoder.te_last = duration;
  131. instance->decoder.parser_step = Nexus_THDecoderStepCheckDuration;
  132. } else {
  133. instance->decoder.parser_step = Nexus_THDecoderStepReset;
  134. }
  135. break;
  136. case Nexus_THDecoderStepCheckDuration:
  137. if(!level) {
  138. if(DURATION_DIFF(duration, ws_protocol_nexus_th_const.te_short * 8) <
  139. ws_protocol_nexus_th_const.te_delta * 4) {
  140. //Found sync
  141. instance->decoder.parser_step = Nexus_THDecoderStepReset;
  142. if((instance->decoder.decode_count_bit ==
  143. ws_protocol_nexus_th_const.min_count_bit_for_found) &&
  144. ws_protocol_nexus_th_check(instance)) {
  145. instance->generic.data = instance->decoder.decode_data;
  146. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  147. ws_protocol_nexus_th_remote_controller(&instance->generic);
  148. if(instance->base.callback)
  149. instance->base.callback(&instance->base, instance->base.context);
  150. instance->decoder.parser_step = Nexus_THDecoderStepCheckDuration;
  151. }
  152. instance->decoder.decode_data = 0;
  153. instance->decoder.decode_count_bit = 0;
  154. break;
  155. } else if(
  156. (DURATION_DIFF(instance->decoder.te_last, ws_protocol_nexus_th_const.te_short) <
  157. ws_protocol_nexus_th_const.te_delta) &&
  158. (DURATION_DIFF(duration, ws_protocol_nexus_th_const.te_short * 2) <
  159. ws_protocol_nexus_th_const.te_delta * 2)) {
  160. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  161. instance->decoder.parser_step = Nexus_THDecoderStepSaveDuration;
  162. } else if(
  163. (DURATION_DIFF(instance->decoder.te_last, ws_protocol_nexus_th_const.te_short) <
  164. ws_protocol_nexus_th_const.te_delta) &&
  165. (DURATION_DIFF(duration, ws_protocol_nexus_th_const.te_short * 4) <
  166. ws_protocol_nexus_th_const.te_delta * 4)) {
  167. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  168. instance->decoder.parser_step = Nexus_THDecoderStepSaveDuration;
  169. } else {
  170. instance->decoder.parser_step = Nexus_THDecoderStepReset;
  171. }
  172. } else {
  173. instance->decoder.parser_step = Nexus_THDecoderStepReset;
  174. }
  175. break;
  176. }
  177. }
  178. uint8_t ws_protocol_decoder_nexus_th_get_hash_data(void* context) {
  179. furi_assert(context);
  180. WSProtocolDecoderNexus_TH* instance = context;
  181. return subghz_protocol_blocks_get_hash_data(
  182. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  183. }
  184. bool ws_protocol_decoder_nexus_th_serialize(
  185. void* context,
  186. FlipperFormat* flipper_format,
  187. SubGhzRadioPreset* preset) {
  188. furi_assert(context);
  189. WSProtocolDecoderNexus_TH* instance = context;
  190. return ws_block_generic_serialize(&instance->generic, flipper_format, preset);
  191. }
  192. bool ws_protocol_decoder_nexus_th_deserialize(void* context, FlipperFormat* flipper_format) {
  193. furi_assert(context);
  194. WSProtocolDecoderNexus_TH* instance = context;
  195. bool ret = false;
  196. do {
  197. if(!ws_block_generic_deserialize(&instance->generic, flipper_format)) {
  198. break;
  199. }
  200. if(instance->generic.data_count_bit !=
  201. ws_protocol_nexus_th_const.min_count_bit_for_found) {
  202. FURI_LOG_E(TAG, "Wrong number of bits in key");
  203. break;
  204. }
  205. ret = true;
  206. } while(false);
  207. return ret;
  208. }
  209. void ws_protocol_decoder_nexus_th_get_string(void* context, FuriString* output) {
  210. furi_assert(context);
  211. WSProtocolDecoderNexus_TH* instance = context;
  212. furi_string_printf(
  213. output,
  214. "%s %dbit\r\n"
  215. "Key:0x%lX%08lX\r\n"
  216. "Sn:0x%lX Ch:%d Bat:%d\r\n"
  217. "Temp:%d.%d C Hum:%d%%",
  218. instance->generic.protocol_name,
  219. instance->generic.data_count_bit,
  220. (uint32_t)(instance->generic.data >> 32),
  221. (uint32_t)(instance->generic.data),
  222. instance->generic.id,
  223. instance->generic.channel,
  224. instance->generic.battery_low,
  225. (int16_t)instance->generic.temp,
  226. abs(((int16_t)(instance->generic.temp * 10) - (((int16_t)instance->generic.temp) * 10))),
  227. instance->generic.humidity);
  228. }