protocol: update commands

master
S.J.R. van Schaik 7 years ago
parent f2dda00741
commit 4e10ada708
  1. 45
      protocol/main.go
  2. 20
      protocol/tbm.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() {

@ -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
}
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)
}

Loading…
Cancel
Save