diff options
| author | Keith Packard <keithp@keithp.com> | 2013-04-28 23:25:37 -0700 | 
|---|---|---|
| committer | Keith Packard <keithp@keithp.com> | 2013-05-07 20:07:53 -0700 | 
| commit | 85d32468210c9989ae52bd29f883c4380af43961 (patch) | |
| tree | c78a12d58c5e3eb449df0307b7d2772841a1954d | |
| parent | 257500776935b5950cd7c49f9c799b3174d9232d (diff) | |
altos: Add ublox checksum app to generate ublox config lines
Signed-off-by: Keith Packard <keithp@keithp.com>
| -rw-r--r-- | src/util/ublox-cksum | 50 | 
1 files changed, 50 insertions, 0 deletions
| diff --git a/src/util/ublox-cksum b/src/util/ublox-cksum new file mode 100644 index 00000000..05e8d6b1 --- /dev/null +++ b/src/util/ublox-cksum @@ -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(); | 
