qmk

QMK Firmware
git clone git://git.z3bra.org/qmk.git
Log | Files | Refs | Submodules | LICENSE

ez.c (10402B)


      1 /* Copyright 2018 Jack Humbert <jack.humb@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 "ez.h"
     17 #include "ch.h"
     18 #include "hal.h"
     19 
     20 keyboard_config_t keyboard_config;
     21 
     22 #ifdef RGB_MATRIX_ENABLE
     23 const is31_led g_is31_leds[DRIVER_LED_TOTAL] = {
     24 /* Refer to IS31 manual for these locations
     25  *   driver
     26  *   |  R location
     27  *   |  |      G location
     28  *   |  |      |      B location
     29  *   |  |      |      | */
     30     {0, B_12,  A_12,  C_12},
     31     {0, B_11,  A_11,  C_11},
     32     {0, B_10,  A_10,  C_10},
     33     {0, B_9,   A_9,   C_9},
     34     {0, B_8,   A_8,   C_8},
     35     {0, B_7,   A_7,   C_7},
     36 
     37     {0, H_12,  G_12,  I_12},
     38     {0, H_11,  G_11,  I_11},
     39     {0, H_10,  G_10,  I_10},
     40     {0, H_9,   G_9,   I_9},
     41     {0, H_8,   G_8,   I_8},
     42     {0, H_7,   G_7,   I_7},
     43 
     44     {0, B_6,   A_6,   C_6},
     45     {0, B_5,   A_5,   C_5},
     46     {0, B_4,   A_4,   C_4},
     47     {0, B_3,   A_3,   C_3},
     48     {0, B_2,   A_2,   C_2},
     49     {0, B_1,   A_1,   C_1},
     50 
     51     {0, H_6,   G_6,   I_6},
     52     {0, H_5,   G_5,   I_5},
     53     {0, H_4,   G_4,   I_4},
     54     {0, H_3,   G_3,   I_3},
     55     {0, H_2,   G_2,   I_2},
     56     {0, H_1,   G_1,   I_1},
     57 
     58     {0, E_12,  D_12,  F_12},
     59     {0, E_11,  D_11,  F_11},
     60     {0, E_10,  D_10,  F_10},
     61     {0, E_9,   D_9,   F_9},
     62     {0, E_8,   D_8,   F_8},
     63     {0, E_7,   D_7,   F_7},
     64 
     65     {0, K_12,  J_12,  L_12},
     66     {0, K_11,  J_11,  L_11},
     67     {0, K_10,  J_10,  L_10},
     68     {0, K_9,   J_9,   L_9},
     69     {0, K_8,   J_8,   L_8},
     70     {0, K_7,   J_7,   L_7},
     71 
     72     {0, E_6,   D_6,   F_6},
     73     {0, E_5,   D_5,   F_5},
     74     {0, E_4,   D_4,   F_4},
     75     {0, E_3,   D_3,   F_3},
     76     {0, E_2,   D_2,   F_2},
     77     {0, E_1,   D_1,   F_1},
     78 
     79     {0, K_6,   J_6,   L_6},
     80     {0, K_5,   J_5,   L_5},
     81     {0, K_4,   J_4,   L_4},
     82     {0, K_3,   J_3,   L_3},
     83     {0, K_2,   J_2,   L_2},
     84 
     85 };
     86 
     87 led_config_t g_led_config = { {
     88     {   0,   1,   2,   3,   4,   5 },
     89     {  12,  13,  14,  15,  16,  17 },
     90     {  24,  25,  26,  27,  28,  29 },
     91     {  36,  37,  38,  45,  46, NO_LED },
     92     {   6,   7,   8,   9,  10,  11 },
     93     {  18,  19,  20,  21,  22,  23 },
     94     {  30,  31,  32,  33,  34,  35 },
     95     {  42,  43,  44,  39,  40,  41 }
     96 }, {
     97     {   0,   0 }, {  20,   0 }, {  40,   0 }, {  61,   0 }, {  81,   0 }, { 101,   0 }, { 122,   0 }, { 142,   0 }, { 162,   0 }, { 183,   0 }, { 203,   0 }, { 223,   0 },
     98     {   0,  21 }, {  20,  21 }, {  40,  21 }, {  61,  21 }, {  81,  21 }, { 101,  21 }, { 122,  21 }, { 142,  21 }, { 162,  21 }, { 183,  21 }, { 203,  21 }, { 223,  21 },
     99     {   0,  42 }, {  20,  42 }, {  40,  42 }, {  61,  42 }, {  81,  42 }, { 101,  42 }, { 122,  42 }, { 142,  42 }, { 162,  42 }, { 183,  42 }, { 203,  42 }, { 223,  42 },
    100     {   0,  63 }, {  20,  63 }, {  40,  63 }, {  61,  63 }, {  81,  63 }, { 111,  63 }, { 142,  63 }, { 162,  63 }, { 183,  63 }, { 203,  63 }, { 223,  63 }
    101 }, {
    102     1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 1,
    103     1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 1,
    104     1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 1,
    105     1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1
    106 } };
    107 
    108 void suspend_power_down_kb(void) {
    109     rgb_matrix_set_color_all(0, 0, 0);
    110     rgb_matrix_set_suspend_state(true);
    111     suspend_power_down_user();
    112 }
    113 
    114  void suspend_wakeup_init_kb(void) {
    115     rgb_matrix_set_suspend_state(false);
    116     suspend_wakeup_init_user();
    117 }
    118 #endif
    119 
    120 /* Left B9   Right B8 */
    121 
    122 // See http://jared.geek.nz/2013/feb/linear-led-pwm
    123 static uint16_t cie_lightness(uint16_t v) {
    124   if (v <= 5243) // if below 8% of max
    125     return v / 9; // same as dividing by 900%
    126   else {
    127     uint32_t y = (((uint32_t) v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare
    128     // to get a useful result with integer division, we shift left in the expression above
    129     // and revert what we've done again after squaring.
    130     y = y * y * y >> 8;
    131     if (y > 0xFFFFUL) // prevent overflow
    132       return 0xFFFFU;
    133     else
    134       return (uint16_t) y;
    135   }
    136 }
    137 
    138 static PWMConfig pwmCFG = {
    139     0xFFFF,/* PWM clock frequency  */
    140     256,/* initial PWM period (in ticks) 1S (1/10kHz=0.1mS 0.1ms*10000 ticks=1S) */
    141     NULL,
    142     {
    143         {PWM_OUTPUT_DISABLED, NULL}, /* channel 0 -> TIM1-CH1 = PA8 */
    144         {PWM_OUTPUT_DISABLED, NULL}, /* channel 1 -> TIM1-CH2 = PA9 */
    145         {PWM_OUTPUT_ACTIVE_HIGH, NULL},
    146         {PWM_OUTPUT_ACTIVE_HIGH, NULL}
    147     },
    148     0, /* HW dependent part.*/
    149     0
    150 };
    151 
    152 static uint32_t planck_ez_right_led_duty;
    153 static uint32_t planck_ez_left_led_duty;
    154 
    155 void planck_ez_right_led_level(uint8_t level) {
    156     planck_ez_right_led_duty = (uint32_t)(cie_lightness(0xFFFF * (uint32_t) level / 255));
    157     if (level == 0) {
    158         // Turn backlight off
    159         pwmDisableChannel(&PWMD4, 2);
    160     } else {
    161         // Turn backlight on
    162         pwmEnableChannel(&PWMD4, 2, PWM_FRACTION_TO_WIDTH(&PWMD4,0xFFFF,planck_ez_right_led_duty));
    163     }
    164 }
    165 
    166 
    167 void planck_ez_right_led_on(void){
    168     pwmEnableChannel(&PWMD4, 2, PWM_FRACTION_TO_WIDTH(&PWMD4,0xFFFF,planck_ez_right_led_duty));
    169 }
    170 
    171 void planck_ez_right_led_off(void){
    172     pwmDisableChannel(&PWMD4, 2);
    173 }
    174 
    175 void planck_ez_left_led_level(uint8_t level) {
    176     planck_ez_left_led_duty = (uint32_t)(cie_lightness(0xFFFF * (uint32_t) level / 255));
    177     if (level == 0) {
    178         // Turn backlight off
    179         pwmDisableChannel(&PWMD4, 3);
    180     } else {
    181         // Turn backlight on
    182         pwmEnableChannel(&PWMD4, 3, PWM_FRACTION_TO_WIDTH(&PWMD4,0xFFFF,planck_ez_left_led_duty));
    183     }
    184 }
    185 
    186 void planck_ez_left_led_on(void){
    187     pwmEnableChannel(&PWMD4, 3, PWM_FRACTION_TO_WIDTH(&PWMD4,0xFFFF,planck_ez_left_led_duty));
    188 }
    189 
    190 void planck_ez_left_led_off(void){
    191     pwmDisableChannel(&PWMD4, 3);
    192 }
    193 
    194 
    195 void led_initialize_hardware(void) {
    196     pwmStart(&PWMD4, &pwmCFG);
    197 
    198     // set up defaults
    199     planck_ez_right_led_level((uint8_t)keyboard_config.led_level * 255 / 4 );
    200     palSetPadMode(GPIOB, 8, PAL_MODE_ALTERNATE(2));
    201     planck_ez_left_led_level((uint8_t)keyboard_config.led_level * 255 / 4 );
    202     palSetPadMode(GPIOB, 9, PAL_MODE_ALTERNATE(2));
    203 
    204 
    205     // turn LEDs off by default
    206     planck_ez_left_led_off();
    207     planck_ez_right_led_off();
    208 }
    209 
    210 void keyboard_pre_init_kb(void) {
    211     if (!eeconfig_is_enabled()) {
    212       eeconfig_init();
    213     }
    214     // read kb settings from eeprom
    215     keyboard_config.raw = eeconfig_read_kb();
    216 #if defined(RGB_MATRIX_ENABLE) && defined(ORYX_CONFIGURATOR)
    217     if (keyboard_config.rgb_matrix_enable) {
    218         rgb_matrix_set_flags(LED_FLAG_ALL);
    219     } else {
    220         rgb_matrix_set_flags(LED_FLAG_NONE);
    221     }
    222 #endif
    223     led_initialize_hardware();
    224     keyboard_pre_init_user();
    225 }
    226 
    227 #if defined(RGB_MATRIX_ENABLE) && defined(ORYX_CONFIGURATOR)
    228 void keyboard_post_init_kb(void) {
    229     rgb_matrix_enable_noeeprom();
    230     keyboard_post_init_user();
    231 }
    232 #endif
    233 
    234 void eeconfig_init_kb(void) {  // EEPROM is getting reset!
    235     keyboard_config.raw = 0;
    236     keyboard_config.rgb_matrix_enable = true;
    237     keyboard_config.led_level = 4;
    238     eeconfig_update_kb(keyboard_config.raw);
    239     eeconfig_init_user();
    240 }
    241 
    242 
    243 #ifdef ORYX_CONFIGURATOR
    244 
    245 #ifndef PLANCK_EZ_USER_LEDS
    246 
    247 #ifndef PLANCK_EZ_LED_LOWER
    248 #    define PLANCK_EZ_LED_LOWER 3
    249 #endif
    250 #ifndef PLANCK_EZ_LED_RAISE
    251 #    define PLANCK_EZ_LED_RAISE 4
    252 #endif
    253 #ifndef PLANCK_EZ_LED_ADJUST
    254 #    define PLANCK_EZ_LED_ADJUST 6
    255 #endif
    256 
    257 layer_state_t layer_state_set_kb(layer_state_t state) {
    258     planck_ez_left_led_off();
    259     planck_ez_right_led_off();
    260     state = layer_state_set_user(state);
    261     uint8_t layer = biton32(state);
    262     switch (layer) {
    263         case PLANCK_EZ_LED_LOWER:
    264             planck_ez_left_led_on();
    265             break;
    266         case PLANCK_EZ_LED_RAISE:
    267             planck_ez_right_led_on();
    268             break;
    269         case PLANCK_EZ_LED_ADJUST:
    270             planck_ez_right_led_on();
    271             planck_ez_left_led_on();
    272             break;
    273         default:
    274             break;
    275     }
    276     return state;
    277 }
    278 #endif
    279 
    280 bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
    281     switch (keycode) {
    282         case LED_LEVEL:
    283             if (record->event.pressed) {
    284                  keyboard_config.led_level++;
    285                  if (keyboard_config.led_level > 4) {
    286                     keyboard_config.led_level = 0;
    287                  }
    288                  planck_ez_right_led_level((uint8_t)keyboard_config.led_level * 255 / 4 );
    289                  planck_ez_left_led_level((uint8_t)keyboard_config.led_level * 255 / 4 );
    290                  eeconfig_update_kb(keyboard_config.raw);
    291                  layer_state_set_kb(layer_state);
    292             }
    293             break;
    294 #ifdef RGB_MATRIX_ENABLE
    295         case TOGGLE_LAYER_COLOR:
    296             if (record->event.pressed) {
    297                 keyboard_config.disable_layer_led ^= 1;
    298                 if (keyboard_config.disable_layer_led)
    299                     rgb_matrix_set_color_all(0, 0, 0);
    300                 eeconfig_update_kb(keyboard_config.raw);
    301             }
    302             break;
    303         case RGB_TOG:
    304             if (record->event.pressed) {
    305               switch (rgb_matrix_get_flags()) {
    306                 case LED_FLAG_ALL: {
    307                     rgb_matrix_set_flags(LED_FLAG_NONE);
    308                     keyboard_config.rgb_matrix_enable = false;
    309                     rgb_matrix_set_color_all(0, 0, 0);
    310                   }
    311                   break;
    312                 default: {
    313                     rgb_matrix_set_flags(LED_FLAG_ALL);
    314                     keyboard_config.rgb_matrix_enable = true;
    315                   }
    316                   break;
    317               }
    318               eeconfig_update_kb(keyboard_config.raw);
    319             }
    320             return false;
    321 #endif
    322     }
    323     return process_record_user(keycode, record);
    324 }
    325 #endif
    326 
    327 #ifdef AUDIO_ENABLE
    328 bool music_mask_kb(uint16_t keycode) {
    329     switch (keycode) {
    330     case QK_LAYER_TAP ... QK_ONE_SHOT_LAYER_MAX:
    331     case QK_LAYER_TAP_TOGGLE ... QK_LAYER_MOD_MAX:
    332     case QK_MOD_TAP ... QK_MOD_TAP_MAX:
    333     case AU_ON ... MUV_DE:
    334     case RESET:
    335     case EEP_RST:
    336         return false;
    337     default:
    338         return music_mask_user(keycode);
    339     }
    340 }
    341 #endif