-
Notifications
You must be signed in to change notification settings - Fork 2
/
extension.go
122 lines (111 loc) · 3.05 KB
/
extension.go
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package eiscp
import (
"encoding/hex"
"strconv"
"strings"
)
// Source name of input channel
type Source string
// Sample sources
const (
SrcVCR Source = "00"
SrcCBL = "01"
SrcGame = "02"
SrcAux1 = "03"
SrcAux2 = "04"
SrcPC = "05"
SrcDVD = "10"
SrcPhono = "22"
SrcCD = "23"
SrcFM = "24"
SrcAM = "25"
SrcTuner = "26"
SrcDLNA2 = "27"
SrcInternetRadio = "28"
SrcUsbFront = "29"
SrcUsbRear = "2A"
SrcNetwork = "2B"
)
// SourceByName - map channel name to source enum const
var SourceByName = map[string]Source{
"vcr": SrcVCR,
"cbl": SrcCBL,
"game": SrcGame,
"aux1": SrcAux1,
"aux2": SrcAux2,
"pc": SrcPC,
"dvd": SrcDVD,
"phono": SrcPhono,
"cd": SrcCD,
"fm": SrcFM,
"am": SrcAM,
"tuner": SrcTuner,
"dlna2": SrcDLNA2,
"internet-radio": SrcInternetRadio,
"usb-front": SrcUsbFront,
"usb-rear": SrcUsbRear,
"network": SrcNetwork,
}
// SourceToName - map source enum to channel name
var SourceToName = map[Source]string{
SrcVCR: "vcr",
SrcCBL: "cbl",
SrcGame: "game",
SrcAux1: "aux1",
SrcAux2: "aux2",
SrcPC: "pc",
SrcDVD: "dvd",
SrcPhono: "phono",
SrcCD: "cd",
SrcFM: "fm",
SrcAM: "am",
SrcTuner: "tuner",
SrcDLNA2: "dlna2",
SrcInternetRadio: "internet-radio",
SrcUsbFront: "usb-front",
SrcUsbRear: "usb-rear",
SrcNetwork: "network",
}
// SetSource - Set Onkyo source channel
func (d *Device) SetSource(source Source) error {
return d.requestSet("SLI", string(source))
}
// GetSource - Get Onkyo source channel. Use SourceToName to get readable name
func (d *Device) GetSource() (Source, error) {
err := d.WriteCommand("SLI", "QSTN")
if err != nil {
return Source("err"), err
}
msg, err := d.ReadMessage()
return Source(msg.ISCP[3:]), err
}
// SetPower - turn on/off Onkyo device
func (d *Device) SetPower(on bool) error {
if on {
return d.requestSet("PWR", "01")
}
return d.requestSet("PWR", "00")
}
// GetPower - get Onkyo power state
func (d *Device) GetPower() (bool, error) {
err := d.WriteCommand("PWR", "QSTN")
if err != nil {
return false, err
}
msg, err := d.ReadMessage()
return string(msg.ISCP[3:]) == "01", err
}
// SetVolume - set master volume in Onkyo receiver
func (d *Device) SetVolume(level uint8) error {
return d.requestSet("MVL", strings.ToUpper(hex.EncodeToString([]byte{level})))
}
// GetVolume - get master volume in Onkyo receiver
func (d *Device) GetVolume() (uint8, error) {
err := d.WriteCommand("MVL", "QSTN")
if err != nil {
return 0, err
}
msg, err := d.ReadMessage()
vol, err := strconv.ParseUint(string(msg.ISCP[3:]), 16, 8)
return uint8(vol), err
}