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
|
package mouse
import (
"machine"
"machine/usb/hid"
)
var Mouse *mouse
type Button byte
const (
Left Button = 1 << iota
Right
Middle
Back
Forward
)
type mouse struct {
buf *hid.RingBuffer
button Button
waitTxc bool
}
func init() {
if Mouse == nil {
Mouse = newMouse()
hid.SetHandler(Mouse)
}
}
// New returns the USB hid-mouse port.
// Deprecated, better to just use Port()
func New() *mouse {
return Port()
}
// Port returns the USB hid-mouse port.
func Port() *mouse {
return Mouse
}
func newMouse() *mouse {
return &mouse{
buf: hid.NewRingBuffer(),
}
}
func (m *mouse) TxHandler() bool {
m.waitTxc = false
if b, ok := m.buf.Get(); ok {
m.waitTxc = true
hid.SendUSBPacket(b[:5])
return true
}
return false
}
func (m *mouse) RxHandler(b []byte) bool {
return false
}
func (m *mouse) tx(b []byte) {
if machine.USBDev.InitEndpointComplete {
if m.waitTxc {
m.buf.Put(b)
} else {
m.waitTxc = true
hid.SendUSBPacket(b)
}
}
}
// Move is a function that moves the mouse cursor.
func (m *mouse) Move(vx, vy int) {
if vx == 0 && vy == 0 {
return
}
if vx < -128 {
vx = -128
}
if vx > 127 {
vx = 127
}
if vy < -128 {
vy = -128
}
if vy > 127 {
vy = 127
}
m.tx([]byte{
0x01, byte(m.button), byte(vx), byte(vy), 0x00,
})
}
// Click clicks the mouse button.
func (m *mouse) Click(btn Button) {
m.Press(btn)
m.Release(btn)
}
// Press presses the given mouse buttons.
func (m *mouse) Press(btn Button) {
m.button |= btn
m.tx([]byte{
0x01, byte(m.button), 0x00, 0x00, 0x00,
})
}
// Release releases the given mouse buttons.
func (m *mouse) Release(btn Button) {
m.button &= ^btn
m.tx([]byte{
0x01, byte(m.button), 0x00, 0x00, 0x00,
})
}
// Wheel controls the mouse wheel.
func (m *mouse) Wheel(v int) {
if v == 0 {
return
}
if v < -128 {
v = -128
}
if v > 127 {
v = 127
}
m.tx([]byte{
0x01, byte(m.button), 0x00, 0x00, byte(v),
})
}
// WheelDown turns the mouse wheel down.
func (m *mouse) WheelDown() {
m.Wheel(-1)
}
// WheelUp turns the mouse wheel up.
func (m *mouse) WheelUp() {
m.Wheel(1)
}
|