mouse_emu.ino 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. mouse_emu.ino
  3. This file contain code that emulate mouse movements
  4. */
  5. uint8_t mouse_button_state = 0x00;
  6. //Move the mouse to given position, range 0 - 4096 for both x and y value
  7. int mouse_move_absolute(uint8_t x_lsb, uint8_t x_msb, uint8_t y_lsb, uint8_t y_msb) {
  8. uint8_t packet[13] = {
  9. 0x57, 0xAB, 0x00, 0x04, 0x07, 0x02,
  10. mouse_button_state,
  11. x_lsb, // X LSB
  12. x_msb, // X MSB
  13. y_lsb, // Y LSB
  14. y_msb, // Y MSB
  15. 0x00, // Scroll
  16. 0x00 // Checksum placeholder
  17. };
  18. packet[12] = calcChecksum(packet, 12);
  19. Serial0_writeBuf(packet, 13);
  20. return 0;
  21. }
  22. //Move the mouse to given relative position
  23. int mouse_move_relative(uint8_t dx, uint8_t dy, uint8_t wheel) {
  24. //Make sure 0x80 is not used
  25. dx = (dx == 0x80)?0x81:dx;
  26. dy = (dy == 0x80)?0x81:dy;
  27. uint8_t packet[11] = {
  28. 0x57, 0xAB, 0x00, 0x05, 0x05, 0x01,
  29. mouse_button_state,
  30. dx,
  31. dy,
  32. wheel,
  33. 0x00 // Checksum placeholder
  34. };
  35. packet[10] = calcChecksum(packet, 10);
  36. Serial0_writeBuf(packet, 11);
  37. return 0;
  38. }
  39. int mouse_scroll_up(uint8_t tilt) {
  40. if (tilt > 0x7F)
  41. tilt = 0x7F;
  42. if (tilt == 0) {
  43. //No need to move
  44. return 0;
  45. }
  46. mouse_move_relative(0, 0, tilt);
  47. return 0;
  48. }
  49. int mouse_scroll_down(uint8_t tilt) {
  50. if (tilt > 0x7E)
  51. tilt = 0x7E;
  52. if (tilt == 0) {
  53. //No need to move
  54. return 0;
  55. }
  56. mouse_move_relative(0, 0, 0xFF-tilt);
  57. return 0;
  58. }
  59. //handle mouse button press events
  60. int mouse_button_press(uint8_t opcode) {
  61. switch (opcode) {
  62. case 0x01: // Left
  63. mouse_button_state |= 0x01;
  64. break;
  65. case 0x02: // Right
  66. mouse_button_state |= 0x02;
  67. break;
  68. case 0x03: // Middle
  69. mouse_button_state |= 0x04;
  70. break;
  71. default:
  72. return -1;
  73. }
  74. // Send updated button state with no movement
  75. mouse_move_relative(0, 0, 0);
  76. return 0;
  77. }
  78. //handle mouse button release events
  79. int mouse_button_release(uint8_t opcode) {
  80. switch (opcode) {
  81. case 0x00: // Release all
  82. mouse_button_state = 0x00;
  83. break;
  84. case 0x01: // Left
  85. mouse_button_state &= ~0x01;
  86. break;
  87. case 0x02: // Right
  88. mouse_button_state &= ~0x02;
  89. break;
  90. case 0x03: // Middle
  91. mouse_button_state &= ~0x04;
  92. break;
  93. default:
  94. return -1;
  95. }
  96. // Send updated button state with no movement
  97. mouse_move_relative(0, 0, 0);
  98. return 0;
  99. }
  100. //Reset all mouse clicks state
  101. void mouse_reset(){
  102. mouse_button_state = 0x00;
  103. mouse_move_relative(0, 0, 0);
  104. }