qmk_firmware

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

rev2.c (3779B)


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