-
Notifications
You must be signed in to change notification settings - Fork 0
/
webcam.go
218 lines (188 loc) · 4.06 KB
/
webcam.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
package main
import (
"fmt"
"math"
"sort"
"strings"
"github.com/blackjack/webcam"
"github.com/evertras/bubble-table/table"
)
type ControlID = webcam.ControlID
// Control .
type Control struct {
ID ControlID
Name string
Min int32
Max int32
Value int32
Step int32
Type controlType
Flags uint32
}
// TODO temporary copy
type controlType int
const (
c_int controlType = iota
c_bool
c_menu
)
func (c Control) String() string {
return fmt.Sprintf("(id:%v val:%v min:%v max:%v name:%v)", c.ID, c.Value, c.Min, c.Max, c.Name)
}
func (c Control) IsBoolean() bool {
return c.Type == c_bool || (c.Min == 0 && c.Max == 1)
}
func (c Control) IsMenu() bool {
return c.Type == c_menu
}
func (c Control) ToggleBoolean() int32 {
if c.Value == c.Min {
return c.Max
}
return c.Min
}
func (c Control) GetValueIncreaseStep() int32 {
switch {
case c.IsBoolean():
return c.ToggleBoolean()
case c.IsMenu():
return min(c.Max, c.Value+1)
default:
return c.GetStepsChange(1)
}
}
func (c Control) GetValueDecreseStep() int32 {
switch {
case c.IsBoolean():
return c.ToggleBoolean()
case c.IsMenu():
return max(c.Min, c.Value-1)
default:
return c.GetStepsChange(-1)
}
}
func (c Control) GetStepsChange(steps int32) int32 {
stepSize := max(1, c.Step)
nextValue := c.Value + steps*stepSize
switch {
case steps < 0:
nextValue = max(nextValue, c.Min)
case steps > 0:
nextValue = min(nextValue, c.Max)
}
return nextValue
}
func (c Control) GetValueIncreasePercent() int32 {
switch {
case c.IsBoolean():
return c.ToggleBoolean()
case c.IsMenu():
return min(c.Max, c.Value+1)
default:
return c.GetPercentChange(0.02)
}
}
func (c Control) GetValueDecreasePercent() int32 {
switch {
case c.IsBoolean():
return c.ToggleBoolean()
case c.IsMenu():
return max(c.Min, c.Value-1)
default:
return c.GetPercentChange(-0.02)
}
}
func (c Control) GetPercentChange(add float64) int32 {
stepSize := max(1, c.Step)
prevPercent := c.Percent()
nextPercent := prevPercent + add
span := float64(c.Max) - float64(c.Min)
nextValue := int32(math.Round(span*nextPercent + float64(c.Min)))
switch {
case add < 0:
nextValue = min(nextValue, c.Value-stepSize)
nextValue = max(nextValue, c.Min)
case add > 0:
nextValue = max(nextValue, c.Value+stepSize)
nextValue = min(nextValue, c.Max)
}
return nextValue
}
func (c Control) Percent() float64 {
return (float64(c.Value) - float64(c.Min)) / (float64(c.Max) - float64(c.Min))
}
func (c Control) Row(barWidth int) table.Row {
var bar string
switch {
case c.IsBoolean():
if c.Value == 0 {
bar = "OFF"
} else {
bar = "ON"
}
case c.IsMenu():
bar = fmt.Sprintf("%v", c.Value)
default:
bar = percentBar(barWidth, c.Percent())
}
return table.NewRow(table.RowData{
"name": c.Name,
"min": c.Min,
"max": c.Max,
"value": c.Value,
"bar": bar,
// hidden
"control": c,
})
}
func newCam(device string) (*Webcam, error) {
cam, err := webcam.Open(device)
if err != nil {
return nil, err
}
return &Webcam{
webcam: cam,
}, nil
}
type Webcam struct {
webcam *webcam.Webcam
}
func (wc *Webcam) getControls() []Control {
controlsMap := wc.webcam.GetControls()
var controls []Control
for ci, c := range controlsMap {
value, err := wc.webcam.GetControl(ci)
if err != nil {
panic(err)
}
controls = append(controls,
Control{
ID: ci,
Name: c.Name,
Min: c.Min,
Max: c.Max,
Step: c.Step,
Type: controlType(c.Type),
Value: value,
})
}
sort.Slice(controls, func(i, j int) bool {
return controls[i].ID < controls[j].ID
})
return controls
}
func (wc *Webcam) Close() error {
return wc.webcam.Close()
}
func percentBar(width int, percent float64) string {
w := float64(width)
fullSize := int(math.Round(w * percent))
emptySize := int(w) - fullSize
return strings.Repeat("█", fullSize) + strings.Repeat("░", emptySize)
}
func controlValue(control Control, percent float64) int32 {
return int32(
min(float64(control.Max),
max((float64(control.Min)),
((float64(control.Max)-float64(control.Min))*percent)+float64(control.Min))))
}