Browse Source

Add Acurite 609TXC protocol to weather station (#1987)

Co-authored-by: あく <alleteam@gmail.com>
Samuel Stauffer 3 years ago
parent
commit
74903ebe6e

+ 247 - 0
applications/plugins/weather_station/protocols/acurite_609txc.c

@@ -0,0 +1,247 @@
+#include "acurite_609txc.h"
+
+#define TAG "WSProtocolAcurite_609TXC"
+
+/*
+ * Help
+ * https://github.com/merbanan/rtl_433/blob/5bef4e43133ac4c0e2d18d36f87c52b4f9458453/src/devices/acurite.c#L216
+ *
+ *     0000 1111 | 0011 0000 | 0101 1100 | 0000 0000 | 1110 0111
+ *     iiii iiii | buuu tttt | tttt tttt | hhhh hhhh | cccc cccc
+ * - i: identification; changes on battery switch
+ * - c: checksum (sum of previous by bytes)
+ * - u: unknown
+ * - b: battery low; flag to indicate low battery voltage
+ * - t: temperature; in °C * 10, 12 bit with complement
+ * - h: humidity
+ *
+ */
+
+static const SubGhzBlockConst ws_protocol_acurite_609txc_const = {
+    .te_short = 500,
+    .te_long = 1000,
+    .te_delta = 150,
+    .min_count_bit_for_found = 40,
+};
+
+struct WSProtocolDecoderAcurite_609TXC {
+    SubGhzProtocolDecoderBase base;
+
+    SubGhzBlockDecoder decoder;
+    WSBlockGeneric generic;
+};
+
+struct WSProtocolEncoderAcurite_609TXC {
+    SubGhzProtocolEncoderBase base;
+
+    SubGhzProtocolBlockEncoder encoder;
+    WSBlockGeneric generic;
+};
+
+typedef enum {
+    Acurite_609TXCDecoderStepReset = 0,
+    Acurite_609TXCDecoderStepSaveDuration,
+    Acurite_609TXCDecoderStepCheckDuration,
+} Acurite_609TXCDecoderStep;
+
+const SubGhzProtocolDecoder ws_protocol_acurite_609txc_decoder = {
+    .alloc = ws_protocol_decoder_acurite_609txc_alloc,
+    .free = ws_protocol_decoder_acurite_609txc_free,
+
+    .feed = ws_protocol_decoder_acurite_609txc_feed,
+    .reset = ws_protocol_decoder_acurite_609txc_reset,
+
+    .get_hash_data = ws_protocol_decoder_acurite_609txc_get_hash_data,
+    .serialize = ws_protocol_decoder_acurite_609txc_serialize,
+    .deserialize = ws_protocol_decoder_acurite_609txc_deserialize,
+    .get_string = ws_protocol_decoder_acurite_609txc_get_string,
+};
+
+const SubGhzProtocolEncoder ws_protocol_acurite_609txc_encoder = {
+    .alloc = NULL,
+    .free = NULL,
+
+    .deserialize = NULL,
+    .stop = NULL,
+    .yield = NULL,
+};
+
+const SubGhzProtocol ws_protocol_acurite_609txc = {
+    .name = WS_PROTOCOL_ACURITE_609TXC_NAME,
+    .type = SubGhzProtocolWeatherStation,
+    .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_868 |
+            SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
+
+    .decoder = &ws_protocol_acurite_609txc_decoder,
+    .encoder = &ws_protocol_acurite_609txc_encoder,
+};
+
+void* ws_protocol_decoder_acurite_609txc_alloc(SubGhzEnvironment* environment) {
+    UNUSED(environment);
+    WSProtocolDecoderAcurite_609TXC* instance = malloc(sizeof(WSProtocolDecoderAcurite_609TXC));
+    instance->base.protocol = &ws_protocol_acurite_609txc;
+    instance->generic.protocol_name = instance->base.protocol->name;
+    return instance;
+}
+
+void ws_protocol_decoder_acurite_609txc_free(void* context) {
+    furi_assert(context);
+    WSProtocolDecoderAcurite_609TXC* instance = context;
+    free(instance);
+}
+
+void ws_protocol_decoder_acurite_609txc_reset(void* context) {
+    furi_assert(context);
+    WSProtocolDecoderAcurite_609TXC* instance = context;
+    instance->decoder.parser_step = Acurite_609TXCDecoderStepReset;
+}
+
+static bool ws_protocol_acurite_609txc_check(WSProtocolDecoderAcurite_609TXC* instance) {
+    if(!instance->decoder.decode_data) return false;
+    uint8_t crc = (uint8_t)(instance->decoder.decode_data >> 32) +
+                  (uint8_t)(instance->decoder.decode_data >> 24) +
+                  (uint8_t)(instance->decoder.decode_data >> 16) +
+                  (uint8_t)(instance->decoder.decode_data >> 8);
+    return (crc == (instance->decoder.decode_data & 0xFF));
+}
+
+/**
+ * Analysis of received data
+ * @param instance Pointer to a WSBlockGeneric* instance
+ */
+static void ws_protocol_acurite_609txc_remote_controller(WSBlockGeneric* instance) {
+    instance->id = (instance->data >> 32) & 0xFF;
+    instance->battery_low = (instance->data >> 31) & 1;
+
+    instance->channel = WS_NO_CHANNEL;
+
+    // Temperature in Celsius is encoded as a 12 bit integer value
+    // multiplied by 10 using the 4th - 6th nybbles (bytes 1 & 2)
+    // negative values are recovered by sign extend from int16_t.
+    int16_t temp_raw =
+        (int16_t)(((instance->data >> 12) & 0xf000) | ((instance->data >> 16) << 4));
+    instance->temp = (temp_raw >> 4) * 0.1f;
+    instance->humidity = (instance->data >> 8) & 0xff;
+    instance->btn = WS_NO_BTN;
+}
+
+void ws_protocol_decoder_acurite_609txc_feed(void* context, bool level, uint32_t duration) {
+    furi_assert(context);
+    WSProtocolDecoderAcurite_609TXC* instance = context;
+
+    switch(instance->decoder.parser_step) {
+    case Acurite_609TXCDecoderStepReset:
+        if((!level) && (DURATION_DIFF(duration, ws_protocol_acurite_609txc_const.te_short * 17) <
+                        ws_protocol_acurite_609txc_const.te_delta * 8)) {
+            //Found syncPrefix
+            instance->decoder.parser_step = Acurite_609TXCDecoderStepSaveDuration;
+            instance->decoder.decode_data = 0;
+            instance->decoder.decode_count_bit = 0;
+        }
+        break;
+
+    case Acurite_609TXCDecoderStepSaveDuration:
+        if(level) {
+            instance->decoder.te_last = duration;
+            instance->decoder.parser_step = Acurite_609TXCDecoderStepCheckDuration;
+        } else {
+            instance->decoder.parser_step = Acurite_609TXCDecoderStepReset;
+        }
+        break;
+
+    case Acurite_609TXCDecoderStepCheckDuration:
+        if(!level) {
+            if(DURATION_DIFF(instance->decoder.te_last, ws_protocol_acurite_609txc_const.te_short) <
+               ws_protocol_acurite_609txc_const.te_delta) {
+                if((DURATION_DIFF(duration, ws_protocol_acurite_609txc_const.te_short) <
+                    ws_protocol_acurite_609txc_const.te_delta) ||
+                   (duration > ws_protocol_acurite_609txc_const.te_long * 3)) {
+                    //Found syncPostfix
+                    instance->decoder.parser_step = Acurite_609TXCDecoderStepReset;
+                    if((instance->decoder.decode_count_bit ==
+                        ws_protocol_acurite_609txc_const.min_count_bit_for_found) &&
+                       ws_protocol_acurite_609txc_check(instance)) {
+                        instance->generic.data = instance->decoder.decode_data;
+                        instance->generic.data_count_bit = instance->decoder.decode_count_bit;
+                        ws_protocol_acurite_609txc_remote_controller(&instance->generic);
+                        if(instance->base.callback)
+                            instance->base.callback(&instance->base, instance->base.context);
+                    }
+                    instance->decoder.decode_data = 0;
+                    instance->decoder.decode_count_bit = 0;
+                } else if(
+                    DURATION_DIFF(duration, ws_protocol_acurite_609txc_const.te_long) <
+                    ws_protocol_acurite_609txc_const.te_delta * 2) {
+                    subghz_protocol_blocks_add_bit(&instance->decoder, 0);
+                    instance->decoder.parser_step = Acurite_609TXCDecoderStepSaveDuration;
+                } else if(
+                    DURATION_DIFF(duration, ws_protocol_acurite_609txc_const.te_long * 2) <
+                    ws_protocol_acurite_609txc_const.te_delta * 4) {
+                    subghz_protocol_blocks_add_bit(&instance->decoder, 1);
+                    instance->decoder.parser_step = Acurite_609TXCDecoderStepSaveDuration;
+                } else {
+                    instance->decoder.parser_step = Acurite_609TXCDecoderStepReset;
+                }
+            } else {
+                instance->decoder.parser_step = Acurite_609TXCDecoderStepReset;
+            }
+        } else {
+            instance->decoder.parser_step = Acurite_609TXCDecoderStepReset;
+        }
+        break;
+    }
+}
+
+uint8_t ws_protocol_decoder_acurite_609txc_get_hash_data(void* context) {
+    furi_assert(context);
+    WSProtocolDecoderAcurite_609TXC* instance = context;
+    return subghz_protocol_blocks_get_hash_data(
+        &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
+}
+
+bool ws_protocol_decoder_acurite_609txc_serialize(
+    void* context,
+    FlipperFormat* flipper_format,
+    SubGhzRadioPreset* preset) {
+    furi_assert(context);
+    WSProtocolDecoderAcurite_609TXC* instance = context;
+    return ws_block_generic_serialize(&instance->generic, flipper_format, preset);
+}
+
+bool ws_protocol_decoder_acurite_609txc_deserialize(void* context, FlipperFormat* flipper_format) {
+    furi_assert(context);
+    WSProtocolDecoderAcurite_609TXC* instance = context;
+    bool ret = false;
+    do {
+        if(!ws_block_generic_deserialize(&instance->generic, flipper_format)) {
+            break;
+        }
+        if(instance->generic.data_count_bit !=
+           ws_protocol_acurite_609txc_const.min_count_bit_for_found) {
+            FURI_LOG_E(TAG, "Wrong number of bits in key");
+            break;
+        }
+        ret = true;
+    } while(false);
+    return ret;
+}
+
+void ws_protocol_decoder_acurite_609txc_get_string(void* context, FuriString* output) {
+    furi_assert(context);
+    WSProtocolDecoderAcurite_609TXC* instance = context;
+    furi_string_printf(
+        output,
+        "%s %dbit\r\n"
+        "Key:0x%lX%08lX\r\n"
+        "Sn:0x%lX Ch:%d  Bat:%d\r\n"
+        "Temp:%3.1f C Hum:%d%%",
+        instance->generic.protocol_name,
+        instance->generic.data_count_bit,
+        (uint32_t)(instance->generic.data >> 40),
+        (uint32_t)(instance->generic.data),
+        instance->generic.id,
+        instance->generic.channel,
+        instance->generic.battery_low,
+        (double)instance->generic.temp,
+        instance->generic.humidity);
+}

+ 79 - 0
applications/plugins/weather_station/protocols/acurite_609txc.h

@@ -0,0 +1,79 @@
+#pragma once
+
+#include <lib/subghz/protocols/base.h>
+
+#include <lib/subghz/blocks/const.h>
+#include <lib/subghz/blocks/decoder.h>
+#include <lib/subghz/blocks/encoder.h>
+#include "ws_generic.h"
+#include <lib/subghz/blocks/math.h>
+
+#define WS_PROTOCOL_ACURITE_609TXC_NAME "Acurite-609TXC"
+
+typedef struct WSProtocolDecoderAcurite_609TXC WSProtocolDecoderAcurite_609TXC;
+typedef struct WSProtocolEncoderAcurite_609TXC WSProtocolEncoderAcurite_609TXC;
+
+extern const SubGhzProtocolDecoder ws_protocol_acurite_609txc_decoder;
+extern const SubGhzProtocolEncoder ws_protocol_acurite_609txc_encoder;
+extern const SubGhzProtocol ws_protocol_acurite_609txc;
+
+/**
+ * Allocate WSProtocolDecoderAcurite_609TXC.
+ * @param environment Pointer to a SubGhzEnvironment instance
+ * @return WSProtocolDecoderAcurite_609TXC* pointer to a WSProtocolDecoderAcurite_609TXC instance
+ */
+void* ws_protocol_decoder_acurite_609txc_alloc(SubGhzEnvironment* environment);
+
+/**
+ * Free WSProtocolDecoderAcurite_609TXC.
+ * @param context Pointer to a WSProtocolDecoderAcurite_609TXC instance
+ */
+void ws_protocol_decoder_acurite_609txc_free(void* context);
+
+/**
+ * Reset decoder WSProtocolDecoderAcurite_609TXC.
+ * @param context Pointer to a WSProtocolDecoderAcurite_609TXC instance
+ */
+void ws_protocol_decoder_acurite_609txc_reset(void* context);
+
+/**
+ * Parse a raw sequence of levels and durations received from the air.
+ * @param context Pointer to a WSProtocolDecoderAcurite_609TXC instance
+ * @param level Signal level true-high false-low
+ * @param duration Duration of this level in, us
+ */
+void ws_protocol_decoder_acurite_609txc_feed(void* context, bool level, uint32_t duration);
+
+/**
+ * Getting the hash sum of the last randomly received parcel.
+ * @param context Pointer to a WSProtocolDecoderAcurite_609TXC instance
+ * @return hash Hash sum
+ */
+uint8_t ws_protocol_decoder_acurite_609txc_get_hash_data(void* context);
+
+/**
+ * Serialize data WSProtocolDecoderAcurite_609TXC.
+ * @param context Pointer to a WSProtocolDecoderAcurite_609TXC instance
+ * @param flipper_format Pointer to a FlipperFormat instance
+ * @param preset The modulation on which the signal was received, SubGhzRadioPreset
+ * @return true On success
+ */
+bool ws_protocol_decoder_acurite_609txc_serialize(
+    void* context,
+    FlipperFormat* flipper_format,
+    SubGhzRadioPreset* preset);
+
+/**
+ * Deserialize data WSProtocolDecoderAcurite_609TXC.
+ * @param context Pointer to a WSProtocolDecoderAcurite_609TXC instance
+ * @param flipper_format Pointer to a FlipperFormat instance
+ * @return true On success
+ */
+bool ws_protocol_decoder_acurite_609txc_deserialize(void* context, FlipperFormat* flipper_format);
+
+/**
+ * Getting a textual representation of the received data.
+ * @param context Pointer to a WSProtocolDecoderAcurite_609TXC instance
+ * @param output Resulting text
+ */
+void ws_protocol_decoder_acurite_609txc_get_string(void* context, FuriString* output);

+ 1 - 0
applications/plugins/weather_station/protocols/protocol_items.c

@@ -6,6 +6,7 @@ const SubGhzProtocol* weather_station_protocol_registry_items[] = {
     &ws_protocol_nexus_th,
     &ws_protocol_nexus_th,
     &ws_protocol_gt_wt_03,
     &ws_protocol_gt_wt_03,
     &ws_protocol_acurite_606tx,
     &ws_protocol_acurite_606tx,
+    &ws_protocol_acurite_609txc,
     &ws_protocol_lacrosse_tx141thbv2,
     &ws_protocol_lacrosse_tx141thbv2,
     &ws_protocol_oregon2,
     &ws_protocol_oregon2,
     &ws_protocol_acurite_592txr,
     &ws_protocol_acurite_592txr,

+ 1 - 0
applications/plugins/weather_station/protocols/protocol_items.h

@@ -6,6 +6,7 @@
 #include "nexus_th.h"
 #include "nexus_th.h"
 #include "gt_wt_03.h"
 #include "gt_wt_03.h"
 #include "acurite_606tx.h"
 #include "acurite_606tx.h"
+#include "acurite_609txc.h"
 #include "lacrosse_tx141thbv2.h"
 #include "lacrosse_tx141thbv2.h"
 #include "oregon2.h"
 #include "oregon2.h"
 #include "acurite_592txr.h"
 #include "acurite_592txr.h"