qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

2019.c (4717B)


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