serial_mouse_mousesystems.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. //#define SERIAL_MOUSE_CENTER_SCROLL
  29. static void print_usb_data(const report_mouse_t *report);
  30. void serial_mouse_task(void) {
  31. /* 5 byte ring buffer */
  32. static uint8_t buffer[5];
  33. static int buffer_cur = 0;
  34. int16_t rcv;
  35. report_mouse_t report = {0, 0, 0, 0, 0};
  36. rcv = serial_recv2();
  37. if (rcv < 0) /* no new data */
  38. return;
  39. if (debug_mouse) xprintf("serial_mouse: byte: %04X\n", rcv);
  40. /*
  41. * Synchronization: mouse(4) says that all
  42. * bytes but the first one in the packet have
  43. * bit 7 == 0, but this is untrue.
  44. * Therefore we discard all bytes up to the
  45. * first one with the characteristic bit pattern.
  46. */
  47. if (buffer_cur == 0 && (rcv >> 3) != 0x10) return;
  48. buffer[buffer_cur++] = (uint8_t)rcv;
  49. if (buffer_cur < 5) return;
  50. buffer_cur = 0;
  51. #ifdef SERIAL_MOUSE_CENTER_SCROLL
  52. if ((buffer[0] & 0x7) == 0x5 && (buffer[1] || buffer[2])) {
  53. /* USB HID uses only values from -127 to 127 */
  54. report.h = MAX((int8_t)buffer[1], -127);
  55. report.v = MAX((int8_t)buffer[2], -127);
  56. print_usb_data(&report);
  57. host_mouse_send(&report);
  58. if (buffer[3] || buffer[4]) {
  59. report.h = MAX((int8_t)buffer[3], -127);
  60. report.v = MAX((int8_t)buffer[4], -127);
  61. print_usb_data(&report);
  62. host_mouse_send(&report);
  63. }
  64. return;
  65. }
  66. #endif
  67. /*
  68. * parse 5 byte packet.
  69. * NOTE: We only get a complete packet
  70. * if the mouse moved or the button states
  71. * change.
  72. */
  73. if (!(buffer[0] & (1 << 2))) report.buttons |= MOUSE_BTN1;
  74. if (!(buffer[0] & (1 << 1))) report.buttons |= MOUSE_BTN3;
  75. if (!(buffer[0] & (1 << 0))) report.buttons |= MOUSE_BTN2;
  76. /* USB HID uses only values from -127 to 127 */
  77. report.x = MAX((int8_t)buffer[1], -127);
  78. report.y = MAX(-(int8_t)buffer[2], -127);
  79. print_usb_data(&report);
  80. host_mouse_send(&report);
  81. if (buffer[3] || buffer[4]) {
  82. report.x = MAX((int8_t)buffer[3], -127);
  83. report.y = MAX(-(int8_t)buffer[4], -127);
  84. print_usb_data(&report);
  85. host_mouse_send(&report);
  86. }
  87. }
  88. static void print_usb_data(const report_mouse_t *report) {
  89. if (!debug_mouse) return;
  90. xprintf("serial_mouse usb: [%02X|%d %d %d %d]\n", report->buttons, report->x, report->y, report->v, report->h);
  91. }