package rs8 import ( "bufio" "encoding/hex" "errors" "fmt" "github.com/tarm/serial" "io" "log" "strings" "time" ) type Connection struct { *serial.Port LightCmdCh chan<- []CmdLight } func Open(name string) (*Connection, error) { p, err := serial.OpenPort(&serial.Config{ Name: name, Baud: 9600, }) if err != nil { return nil, err } c := &Connection{p, nil} go c.handleCmds() return c, nil } type Event interface { event() } type EventButton struct { Type EventButtonType Direction Direction Value uint8 } type Direction uint8 const ( Down Direction = iota Up ) func (*EventButton) event() {} type EventButtonType uint8 const ( ButtonProgram EventButtonType = 0b0010 ButtonPreview = 0b0001 ButtonAutoTakeDSK = 0b0011 ) type EventSlider struct { Type EventSliderType Value uint8 } func (*EventSlider) event() {} type EventSliderType uint8 const ( SliderTbar EventSliderType = iota RotA RotB RotC ) type CmdLight struct { Type EventButtonType Value uint8 State Light } type Light uint8 const ( LightOn Light = iota LightOff ) // first nibble of first byte is ignored func (c *Connection) WriteCmd(cmd [2]byte) (err error) { h := strings.ToUpper(hex.EncodeToString(cmd[:])) log.Printf("sending: ~%s\r", h[1:]) _, err = fmt.Fprintf(c.Port, "~%s\r", h[1:]) return } func (c *Connection) handleCmds() { ch := make(chan []CmdLight) c.LightCmdCh = ch ledmap := make(map[EventButtonType]uint8) ledmap[ButtonProgram] = 0xFF ledmap[ButtonPreview] = 0xFF ledmap[ButtonAutoTakeDSK] = 0xFF for { changed := make(map[EventButtonType]bool) cmds := <-ch //log.Printf("got cmds: %d %#v", len(cmds), nil) for _, cmd := range cmds { var ( oldstate = T(ledmap[cmd.Type]&(1<