You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
1.7 KiB
82 lines
1.7 KiB
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
)
|
|
|
|
type CommandFunc func(tbm *TBM, args []string) error
|
|
|
|
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}
|
|
)
|
|
|
|
func cmd_generic(tbm *TBM, command string, args []string) error {
|
|
cmd := &Command{command, args}
|
|
tbm.Commands <- cmd
|
|
res := <-tbm.Results
|
|
|
|
if res.errcode[0] != '0' {
|
|
return fmt.Errorf("Error: %s", res.errcode)
|
|
}
|
|
fmt.Printf("%s", res.output)
|
|
|
|
return nil
|
|
}
|
|
|
|
func cmd_hi(tbm *TBM, args []string) error {
|
|
return cmd_generic(tbm, "hi", []string{"v20170802 testing"})
|
|
}
|
|
|
|
func cmd_ls(tbm *TBM, args []string) error {
|
|
return cmd_generic(tbm, "ls", args)
|
|
}
|
|
|
|
func cmd_cat(tbm *TBM, args []string) error {
|
|
return cmd_generic(tbm, "cat", args)
|
|
}
|
|
|
|
func cmd_time(tbm *TBM, args []string) error {
|
|
return cmd_generic(tbm, "time", args)
|
|
}
|
|
|
|
func cmd_bootversion(tbm *TBM, args []string) error {
|
|
return cmd_generic(tbm, "booting", args)
|
|
}
|
|
|
|
func cmd_bootok(tbm *TBM, args []string) error {
|
|
return cmd_generic(tbm, "booting", []string{"ok"})
|
|
}
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
|
|
args := flag.Args()
|
|
if len(args) == 0 {
|
|
log.Fatalf("Please specify at least one command")
|
|
}
|
|
command := args[0]
|
|
|
|
command_function, ok := commands[command]
|
|
|
|
if !ok {
|
|
log.Fatalf("Unknown command: %s", command)
|
|
}
|
|
|
|
t := &TBM{}
|
|
err := t.Connect(*serial_device, *serial_baudrate)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
t.Handle()
|
|
defer t.Close()
|
|
|
|
err = command_function(t, args[1:])
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|