github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/machine/usb/hid/mouse/mouse.go (about)

     1  package mouse
     2  
     3  import (
     4  	"machine"
     5  	"machine/usb/hid"
     6  )
     7  
     8  var Mouse *mouse
     9  
    10  type Button byte
    11  
    12  const (
    13  	Left Button = 1 << iota
    14  	Right
    15  	Middle
    16  	Back
    17  	Forward
    18  )
    19  
    20  type mouse struct {
    21  	buf     *hid.RingBuffer
    22  	button  Button
    23  	waitTxc bool
    24  }
    25  
    26  func init() {
    27  	if Mouse == nil {
    28  		Mouse = newMouse()
    29  		hid.SetHandler(Mouse)
    30  	}
    31  }
    32  
    33  // New returns the USB hid-mouse port.
    34  // Deprecated, better to just use Port()
    35  func New() *mouse {
    36  	return Port()
    37  }
    38  
    39  // Port returns the USB hid-mouse port.
    40  func Port() *mouse {
    41  	return Mouse
    42  }
    43  
    44  func newMouse() *mouse {
    45  	return &mouse{
    46  		buf: hid.NewRingBuffer(),
    47  	}
    48  }
    49  
    50  func (m *mouse) TxHandler() bool {
    51  	m.waitTxc = false
    52  	if b, ok := m.buf.Get(); ok {
    53  		m.waitTxc = true
    54  		hid.SendUSBPacket(b[:5])
    55  		return true
    56  	}
    57  	return false
    58  }
    59  
    60  func (m *mouse) RxHandler(b []byte) bool {
    61  	return false
    62  }
    63  
    64  func (m *mouse) tx(b []byte) {
    65  	if machine.USBDev.InitEndpointComplete {
    66  		if m.waitTxc {
    67  			m.buf.Put(b)
    68  		} else {
    69  			m.waitTxc = true
    70  			hid.SendUSBPacket(b)
    71  		}
    72  	}
    73  }
    74  
    75  // Move is a function that moves the mouse cursor.
    76  func (m *mouse) Move(vx, vy int) {
    77  	if vx == 0 && vy == 0 {
    78  		return
    79  	}
    80  
    81  	if vx < -128 {
    82  		vx = -128
    83  	}
    84  	if vx > 127 {
    85  		vx = 127
    86  	}
    87  
    88  	if vy < -128 {
    89  		vy = -128
    90  	}
    91  	if vy > 127 {
    92  		vy = 127
    93  	}
    94  
    95  	m.tx([]byte{
    96  		0x01, byte(m.button), byte(vx), byte(vy), 0x00,
    97  	})
    98  }
    99  
   100  // Click clicks the mouse button.
   101  func (m *mouse) Click(btn Button) {
   102  	m.Press(btn)
   103  	m.Release(btn)
   104  }
   105  
   106  // Press presses the given mouse buttons.
   107  func (m *mouse) Press(btn Button) {
   108  	m.button |= btn
   109  	m.tx([]byte{
   110  		0x01, byte(m.button), 0x00, 0x00, 0x00,
   111  	})
   112  }
   113  
   114  // Release releases the given mouse buttons.
   115  func (m *mouse) Release(btn Button) {
   116  	m.button &= ^btn
   117  	m.tx([]byte{
   118  		0x01, byte(m.button), 0x00, 0x00, 0x00,
   119  	})
   120  }
   121  
   122  // Wheel controls the mouse wheel.
   123  func (m *mouse) Wheel(v int) {
   124  	if v == 0 {
   125  		return
   126  	}
   127  
   128  	if v < -128 {
   129  		v = -128
   130  	}
   131  	if v > 127 {
   132  		v = 127
   133  	}
   134  
   135  	m.tx([]byte{
   136  		0x01, byte(m.button), 0x00, 0x00, byte(v),
   137  	})
   138  }
   139  
   140  // WheelDown turns the mouse wheel down.
   141  func (m *mouse) WheelDown() {
   142  	m.Wheel(-1)
   143  }
   144  
   145  // WheelUp turns the mouse wheel up.
   146  func (m *mouse) WheelUp() {
   147  	m.Wheel(1)
   148  }