Browse Source

Add Mouse horizontal scroll (thanks Xenoamor)

https://forum.pjrc.com/threads/33017-Teensy-3-1-Mouse-horizontal-scroll
teensy4-core
PaulStoffregen 8 years ago
parent
commit
8b85a32552
3 changed files with 22 additions and 11 deletions
  1. +8
    -1
      teensy3/usb_desc.c
  2. +5
    -3
      teensy3/usb_mouse.c
  3. +9
    -7
      teensy3/usb_mouse.h

+ 8
- 1
teensy3/usb_desc.c View File

static uint8_t device_descriptor[] = { static uint8_t device_descriptor[] = {
18, // bLength 18, // bLength
1, // bDescriptorType 1, // bDescriptorType
0x00, 0x02, // bcdUSB
0x01, 0x01, // bcdUSB
#ifdef DEVICE_CLASS #ifdef DEVICE_CLASS
DEVICE_CLASS, // bDeviceClass DEVICE_CLASS, // bDeviceClass
#else #else
0x75, 0x08, // Report Size (8), 0x75, 0x08, // Report Size (8),
0x95, 0x03, // Report Count (3), 0x95, 0x03, // Report Count (3),
0x81, 0x06, // Input (Data, Variable, Relative) 0x81, 0x06, // Input (Data, Variable, Relative)
0x05, 0x0C, // Usage Page (Consumer)
0x0A, 0x38, 0x02, // Usage (AC Pan)
0x15, 0x81, // Logical Minimum (-127)
0x25, 0x7F, // Logical Maximum (127)
0x75, 0x08, // Report Size (8),
0x95, 0x01, // Report Count (1),
0x81, 0x06, // Input (Data, Variable, Relative)
0xC0, // End Collection 0xC0, // End Collection
0x05, 0x01, // Usage Page (Generic Desktop) 0x05, 0x01, // Usage Page (Generic Desktop)
0x09, 0x02, // Usage (Mouse) 0x09, 0x02, // Usage (Mouse)

+ 5
- 3
teensy3/usb_mouse.c View File

if (back) mask |= 8; if (back) mask |= 8;
if (forward) mask |= 16; if (forward) mask |= 16;
usb_mouse_buttons_state = mask; usb_mouse_buttons_state = mask;
return usb_mouse_move(0, 0, 0);
return usb_mouse_move(0, 0, 0, 0);
} }








