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.

125 lines
3.7 KiB

  1. // Copyright 2022 zzeneg (@zzeneg)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "rev2.h"
  4. #include "gpio.h"
  5. #ifdef ENCODER_ENABLE // code based on encoder.c
  6. #define ENCODER_PIN_A (((pin_t[])ENCODERS_PAD_A)[0])
  7. #define ENCODER_PIN_B (((pin_t[])ENCODERS_PAD_B)[0])
  8. // custom handler that returns encoder B pin status from slave side
  9. void encoder_sync_slave_handler(uint8_t in_buflen, const void *in_data, uint8_t out_buflen, void *out_data) {
  10. *(uint8_t *)out_data = readPin(ENCODER_PIN_B) ? 1 : 0;
  11. }
  12. void encoder_quadrature_init_pin(uint8_t index, bool pad_b) {}
  13. uint8_t encoder_quadrature_read_pin(uint8_t index, bool pad_b) {
  14. if(pad_b) {
  15. uint8_t data = 0;
  16. transaction_rpc_recv(ENCODER_SYNC, sizeof(data), &data);
  17. return data;
  18. }
  19. return readPin(ENCODER_PIN_A) ? 1 : 0;
  20. }
  21. #endif // ENCODER_ENABLE
  22. #ifdef PICA40_RGBLIGHT_TIMEOUT
  23. uint16_t check_rgblight_timer = 0;
  24. uint16_t idle_timer = 0;
  25. int8_t counter = 0;
  26. bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  27. if (record->event.pressed && timer_elapsed(idle_timer) > 1000) {
  28. idle_timer = timer_read();
  29. counter = 0;
  30. if (!rgblight_is_enabled()) {
  31. rgblight_enable_noeeprom();
  32. }
  33. }
  34. return process_record_user(keycode, record);
  35. }
  36. #endif // PICA40_RGBLIGHT_TIMEOUT
  37. #if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_LAYERS)
  38. uint16_t check_layer_timer = 0;
  39. bool is_layer_active = false;
  40. bool should_set_rgblight = false;
  41. #endif // defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_LAYERS)
  42. void keyboard_post_init_kb(void) {
  43. setPinOutput(PICA40_RGB_POWER_PIN);
  44. #ifdef ENCODER_ENABLE
  45. setPinInputHigh(ENCODER_PIN_A);
  46. setPinInputHigh(ENCODER_PIN_B);
  47. transaction_register_rpc(ENCODER_SYNC, encoder_sync_slave_handler);
  48. #endif // ENCODER_ENABLE
  49. #ifdef PICA40_RGBLIGHT_TIMEOUT
  50. idle_timer = timer_read();
  51. check_rgblight_timer = timer_read();
  52. rgblight_enable_noeeprom();
  53. #endif // RGBLIGHT_ENABLE
  54. #if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_LAYERS)
  55. check_layer_timer = timer_read();
  56. #endif // defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_LAYERS)
  57. keyboard_post_init_user();
  58. }
  59. void housekeeping_task_kb(void) {
  60. #ifdef PICA40_RGBLIGHT_TIMEOUT
  61. if (is_keyboard_master()) {
  62. if (timer_elapsed(check_rgblight_timer) > 1000) {
  63. check_rgblight_timer = timer_read();
  64. if (rgblight_is_enabled() && timer_elapsed(idle_timer) > 10000) {
  65. idle_timer = timer_read();
  66. counter++;
  67. }
  68. if (rgblight_is_enabled() && counter > PICA40_RGBLIGHT_TIMEOUT * 6) {
  69. counter = 0;
  70. rgblight_disable_noeeprom();
  71. }
  72. }
  73. }
  74. #endif // PICA40_RGBLIGHT_TIMEOUT
  75. #if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_LAYERS)
  76. if (timer_elapsed(check_layer_timer) > 100) {
  77. check_layer_timer = timer_read();
  78. if (should_set_rgblight) {
  79. // set in the next housekeeping cycle after setting pin to avoid issues
  80. rgblight_set();
  81. should_set_rgblight = false;
  82. }
  83. bool current_is_layer_active = false;
  84. for (uint8_t i = 0; i < RGBLIGHT_MAX_LAYERS; i++) {
  85. current_is_layer_active = current_is_layer_active || rgblight_get_layer_state(i);
  86. }
  87. if (is_layer_active != current_is_layer_active) {
  88. is_layer_active = current_is_layer_active;
  89. should_set_rgblight = true;
  90. if (is_layer_active) {
  91. writePinHigh(PICA40_RGB_POWER_PIN);
  92. } else {
  93. writePinLow(PICA40_RGB_POWER_PIN);
  94. }
  95. }
  96. }
  97. #endif // defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_LAYERS)
  98. housekeeping_task_user();
  99. }