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.

ps2_mouse.c 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 <stdbool.h>
  15. #include<avr/io.h>
  16. #include<util/delay.h>
  17. #include "ps2.h"
  18. #include "ps2_mouse.h"
  19. #include "usb_mouse.h"
  20. #define PS2_MOUSE_DEBUG
  21. #ifdef PS2_MOUSE_DEBUG
  22. # include "print.h"
  23. # include "debug.h"
  24. #else
  25. # define print(s)
  26. # define phex(h)
  27. # define phex16(h)
  28. #endif
  29. bool ps2_mouse_enable = true;
  30. uint8_t ps2_mouse_x = 0;
  31. uint8_t ps2_mouse_y = 0;
  32. uint8_t ps2_mouse_btn = 0;
  33. uint8_t ps2_mouse_error_count = 0;
  34. static uint8_t ps2_mouse_btn_prev = 0;
  35. uint8_t ps2_mouse_init(void) {
  36. uint8_t rcv;
  37. if (!ps2_mouse_enable) return 1;
  38. ps2_host_init();
  39. _delay_ms(1000); // wait for powering up
  40. // send Reset
  41. rcv = ps2_host_send(0xFF);
  42. print("ps2_mouse_init: send Reset: ");
  43. phex(rcv); phex(ps2_error); print("\n");
  44. // read completion code of BAT
  45. rcv = ps2_host_recv();
  46. print("ps2_mouse_init: read BAT: ");
  47. phex(rcv); phex(ps2_error); print("\n");
  48. // read Device ID
  49. rcv = ps2_host_recv();
  50. print("ps2_mouse_init: read DevID: ");
  51. phex(rcv); phex(ps2_error); print("\n");
  52. // send Enable Data Reporting
  53. rcv = ps2_host_send(0xF4);
  54. print("ps2_mouse_init: send 0xF4: ");
  55. phex(rcv); phex(ps2_error); print("\n");
  56. // send Set Remote mode
  57. rcv = ps2_host_send(0xF0);
  58. print("ps2_mouse_init: send 0xF0: ");
  59. phex(rcv); phex(ps2_error); print("\n");
  60. return 0;
  61. }
  62. uint8_t ps2_mouse_read(void)
  63. {
  64. uint8_t rcv;
  65. if (!ps2_mouse_enable) return 1;
  66. rcv = ps2_host_send(0xEB);
  67. if(rcv==0xFA) {
  68. ps2_mouse_btn = ps2_host_recv_response();
  69. ps2_mouse_x = ps2_host_recv_response();
  70. ps2_mouse_y = ps2_host_recv_response();
  71. }
  72. return 0;
  73. }
  74. bool ps2_mouse_changed(void)
  75. {
  76. return (ps2_mouse_x || ps2_mouse_y || (ps2_mouse_btn & PS2_MOUSE_BTN_MASK) != ps2_mouse_btn_prev);
  77. }
  78. #define PS2_MOUSE_SCROLL_BUTTON 0x04
  79. void ps2_mouse_usb_send(void)
  80. {
  81. static bool scrolled = false;
  82. if (!ps2_mouse_enable) return;
  83. if (ps2_mouse_changed()) {
  84. int8_t x, y, v, h;
  85. x = y = v = h = 0;
  86. // convert scale of X, Y: PS/2(-256/255) -> USB(-127/127)
  87. if (ps2_mouse_btn & (1<<PS2_MOUSE_X_SIGN))
  88. x = ps2_mouse_x > 128 ? (int8_t)ps2_mouse_x : -127;
  89. else
  90. x = ps2_mouse_x < 128 ? (int8_t)ps2_mouse_x : 127;
  91. if (ps2_mouse_btn & (1<<PS2_MOUSE_Y_SIGN))
  92. y = ps2_mouse_y > 128 ? (int8_t)ps2_mouse_y : -127;
  93. else
  94. y = ps2_mouse_y < 128 ? (int8_t)ps2_mouse_y : 127;
  95. // Y is needed to reverse
  96. y = -y;
  97. if (ps2_mouse_btn & PS2_MOUSE_SCROLL_BUTTON) {
  98. // scroll
  99. if (x > 0 || x < 0) h = (x > 64 ? 64 : (x < -64 ? -64 :x));
  100. if (y > 0 || y < 0) v = (y > 64 ? 64 : (y < -64 ? -64 :y));
  101. if (h || v) {
  102. scrolled = true;
  103. usb_mouse_send(0,0, -v/16, h/16, 0);
  104. _delay_ms(100);
  105. }
  106. } else if (!scrolled && (ps2_mouse_btn_prev & PS2_MOUSE_SCROLL_BUTTON)) {
  107. usb_mouse_send(0,0,0,0, PS2_MOUSE_SCROLL_BUTTON);
  108. _delay_ms(100);
  109. usb_mouse_send(0,0,0,0, 0);
  110. } else {
  111. scrolled = false;
  112. usb_mouse_send(x, y, 0, 0, ps2_mouse_btn & PS2_MOUSE_BTN_MASK);
  113. }
  114. ps2_mouse_btn_prev = (ps2_mouse_btn & PS2_MOUSE_BTN_MASK);
  115. ps2_mouse_print();
  116. }
  117. ps2_mouse_x = 0;
  118. ps2_mouse_y = 0;
  119. ps2_mouse_btn = 0;
  120. }
  121. void ps2_mouse_print(void)
  122. {
  123. if (!debug_mouse) return;
  124. print("ps2_mouse[btn|x y]: ");
  125. phex(ps2_mouse_btn); print("|");
  126. phex(ps2_mouse_x); print(" ");
  127. phex(ps2_mouse_y); print("\n");
  128. }
  129. /* PS/2 Mouse Synopsis
  130. * http://www.computer-engineering.org/ps2mouse/
  131. *
  132. * Command:
  133. * 0xFF: Reset
  134. * 0xF6: Set Defaults Sampling; rate=100, resolution=4cnt/mm, scaling=1:1, reporting=disabled
  135. * 0xF5: Disable Data Reporting
  136. * 0xF4: Enable Data Reporting
  137. * 0xF3: Set Sample Rate
  138. * 0xF2: Get Device ID
  139. * 0xF0: Set Remote Mode
  140. * 0xEB: Read Data
  141. * 0xEA: Set Stream Mode
  142. * 0xE9: Status Request
  143. * 0xE8: Set Resolution
  144. * 0xE7: Set Scaling 2:1
  145. * 0xE6: Set Scaling 1:1
  146. *
  147. * Mode:
  148. * Stream Mode: devices sends the data when it changs its state
  149. * Remote Mode: host polls the data periodically
  150. *
  151. * This code uses Remote Mode and polls the data with Read Data(0xEB).
  152. *
  153. * Data format:
  154. * byte|7 6 5 4 3 2 1 0
  155. * ----+--------------------------------------------------------------
  156. * 0|Yovflw Xovflw Ysign Xsign 1 Middle Right Left
  157. * 1| X movement(0-255)
  158. * 2| Y movement(0-255)
  159. */