plover_hid.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. // Data field must hold a bit for every button in the range or the bit-packing below overflows.
  22. STATIC_ASSERT(sizeof(((report_plover_hid_t *)0)->data) * CHAR_BIT >= PLV_BTN_COUNT, "Plover HID report data too small for the button range");
  23. static report_plover_hid_t plover_hid_report = {.report_id = REPORT_ID_PLOVER_HID};
  24. static bool plover_hid_report_updated = false;
  25. void plover_hid_update(uint8_t button, bool pressed) {
  26. if (button >= PLV_BTN_COUNT) {
  27. return;
  28. }
  29. if (pressed) {
  30. plover_hid_report.data[button / 8] |= (1 << (7 - (button % 8)));
  31. } else {
  32. plover_hid_report.data[button / 8] &= ~(1 << (7 - (button % 8)));
  33. }
  34. plover_hid_report_updated = true;
  35. }
  36. void plover_hid_task(void) {
  37. if (!plover_hid_report_updated) {
  38. return;
  39. }
  40. host_plover_hid_send(&plover_hid_report);
  41. plover_hid_report_updated = false;
  42. }