qmk_firmware

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

ec_980c.c (6711B)


      1 /* Copyright 2023 Cipulot
      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 3 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 
     17 #include "ec_switch_matrix.h"
     18 #include "quantum.h"
     19 
     20 void eeconfig_init_kb(void) {
     21     // Default values
     22     eeprom_ec_config.num.h                          = 0;
     23     eeprom_ec_config.num.s                          = 0;
     24     eeprom_ec_config.num.v                          = 60;
     25     eeprom_ec_config.num.enabled                    = true;
     26     eeprom_ec_config.caps.h                         = 0;
     27     eeprom_ec_config.caps.s                         = 0;
     28     eeprom_ec_config.caps.v                         = 60;
     29     eeprom_ec_config.caps.enabled                   = true;
     30     eeprom_ec_config.scroll.h                       = 0;
     31     eeprom_ec_config.scroll.s                       = 0;
     32     eeprom_ec_config.scroll.v                       = 60;
     33     eeprom_ec_config.scroll.enabled                 = true;
     34     eeprom_ec_config.actuation_mode                 = DEFAULT_ACTUATION_MODE;
     35     eeprom_ec_config.mode_0_actuation_threshold     = DEFAULT_MODE_0_ACTUATION_LEVEL;
     36     eeprom_ec_config.mode_0_release_threshold       = DEFAULT_MODE_0_RELEASE_LEVEL;
     37     eeprom_ec_config.mode_1_initial_deadzone_offset = DEFAULT_MODE_1_INITIAL_DEADZONE_OFFSET;
     38     eeprom_ec_config.mode_1_actuation_offset        = DEFAULT_MODE_1_ACTUATION_OFFSET;
     39     eeprom_ec_config.mode_1_release_offset          = DEFAULT_MODE_1_RELEASE_OFFSET;
     40 
     41     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
     42         for (uint8_t col = 0; col < MATRIX_COLS; col++) {
     43             eeprom_ec_config.bottoming_reading[row][col] = DEFAULT_BOTTOMING_READING;
     44         }
     45     }
     46     // Write default value to EEPROM now
     47     eeconfig_update_kb_datablock(&eeprom_ec_config, 0, EECONFIG_KB_DATA_SIZE);
     48 
     49     eeconfig_init_user();
     50 }
     51 
     52 // On Keyboard startup
     53 void keyboard_post_init_kb(void) {
     54     // Read custom menu variables from memory
     55     eeconfig_read_kb_datablock(&eeprom_ec_config, 0, EECONFIG_KB_DATA_SIZE);
     56 
     57     // Set runtime values to EEPROM values
     58     ec_config.actuation_mode                 = eeprom_ec_config.actuation_mode;
     59     ec_config.mode_0_actuation_threshold     = eeprom_ec_config.mode_0_actuation_threshold;
     60     ec_config.mode_0_release_threshold       = eeprom_ec_config.mode_0_release_threshold;
     61     ec_config.mode_1_initial_deadzone_offset = eeprom_ec_config.mode_1_initial_deadzone_offset;
     62     ec_config.mode_1_actuation_offset        = eeprom_ec_config.mode_1_actuation_offset;
     63     ec_config.mode_1_release_offset          = eeprom_ec_config.mode_1_release_offset;
     64     ec_config.bottoming_calibration          = false;
     65     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
     66         for (uint8_t col = 0; col < MATRIX_COLS; col++) {
     67             ec_config.bottoming_calibration_starter[row][col]           = true;
     68             ec_config.bottoming_reading[row][col]                       = eeprom_ec_config.bottoming_reading[row][col];
     69             ec_config.rescaled_mode_0_actuation_threshold[row][col]     = rescale(ec_config.mode_0_actuation_threshold, 0, 1023, ec_config.noise_floor[row][col], eeprom_ec_config.bottoming_reading[row][col]);
     70             ec_config.rescaled_mode_0_release_threshold[row][col]       = rescale(ec_config.mode_0_release_threshold, 0, 1023, ec_config.noise_floor[row][col], eeprom_ec_config.bottoming_reading[row][col]);
     71             ec_config.rescaled_mode_1_initial_deadzone_offset[row][col] = rescale(ec_config.mode_1_initial_deadzone_offset, 0, 1023, ec_config.noise_floor[row][col], eeprom_ec_config.bottoming_reading[row][col]);
     72             ec_config.rescaled_mode_1_actuation_offset[row][col]        = rescale(ec_config.mode_1_actuation_offset, 0, 1023, ec_config.noise_floor[row][col], eeprom_ec_config.bottoming_reading[row][col]);
     73             ec_config.rescaled_mode_1_release_offset[row][col]          = rescale(ec_config.mode_1_release_offset, 0, 1023, ec_config.noise_floor[row][col], eeprom_ec_config.bottoming_reading[row][col]);
     74         }
     75     }
     76 
     77     // Call the indicator callback to set the indicator color
     78     rgb_matrix_indicators_kb();
     79 
     80     keyboard_post_init_user();
     81 }
     82 
     83 // INDICATOR CALLBACK ------------------------------------------------------------------------------
     84 /* LED index to physical position
     85  *
     86  * LED0 | LED1 |  LED2
     87  * -----+------+--------
     88  * Num  | Caps | Scroll |
     89  */
     90 bool rgb_matrix_indicators_kb(void) {
     91     if (!rgb_matrix_indicators_user()) {
     92         return false;
     93     }
     94 
     95     if (eeprom_ec_config.num.enabled) {
     96         // The rgb_matrix_set_color function needs an RGB code to work, so first the indicator color is cast to an HSV value and then translated to RGB
     97         hsv_t hsv_num_indicator_color = {eeprom_ec_config.num.h, eeprom_ec_config.num.s, eeprom_ec_config.num.v};
     98         rgb_t rgb_num_indicator_color = hsv_to_rgb(hsv_num_indicator_color);
     99         if (host_keyboard_led_state().num_lock)
    100             rgb_matrix_set_color(NUM_INDICATOR_INDEX, rgb_num_indicator_color.r, rgb_num_indicator_color.g, rgb_num_indicator_color.b);
    101         else
    102             rgb_matrix_set_color(NUM_INDICATOR_INDEX, 0, 0, 0);
    103     }
    104     if (eeprom_ec_config.caps.enabled) {
    105         hsv_t hsv_caps_indicator_color = {eeprom_ec_config.caps.h, eeprom_ec_config.caps.s, eeprom_ec_config.caps.v};
    106         rgb_t rgb_caps_indicator_color = hsv_to_rgb(hsv_caps_indicator_color);
    107         if (host_keyboard_led_state().caps_lock)
    108             rgb_matrix_set_color(CAPS_INDICATOR_INDEX, rgb_caps_indicator_color.r, rgb_caps_indicator_color.g, rgb_caps_indicator_color.b);
    109         else
    110             rgb_matrix_set_color(CAPS_INDICATOR_INDEX, 0, 0, 0);
    111     }
    112     if (eeprom_ec_config.scroll.enabled) {
    113         hsv_t hsv_scroll_indicator_color = {eeprom_ec_config.scroll.h, eeprom_ec_config.scroll.s, eeprom_ec_config.scroll.v};
    114         rgb_t rgb_scroll_indicator_color = hsv_to_rgb(hsv_scroll_indicator_color);
    115         if (host_keyboard_led_state().scroll_lock)
    116             rgb_matrix_set_color(SCROLL_INDICATOR_INDEX, rgb_scroll_indicator_color.r, rgb_scroll_indicator_color.g, rgb_scroll_indicator_color.b);
    117         else
    118             rgb_matrix_set_color(SCROLL_INDICATOR_INDEX, 0, 0, 0);
    119     }
    120 
    121     return true;
    122 }