Skip to content

Commit

Permalink
kd6rmx add windows support
Browse files Browse the repository at this point in the history
  • Loading branch information
asasi-wu committed Oct 4, 2023
1 parent d3923fa commit 54cf8d9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ module github.com/northvolt/go-kd6rmx
go 1.17

require github.com/peterbourgon/ff/v3 v3.1.0

require (
github.com/creack/goselect v0.1.2 // indirect
go.bug.st/serial v1.6.1 // indirect
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/creack/goselect v0.1.2 h1:2DNy14+JPjRBgPzAd1thbQp4BSIihxcBf0IXhQXDRa0=
github.com/creack/goselect v0.1.2/go.mod h1:a/NhLweNvqIYMuxcMOuWY516Cimucms3DglDzQP3hKY=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys=
github.com/peterbourgon/ff/v3 v3.1.0 h1:5JAeDK5j/zhKFjyHEZQXwXBoDijERaos10RE+xamOsY=
github.com/peterbourgon/ff/v3 v3.1.0/go.mod h1:XNJLY8EIl6MjMVjBS4F0+G0LYoAqs0DTa4rmHHukKDE=
go.bug.st/serial v1.6.1 h1:VSSWmUxlj1T/YlRo2J104Zv3wJFrjHIl/T3NeruWAHY=
go.bug.st/serial v1.6.1/go.mod h1:UABfsluHAiaNI+La2iESysd9Vetq7VRdpxvjx7CmmOE=
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 h1:v6hYoSR9T5oet+pMXwUWkbiVqx/63mlHjefrHmxwfeY=
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
46 changes: 27 additions & 19 deletions kd6rmx.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import (
"strconv"
"strings"
"time"

"go.bug.st/serial"
)

// Sensor is a wrapper for control functions for the KD6RMX contact image sensor.
type Sensor struct {
Port string
BaudRate int
Logging bool
FileLogging bool
}
Expand All @@ -24,10 +27,13 @@ func (cis Sensor) CommunicationSpeed(baud int) error {
switch baud {
case 9600:
param = "00"
cis.BaudRate = 9600
case 19200:
param = "01"
cis.BaudRate = 19200
case 115200:
param = "02"
cis.BaudRate = 115200
default:
return errors.New("invalid baud rate")
}
Expand Down Expand Up @@ -132,8 +138,7 @@ const (
//
// For example:
//
// cis.PixelOutputFormat(kd6rmx.PixelOutputBits10, kd6rmx.PixelOutputSerial, kd6rmx.PixelOutputBase, 1)
//
// cis.PixelOutputFormat(kd6rmx.PixelOutputBits10, kd6rmx.PixelOutputSerial, kd6rmx.PixelOutputBase, 1)
func (cis Sensor) PixelOutputFormat(bits PixelOutputBits, i PixelOutputInterface, conf PixelOutputConfig, number int) error {
var param string
switch bits {
Expand Down Expand Up @@ -304,10 +309,11 @@ func (cis Sensor) InternalSync(val int) error {
// LoadSettings loads the sensor's active settings with one of the memory presets.
//
// Valid presets are:
// 0 (factory defaults)
// 1 (user settings 1)
// 2 (user settings 2)
// 3 (user settings 3)
//
// 0 (factory defaults)
// 1 (user settings 1)
// 2 (user settings 2)
// 3 (user settings 3)
func (cis Sensor) LoadSettings(preset int) error {
if preset < 0 || preset > 4 {
return errors.New("invalid preset for LoadSettings")
Expand All @@ -324,9 +330,10 @@ func (cis Sensor) LoadSettings(preset int) error {
// SaveSettings saves the sensor's active settings to one of the memory presets.
//
// Valid presets are:
// 1 (user settings 1)
// 2 (user settings 2)
// 3 (user settings 3)
//
// 1 (user settings 1)
// 2 (user settings 2)
// 3 (user settings 3)
func (cis Sensor) SaveSettings(preset int) error {
if preset < 1 || preset > 4 {
return errors.New("invalid preset for SaveSettings")
Expand All @@ -343,10 +350,11 @@ func (cis Sensor) SaveSettings(preset int) error {
// LEDControl sets the LED settings. You can do the following:
// Turn on/off the A and B LEDS individually.
// Set the pulse divider to be used for both LEDS so they are:
// 1 = on all of the time
// 2 = on one half of the time
// 4 = on one quarter of the time
// 8 = on one eighth of the time
//
// 1 = on all of the time
// 2 = on one half of the time
// 4 = on one quarter of the time
// 8 = on one eighth of the time
func (cis Sensor) LEDControl(leds string, on bool, pulsedivider int) error {
var pd int
switch pulsedivider {
Expand Down Expand Up @@ -584,12 +592,13 @@ func (cis Sensor) SoftwareReset() error {
}

func (cis Sensor) SendCommand(cmd string, params string) (string, error) {

f, err := os.OpenFile(cis.Port, os.O_RDWR|os.O_APPEND, 0777)
mode := &serial.Mode{
BaudRate: cis.BaudRate,
}
port, err := serial.Open(cis.Port, mode)
if err != nil {
return "", fmt.Errorf("error opening control port: %v", err)
}
defer f.Close()

write_string := cmd + params + "\r"
dt_string := time.Now().Format("2006-01-02 15:04:05.000000000")
Expand All @@ -614,7 +623,7 @@ func (cis Sensor) SendCommand(cmd string, params string) (string, error) {
fmt.Println("send: ", write_string)
}

_, err = f.Write([]byte(write_string))
_, err = port.Write([]byte(write_string))
if err != nil {
return "", fmt.Errorf("error sending command: %v", err)
}
Expand All @@ -623,8 +632,7 @@ func (cis Sensor) SendCommand(cmd string, params string) (string, error) {
var result string
start := time.Now()
for {

n, err := f.Read(buf)
n, err := port.Read(buf)
if err != nil {
if err == io.EOF {
if time.Since(start) > time.Second*10 {
Expand Down

0 comments on commit 54cf8d9

Please sign in to comment.