forked from OliRead/usbdmx
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusbdmx.go
More file actions
101 lines (84 loc) · 2.58 KB
/
usbdmx.go
File metadata and controls
101 lines (84 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package usbdmx
import (
"fmt"
"strconv"
"github.com/BurntSushi/toml"
"github.com/google/gousb"
)
// Controller Generic interface for all USB DMX controllers
type Controller interface {
Connect() (err error)
GetSerial() (info string, err error)
GetProduct() (info string, err error)
SetChannel(channel int16, value byte) error
GetChannel(channel int16) (byte, error)
Render() error
}
// ControllerConfig configuration for controlling device
type ControllerConfig struct {
VID uint16 `toml:"vid"`
PID uint16 `toml:"pid"`
OutputInterfaceID int `toml:"output_interface_id"`
InputInterfaceID int `toml:"input_interface_id"`
DebugLevel int `toml:"debug_level"`
Context *gousb.Context
}
// ReadConfigFile reads device configuration information from file
func ReadConfigFile(path string) (ControllerConfig, error) {
type raw struct {
VID string `toml:"VID"`
PID string `toml:"PID"`
OutputInterfaceID string `toml:"outputInterfaceID"`
InputInterfaceID string `toml:"inputInterfaceID"`
DebugLevel int `toml:"debugLevel"`
}
rawConf := raw{}
conf := ControllerConfig{}
if _, err := toml.DecodeFile(path, &rawConf); err != nil {
return conf, err
}
vid, err := strconv.ParseUint(rawConf.VID, 16, 16)
if err != nil {
return conf, err
}
pid, err := strconv.ParseUint(rawConf.PID, 16, 16)
if err != nil {
return conf, err
}
oiid, err := strconv.ParseInt(rawConf.OutputInterfaceID, 16, 16)
if err != nil {
return conf, err
}
iiid, err := strconv.ParseInt(rawConf.InputInterfaceID, 16, 16)
if err != nil {
return conf, err
}
conf.VID = uint16(vid)
conf.PID = uint16(pid)
conf.OutputInterfaceID = int(oiid)
conf.InputInterfaceID = int(iiid)
conf.DebugLevel = rawConf.DebugLevel
return conf, nil
}
// NewConfig helper function for creating a new ControllerConfig
func NewConfig(vid, pid uint16, outputInterfaceID, inputInterfaceID, debugLevel int) ControllerConfig {
return ControllerConfig{
VID: vid,
PID: pid,
OutputInterfaceID: outputInterfaceID,
InputInterfaceID: inputInterfaceID,
DebugLevel: debugLevel,
}
}
// ValidateDMXChannel helper function for ensuring channel is within range
func ValidateDMXChannel(channel int) (err error) {
if channel < 1 || channel > 512 {
return fmt.Errorf("channel %d out of range, must be between 1 and 512", channel)
}
return nil
}
// GetUSBContext gets a gousb/context for a given configuration
func (c *ControllerConfig) GetUSBContext() {
c.Context = gousb.NewContext()
c.Context.Debug(c.DebugLevel)
}