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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

serial_mouse_mousesystems.c 3.7KB

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