altos: Add ublox checksum app to generate ublox config lines
authorKeith Packard <keithp@keithp.com>
Mon, 29 Apr 2013 06:25:37 +0000 (23:25 -0700)
committerKeith Packard <keithp@keithp.com>
Wed, 8 May 2013 03:07:53 +0000 (20:07 -0700)
Signed-off-by: Keith Packard <keithp@keithp.com>
src/util/ublox-cksum [new file with mode: 0644]

diff --git a/src/util/ublox-cksum b/src/util/ublox-cksum
new file mode 100644 (file)
index 0000000..05e8d6b
--- /dev/null
@@ -0,0 +1,50 @@
+#!/usr/bin/env nickle
+
+typedef struct {
+       int     a, b;
+} ck_t;
+
+/* Fletcher algorithm */
+ck_t checksum(int[] msg)
+{
+       ck_t    ck = { .a = 0, .b = 0 };
+       for (int i = 4; i < dim(msg); i++) {
+               ck.a += msg[i];
+               ck.b += ck.a;
+               ck.a &= 0xff;
+               ck.b &= 0xff;
+       }
+       return ck;
+}
+
+void main()
+{
+       string[...]     input;
+       int[...]        msg;
+
+       setdim(input, 0);
+       while (!File::end(stdin)) {
+               input[dim(input)] = gets();
+       }
+
+       setdim(msg, 0);
+       for (int i = 0; i < dim(input); i++) {
+               string[*] words = String::wordsplit(input[i], " ,\t");
+               for (int j = 0; j < dim(words); j++) {
+                       if (words[j] == "/" + "*")
+                               break;
+                       if (String::length(words[j]) > 0 &&
+                           Ctype::isdigit(words[j][0])) {
+                               msg[dim(msg)] = string_to_integer(words[j]);
+                       }
+                }
+       }
+       printf("\t0xb5, 0x62, \t\t/* length: %d bytes */\n", dim(msg));
+       for (int i = 0; i < dim(input); i++)
+               printf("%s\n", input[i]);
+       ck_t ck = checksum(msg);
+       printf ("\t0x%02x, 0x%02x,\n",
+               ck.a, ck.b);
+}
+
+main();