123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- #include <stdint.h>
- #include <avr/io.h>
- #include <util/delay.h>
- #include "serial.h"
- #include "serial_mouse.h"
- #include "report.h"
- #include "host.h"
- #include "timer.h"
- #include "print.h"
- #include "debug.h"
- #ifdef MAX
- #undef MAX
- #endif
- #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
- static void print_usb_data(const report_mouse_t *report);
- void serial_mouse_task(void)
- {
-
- static uint8_t buffer[5];
- static int buffer_cur = 0;
- int16_t rcv;
- report_mouse_t report = {0, 0, 0, 0, 0};
- rcv = serial_recv2();
- if (rcv < 0)
-
- return;
- if (debug_mouse)
- xprintf("serial_mouse: byte: %04X\n", rcv);
-
- if (buffer_cur == 0 && (rcv >> 3) != 0x10)
- return;
- buffer[buffer_cur++] = (uint8_t)rcv;
- if (buffer_cur < 5)
- return;
- buffer_cur = 0;
- #ifdef SERIAL_MOUSE_CENTER_SCROLL
- if ((buffer[0] & 0x7) == 0x5 && (buffer[1] || buffer[2])) {
-
- report.h = MAX((int8_t)buffer[1], -127);
- report.v = MAX((int8_t)buffer[2], -127);
- print_usb_data(&report);
- host_mouse_send(&report);
- if (buffer[3] || buffer[4]) {
- report.h = MAX((int8_t)buffer[3], -127);
- report.v = MAX((int8_t)buffer[4], -127);
- print_usb_data(&report);
- host_mouse_send(&report);
- }
- return;
- }
- #endif
-
- if (!(buffer[0] & (1 << 2)))
- report.buttons |= MOUSE_BTN1;
- if (!(buffer[0] & (1 << 1)))
- report.buttons |= MOUSE_BTN3;
- if (!(buffer[0] & (1 << 0)))
- report.buttons |= MOUSE_BTN2;
-
- report.x = MAX((int8_t)buffer[1], -127);
- report.y = MAX(-(int8_t)buffer[2], -127);
- print_usb_data(&report);
- host_mouse_send(&report);
- if (buffer[3] || buffer[4]) {
- report.x = MAX((int8_t)buffer[3], -127);
- report.y = MAX(-(int8_t)buffer[4], -127);
- print_usb_data(&report);
- host_mouse_send(&report);
- }
- }
- static void print_usb_data(const report_mouse_t *report)
- {
- if (!debug_mouse)
- return;
- xprintf("serial_mouse usb: [%02X|%d %d %d %d]\n",
- report->buttons, report->x, report->y,
- report->v, report->h);
- }
|