Keyboard firmwares for Atmel AVR and Cortex-M
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.

ps2_mouse.c 5.3KB

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