-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkeypad.go
271 lines (236 loc) · 5.97 KB
/
keypad.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright 2014 Google. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package lutron
import (
"fmt"
"log"
"strconv"
"strings"
"sync"
)
const (
ButtonPress = 3
ButtonRelease = 4
// Valid states for an LED on a keypad.
LedOff = 0
LedOn = 1
LedNormalFlash = 2 // 1 flash every second.
LedRapidFlash = 3 // 10 flashes every second.
LedUndefined = 255
// IDs of buttons on a Pico remote "keypad".
PicoButtonOn = 2
PicoButtonPreset = 3
PicoButtonOff = 4
PicoButtonRaise = 5
PicoButtonLower = 6
)
type Keypad struct {
Component
mu sync.Mutex
buttons []keypadMonitor
pressed []keypadMonitor
leds []*ledMonitor
pending []ledMonitor
}
type keypadMonitor struct {
id uint8
events uint8
signal chan uint8
}
type ledMonitor struct {
keypadMonitor
state uint8
valid bool
}
type KeypadButton struct {
k *Keypad
id uint8
}
// Create a reference to a button on the keypad. Buttons are numbered 1-N.
// See integration guide for mapping, e.g. 1B keypads use only button 4.
func (k *Keypad) Button(button uint8) *KeypadButton {
return &KeypadButton{k, button}
}
// Press the button on the keypad by sending ButtonPress immediately
// followed by ButtonRelease. The returned channel is signaled once
// with ButtonRelease when the repeater has acknowledged the action.
//
// If the application is monitoring the button the monitoring channel(s)
// will also be signaled as the repeater acknowledges the action.
func (b *KeypadButton) Press() chan uint8 {
k := b.k
k.mu.Lock()
defer k.mu.Unlock()
c := make(chan uint8, 1)
m := keypadMonitor{id: b.id, events: 1 << ButtonRelease, signal: c}
k.pressed = append(k.pressed, m)
k.Execute(fmt.Sprintf("%d,%d", b.id, ButtonPress))
k.Execute(fmt.Sprintf("%d,%d", b.id, ButtonRelease))
return c
}
// Set the state of a button's LED to LedOn, LedOff, LedNormalFlash
// or LedRapidFlash. LED states can only be set if the button is
// unconfigured in the RadioRA2 software.
func (b *KeypadButton) SetLed(state uint8) chan uint8 {
k := b.k
k.mu.Lock()
defer k.mu.Unlock()
m := ledMonitor{
keypadMonitor: keypadMonitor{
id: b.id,
signal: make(chan uint8, 1)},
state: state}
k.pending = append(k.pending, m)
k.Execute(fmt.Sprintf("%d,9,%d", 80+b.id, state))
return m.signal
}
// Creates a new channel receiving ButtonPress each time the button
// is pressed. ButtonRelease events are not sent.
func (b *KeypadButton) Monitor() chan uint8 {
k := b.k
m := keypadMonitor{
id: b.id,
events: 1 << ButtonPress,
signal: make(chan uint8, 5)}
k.mu.Lock()
defer k.mu.Unlock()
k.buttons = append(k.buttons, m)
return m.signal
}
// Creates a new channel receiving ButtonPress followed by ButtonRelease
// each time the button is pressed. The common usage is to monitor only
// ButtonPress with Monitor() as most callers do not need to observe
// ButtonRelease.
func (b *KeypadButton) MonitorButton() chan uint8 {
k := b.k
m := keypadMonitor{
id: b.id,
events: (1 << ButtonPress) | (1 << ButtonRelease),
signal: make(chan uint8, 10)}
k.mu.Lock()
defer k.mu.Unlock()
k.buttons = append(k.buttons, m)
return m.signal
}
// Creates a new channel receiving LED state change events. Monitoring LEDs
// can be a useful way to react when a specific scene is selected or lights
// in a room are turned on or turned off. If no events are selected LedOff
// and LedOn will be selected by default.
func (b *KeypadButton) MonitorLed(events ...uint8) chan uint8 {
var mask uint8 = 0
if len(events) == 0 {
mask = (1 << LedOff) | (1 << LedOn)
} else {
for _, e := range events {
mask = mask | uint8(1<<e)
}
}
k := b.k
m := &ledMonitor{keypadMonitor: keypadMonitor{
id: b.id,
events: mask,
signal: make(chan uint8, 5)}}
k.mu.Lock()
defer k.mu.Unlock()
for _, e := range k.leds {
if e.valid && e.id == m.id {
m.state = e.state
m.valid = true
m.signal <- e.state
}
}
if !m.valid {
k.Query(fmt.Sprintf("%d,9", 80+m.id))
}
k.leds = append(k.leds, m)
return m.signal
}
func (k *Keypad) handleEvent(event string) error {
n := strings.Split(event, ",")
c, err := strconv.Atoi(n[0])
if err != nil {
return err
}
if 1 <= c && c <= 25 && len(n) == 2 {
// Button press or release on keypad.
action, err := strconv.Atoi(n[1])
if err != nil {
return err
}
k.handleButton(uint8(c), uint8(action))
} else if 81 <= c && c <= 95 && len(n) == 3 && n[1] == "9" {
// LED state change on keypad.
state, err := strconv.Atoi(n[2])
if err != nil {
return err
}
k.handleLed(uint8(c-80), uint8(state))
} else {
log.Printf("keypad %d ignoring %s", k.Id, event)
}
return nil
}
func (k *Keypad) handleButton(button, action uint8) {
k.mu.Lock()
defer k.mu.Unlock()
for _, b := range k.buttons {
if b.id == button && b.events&(1<<action) != 0 {
b.signal <- action
}
}
var r []keypadMonitor = nil
for _, b := range k.pressed {
if b.id == button && b.events&(1<<action) != 0 {
b.signal <- action
close(b.signal)
} else {
r = append(r, b)
}
}
k.pressed = r
}
func (k *Keypad) handleLed(led, state uint8) {
k.mu.Lock()
defer k.mu.Unlock()
for _, e := range k.leds {
if e.id == led && e.events&(1<<state) != 0 {
if !e.valid || e.state != state {
e.signal <- state
e.state = state
e.valid = true
}
}
}
var r []ledMonitor = nil
for _, b := range k.pending {
if b.id == led && b.state == state {
b.signal <- state
close(b.signal)
} else {
r = append(r, b)
}
}
k.pending = r
}
func (k *Keypad) reconnect() {
k.mu.Lock()
defer k.mu.Unlock()
// Key presses sent before connection lost have unknown results.
// Signal waiters to prevent deadlocking the integration.
for _, m := range k.pressed {
m.signal <- 0
close(m.signal)
}
k.pressed = nil
// Query LED states as they may have changed before reconnect.
p := 0
for _, l := range k.leds {
m := 1 << l.id
if p&m == 0 {
p = p | m
k.Query(fmt.Sprintf("%d,9", 80+l.id))
}
}
}