Add the serial read functionality and run into troubles with vim

This commit is contained in:
Aaron Fischer 2016-07-21 23:04:48 +02:00
parent 05c15ab624
commit 145ecf4c4c

View file

@ -1,25 +1,46 @@
package main package main
// Create a new virtual serial device:
// socat PTY,link=/dev/ttyS10 PTY,link=/dev/ttyS11
import ( import (
"log" "log"
"os" "os"
"path" "path"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"github.com/tarm/serial"
"github.com/mkideal/cli" "github.com/mkideal/cli"
) )
// TODO: Make the error code optional (as a parameter)
func stop() { func stop() {
log.Println("Have a nice day.") log.Println("Have a nice day.")
// TODO: Make the error code optional (as a parameter) os.Exit(0)
os.Exit(1)
} }
func waitAndExecuteCommandsFromDevice() { func waitAndExecuteCommandsFromDevice(device string) {
// TODO: Establish connection to the device config := &serial.Config{Name: device, Baud: 115200}
// TODO: Wait for action loop serial, err := serial.OpenPort(config)
// TODO: Execute action
// TODO: Wait for action loop ... if err != nil {
log.Fatal(err)
}
log.Printf("Open the connection to the n3rdpad over %s", device)
// Read from serial
buffer := make([]byte, 128)
for {
num, err := serial.Read(buffer)
if err != nil {
log.Fatal(err)
}
// TODO: Do something with the incomind commands
log.Printf("%q", buffer[:num])
}
} }
func watchForConfigChanges(configFile string) { func watchForConfigChanges(configFile string) {
@ -47,6 +68,10 @@ func watchForConfigChanges(configFile string) {
// TODO: Close the connection? // TODO: Close the connection?
} }
// TODO: vim has a very strange behaviour to save a file. There is
// no WRITE event triggered. Instead: RENAME -> CHMOD -> REMOVE is
// called on that file and after that, this mechanism lost track of
// the file. Why is that tha case? Cant find a valid answer to that.
if event.Op&fsnotify.Rename == fsnotify.Rename || if event.Op&fsnotify.Rename == fsnotify.Rename ||
event.Op&fsnotify.Remove == fsnotify.Remove { event.Op&fsnotify.Remove == fsnotify.Remove {
log.Println("The configuration file is renamed or removed. We cant do any further changes without the config file. So create the file or rename it back and restart the agent.") log.Println("The configuration file is renamed or removed. We cant do any further changes without the config file. So create the file or rename it back and restart the agent.")
@ -70,6 +95,7 @@ func watchForConfigChanges(configFile string) {
type argT struct { type argT struct {
cli.Helper cli.Helper
ConfigFile string `cli:"c,config" usage:"specify a keymap config file"` ConfigFile string `cli:"c,config" usage:"specify a keymap config file"`
Device string `cli:"d,device" usage:"specify the serial port, the n3rdpad is hooked to"`
} }
func main() { func main() {
@ -81,12 +107,16 @@ func main() {
if args.ConfigFile != "" { if args.ConfigFile != "" {
configFile = args.ConfigFile configFile = args.ConfigFile
} }
if args.Device == "" {
log.Fatal("You need to specify the serial device with the --device parameter.")
stop();
}
if !args.Help { if !args.Help {
// Start up the application // Start up the application
log.Println("Using the configuration file", configFile) log.Println("Using the configuration file", configFile)
watchForConfigChanges(configFile) go watchForConfigChanges(configFile)
waitAndExecuteCommandsFromDevice() waitAndExecuteCommandsFromDevice(args.Device)
stop() stop()
} }