thermopro_tx4.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include "thermopro_tx4.h"
  2. #define TAG "WSProtocolThermoPRO_TX4"
  3. /*
  4. * Help
  5. * https://github.com/merbanan/rtl_433/blob/master/src/devices/thermopro_tx2.c
  6. *
  7. * The sensor sends 37 bits 6 times, before the first packet there is a sync pulse.
  8. * The packets are ppm modulated (distance coding) with a pulse of ~500 us
  9. * followed by a short gap of ~2000 us for a 0 bit or a long ~4000 us gap for a
  10. * 1 bit, the sync gap is ~9000 us.
  11. * The data is grouped in 9 nibbles
  12. * [type] [id0] [id1] [flags] [temp0] [temp1] [temp2] [humi0] [humi1]
  13. * - type: 4 bit fixed 1001 (9) or 0110 (5)
  14. * - id: 8 bit a random id that is generated when the sensor starts, could include battery status
  15. * the same batteries often generate the same id
  16. * - flags(3): is 1 when the battery is low, otherwise 0 (ok)
  17. * - flags(2): is 1 when the sensor sends a reading when pressing the button on the sensor
  18. * - flags(1,0): the channel number that can be set by the sensor (1, 2, 3, X)
  19. * - temp: 12 bit signed scaled by 10
  20. * - humi: 8 bit always 11001100 (0xCC) if no humidity sensor is available
  21. *
  22. */
  23. #define THERMO_PRO_TX4_TYPE_1 0b1001
  24. #define THERMO_PRO_TX4_TYPE_2 0b0110
  25. static const SubGhzBlockConst ws_protocol_thermopro_tx4_const = {
  26. .te_short = 500,
  27. .te_long = 2000,
  28. .te_delta = 150,
  29. .min_count_bit_for_found = 37,
  30. };
  31. struct WSProtocolDecoderThermoPRO_TX4 {
  32. SubGhzProtocolDecoderBase base;
  33. SubGhzBlockDecoder decoder;
  34. WSBlockGeneric generic;
  35. };
  36. struct WSProtocolEncoderThermoPRO_TX4 {
  37. SubGhzProtocolEncoderBase base;
  38. SubGhzProtocolBlockEncoder encoder;
  39. WSBlockGeneric generic;
  40. };
  41. typedef enum {
  42. ThermoPRO_TX4DecoderStepReset = 0,
  43. ThermoPRO_TX4DecoderStepSaveDuration,
  44. ThermoPRO_TX4DecoderStepCheckDuration,
  45. } ThermoPRO_TX4DecoderStep;
  46. const SubGhzProtocolDecoder ws_protocol_thermopro_tx4_decoder = {
  47. .alloc = ws_protocol_decoder_thermopro_tx4_alloc,
  48. .free = ws_protocol_decoder_thermopro_tx4_free,
  49. .feed = ws_protocol_decoder_thermopro_tx4_feed,
  50. .reset = ws_protocol_decoder_thermopro_tx4_reset,
  51. .get_hash_data = ws_protocol_decoder_thermopro_tx4_get_hash_data,
  52. .serialize = ws_protocol_decoder_thermopro_tx4_serialize,
  53. .deserialize = ws_protocol_decoder_thermopro_tx4_deserialize,
  54. .get_string = ws_protocol_decoder_thermopro_tx4_get_string,
  55. };
  56. const SubGhzProtocolEncoder ws_protocol_thermopro_tx4_encoder = {
  57. .alloc = NULL,
  58. .free = NULL,
  59. .deserialize = NULL,
  60. .stop = NULL,
  61. .yield = NULL,
  62. };
  63. const SubGhzProtocol ws_protocol_thermopro_tx4 = {
  64. .name = WS_PROTOCOL_THERMOPRO_TX4_NAME,
  65. .type = SubGhzProtocolWeatherStation,
  66. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_868 |
  67. SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
  68. .decoder = &ws_protocol_thermopro_tx4_decoder,
  69. .encoder = &ws_protocol_thermopro_tx4_encoder,
  70. };
  71. void* ws_protocol_decoder_thermopro_tx4_alloc(SubGhzEnvironment* environment) {
  72. UNUSED(environment);
  73. WSProtocolDecoderThermoPRO_TX4* instance = malloc(sizeof(WSProtocolDecoderThermoPRO_TX4));
  74. instance->base.protocol = &ws_protocol_thermopro_tx4;
  75. instance->generic.protocol_name = instance->base.protocol->name;
  76. return instance;
  77. }
  78. void ws_protocol_decoder_thermopro_tx4_free(void* context) {
  79. furi_assert(context);
  80. WSProtocolDecoderThermoPRO_TX4* instance = context;
  81. free(instance);
  82. }
  83. void ws_protocol_decoder_thermopro_tx4_reset(void* context) {
  84. furi_assert(context);
  85. WSProtocolDecoderThermoPRO_TX4* instance = context;
  86. instance->decoder.parser_step = ThermoPRO_TX4DecoderStepReset;
  87. }
  88. static bool ws_protocol_thermopro_tx4_check(WSProtocolDecoderThermoPRO_TX4* instance) {
  89. uint8_t type = instance->decoder.decode_data >> 33;
  90. if((type == THERMO_PRO_TX4_TYPE_1) || (type == THERMO_PRO_TX4_TYPE_2)) {
  91. return true;
  92. } else {
  93. return false;
  94. }
  95. }
  96. /**
  97. * Analysis of received data
  98. * @param instance Pointer to a WSBlockGeneric* instance
  99. */
  100. static void ws_protocol_thermopro_tx4_remote_controller(WSBlockGeneric* instance) {
  101. instance->id = (instance->data >> 25) & 0xFF;
  102. instance->battery_low = (instance->data >> 24) & 1;
  103. instance->btn = (instance->data >> 23) & 1;
  104. instance->channel = ((instance->data >> 21) & 0x03) + 1;
  105. if(!((instance->data >> 20) & 1)) {
  106. instance->temp = (float)((instance->data >> 9) & 0x07FF) / 10.0f;
  107. } else {
  108. instance->temp = (float)((~(instance->data >> 9) & 0x07FF) + 1) / -10.0f;
  109. }
  110. instance->humidity = (instance->data >> 1) & 0xFF;
  111. }
  112. void ws_protocol_decoder_thermopro_tx4_feed(void* context, bool level, uint32_t duration) {
  113. furi_assert(context);
  114. WSProtocolDecoderThermoPRO_TX4* instance = context;
  115. switch(instance->decoder.parser_step) {
  116. case ThermoPRO_TX4DecoderStepReset:
  117. if((!level) && (DURATION_DIFF(duration, ws_protocol_thermopro_tx4_const.te_short * 18) <
  118. ws_protocol_thermopro_tx4_const.te_delta * 10)) {
  119. //Found sync
  120. instance->decoder.parser_step = ThermoPRO_TX4DecoderStepSaveDuration;
  121. instance->decoder.decode_data = 0;
  122. instance->decoder.decode_count_bit = 0;
  123. }
  124. break;
  125. case ThermoPRO_TX4DecoderStepSaveDuration:
  126. if(level) {
  127. instance->decoder.te_last = duration;
  128. instance->decoder.parser_step = ThermoPRO_TX4DecoderStepCheckDuration;
  129. } else {
  130. instance->decoder.parser_step = ThermoPRO_TX4DecoderStepReset;
  131. }
  132. break;
  133. case ThermoPRO_TX4DecoderStepCheckDuration:
  134. if(!level) {
  135. if(DURATION_DIFF(duration, ws_protocol_thermopro_tx4_const.te_short * 18) <
  136. ws_protocol_thermopro_tx4_const.te_delta * 10) {
  137. //Found sync
  138. instance->decoder.parser_step = ThermoPRO_TX4DecoderStepReset;
  139. if((instance->decoder.decode_count_bit ==
  140. ws_protocol_thermopro_tx4_const.min_count_bit_for_found) &&
  141. ws_protocol_thermopro_tx4_check(instance)) {
  142. instance->generic.data = instance->decoder.decode_data;
  143. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  144. ws_protocol_thermopro_tx4_remote_controller(&instance->generic);
  145. if(instance->base.callback)
  146. instance->base.callback(&instance->base, instance->base.context);
  147. instance->decoder.parser_step = ThermoPRO_TX4DecoderStepCheckDuration;
  148. }
  149. instance->decoder.decode_data = 0;
  150. instance->decoder.decode_count_bit = 0;
  151. break;
  152. } else if(
  153. (DURATION_DIFF(
  154. instance->decoder.te_last, ws_protocol_thermopro_tx4_const.te_short) <
  155. ws_protocol_thermopro_tx4_const.te_delta) &&
  156. (DURATION_DIFF(duration, ws_protocol_thermopro_tx4_const.te_long) <
  157. ws_protocol_thermopro_tx4_const.te_delta * 2)) {
  158. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  159. instance->decoder.parser_step = ThermoPRO_TX4DecoderStepSaveDuration;
  160. } else if(
  161. (DURATION_DIFF(
  162. instance->decoder.te_last, ws_protocol_thermopro_tx4_const.te_short) <
  163. ws_protocol_thermopro_tx4_const.te_delta) &&
  164. (DURATION_DIFF(duration, ws_protocol_thermopro_tx4_const.te_long * 2) <
  165. ws_protocol_thermopro_tx4_const.te_delta * 4)) {
  166. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  167. instance->decoder.parser_step = ThermoPRO_TX4DecoderStepSaveDuration;
  168. } else {
  169. instance->decoder.parser_step = ThermoPRO_TX4DecoderStepReset;
  170. }
  171. } else {
  172. instance->decoder.parser_step = ThermoPRO_TX4DecoderStepReset;
  173. }
  174. break;
  175. }
  176. }
  177. uint8_t ws_protocol_decoder_thermopro_tx4_get_hash_data(void* context) {
  178. furi_assert(context);
  179. WSProtocolDecoderThermoPRO_TX4* instance = context;
  180. return subghz_protocol_blocks_get_hash_data(
  181. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  182. }
  183. SubGhzProtocolStatus ws_protocol_decoder_thermopro_tx4_serialize(
  184. void* context,
  185. FlipperFormat* flipper_format,
  186. SubGhzRadioPreset* preset) {
  187. furi_assert(context);
  188. WSProtocolDecoderThermoPRO_TX4* instance = context;
  189. return ws_block_generic_serialize(&instance->generic, flipper_format, preset);
  190. }
  191. SubGhzProtocolStatus
  192. ws_protocol_decoder_thermopro_tx4_deserialize(void* context, FlipperFormat* flipper_format) {
  193. furi_assert(context);
  194. WSProtocolDecoderThermoPRO_TX4* instance = context;
  195. return ws_block_generic_deserialize_check_count_bit(
  196. &instance->generic,
  197. flipper_format,
  198. ws_protocol_thermopro_tx4_const.min_count_bit_for_found);
  199. }
  200. void ws_protocol_decoder_thermopro_tx4_get_string(void* context, FuriString* output) {
  201. furi_assert(context);
  202. WSProtocolDecoderThermoPRO_TX4* instance = context;
  203. furi_string_printf(
  204. output,
  205. "%s %dbit\r\n"
  206. "Key:0x%lX%08lX\r\n"
  207. "Sn:0x%lX Ch:%d Bat:%d\r\n"
  208. "Temp:%3.1f C Hum:%d%%",
  209. instance->generic.protocol_name,
  210. instance->generic.data_count_bit,
  211. (uint32_t)(instance->generic.data >> 32),
  212. (uint32_t)(instance->generic.data),
  213. instance->generic.id,
  214. instance->generic.channel,
  215. instance->generic.battery_low,
  216. (double)instance->generic.temp,
  217. instance->generic.humidity);
  218. }