-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaxisdevice.go
163 lines (116 loc) · 3.17 KB
/
axisdevice.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
package inputeventsubsystem
import (
"errors"
)
var (
ErrDeviceHasNoAxis = errors.New("the device has no axis")
)
type axis_correct struct {
Coef []int32
Maximum int32
Minimum int32
Median int32
Scale float64
}
type AxisDevice struct {
Device *Device
useDeadZone bool
axis_corrections map[int]axis_correct
}
func CreateAxisDeviceFromAbsInfo(e *Device, absinfos map[int]AbsInfo, useDeadZone bool, overrideFlatValue int) *AxisDevice {
var ad AxisDevice
ad.axis_corrections = make(map[int]axis_correct, 0)
ad.useDeadZone = useDeadZone
ad.Device = e
for abs_code, absinfo := range absinfos {
var a axis_correct
a.Coef = make([]int32, 3)
if absinfo.Maximum == 0 && absinfo.Minimum == 0 {
absinfo.Minimum = -32767
absinfo.Maximum = 32767
}
if useDeadZone {
if overrideFlatValue >= 0 {
absinfo.Flat = int32(overrideFlatValue)
}
a.Coef[0] = (absinfo.Maximum + absinfo.Minimum) - 2*absinfo.Flat
a.Coef[1] = (absinfo.Maximum + absinfo.Minimum) + 2*absinfo.Flat
t := ((absinfo.Maximum - absinfo.Minimum) - 4*absinfo.Flat)
if t != 0 {
a.Coef[2] = (1 << 28) / t
} else {
a.Coef[2] = 0
}
}
a.Maximum = absinfo.Maximum
a.Minimum = absinfo.Minimum
var value_range float64 = float64(absinfo.Maximum - absinfo.Minimum - 1)
var output_range float64 = float64(65534)
a.Scale = float64(output_range) / float64(value_range)
a.Median = int32(value_range) / 2
ad.axis_corrections[abs_code] = a
}
return &ad
}
func (e *Device) AxisDevice(UseDeadZone bool, overrideFlatValue int) (*AxisDevice, error) {
var ad *AxisDevice
var err error
if _, ok := e.Capabilities[EV_ABS]; ok {
ad = CreateAxisDeviceFromAbsInfo(e, e.Absinfos, UseDeadZone, overrideFlatValue)
} else {
err = ErrDeviceHasNoAxis
}
return ad, err
}
func (a *AxisDevice) GetRange(which int) (int32, int32) {
if array_correction, ok := a.axis_corrections[which]; ok {
return array_correction.Minimum, array_correction.Maximum
}
return 0, 0
}
func (a *AxisDevice) CorrectAxisWithDeadzone(which int, value int32, deadzone bool) int32 {
if a.useDeadZone && deadzone {
if array_correction, ok := a.axis_corrections[which]; !ok {
return value
} else {
value = value * 2
if value > array_correction.Coef[0] {
if value < array_correction.Coef[1] {
return 0
}
value = value - array_correction.Coef[1]
} else {
value = value - array_correction.Coef[0]
}
value = value * array_correction.Coef[2]
value >>= 13
}
} else {
if array_correction, ok := a.axis_corrections[which]; !ok {
return value
} else {
offset_scale := (value - array_correction.Minimum)
value = int32(float64(offset_scale-array_correction.Median) * array_correction.Scale)
}
}
/* Clamp and return */
if value < -32767 {
return -32767
}
if value > 32767 {
return 32767
}
return value
}
func (a *AxisDevice) CorrectAxis(which int, value int32) int32 {
return a.CorrectAxisWithDeadzone(which, value, true)
}
func (a *AxisDevice) Read() chan []*Event {
return a.Device.Read()
}
func (a *AxisDevice) ReadDone(events []*Event) {
a.Device.ReadDone(events)
}
func (a *AxisDevice) Close() error {
return a.Device.Close()
}