// Move the mouse. x, y and wheel are -127 to 127. Use 0 for no movement. // Move the mouse. x, y and wheel are -127 to 127. Use 0 for no movement.
int usb_mouse_move(int8_t x, int8_t y, int8_t wheel)
int usb_mouse_move(int8_t x, int8_t y, int8_t wheel, int8_t horiz)
{ {
uint32_t wait_count=0; uint32_t wait_count=0;
usb_packet_t *tx_packet; usb_packet_t *tx_packet;
if (x == -128) x = -127; if (x == -128) x = -127;
if (y == -128) y = -127; if (y == -128) y = -127;
if (wheel == -128) wheel = -127; if (wheel == -128) wheel = -127;
if (horiz == -128) horiz = -127;


while (1) { while (1) {
if (!usb_configuration) { if (!usb_configuration) {
*(tx_packet->buf + 2) = x; *(tx_packet->buf + 2) = x;
*(tx_packet->buf + 3) = y; *(tx_packet->buf + 3) = y;
*(tx_packet->buf + 4) = wheel; *(tx_packet->buf + 4) = wheel;
tx_packet->len = 5;
*(tx_packet->buf + 5) = horiz; // horizontal scroll
tx_packet->len = 6;
usb_tx(MOUSE_ENDPOINT, tx_packet); usb_tx(MOUSE_ENDPOINT, tx_packet);
return 0; return 0;
} }

+ 9
- 7
teensy3/usb_mouse.h View File

extern "C" { extern "C" {
#endif #endif
int usb_mouse_buttons(uint8_t left, uint8_t middle, uint8_t right, uint8_t back, uint8_t forward); int usb_mouse_buttons(uint8_t left, uint8_t middle, uint8_t right, uint8_t back, uint8_t forward);
int usb_mouse_move(int8_t x, int8_t y, int8_t wheel);
int usb_mouse_move(int8_t x, int8_t y, int8_t wheel, int8_t horiz);
int usb_mouse_position(uint16_t x, uint16_t y); int usb_mouse_position(uint16_t x, uint16_t y);
void usb_mouse_screen_size(uint16_t width, uint16_t height, uint8_t mac); void usb_mouse_screen_size(uint16_t width, uint16_t height, uint8_t mac);
extern uint8_t usb_mouse_buttons_state; extern uint8_t usb_mouse_buttons_state;
public: public:
void begin(void) { } void begin(void) { }
void end(void) { } void end(void) { }
void move(int8_t x, int8_t y, int8_t wheel=0) { usb_mouse_move(x, y, wheel); }
void move(int8_t x, int8_t y, int8_t wheel=0, int8_t horiz=0) {
usb_mouse_move(x, y, wheel, horiz);
}
void moveTo(uint16_t x, uint16_t y) { usb_mouse_position(x, y); } void moveTo(uint16_t x, uint16_t y) { usb_mouse_position(x, y); }
void screenSize(uint16_t width, uint16_t height, bool isMacintosh = false) { void screenSize(uint16_t width, uint16_t height, bool isMacintosh = false) {
usb_mouse_screen_size(width, height, isMacintosh ? 1 : 0); usb_mouse_screen_size(width, height, isMacintosh ? 1 : 0);
} }
void click(uint8_t b = MOUSE_LEFT) { void click(uint8_t b = MOUSE_LEFT) {
usb_mouse_buttons_state = b; usb_mouse_buttons_state = b;
usb_mouse_move(0, 0, 0);
usb_mouse_move(0, 0, 0, 0);
usb_mouse_buttons_state = 0; usb_mouse_buttons_state = 0;
usb_mouse_move(0, 0, 0);
usb_mouse_move(0, 0, 0, 0);
} }
void scroll(int8_t wheel) { usb_mouse_move(0, 0, wheel); }
void scroll(int8_t wheel, int8_t horiz=0) { usb_mouse_move(0, 0, wheel, horiz); }
void set_buttons(uint8_t left, uint8_t middle=0, uint8_t right=0, uint8_t back=0, uint8_t forward=0) { void set_buttons(uint8_t left, uint8_t middle=0, uint8_t right=0, uint8_t back=0, uint8_t forward=0) {
usb_mouse_buttons(left, middle, right, back, forward); usb_mouse_buttons(left, middle, right, back, forward);
} }
uint8_t buttons = usb_mouse_buttons_state | (b & MOUSE_ALL); uint8_t buttons = usb_mouse_buttons_state | (b & MOUSE_ALL);
if (buttons != usb_mouse_buttons_state) { if (buttons != usb_mouse_buttons_state) {
usb_mouse_buttons_state = buttons; usb_mouse_buttons_state = buttons;
usb_mouse_move(0, 0, 0);
usb_mouse_move(0, 0, 0, 0);
} }
} }
void release(uint8_t b = MOUSE_LEFT) { void release(uint8_t b = MOUSE_LEFT) {
uint8_t buttons = usb_mouse_buttons_state & ~(b & MOUSE_ALL); uint8_t buttons = usb_mouse_buttons_state & ~(b & MOUSE_ALL);
if (buttons != usb_mouse_buttons_state) { if (buttons != usb_mouse_buttons_state) {
usb_mouse_buttons_state = buttons; usb_mouse_buttons_state = buttons;
usb_mouse_move(0, 0, 0);
usb_mouse_move(0, 0, 0, 0);
} }
} }
bool isPressed(uint8_t b = MOUSE_ALL) { bool isPressed(uint8_t b = MOUSE_ALL) {

Loading…
Cancel
Save