plover_hid.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /* Copyright 2021 dnaq
  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 <limits.h>
  17. #include "plover_hid.h"
  18. #include "report.h"
  19. #include "host.h"
  20. #include "compiler_support.h"
  21. // The button index handed to plover_hid_update() is `keycode - QK_PLOVER_HID`, so the report's
  22. // data field must hold a bit for every keycode in the range or the bit-packing below overflows.
  23. STATIC_ASSERT(sizeof(((report_plover_hid_t *)0)->data) * CHAR_BIT >= (QK_PLOVER_HID_MAX - QK_PLOVER_HID + 1), "Plover HID report data too small for the QK_PLOVER_HID keycode range");
  24. static report_plover_hid_t plover_hid_report = {.report_id = REPORT_ID_PLOVER_HID};
  25. static bool plover_hid_report_updated = false;
  26. void plover_hid_update(uint8_t button, bool pressed) {
  27. if (pressed) {
  28. plover_hid_report.data[button / 8] |= (1 << (7 - (button % 8)));
  29. } else {
  30. plover_hid_report.data[button / 8] &= ~(1 << (7 - (button % 8)));
  31. }
  32. plover_hid_report_updated = true;
  33. }
  34. void plover_hid_task(void) {
  35. if (!plover_hid_report_updated) {
  36. return;
  37. }
  38. host_plover_hid_send(&plover_hid_report);
  39. plover_hid_report_updated = false;
  40. }