2019.c 5.1 KB

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