From 4e10ada7080941d32885c2c6a0e4fc8d8b9fc938 Mon Sep 17 00:00:00 2001 From: "S.J.R. van Schaik" Date: Tue, 31 Oct 2017 17:55:02 +0100 Subject: [PATCH] protocol: update commands --- protocol/main.go | 45 ++++++++++++++++++++++++++++++++++++--------- protocol/tbm.go | 22 +++++++++++++++++----- 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/protocol/main.go b/protocol/main.go index 6c8cd1f..a876643 100644 --- a/protocol/main.go +++ b/protocol/main.go @@ -12,11 +12,22 @@ var ( serial_device = flag.String("serial-device", "/dev/ttyUSB0", "Serial device to use") serial_baudrate = flag.Int("serial-baud", 9600, "Serial baud rate") - commands = map[string]CommandFunc{"hi": cmd_hi, "ls": cmd_ls, "cat": cmd_cat, "time": cmd_time, "bootversion": cmd_bootversion, "bootok": cmd_bootok} + commands = map[string]CommandFunc{ + "hi": cmd_hi, + "buzzer": cmd_buzzer, + "led": cmd_led, + "reset": cmd_reset, + "ls": cmd_ls, + "cat": cmd_cat, + "date": cmd_date, + "time": cmd_time, + "boot_version": cmd_bootversion, + "boot_ok": cmd_bootok, + } ) -func cmd_generic(tbm *TBM, command string, args []string) error { - cmd := &Command{command, args} +func cmd_generic(tbm *TBM, command string, args []string, payload []byte) error { + cmd := &Command{command, args, payload} tbm.Commands <- cmd res := <-tbm.Results @@ -29,27 +40,43 @@ func cmd_generic(tbm *TBM, command string, args []string) error { } func cmd_hi(tbm *TBM, args []string) error { - return cmd_generic(tbm, "hi", []string{"v20170802 testing"}) + return cmd_generic(tbm, "hi", []string{"v20170802 testing"}, nil) +} + +func cmd_buzzer(tbm *TBM, args []string) error { + return cmd_generic(tbm, "buzzer", args, nil) +} + +func cmd_led(tbm *TBM, args []string) error { + return cmd_generic(tbm, "led", args, nil) +} + +func cmd_reset(tbm *TBM, args []string) error { + return cmd_generic(tbm, "reset", args, nil) } func cmd_ls(tbm *TBM, args []string) error { - return cmd_generic(tbm, "ls", args) + return cmd_generic(tbm, "ls", args, nil) } func cmd_cat(tbm *TBM, args []string) error { - return cmd_generic(tbm, "cat", args) + return cmd_generic(tbm, "cat", args, nil) +} + +func cmd_date(tbm *TBM, args []string) error { + return cmd_generic(tbm, "time", args, nil) } func cmd_time(tbm *TBM, args []string) error { - return cmd_generic(tbm, "time", args) + return cmd_generic(tbm, "time", args, nil) } func cmd_bootversion(tbm *TBM, args []string) error { - return cmd_generic(tbm, "booting", args) + return cmd_generic(tbm, "booting", args, nil) } func cmd_bootok(tbm *TBM, args []string) error { - return cmd_generic(tbm, "booting", []string{"ok"}) + return cmd_generic(tbm, "booting", []string{"ok"}, nil) } func main() { diff --git a/protocol/tbm.go b/protocol/tbm.go index 2b58b0f..e56e95a 100644 --- a/protocol/tbm.go +++ b/protocol/tbm.go @@ -5,6 +5,7 @@ import ( "io" "log" "strings" + "time" "github.com/tarm/serial" ) @@ -18,6 +19,7 @@ type TBM struct { type Command struct { command string args []string + payload []byte } type Result struct { @@ -124,8 +126,9 @@ func handleResults(in io.Reader, c chan *Result) { } } -func serialise(out io.Writer, command string, args []string) error { +func serialise(out io.Writer, command string, args []string, payload []byte) error { var quoted_args []string + var err error quoted_args = append(quoted_args, command) @@ -137,20 +140,29 @@ func serialise(out io.Writer, command string, args []string) error { output := []byte(out_string) output = append(output, '\r') - //output = append(output, 0x4) - _, err := out.Write(output) + _, err = out.Write(output) // err is non nil if n != len(output) if err != nil { return err } - return nil + if payload == nil { + return nil + } + + // FIXME: wait for command to finish + time.Sleep(time.Second) + + output = append(payload, 0x4) + _, err = out.Write(output) + + return err } func handleCommands(out io.Writer, c chan *Command) { for command := range c { - err := serialise(out, command.command, command.args) + err := serialise(out, command.command, command.args, command.payload) if err != nil { log.Fatal(err) }