parent
a7c21e8147
commit
6e6f61d04b
@ -1,20 +1,82 @@ |
||||
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("/dev/ttyUSB0") |
||||
err := t.Connect(*serial_device, *serial_baudrate) |
||||
if err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
t.Handle() |
||||
defer t.Close() |
||||
|
||||
//cmd := &Command{"Hi", []string{"v20170802 testing"}}
|
||||
//t.Commands <- cmd
|
||||
res := <-t.Results |
||||
log.Printf("Got result: %q", res) |
||||
err = command_function(t, args[1:]) |
||||
if err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue