summaryrefslogtreecommitdiff
path: root/n/avr/proto/protodec
diff options
context:
space:
mode:
Diffstat (limited to 'n/avr/proto/protodec')
-rwxr-xr-xn/avr/proto/protodec97
1 files changed, 97 insertions, 0 deletions
diff --git a/n/avr/proto/protodec b/n/avr/proto/protodec
new file mode 100755
index 0000000..0ee929b
--- /dev/null
+++ b/n/avr/proto/protodec
@@ -0,0 +1,97 @@
+#!/usr/bin/perl -w
+use strict;
+
+sub syntax
+{
+ print <<EOF;
+$0 - Décodeur de protocol série pour le robot.
+Syntaxe : $0 code [colonnes] [code [colonnes] ...
+Exemple : $0 l 1 2u 3-4 m 2
+décode pour les codes l et m. Pour l, décode la première colonne en 8 bits, la
+deuxième en 8 bits non signés, les troisième et quatrième en 16 bits.
+EOF
+ exit 1;
+}
+
+sub cvhexu
+{
+ my $h = join '', @_;
+ return hex $h;
+}
+
+sub cvhex
+{
+ my $h = cvhexu @_;
+ my $b = 8 * scalar @_;
+ if ($h >= 2 ** ($b - 1)) {
+ return -(2 ** $b - $h);
+ } else {
+ return $h;
+ }
+}
+
+sub prcmd
+{
+ my ($cmd, $c, @v) = @_;
+ return unless exists $$cmd{$c};
+ print $c;
+ for (@{$$cmd{$c}})
+ {
+ /^(\d)-(\d)(u?)$/;
+ if ($3 eq 'u') {
+ print ' ', cvhexu @v[$1 - 1 .. $2 - 1];
+ } else {
+ print ' ', cvhex @v[$1 - 1 .. $2 - 1];
+ }
+ }
+ print "\n";
+};
+
+my %cmd;
+my ($acmd, @acmdl);
+
+while ($_ = shift)
+{
+ /^[a-zA-Z]$/ and do {
+ $cmd{$acmd} = [ @acmdl ] if defined $acmd;
+ @acmdl = ();
+ $acmd = $_;
+ next;
+ };
+ /^([0-8])(u?)$/ and do {
+ syntax if !defined $acmd;
+ push @acmdl, "$1-$1$2";
+ next;
+ };
+ /^([0-8])-([0-8])(u?)$/ and do {
+ syntax if !defined $acmd;
+ syntax if $2 <= $1;
+ push @acmdl, $_;
+ next;
+ };
+ syntax;
+}
+$cmd{$acmd} = [ @acmdl ] if defined $acmd;
+
+syntax if !scalar %cmd;
+
+#print $_, ' ', join (' ', @{$cmd{$_}}), "\n" for keys %cmd;
+
+while (<>)
+{
+ chomp;
+ if (/^!([a-zA-Z])(\w\w)?(\w\w)?(\w\w)?(\w\w)?(\w\w)?(\w\w)?(\w\w)?(\w\w)?(\w\w)?$/o)
+ {
+ my @m;
+ push @m, $1 if defined $1;
+ push @m, $2 if defined $2;
+ push @m, $3 if defined $3;
+ push @m, $4 if defined $4;
+ push @m, $5 if defined $5;
+ push @m, $6 if defined $6;
+ push @m, $7 if defined $7;
+ push @m, $8 if defined $8;
+ push @m, $9 if defined $9;
+ prcmd \%cmd, @m;
+ }
+}