qmk_firmware

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

rev1.c (6138B)


      1 /*
      2  * ----------------------------------------------------------------------------
      3  * "THE BEER-WARE LICENSE" (Revision 42):
      4  * <https://github.com/Legonut> wrote this file.  As long as you retain this
      5  * notice you can do whatever you want with this stuff. If we meet some day, and
      6  * you think this stuff is worth it, you can buy me a beer in return. David Rauseo
      7  * ----------------------------------------------------------------------------
      8  */
      9 
     10 #include "rev1.h"
     11 #include "transactions.h"
     12 #include "split_util.h"
     13 
     14 #define NUMBER_OF_TOUCH_ENCODERS 2
     15 #define TOUCH_ENCODER_OPTIONS TOUCH_SEGMENTS + 2
     16 
     17 typedef struct PACKED {
     18     uint8_t r;
     19     uint8_t c;
     20 } encodermap_t;
     21 
     22 const encodermap_t touch_encoder_map[NUMBER_OF_TOUCH_ENCODERS][TOUCH_ENCODER_OPTIONS] =
     23 {
     24     { { 1, 7 }, { 0, 7 }, { 2, 7 }, {  5, 6 }, {  5, 7 }, }, // Touch Encoder 0 matrix entries
     25     { { 7, 7 }, { 6, 7 }, { 8, 7 }, { 11, 6 }, { 11, 7 }, }  // Touch Encoder 1 matrix entries
     26 };
     27 
     28 static bool limit_lightning = true;
     29 
     30 RGB rgb_matrix_hsv_to_rgb(hsv_t hsv) {
     31     if (limit_lightning) hsv.v /= 2;
     32     return hsv_to_rgb(hsv);
     33 }
     34 
     35 bool dip_switch_update_kb(uint8_t index, bool active) {
     36     if (!dip_switch_update_user(index, active))
     37         return false;
     38 
     39     switch(index) {
     40         case 0: {
     41             limit_lightning = active;
     42             break;
     43         }
     44         case 1: {
     45             // Handle RGB Encoder switch press
     46             action_exec(MAKE_KEYEVENT(isLeftHand ? 4 : 10, 6, active));
     47             break;
     48         }
     49     }
     50     return false;
     51 }
     52 
     53 static void process_encoder_matrix(encodermap_t pos) {
     54     action_exec(MAKE_KEYEVENT(pos.r, pos.c, true));
     55 #if TAP_CODE_DELAY > 0
     56     wait_ms(TAP_CODE_DELAY);
     57 #endif
     58     action_exec(MAKE_KEYEVENT(pos.r, pos.c, false));
     59 }
     60 
     61 bool touch_encoder_update_kb(uint8_t index, bool clockwise) {
     62     if (!touch_encoder_update_user(index, clockwise))
     63         return false;
     64 
     65     // Mapping clockwise (typically increase) to zero, and counter clockwise (decrease) to 1
     66     process_encoder_matrix(touch_encoder_map[index][clockwise ? 0 : 1]);
     67     return false;
     68 }
     69 
     70 bool touch_encoder_tapped_kb(uint8_t index, uint8_t section) {
     71     if (!touch_encoder_tapped_user(index, section))
     72         return false;
     73 
     74     process_encoder_matrix(touch_encoder_map[index][section + 2]);
     75     return false;
     76 }
     77 
     78 void matrix_slave_scan_kb(void) {
     79     dip_switch_read(false);
     80     matrix_slave_scan_user();
     81 }
     82 
     83 #ifdef RGB_MATRIX_ENABLE
     84 void rgb_matrix_increase_flags(void)
     85 {
     86     switch (rgb_matrix_get_flags()) {
     87         case LED_FLAG_ALL: {
     88             rgb_matrix_set_flags(LED_FLAG_KEYLIGHT | LED_FLAG_MODIFIER);
     89             rgb_matrix_set_color_all(0, 0, 0);
     90             }
     91             break;
     92         case LED_FLAG_KEYLIGHT | LED_FLAG_MODIFIER: {
     93             rgb_matrix_set_flags(LED_FLAG_UNDERGLOW);
     94             rgb_matrix_set_color_all(0, 0, 0);
     95             }
     96             break;
     97         case LED_FLAG_UNDERGLOW: {
     98             rgb_matrix_set_flags(LED_FLAG_NONE);
     99             rgb_matrix_disable_noeeprom();
    100             }
    101             break;
    102         default: {
    103             rgb_matrix_set_flags(LED_FLAG_ALL);
    104             rgb_matrix_enable_noeeprom();
    105             }
    106             break;
    107     }
    108 }
    109 #endif
    110 
    111 
    112 __attribute__((weak))
    113 void render_layer_status(void) {
    114     // Keymap specific, expected to be overridden
    115     // Host Keyboard Layer Status
    116     oled_write_P(PSTR("Layer"), false);
    117     oled_write_ln_P(PSTR("Undef"), false);
    118 }
    119 
    120 __attribute__((weak))
    121 void render_leds_status(void)
    122 {
    123     // Host Keyboard LED Status
    124     static const char PROGMEM led_icon[] = {
    125         0x0F,0x3A,0
    126     };
    127     oled_write_P(led_icon, false);
    128     led_t led_state = host_keyboard_led_state();
    129     oled_write_P(   led_state.num_lock ? PSTR("N") : PSTR(" "), false);
    130     oled_write_P(  led_state.caps_lock ? PSTR("C") : PSTR(" "), false);
    131     oled_write_P(led_state.scroll_lock ? PSTR("S") : PSTR(" "), false);
    132 }
    133 
    134 __attribute__((weak))
    135 void render_touch_status(void)
    136 {
    137     // Host Touch LED Status
    138     static const char PROGMEM touch_icon[] = {
    139         0x12,0x3A,0
    140     };
    141     oled_write_P(touch_icon, false);
    142     oled_write_P(         touch_encoder_is_on() ? PSTR("T") : PSTR(" "), false);
    143     oled_write_P(touch_encoder_is_calibrating() ? PSTR("C") : PSTR(" "), false);
    144     oled_write_P(PSTR(" "), false);
    145 }
    146 
    147 __attribute__((weak))
    148 void render_audio_status(void)
    149 {
    150     // Host Audio Status
    151     static const char PROGMEM audio_icon[] = {
    152         0x0E,0x3A,0
    153     };
    154     oled_write_P(audio_icon, false);
    155     oled_write_P( audio_is_on() ? PSTR("A")  : PSTR(" "), false);
    156     oled_write_P(is_clicky_on() ? PSTR("C")  : PSTR(" "), false);
    157     oled_write_P( is_music_on() ? PSTR("M")  : PSTR(" "), false);
    158 }
    159 
    160 oled_rotation_t oled_init_kb(oled_rotation_t rotation) {
    161     // Sol 3 uses OLED_ROTATION_270 for default rotation on both halves
    162     return oled_init_user(OLED_ROTATION_270);
    163 }
    164 
    165 bool oled_task_kb(void) {
    166     if (!oled_task_user())
    167         return false;
    168 
    169     if (is_keyboard_left()) {
    170         render_icon();
    171         oled_write_P(PSTR("     "), false);
    172         render_layer_status();
    173         oled_write_P(PSTR("     "), false);
    174         render_leds_status();
    175         oled_write_P(PSTR("     "), false);
    176         render_touch_status();
    177         oled_write_P(PSTR("     "), false);
    178         render_audio_status();
    179     }
    180     else {
    181         render_icon();
    182         oled_write_P(PSTR("     "), false);
    183         render_rgb_menu();
    184     }
    185     return false;
    186 }
    187 
    188 bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
    189     if (!process_record_user(keycode, record))
    190         return false;
    191 
    192     switch(keycode) {
    193 #ifdef RGB_MATRIX_ENABLE
    194         case QK_RGB_MATRIX_TOGGLE:
    195             if (record->event.pressed) {
    196                 rgb_matrix_increase_flags();
    197             }
    198             return false;
    199 #endif
    200     }
    201     return true;
    202 };
    203 
    204 void keyboard_post_init_kb(void) {
    205     touch_encoder_init();
    206     transaction_register_rpc(TOUCH_ENCODER_SYNC, touch_encoder_slave_sync);
    207     transaction_register_rpc(RGB_MENU_SYNC, rgb_menu_slave_sync);
    208     keyboard_post_init_user();
    209 }
    210 
    211 void housekeeping_task_kb(void) {
    212     touch_encoder_update(TOUCH_ENCODER_SYNC);
    213     rgb_menu_update(RGB_MENU_SYNC);
    214 }