Browse Source

USB Mouse on Teensy 3.x to use relative+absolulte hybrid

teensy4-core
PaulStoffregen 10 years ago
parent
commit
054c319248
2 changed files with 59 additions and 44 deletions
  1. +17
    -11
      teensy3/usb_desc.c
  2. +42
    -33
      teensy3/usb_mouse.c

+ 17
- 11
teensy3/usb_desc.c View File

0x05, 0x01, // Usage Page (Generic Desktop) 0x05, 0x01, // Usage Page (Generic Desktop)
0x09, 0x02, // Usage (Mouse) 0x09, 0x02, // Usage (Mouse)
0xA1, 0x01, // Collection (Application) 0xA1, 0x01, // Collection (Application)
0x85, 0x01, // REPORT_ID (1)
0x05, 0x09, // Usage Page (Button) 0x05, 0x09, // Usage Page (Button)
0x19, 0x01, // Usage Minimum (Button #1) 0x19, 0x01, // Usage Minimum (Button #1)
0x29, 0x03, // Usage Maximum (Button #3)
0x29, 0x08, // Usage Maximum (Button #8)
0x15, 0x00, // Logical Minimum (0) 0x15, 0x00, // Logical Minimum (0)
0x25, 0x01, // Logical Maximum (1) 0x25, 0x01, // Logical Maximum (1)
0x95, 0x03, // Report Count (3)
0x95, 0x08, // Report Count (8)
0x75, 0x01, // Report Size (1) 0x75, 0x01, // Report Size (1)
0x81, 0x02, // Input (Data, Variable, Absolute) 0x81, 0x02, // Input (Data, Variable, Absolute)
0x95, 0x01, // Report Count (1)
0x75, 0x05, // Report Size (5)
0x81, 0x03, // Input (Constant)
0x05, 0x01, // Usage Page (Generic Desktop) 0x05, 0x01, // Usage Page (Generic Desktop)
0x09, 0x30, // Usage (X) 0x09, 0x30, // Usage (X)
0x09, 0x31, // Usage (Y) 0x09, 0x31, // Usage (Y)
0x15, 0x00, // Logical Minimum (0)
0x26, 0xFF, 0x7F, // Logical Maximum (32767)
0x75, 0x10, // Report Size (16),
0x95, 0x02, // Report Count (2),
0x81, 0x02, // Input (Data, Variable, Absolute)
0x09, 0x38, // Usage (Wheel) 0x09, 0x38, // Usage (Wheel)
0x15, 0x81, // Logical Minimum (-127) 0x15, 0x81, // Logical Minimum (-127)
0x25, 0x7F, // Logical Maximum (127) 0x25, 0x7F, // Logical Maximum (127)
0x75, 0x08, // Report Size (8), 0x75, 0x08, // Report Size (8),
0x95, 0x01, // Report Count (1),
0x95, 0x03, // Report Count (3),
0x81, 0x06, // Input (Data, Variable, Relative) 0x81, 0x06, // Input (Data, Variable, Relative)
0xC0, // End Collection
0x05, 0x01, // Usage Page (Generic Desktop)
0x09, 0x02, // Usage (Mouse)
0xA1, 0x01, // Collection (Application)
0x85, 0x02, // REPORT_ID (2)
0x05, 0x01, // Usage Page (Generic Desktop)
0x09, 0x30, // Usage (X)
0x09, 0x31, // Usage (Y)
0x15, 0x00, // Logical Minimum (0)
0x26, 0xFF, 0x7F, // Logical Maximum (32767)
0x75, 0x10, // Report Size (16),
0x95, 0x02, // Report Count (2),
0x81, 0x02, // Input (Data, Variable, Absolute)
0xC0 // End Collection 0xC0 // End Collection
}; };
#endif #endif

+ 42
- 33
teensy3/usb_mouse.c View File

