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.

263 lines
9.5 KiB

  1. /*
  2. Copyright 2011,2013 Jun Wako <wakojun@gmail.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 <stdbool.h>
  15. #include <avr/io.h>
  16. #include <util/delay.h>
  17. #include "ps2_mouse.h"
  18. #include "host.h"
  19. #include "timer.h"
  20. #include "print.h"
  21. #include "report.h"
  22. #include "debug.h"
  23. #include "ps2.h"
  24. /* ============================= MACROS ============================ */
  25. static report_mouse_t mouse_report = {};
  26. static inline void ps2_mouse_print_report(report_mouse_t *mouse_report);
  27. static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report);
  28. static inline void ps2_mouse_clear_report(report_mouse_t *mouse_report);
  29. static inline void ps2_mouse_enable_scrolling(void);
  30. static inline void ps2_mouse_scroll_button_task(report_mouse_t *mouse_report);
  31. /* ============================= IMPLEMENTATION ============================ */
  32. /* supports only 3 button mouse at this time */
  33. void ps2_mouse_init(void) {
  34. ps2_host_init();
  35. _delay_ms(PS2_MOUSE_INIT_DELAY); // wait for powering up
  36. PS2_MOUSE_SEND(PS2_MOUSE_RESET, "ps2_mouse_init: sending reset");
  37. PS2_MOUSE_RECEIVE("ps2_mouse_init: read BAT");
  38. PS2_MOUSE_RECEIVE("ps2_mouse_init: read DevID");
  39. #ifdef PS2_MOUSE_USE_REMOTE_MODE
  40. ps2_mouse_set_remote_mode();
  41. #else
  42. ps2_mouse_enable_data_reporting();
  43. #endif
  44. #ifdef PS2_MOUSE_ENABLE_SCROLLING
  45. ps2_mouse_enable_scrolling();
  46. #endif
  47. #ifdef PS2_MOUSE_USE_2_1_SCALING
  48. ps2_mouse_set_scaling_2_1();
  49. #endif
  50. ps2_mouse_init_user();
  51. }
  52. __attribute__((weak)) void ps2_mouse_init_user(void) {}
  53. __attribute__((weak)) void ps2_mouse_moved_user(report_mouse_t *mouse_report) {}
  54. void ps2_mouse_task(void) {
  55. static uint8_t buttons_prev = 0;
  56. extern int tp_buttons;
  57. /* receives packet from mouse */
  58. uint8_t rcv;
  59. rcv = ps2_host_send(PS2_MOUSE_READ_DATA);
  60. if (rcv == PS2_ACK) {
  61. mouse_report.buttons = ps2_host_recv_response() | tp_buttons;
  62. mouse_report.x = ps2_host_recv_response() * PS2_MOUSE_X_MULTIPLIER;
  63. mouse_report.y = ps2_host_recv_response() * PS2_MOUSE_Y_MULTIPLIER;
  64. #ifdef PS2_MOUSE_ENABLE_SCROLLING
  65. mouse_report.v = -(ps2_host_recv_response() & PS2_MOUSE_SCROLL_MASK) * PS2_MOUSE_V_MULTIPLIER;
  66. #endif
  67. } else {
  68. if (debug_mouse) print("ps2_mouse: fail to get mouse packet\n");
  69. return;
  70. }
  71. /* if mouse moves or buttons state changes */
  72. if (mouse_report.x || mouse_report.y || mouse_report.v || ((mouse_report.buttons ^ buttons_prev) & PS2_MOUSE_BTN_MASK)) {
  73. #ifdef PS2_MOUSE_DEBUG_RAW
  74. // Used to debug raw ps2 bytes from mouse
  75. ps2_mouse_print_report(&mouse_report);
  76. #endif
  77. buttons_prev = mouse_report.buttons;
  78. ps2_mouse_convert_report_to_hid(&mouse_report);
  79. #if PS2_MOUSE_SCROLL_BTN_MASK
  80. ps2_mouse_scroll_button_task(&mouse_report);
  81. #endif
  82. if (mouse_report.x || mouse_report.y || mouse_report.v) {
  83. ps2_mouse_moved_user(&mouse_report);
  84. }
  85. #ifdef PS2_MOUSE_DEBUG_HID
  86. // Used to debug the bytes sent to the host
  87. ps2_mouse_print_report(&mouse_report);
  88. #endif
  89. host_mouse_send(&mouse_report);
  90. }
  91. ps2_mouse_clear_report(&mouse_report);
  92. }
  93. void ps2_mouse_disable_data_reporting(void) { PS2_MOUSE_SEND(PS2_MOUSE_DISABLE_DATA_REPORTING, "ps2 mouse disable data reporting"); }
  94. void ps2_mouse_enable_data_reporting(void) { PS2_MOUSE_SEND(PS2_MOUSE_ENABLE_DATA_REPORTING, "ps2 mouse enable data reporting"); }
  95. void ps2_mouse_set_remote_mode(void) {
  96. PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_REMOTE_MODE, "ps2 mouse set remote mode");
  97. ps2_mouse_mode = PS2_MOUSE_REMOTE_MODE;
  98. }
  99. void ps2_mouse_set_stream_mode(void) {
  100. PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_STREAM_MODE, "ps2 mouse set stream mode");
  101. ps2_mouse_mode = PS2_MOUSE_STREAM_MODE;
  102. }
  103. void ps2_mouse_set_scaling_2_1(void) { PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_SCALING_2_1, "ps2 mouse set scaling 2:1"); }
  104. void ps2_mouse_set_scaling_1_1(void) { PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_SCALING_1_1, "ps2 mouse set scaling 1:1"); }
  105. void ps2_mouse_set_resolution(ps2_mouse_resolution_t resolution) { PS2_MOUSE_SET_SAFE(PS2_MOUSE_SET_RESOLUTION, resolution, "ps2 mouse set resolution"); }
  106. void ps2_mouse_set_sample_rate(ps2_mouse_sample_rate_t sample_rate) { PS2_MOUSE_SET_SAFE(PS2_MOUSE_SET_SAMPLE_RATE, sample_rate, "ps2 mouse set sample rate"); }
  107. /* ============================= HELPERS ============================ */
  108. #define X_IS_NEG (mouse_report->buttons & (1 << PS2_MOUSE_X_SIGN))
  109. #define Y_IS_NEG (mouse_report->buttons & (1 << PS2_MOUSE_Y_SIGN))
  110. #define X_IS_OVF (mouse_report->buttons & (1 << PS2_MOUSE_X_OVFLW))
  111. #define Y_IS_OVF (mouse_report->buttons & (1 << PS2_MOUSE_Y_OVFLW))
  112. static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report) {
  113. // PS/2 mouse data is '9-bit integer'(-256 to 255) which is comprised of sign-bit and 8-bit value.
  114. // bit: 8 7 ... 0
  115. // sign \8-bit/
  116. //
  117. // Meanwhile USB HID mouse indicates 8bit data(-127 to 127), note that -128 is not used.
  118. //
  119. // This converts PS/2 data into HID value. Use only -127-127 out of PS/2 9-bit.
  120. mouse_report->x = X_IS_NEG ? ((!X_IS_OVF && -127 <= mouse_report->x && mouse_report->x <= -1) ? mouse_report->x : -127) : ((!X_IS_OVF && 0 <= mouse_report->x && mouse_report->x <= 127) ? mouse_report->x : 127);
  121. mouse_report->y = Y_IS_NEG ? ((!Y_IS_OVF && -127 <= mouse_report->y && mouse_report->y <= -1) ? mouse_report->y : -127) : ((!Y_IS_OVF && 0 <= mouse_report->y && mouse_report->y <= 127) ? mouse_report->y : 127);
  122. // remove sign and overflow flags
  123. mouse_report->buttons &= PS2_MOUSE_BTN_MASK;
  124. #ifdef PS2_MOUSE_INVERT_X
  125. mouse_report->x = -mouse_report->x;
  126. #endif
  127. #ifndef PS2_MOUSE_INVERT_Y // NOTE if not!
  128. // invert coordinate of y to conform to USB HID mouse
  129. mouse_report->y = -mouse_report->y;
  130. #endif
  131. #ifdef PS2_MOUSE_ROTATE
  132. int8_t x = mouse_report->x;
  133. int8_t y = mouse_report->y;
  134. # if PS2_MOUSE_ROTATE == 90
  135. mouse_report->x = y;
  136. mouse_report->y = -x;
  137. # elif PS2_MOUSE_ROTATE == 180
  138. mouse_report->x = -x;
  139. mouse_report->y = -y;
  140. # elif PS2_MOUSE_ROTATE == 270
  141. mouse_report->x = -y;
  142. mouse_report->y = x;
  143. # endif
  144. #endif
  145. }
  146. static inline void ps2_mouse_clear_report(report_mouse_t *mouse_report) {
  147. mouse_report->x = 0;
  148. mouse_report->y = 0;
  149. mouse_report->v = 0;
  150. mouse_report->h = 0;
  151. mouse_report->buttons = 0;
  152. }
  153. static inline void ps2_mouse_print_report(report_mouse_t *mouse_report) {
  154. if (!debug_mouse) return;
  155. print("ps2_mouse: [");
  156. print_hex8(mouse_report->buttons);
  157. print("|");
  158. print_hex8((uint8_t)mouse_report->x);
  159. print(" ");
  160. print_hex8((uint8_t)mouse_report->y);
  161. print(" ");
  162. print_hex8((uint8_t)mouse_report->v);
  163. print(" ");
  164. print_hex8((uint8_t)mouse_report->h);
  165. print("]\n");
  166. }
  167. static inline void ps2_mouse_enable_scrolling(void) {
  168. PS2_MOUSE_SEND(PS2_MOUSE_SET_SAMPLE_RATE, "Initiaing scroll wheel enable: Set sample rate");
  169. PS2_MOUSE_SEND(200, "200");
  170. PS2_MOUSE_SEND(PS2_MOUSE_SET_SAMPLE_RATE, "Set sample rate");
  171. PS2_MOUSE_SEND(100, "100");
  172. PS2_MOUSE_SEND(PS2_MOUSE_SET_SAMPLE_RATE, "Set sample rate");
  173. PS2_MOUSE_SEND(80, "80");
  174. PS2_MOUSE_SEND(PS2_MOUSE_GET_DEVICE_ID, "Finished enabling scroll wheel");
  175. _delay_ms(20);
  176. }
  177. #define PRESS_SCROLL_BUTTONS mouse_report->buttons |= (PS2_MOUSE_SCROLL_BTN_MASK)
  178. #define RELEASE_SCROLL_BUTTONS mouse_report->buttons &= ~(PS2_MOUSE_SCROLL_BTN_MASK)
  179. static inline void ps2_mouse_scroll_button_task(report_mouse_t *mouse_report) {
  180. static enum {
  181. SCROLL_NONE,
  182. SCROLL_BTN,
  183. SCROLL_SENT,
  184. } scroll_state = SCROLL_NONE;
  185. static uint16_t scroll_button_time = 0;
  186. if (PS2_MOUSE_SCROLL_BTN_MASK == (mouse_report->buttons & (PS2_MOUSE_SCROLL_BTN_MASK))) {
  187. // All scroll buttons are pressed
  188. if (scroll_state == SCROLL_NONE) {
  189. scroll_button_time = timer_read();
  190. scroll_state = SCROLL_BTN;
  191. }
  192. // If the mouse has moved, update the report to scroll instead of move the mouse
  193. if (mouse_report->x || mouse_report->y) {
  194. scroll_state = SCROLL_SENT;
  195. mouse_report->v = -mouse_report->y / (PS2_MOUSE_SCROLL_DIVISOR_V);
  196. mouse_report->h = mouse_report->x / (PS2_MOUSE_SCROLL_DIVISOR_H);
  197. mouse_report->x = 0;
  198. mouse_report->y = 0;
  199. #ifdef PS2_MOUSE_INVERT_H
  200. mouse_report->h = -mouse_report->h;
  201. #endif
  202. #ifdef PS2_MOUSE_INVERT_V
  203. mouse_report->v = -mouse_report->v;
  204. #endif
  205. }
  206. } else if (0 == (PS2_MOUSE_SCROLL_BTN_MASK & mouse_report->buttons)) {
  207. // None of the scroll buttons are pressed
  208. #if PS2_MOUSE_SCROLL_BTN_SEND
  209. if (scroll_state == SCROLL_BTN && timer_elapsed(scroll_button_time) < PS2_MOUSE_SCROLL_BTN_SEND) {
  210. PRESS_SCROLL_BUTTONS;
  211. host_mouse_send(mouse_report);
  212. _delay_ms(100);
  213. RELEASE_SCROLL_BUTTONS;
  214. }
  215. #endif
  216. scroll_state = SCROLL_NONE;
  217. }
  218. RELEASE_SCROLL_BUTTONS;
  219. }