logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

2019.c (4719B)


  1. /* Copyright 2017 Zach White <skullydazed@gmail.com>
  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 "2019.h"
  17. void matrix_init_kb(void) {
  18. // Set our LED pins as output
  19. gpio_set_pin_output(D6);
  20. // Set our Tilt Sensor pins as input
  21. gpio_set_pin_input_high(SHAKE_PIN_A);
  22. gpio_set_pin_input_high(SHAKE_PIN_B);
  23. // Run the keymap level init
  24. matrix_init_user();
  25. }
  26. #ifdef DRAWING_ENABLE
  27. bool drawing_mode = false;
  28. bool btn1_pressed = false;
  29. bool btn2_pressed = false;
  30. bool btn3_pressed = false;
  31. bool btn4_pressed = false;
  32. void check_encoder_buttons(void) {
  33. if (btn1_pressed && btn2_pressed && btn3_pressed && btn4_pressed) {
  34. // All 4 buttons pressed, toggle drawing mode
  35. if (drawing_mode) {
  36. dprintf("Turning drawing mode off.\n");
  37. drawing_mode = false;
  38. gpio_write_pin_low(D6);
  39. unregister_code(KC_BTN1);
  40. } else {
  41. dprintf("Turning drawing mode on.\n");
  42. drawing_mode = true;
  43. gpio_write_pin_high(D6);
  44. register_code(KC_BTN1);
  45. }
  46. }
  47. }
  48. #endif
  49. #ifdef SHAKE_ENABLE
  50. uint8_t tilt_state = 0x11;
  51. uint8_t detected_shakes = 0;
  52. static uint16_t shake_timer;
  53. #endif
  54. void housekeeping_task_kb(void) {
  55. #ifdef SHAKE_ENABLE
  56. // Read the current state of the tilt sensor. It is physically
  57. // impossible for both pins to register a low state at the same time.
  58. uint8_t tilt_read = (gpio_read_pin(SHAKE_PIN_A) << 4) | gpio_read_pin(SHAKE_PIN_B);
  59. // Check to see if the tilt sensor has changed state since our last read
  60. if (tilt_state != tilt_read) {
  61. shake_timer = timer_read();
  62. detected_shakes++;
  63. tilt_state = tilt_read;
  64. }
  65. if ((detected_shakes > 0) && (timer_elapsed(shake_timer) > SHAKE_TIMEOUT)) {
  66. if (detected_shakes > SHAKE_COUNT) {
  67. dprintf("Shake triggered! We detected %d shakes.\n", detected_shakes);
  68. tap_code16(SHAKE_KEY);
  69. } else {
  70. dprintf("Shake not triggered! We detected %d shakes.\n", detected_shakes);
  71. }
  72. detected_shakes = 0;
  73. }
  74. #endif
  75. }
  76. bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  77. #ifdef DRAWING_ENABLE
  78. if (keycode == ENC_BTN1) {
  79. if (record->event.pressed) {
  80. btn1_pressed = true;
  81. register_code(KC_BTN1);
  82. } else {
  83. btn1_pressed = false;
  84. unregister_code(KC_BTN1);
  85. }
  86. }
  87. if (keycode == ENC_BTN2) {
  88. if (record->event.pressed) {
  89. btn2_pressed = true;
  90. register_code(KC_BTN2);
  91. } else {
  92. btn2_pressed = false;
  93. unregister_code(KC_BTN2);
  94. }
  95. }
  96. if (keycode == ENC_BTN3) {
  97. if (record->event.pressed) {
  98. btn3_pressed = true;
  99. register_code(KC_BTN3);
  100. } else {
  101. btn3_pressed = false;
  102. unregister_code(KC_BTN3);
  103. }
  104. }
  105. if (keycode == ENC_BTN4) {
  106. if (record->event.pressed) {
  107. btn4_pressed = true;
  108. register_code(KC_BTN4);
  109. } else {
  110. btn4_pressed = false;
  111. unregister_code(KC_BTN4);
  112. }
  113. }
  114. check_encoder_buttons();
  115. #endif
  116. return process_record_user(keycode, record);
  117. }
  118. bool encoder_update_kb(uint8_t index, bool clockwise) {
  119. if (encoder_update_user(index, clockwise)) {
  120. // Encoder 1, outside left
  121. if (index == 0 && clockwise) {
  122. tap_code(KC_MS_U); // turned right
  123. } else if (index == 0) {
  124. tap_code(KC_MS_D); // turned left
  125. }
  126. // Encoder 2, inside left
  127. else if (index == 1 && clockwise) {
  128. tap_code(KC_WH_D); // turned right
  129. } else if (index == 1) {
  130. tap_code(KC_WH_U); // turned left
  131. }
  132. // Encoder 3, inside right
  133. else if (index == 2 && clockwise) {
  134. tap_code(KC_VOLU); // turned right
  135. } else if (index == 2) {
  136. tap_code(KC_VOLD); // turned left
  137. }
  138. // Encoder 4, outside right
  139. else if (index == 3 && clockwise) {
  140. tap_code(KC_MS_R); // turned right
  141. } else if (index == 3) {
  142. tap_code(KC_MS_L); // turned left
  143. }
  144. }
  145. return true;
  146. }