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.

101 lines
2.9 KiB

  1. /* Copyright 2021
  2. *
  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. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "report.h"
  17. #include "uart.h"
  18. #ifndef RN42_BAUD_RATE
  19. # define RN42_BAUD_RATE 115200
  20. #endif
  21. // https://cdn.sparkfun.com/datasheets/Wireless/Bluetooth/bluetooth_cr_UG-v1.0r.pdf#G7.663734
  22. static inline uint16_t rn42_consumer_usage_to_bitmap(uint16_t usage) {
  23. switch (usage) {
  24. case AC_HOME:
  25. return 0x0001;
  26. case AL_EMAIL:
  27. return 0x0002;
  28. case AC_SEARCH:
  29. return 0x0004;
  30. case AL_KEYBOARD_LAYOUT:
  31. return 0x0008;
  32. case AUDIO_VOL_UP:
  33. return 0x0010;
  34. case AUDIO_VOL_DOWN:
  35. return 0x0020;
  36. case AUDIO_MUTE:
  37. return 0x0040;
  38. case TRANSPORT_PLAY_PAUSE:
  39. return 0x0080;
  40. case TRANSPORT_NEXT_TRACK:
  41. return 0x0100;
  42. case TRANSPORT_PREV_TRACK:
  43. return 0x0200;
  44. case TRANSPORT_STOP:
  45. return 0x0400;
  46. case TRANSPORT_EJECT:
  47. return 0x0800;
  48. case TRANSPORT_FAST_FORWARD:
  49. return 0x1000;
  50. case TRANSPORT_REWIND:
  51. return 0x2000;
  52. case TRANSPORT_STOP_EJECT:
  53. return 0x4000;
  54. case AL_LOCAL_BROWSER:
  55. return 0x8000;
  56. default:
  57. return 0;
  58. }
  59. }
  60. void rn42_init(void) {
  61. uart_init(RN42_BAUD_RATE);
  62. }
  63. void rn42_send_keyboard(report_keyboard_t *report) {
  64. uart_write(0xFD);
  65. uart_write(0x09);
  66. uart_write(0x01);
  67. uart_write(report->mods);
  68. uart_write(0x00);
  69. for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
  70. uart_write(report->keys[i]);
  71. }
  72. }
  73. void rn42_send_mouse(report_mouse_t *report) {
  74. uart_write(0xFD);
  75. uart_write(0x00);
  76. uart_write(0x03);
  77. uart_write(report->buttons);
  78. uart_write(report->x);
  79. uart_write(report->y);
  80. uart_write(report->v); // should try sending the wheel v here
  81. uart_write(report->h); // should try sending the wheel h here
  82. uart_write(0x00);
  83. }
  84. void rn42_send_consumer(uint16_t data) {
  85. static uint16_t last_data = 0;
  86. if (data == last_data) return;
  87. last_data = data;
  88. uint16_t bitmap = rn42_consumer_usage_to_bitmap(data);
  89. uart_write(0xFD);
  90. uart_write(0x03);
  91. uart_write(0x03);
  92. uart_write(bitmap & 0xFF);
  93. uart_write((bitmap >> 8) & 0xFF);
  94. }