You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.0 KiB

  1. /*
  2. Copyright 2014 Robin Haberkorn <robin.haberkorn@googlemail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdint.h>
  15. #include <avr/io.h>
  16. #include <util/delay.h>
  17. #include "serial.h"
  18. #include "serial_mouse.h"
  19. #include "report.h"
  20. #include "host.h"
  21. #include "timer.h"
  22. #include "print.h"
  23. #include "debug.h"
  24. #ifdef MAX
  25. # undef MAX
  26. #endif
  27. #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
  28. static void print_usb_data(const report_mouse_t *report);
  29. void serial_mouse_task(void) {
  30. /* 3 byte ring buffer */
  31. static uint8_t buffer[3];
  32. static int buffer_cur = 0;
  33. static report_mouse_t report = {};
  34. int16_t rcv;
  35. rcv = serial_recv2();
  36. if (rcv < 0) /* no new data */
  37. return;
  38. if (debug_mouse) xprintf("serial_mouse: byte: %04X\n", rcv);
  39. /*
  40. * If bit 6 is one, this signals the beginning
  41. * of a 3 byte sequence/packet.
  42. */
  43. if (rcv & (1 << 6)) buffer_cur = 0;
  44. buffer[buffer_cur] = (uint8_t)rcv;
  45. if (buffer_cur == 0 && buffer[buffer_cur] == 0x20) {
  46. /*
  47. * Logitech extension: This must be a follow-up on
  48. * the last 3-byte packet signaling a middle button click
  49. */
  50. report.buttons |= MOUSE_BTN3;
  51. report.x = report.y = 0;
  52. print_usb_data(&report);
  53. host_mouse_send(&report);
  54. return;
  55. }
  56. buffer_cur++;
  57. if (buffer_cur < 3) return;
  58. buffer_cur = 0;
  59. /*
  60. * parse 3 byte packet.
  61. * NOTE: We only get a complete packet
  62. * if the mouse moved or the button states
  63. * change.
  64. */
  65. report.buttons = 0;
  66. if (buffer[0] & (1 << 5)) report.buttons |= MOUSE_BTN1;
  67. if (buffer[0] & (1 << 4)) report.buttons |= MOUSE_BTN2;
  68. report.x = (buffer[0] << 6) | buffer[1];
  69. report.y = ((buffer[0] << 4) & 0xC0) | buffer[2];
  70. /* USB HID uses values from -127 to 127 only */
  71. report.x = MAX(report.x, -127);
  72. report.y = MAX(report.y, -127);
  73. #if 0
  74. if (!report.buttons && !report.x && !report.y) {
  75. /*
  76. * Microsoft extension: Middle mouse button pressed
  77. * FIXME: I don't know how exactly this extension works.
  78. */
  79. report.buttons |= MOUSE_BTN3;
  80. }
  81. #endif
  82. print_usb_data(&report);
  83. host_mouse_send(&report);
  84. }
  85. static void print_usb_data(const report_mouse_t *report) {
  86. if (!debug_mouse) return;
  87. xprintf("serial_mouse usb: [%02X|%d %d %d %d]\n", report->buttons, report->x, report->y, report->v, report->h);
  88. }