mouse_emu.ino 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. void mouse_move_absolute(uint8_t x_lsb, uint8_t x_msb, uint8_t y_lsb, uint8_t y_msb) {
  8. uint8_t packet[12] = {
  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 // Checksum placeholder
  16. };
  17. packet[11] = calcChecksum(packet, 11);
  18. Serial0_writeBuf(packet, 12);
  19. }
  20. //Move the mouse to given relative position
  21. void mouse_move_relative(int8_t dx, int8_t dy, int8_t wheel) {
  22. uint8_t packet[11] = {
  23. 0x57, 0xAB, 0x00, 0x05, 0x05, 0x01,
  24. mouse_button_state,
  25. dx,
  26. dy,
  27. wheel,
  28. 0x00 // Checksum placeholder
  29. };
  30. packet[10] = calcChecksum(packet, 10);
  31. Serial0_writeBuf(packet, 11);
  32. }
  33. int mouse_scroll_up(uint8_t tilt) {
  34. if (tilt > 0x7F)
  35. tilt = 0x7F;
  36. if (tilt == 0) {
  37. //No need to move
  38. return 0;
  39. }
  40. mouse_move_relative(0, 0, tilt);
  41. return 0;
  42. }
  43. int mouse_scroll_down(uint8_t tilt) {
  44. if (tilt > 0x7E)
  45. tilt = 0x7E;
  46. if (tilt == 0) {
  47. //No need to move
  48. return 0;
  49. }
  50. mouse_move_relative(0, 0, 0xFF-tilt);
  51. return 0;
  52. }
  53. //handle mouse button press events
  54. int mouse_button_press(uint8_t opcode) {
  55. switch (opcode) {
  56. case 0x01: // Left
  57. mouse_button_state |= 0x01;
  58. break;
  59. case 0x02: // Right
  60. mouse_button_state |= 0x02;
  61. break;
  62. case 0x03: // Middle
  63. mouse_button_state |= 0x04;
  64. break;
  65. default:
  66. return -1;
  67. }
  68. // Send updated button state with no movement
  69. mouse_move_relative(0, 0, 0);
  70. return 0;
  71. }
  72. //handle mouse button release events
  73. int mouse_button_release(uint8_t opcode) {
  74. switch (opcode) {
  75. case 0x00: // Release all
  76. mouse_button_state = 0x00;
  77. break;
  78. case 0x01: // Left
  79. mouse_button_state &= ~0x01;
  80. break;
  81. case 0x02: // Right
  82. mouse_button_state &= ~0x02;
  83. break;
  84. case 0x03: // Middle
  85. mouse_button_state &= ~0x04;
  86. break;
  87. default:
  88. return -1;
  89. }
  90. // Send updated button state with no movement
  91. mouse_move_relative(0, 0, 0);
  92. return 0;
  93. }