// 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)
{ {
uint32_t wait_count=0, val32;
uint32_t wait_count=0;
usb_packet_t *tx_packet; usb_packet_t *tx_packet;
uint16_t newval;


//serial_print("move");
//serial_print("\n");
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 (x > 0) {
newval = usb_mouse_position_x + x;
if (newval >= usb_mouse_resolution_x) newval = usb_mouse_resolution_x - 1;
usb_mouse_position_x = newval;
} else if (x < 0) {
newval = usb_mouse_position_x + x;
if (newval & 0x8000) newval = 0;
usb_mouse_position_x = newval;
}
if (y > 0) {
newval = usb_mouse_position_y + y;
if (newval >= usb_mouse_resolution_y) newval = usb_mouse_resolution_y - 1;
usb_mouse_position_y = newval;
} else if (y < 0) {
newval = usb_mouse_position_y + y;
if (newval & 0x8000) newval = 0;
usb_mouse_position_y = newval;
}

while (1) {
if (!usb_configuration) {
return -1;
}
if (usb_tx_packet_count(MOUSE_ENDPOINT) < TX_PACKET_LIMIT) {
tx_packet = usb_malloc();
if (tx_packet) break;
}
if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
transmit_previous_timeout = 1;
return -1;
}
yield();
}
transmit_previous_timeout = 0;
*(tx_packet->buf + 0) = 1;
*(tx_packet->buf + 1) = usb_mouse_buttons_state;
*(tx_packet->buf + 2) = x;
*(tx_packet->buf + 3) = y;
*(tx_packet->buf + 4) = wheel;
tx_packet->len = 5;
usb_tx(MOUSE_ENDPOINT, tx_packet);
return 0;
}

int usb_mouse_position(uint16_t x, uint16_t y)
{
uint32_t wait_count=0, val32;
usb_packet_t *tx_packet;

if (x >= usb_mouse_resolution_x) x = usb_mouse_resolution_x - 1;
usb_mouse_position_x = x;
if (y >= usb_mouse_resolution_y) y = usb_mouse_resolution_y - 1;
usb_mouse_position_y = y;

while (1) { while (1) {
if (!usb_configuration) { if (!usb_configuration) {
return -1; return -1;
yield(); yield();
} }
transmit_previous_timeout = 0; transmit_previous_timeout = 0;
*(tx_packet->buf) = usb_mouse_buttons_state;
*(tx_packet->buf + 0) = 2;
val32 = usb_mouse_position_x * usb_mouse_scale_x + usb_mouse_offset_x; val32 = usb_mouse_position_x * usb_mouse_scale_x + usb_mouse_offset_x;
//serial_print("move:");
//serial_print("position:");
//serial_phex16(usb_mouse_position_x); //serial_phex16(usb_mouse_position_x);
//serial_print("->"); //serial_print("->");
//serial_phex32(val32); //serial_phex32(val32);
//serial_print("\n"); //serial_print("\n");
*(tx_packet->buf + 3) = val32 >> 16; *(tx_packet->buf + 3) = val32 >> 16;
*(tx_packet->buf + 4) = val32 >> 24; *(tx_packet->buf + 4) = val32 >> 24;
*(tx_packet->buf + 5) = wheel;
tx_packet->len = 6;
tx_packet->len = 5;
usb_tx(MOUSE_ENDPOINT, tx_packet); usb_tx(MOUSE_ENDPOINT, tx_packet);
return 0; return 0;
} }


int usb_mouse_position(uint16_t x, uint16_t y)
{
if (x >= usb_mouse_resolution_x) x = usb_mouse_resolution_x - 1;
usb_mouse_position_x = x;
if (y >= usb_mouse_resolution_y) y = usb_mouse_resolution_y - 1;
usb_mouse_position_y = y;
return usb_mouse_move(0, 0, 0);
}

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)
{ {
if (width < 128) width = 128; if (width < 128) width = 128;

Loading…
Cancel
Save