rn42.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 "rn42.h"
  17. #include "report.h"
  18. #include "uart.h"
  19. #ifndef RN42_BAUD_RATE
  20. # define RN42_BAUD_RATE 115200
  21. #endif
  22. // https://cdn.sparkfun.com/datasheets/Wireless/Bluetooth/bluetooth_cr_UG-v1.0r.pdf#G7.663734
  23. static inline uint16_t rn42_consumer_usage_to_bitmap(uint16_t usage) {
  24. switch (usage) {
  25. case AC_HOME:
  26. return 0x0001;
  27. case AL_EMAIL:
  28. return 0x0002;
  29. case AC_SEARCH:
  30. return 0x0004;
  31. case AL_KEYBOARD_LAYOUT:
  32. return 0x0008;
  33. case AUDIO_VOL_UP:
  34. return 0x0010;
  35. case AUDIO_VOL_DOWN:
  36. return 0x0020;
  37. case AUDIO_MUTE:
  38. return 0x0040;
  39. case TRANSPORT_PLAY_PAUSE:
  40. return 0x0080;
  41. case TRANSPORT_NEXT_TRACK:
  42. return 0x0100;
  43. case TRANSPORT_PREV_TRACK:
  44. return 0x0200;
  45. case TRANSPORT_STOP:
  46. return 0x0400;
  47. case TRANSPORT_EJECT:
  48. return 0x0800;
  49. case TRANSPORT_FAST_FORWARD:
  50. return 0x1000;
  51. case TRANSPORT_REWIND:
  52. return 0x2000;
  53. case TRANSPORT_STOP_EJECT:
  54. return 0x4000;
  55. case AL_LOCAL_BROWSER:
  56. return 0x8000;
  57. default:
  58. return 0;
  59. }
  60. }
  61. void rn42_init(void) {
  62. uart_init(RN42_BAUD_RATE);
  63. }
  64. void rn42_send_keyboard(report_keyboard_t *report) {
  65. uart_write(0xFD);
  66. uart_write(0x09);
  67. uart_write(0x01);
  68. uart_write(report->mods);
  69. uart_write(0x00);
  70. uart_write(report->keys[0]);
  71. uart_write(report->keys[1]);
  72. uart_write(report->keys[2]);
  73. uart_write(report->keys[3]);
  74. uart_write(report->keys[4]);
  75. uart_write(report->keys[5]);
  76. }
  77. void rn42_send_mouse(report_mouse_t *report) {
  78. uart_write(0xFD);
  79. uart_write(0x05);
  80. uart_write(0x02);
  81. uart_write(report->buttons);
  82. uart_write(report->x);
  83. uart_write(report->y);
  84. uart_write(report->v);
  85. }
  86. void rn42_send_consumer(uint16_t usage) {
  87. uint16_t bitmap = rn42_consumer_usage_to_bitmap(usage);
  88. uart_write(0xFD);
  89. uart_write(0x03);
  90. uart_write(0x03);
  91. uart_write(bitmap & 0xFF);
  92. uart_write(bitmap >> 8);
  93. }