-
Notifications
You must be signed in to change notification settings - Fork 4
/
multipanel.go
243 lines (224 loc) · 5.39 KB
/
multipanel.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package fpanels
import (
"fmt"
"sync"
"github.com/google/gousb"
)
// Multi panel switches and buttons
const (
RotALT SwitchID = iota
RotVS
RotIAS
RotHDG
RotCRS
EncCW
EncCCW
BtnAP
BtnHDG
BtnNAV
BtnIAS
BtnALT
BtnVS
BtnAPR
BtnREV
AutoThrottle
FlapsUp
FlapsDown
TrimDown
TrimUp
)
// Multi panel button LED lights
const (
LEDAP byte = 1 << iota
LEDHDG
LEDNAV
LEDIAS
LEDALT
LEDVS
LEDAPR
LEDREV
)
const multiDash = 0xde
// Multi panel displays
const (
Row1 DisplayID = iota
Row2
)
// MultiPanel represents a Saitek/Logitech multi panel. The panel has:
//
// - A five position switch
//
// - Eight push buttons with individually controlable backlight.
//
// - A rotary encoder
//
// - A two position switch
//
// - A two position momentary switch
//
// - A pitch trim rotary encoder
//
// - A two row segment display with five numbers on each row. Use DisplayString or DisplayInt to display
// text on the panels. The displays are identified by the Row1 and Row2 constants.
type MultiPanel struct {
panel
}
// NewMultiPanel creates a new instances of the Logitech/Saitek multipanel
func NewMultiPanel() (*MultiPanel, error) {
var err error
panel := MultiPanel{}
panel.id = Multi
panel.displayState = make([]byte, 12)
for i := range panel.displayState {
panel.displayState[i] = blank
}
panel.displayState[10] = 0x00
panel.displayState[11] = 0xff
panel.displayDirty = true
panel.displayCond = sync.NewCond(&panel.displayMutex)
panel.ctx = gousb.NewContext()
panel.device, err = panel.ctx.OpenDeviceWithVIDPID(USBVendorPanel, USBProductMulti)
if panel.device == nil || err != nil {
panel.Close()
return nil, err
}
panel.device.SetAutoDetach(true)
// Initialize switches
panel.intf, panel.intfDone, err = panel.device.DefaultInterface()
if err != nil {
panel.Close()
return nil, err
}
panel.inEndpoint, err = panel.intf.InEndpoint(1)
if err != nil {
panel.Close()
return nil, err
}
// FIX: Add WaitGroup
panel.switchCh = make(chan SwitchState)
go panel.readSwitches()
go panel.refreshDisplay()
panel.connected = true
return &panel, nil
}
// LEDs turns on/off the LEDs given by leds. See the LED* constants.
// For example calling
// panel.LEDs(LEDAP | LEDVS)
// will turn on the AP and VS LEDs and turn off all other LEDs.
func (panel *MultiPanel) LEDs(leds byte) {
panel.displayMutex.Lock()
panel.displayState[10] = leds
panel.displayDirty = true
panel.displayCond.Signal()
panel.displayMutex.Unlock()
}
// LEDsOn turns on the LEDs given by leds and leaves all other LED states
// intact. See the LED* constants. Multiple LEDs can be ORed together,
// for example
// panel.LEDsOn(LEDAP | LEDVS)
func (panel *MultiPanel) LEDsOn(leds byte) {
panel.displayMutex.Lock()
panel.displayState[10] = panel.displayState[10] | leds
panel.displayDirty = true
panel.displayCond.Signal()
panel.displayMutex.Unlock()
}
// LEDsOff turns off the LEDs given by leds and leaves all other LED states
// intact. See the LED* constants. Multiple LEDs can be ORed together.
// For example
// panel.LEDsOff(LEDAP | LEDVS)
func (panel *MultiPanel) LEDsOff(leds byte) {
panel.displayMutex.Lock()
panel.displayState[10] = panel.displayState[10] & ^leds
panel.displayDirty = true
panel.displayCond.Signal()
panel.displayMutex.Unlock()
}
// LEDsOnOff turns on or off the LEDs given by leds. If val is 0 then
// the LEDs will be turned offm else they will be turned on. All
// other LEDs are left intact. See the LED* constants.
// Multiple LEDs can be ORed together, for example
// panel.LEDsOnOff(LEDAP | LEDVS, 1)
func (panel *MultiPanel) LEDsOnOff(leds byte, val float64) {
if val > 0 {
panel.LEDsOn(leds)
} else {
panel.LEDsOff(leds)
}
}
// DisplayString displays the string given by s on the display given by
// display. The string is limited to the numbers 0-9 and spaces. Row2 can
// additionally show a dash/minus '-'. If any other char is used the
// underlying previous character is left intact. This allows you to update
// different areas of the dislay in sepeate calls. For example:
// panel.DisplayString(Row1, "12 ")
// panel.DisplayString(Row1, "** 34")
// panel.DisplayString(Row1, "** 56")
// will display the the following sequence on the upper display:
// 12
// 12 34
// 12 56
func (panel *MultiPanel) DisplayString(display DisplayID, s string) {
if display != Row1 && display != Row2 {
return
}
var d [5]byte
charmap := map[rune]byte{
'0': 0,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
' ': blank,
'-': multiDash,
}
displayStart := int(display) * 5
disp := panel.displayState[displayStart : displayStart+5]
dIdx := 0
for _, c := range s {
if dIdx > 4 {
break
}
char, ok := charmap[c]
if ok {
d[dIdx] = char
} else {
// leave current char as is
d[dIdx] = disp[dIdx]
}
dIdx++
}
panel.displayMutex.Lock()
defer panel.displayMutex.Unlock()
panel.displayDirty = true
panel.displayCond.Signal()
dIdx--
// align right and fill with blanks
for i := 4; i >= 0; i-- {
if dIdx < 0 {
disp[i] = blank
} else {
disp[i] = d[dIdx]
}
dIdx--
}
}
// DisplayInt will display the integer n on the given display
func (panel *MultiPanel) DisplayInt(display DisplayID, n int) {
s := fmt.Sprintf("%d", n)
panel.DisplayString(display, s)
}
func (panel *MultiPanel) noZeroSwitch(s SwitchID) bool {
if s >= RotALT && s <= EncCCW {
return true
}
if s == TrimDown || s == TrimUp {
return true
}
return false
}