-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtouchscreen.go
More file actions
53 lines (48 loc) · 1.29 KB
/
touchscreen.go
File metadata and controls
53 lines (48 loc) · 1.29 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
package touch
// TODO: This needs to be based around an affine transform
type TouchscreenCalibration struct {
MinX, MinY, MaxX, MaxY int
Weak, Strong int
// Cached Values for faster conversions
convW, convH, convZ int
swapAxes bool
}
// prepare updates cached values used to adjust touch events.
// Must call after any changes to Min/Max values or orientation.
func (c *TouchscreenCalibration) prepare(w, h int) {
c.convW = (w << 16) / (c.MaxX - c.MinX)
c.convH = (h << 16) / (c.MinY - c.MaxY)
c.convZ = (1 << 24) / (c.Weak - c.Strong)
}
func (c *TouchscreenCalibration) Adjust(ev *TouchEvent) {
// Nil calibrations are callable with no effect.
if c == nil {
return
}
if c.swapAxes {
ev.X, ev.Y = ev.Y, ev.X
}
ev.X = ((ev.X - c.MinX) * c.convW) >> 16
ev.Y = ((ev.Y - c.MaxY) * c.convH) >> 16
ev.Pressure = ((ev.Pressure - c.Strong) * c.convZ) >> 16
}
func (c *TouchscreenCalibration) orient(angle int) {
switch angle {
case 0:
break
case 90:
c.swapAxes = true
// Reverse Y-Direction
c.MaxY, c.MinY = c.MinY, c.MaxY
case 270:
c.swapAxes = true
// Reverse X-Direction
c.MinX, c.MaxX = c.MaxX, c.MinX
case 180:
// Reverse both axes
c.MinX, c.MaxX = c.MaxX, c.MinX
c.MaxY, c.MinY = c.MinY, c.MaxY
default:
panic("unsupported rotation angle")
}
}