-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuimodule.go
More file actions
193 lines (164 loc) · 4.68 KB
/
uimodule.go
File metadata and controls
193 lines (164 loc) · 4.68 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
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
package bootstrap
import (
"context"
"sync"
"time"
europi "github.com/awonak/EuroPiGo"
"github.com/awonak/EuroPiGo/debounce"
"github.com/awonak/EuroPiGo/hardware/hal"
)
// LongPressDuration is the amount of time a button is in a held/pressed state before
// it is considered to be a 'long' press.
// TODO: This is eventually intended to be a persisted setting, configurable by the user.
const LongPressDuration = time.Millisecond * 650
type uiModule struct {
screen UserInterface[europi.Hardware]
logoPainter UserInterfaceLogoPainter[europi.Hardware]
repaintCh chan struct{}
stop context.CancelFunc
wg sync.WaitGroup
}
func (u *uiModule) setup(e europi.Hardware, screen UserInterface[europi.Hardware]) {
b1 := europi.Button(e, 0)
b2 := europi.Button(e, 1)
ui.screen = screen
if ui.screen == nil {
return
}
ui.logoPainter, _ = screen.(UserInterfaceLogoPainter[europi.Hardware])
ui.repaintCh = make(chan struct{}, 1)
var (
inputB1 func(e europi.Hardware, value bool, deltaTime time.Duration)
inputB1L func(e europi.Hardware, deltaTime time.Duration)
)
if in, ok := screen.(UserInterfaceButton1[europi.Hardware]); ok {
var debounceDelay time.Duration
if db, ok := screen.(UserInterfaceButton1Debounce); ok {
debounceDelay = db.Button1Debounce()
}
inputDB := debounce.NewDebouncer(func(value bool, deltaTime time.Duration) {
if !value {
in.Button1(e, deltaTime)
}
}).Debounce(debounceDelay)
inputB1 = func(e europi.Hardware, value bool, deltaTime time.Duration) {
inputDB(value)
}
} else if in, ok := screen.(UserInterfaceButton1Ex[europi.Hardware]); ok {
inputB1 = in.Button1Ex
}
if in, ok := screen.(UserInterfaceButton1Long[europi.Hardware]); ok {
inputB1L = in.Button1Long
}
ui.setupButton(e, b1, inputB1, inputB1L)
var (
inputB2 func(e europi.Hardware, value bool, deltaTime time.Duration)
inputB2L func(e europi.Hardware, deltaTime time.Duration)
)
if in, ok := screen.(UserInterfaceButton2[europi.Hardware]); ok {
var debounceDelay time.Duration
if db, ok := screen.(UserInterfaceButton2Debounce); ok {
debounceDelay = db.Button2Debounce()
}
inputDB := debounce.NewDebouncer(func(value bool, deltaTime time.Duration) {
if !value {
in.Button2(e, deltaTime)
}
}).Debounce(debounceDelay)
inputB2 = func(e europi.Hardware, value bool, deltaTime time.Duration) {
inputDB(value)
}
} else if in, ok := screen.(UserInterfaceButton2Ex[europi.Hardware]); ok {
inputB2 = in.Button2Ex
}
if in, ok := screen.(UserInterfaceButton2Long[europi.Hardware]); ok {
inputB2L = in.Button2Long
}
ui.setupButton(e, b2, inputB2, inputB2L)
}
func (u *uiModule) start(ctx context.Context, e europi.Hardware, interval time.Duration) {
ui.wg.Add(1)
go ui.run(ctx, e, interval)
}
func (u *uiModule) wait() {
u.wg.Wait()
}
func (u *uiModule) repaint() {
if u.repaintCh != nil {
u.repaintCh <- struct{}{}
}
}
func (u *uiModule) shutdown() {
if u.stop != nil {
u.stop()
}
if ui.repaintCh != nil {
close(ui.repaintCh)
}
ui.wait()
}
func (u *uiModule) run(ctx context.Context, e europi.Hardware, interval time.Duration) {
defer u.wg.Done()
disp := europi.Display(e)
if disp == nil {
// no display means no ui
// TODO: make uiModule work when any user input/output is specified, not just display
return
}
myCtx, cancel := context.WithCancel(ctx)
ui.stop = cancel
defer ui.stop()
t := time.NewTicker(interval)
defer t.Stop()
paint := func(deltaTime time.Duration) {
disp.ClearBuffer()
if u.logoPainter != nil {
u.logoPainter.PaintLogo(e, deltaTime)
}
u.screen.Paint(e, deltaTime)
_ = disp.Display()
}
lastTime := time.Now()
for {
select {
case <-myCtx.Done():
return
case <-ui.repaintCh:
now := time.Now()
deltaTime := now.Sub(lastTime)
lastTime = now
paint(deltaTime)
case now := <-t.C:
deltaTime := now.Sub(lastTime)
lastTime = now
paint(deltaTime)
}
}
}
func (u *uiModule) setupButton(e europi.Hardware, btn hal.ButtonInput, onShort func(e europi.Hardware, value bool, deltaTime time.Duration), onLong func(e europi.Hardware, deltaTime time.Duration)) {
if btn == nil {
return
}
if onShort == nil && onLong == nil {
return
}
if onShort == nil {
// no-op
onShort = func(e europi.Hardware, value bool, deltaTime time.Duration) {}
}
// if no long-press handler present, just reuse short-press handler
if onLong == nil {
onLong = func(e europi.Hardware, deltaTime time.Duration) {
onShort(e, false, deltaTime)
}
}
btn.HandlerEx(hal.ChangeAny, func(value bool, deltaTime time.Duration) {
if value {
onShort(e, value, deltaTime)
} else if deltaTime < LongPressDuration {
onShort(e, value, deltaTime)
} else {
onLong(e, deltaTime)
}
})
}