ao-tools/lib: Add cc_telemetry_unparse
authorKeith Packard <keithp@keithp.com>
Sun, 10 Mar 2013 04:39:31 +0000 (20:39 -0800)
committerKeith Packard <keithp@keithp.com>
Sun, 10 Mar 2013 04:39:31 +0000 (20:39 -0800)
This takes a telemetry structure and generates a string version

Signed-off-by: Keith Packard <keithp@keithp.com>
ao-tools/lib/cc-telemetry.c
ao-tools/lib/cc-telemetry.h

index 99da2680c1cdca4fe3be0a9143a7e4f1781b7a67..88da7f03febfc82f43f9ba05c7db608806bd2ecc 100644 (file)
@@ -60,3 +60,33 @@ cc_telemetry_parse(const char *input_line, union ao_telemetry_all *telemetry)
        memcpy(telemetry, hex+1, 34);
        return TRUE;
 }
        memcpy(telemetry, hex+1, 34);
        return TRUE;
 }
+
+uint8_t
+cc_telemetry_cksum(const union ao_telemetry_all *telemetry)
+{
+       const uint8_t   *x = (const uint8_t *) telemetry;
+       int i;
+       uint8_t sum = 0x5a;
+       for (i = 0; i < 34; i++)
+               sum += x[i];
+       return sum;
+}
+
+void
+cc_telemetry_unparse(const union ao_telemetry_all *telemetry, char output_line[CC_TELEMETRY_BUFSIZE])
+{
+       uint8_t hex[36];
+       int i;
+       int p;
+
+       hex[0] = 34;
+       memcpy(hex+1, telemetry, 34);
+       hex[35] = cc_telemetry_cksum(telemetry);
+       strcpy(output_line, "TELEM ");
+       p = strlen(output_line);
+       for (i = 0; i < 36; i++) {
+               sprintf(output_line + p, "%02x", hex[i]);
+               p += 2;
+       }
+}
+               
index e849cd3b66e1c33422b6e7bc0251436e8490a99e..9a5be49f9a76d2e9cd2122e951838cf9d5e2eed7 100644 (file)
@@ -237,7 +237,19 @@ union ao_telemetry_all {
        struct ao_telemetry_baro                baro;
 };
 
        struct ao_telemetry_baro                baro;
 };
 
+#define CC_TELEMETRY_HEADER    "TELEM"
+
+/* "TELEM " 1 byte length 32 data bytes 1 rssi 1 status 1 checksum 1 null */
+
+#define CC_TELEMETRY_BUFSIZE   (6 + (1 + 32 + 3) * 2 + 1)
+
 int
 cc_telemetry_parse(const char *input_line, union ao_telemetry_all *telemetry);
 
 int
 cc_telemetry_parse(const char *input_line, union ao_telemetry_all *telemetry);
 
+uint8_t
+cc_telemetry_cksum(const union ao_telemetry_all *telemetry);
+
+void
+cc_telemetry_unparse(const union ao_telemetry_all *telemetry, char output_line[CC_TELEMETRY_BUFSIZE]);
+
 #endif
 #endif