qmk

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

satisfaction_encoder.c (4918B)


      1 #include "satisfaction75.h"
      2 
      3 void pre_encoder_mode_change(){
      4   if(encoder_mode == ENC_MODE_CLOCK_SET){
      5     RTCDateTime timespec;
      6     timespec.year = year_config;
      7     timespec.month = month_config;
      8     timespec.day = day_config;
      9     // timespec.dayofweek = last_timespec.dayofweek;
     10     // timespec.dstflag = last_timespec.dstflag;
     11     timespec.millisecond = (hour_config * 60 + minute_config) * 60 * 1000;
     12     rtcSetTime(&RTCD1, &timespec);
     13   } else if (encoder_mode == ENC_MODE_BACKLIGHT){
     14     save_backlight_config_to_eeprom();
     15   }
     16 }
     17 
     18 void post_encoder_mode_change(){
     19   if(encoder_mode == ENC_MODE_CLOCK_SET){
     20     hour_config = (last_minute / 60);
     21     minute_config = last_minute % 60;
     22     year_config = last_timespec.year;
     23     month_config = last_timespec.month;
     24     day_config = last_timespec.day;
     25     time_config_idx = 0;
     26   }
     27 }
     28 
     29 void change_encoder_mode(bool negative){
     30   pre_encoder_mode_change();
     31   if(enabled_encoder_modes == 0){
     32     enabled_encoder_modes = 0x1F;
     33   }
     34   do {
     35     if(negative){
     36       if (encoder_mode == 0){
     37         encoder_mode = _NUM_ENCODER_MODES - 1;
     38       } else{
     39         encoder_mode = encoder_mode - 1;
     40       }
     41     } else {
     42         encoder_mode = (encoder_mode + 1) % _NUM_ENCODER_MODES;
     43     }
     44   } while(((1 << encoder_mode) & enabled_encoder_modes) == 0);
     45   post_encoder_mode_change();
     46 }
     47 
     48 void update_time_config(int8_t increment){
     49   uint8_t day_limit = 31;
     50   uint16_t adjusted_year = 1980 + year_config;
     51   switch(time_config_idx){
     52     case 0: // hour
     53     default:
     54       hour_config = (hour_config + increment) % 24;
     55       if (hour_config < 0){
     56         hour_config += 24;
     57       }
     58       break;
     59     case 1: // minute
     60       minute_config = (minute_config + increment) % 60;
     61       if (minute_config < 0){
     62         minute_config += 60;
     63       }
     64       break;
     65     case 2: // year
     66       year_config += increment;
     67       break;
     68     case 3: // month
     69       month_config = (month_config % 12) + increment;
     70       if (month_config <= 0){
     71         month_config += 12;
     72       }
     73       break;
     74     case 4: //day
     75       if (month_config == 9 || month_config == 4 || month_config == 6 || month_config == 11){
     76         day_limit = 30;
     77       } else if(month_config == 2){
     78         day_limit = adjusted_year % 4 == 0 && !(adjusted_year % 100 == 0 && adjusted_year % 400 != 0) ? 29 : 28;
     79       }
     80       day_config = (day_config % day_limit) + increment;
     81       if(day_config <= 0){
     82         day_config += day_limit;
     83       }
     84       break;
     85   }
     86 }
     87 
     88 uint16_t handle_encoder_clockwise(){
     89   uint16_t mapped_code = 0;
     90   switch(encoder_mode){
     91     default:
     92     case ENC_MODE_VOLUME:
     93       mapped_code = KC_VOLU;
     94       break;
     95     case ENC_MODE_MEDIA:
     96       mapped_code = KC_MEDIA_NEXT_TRACK;
     97       break;
     98     case ENC_MODE_SCROLL:
     99       mapped_code = KC_WH_D;
    100       break;
    101     case ENC_MODE_BACKLIGHT:
    102       kb_backlight_config.level = kb_backlight_config.level + 1;
    103       if(kb_backlight_config.level > BACKLIGHT_LEVELS){
    104         kb_backlight_config.level = BACKLIGHT_LEVELS;
    105       }
    106       backlight_set(kb_backlight_config.level);
    107       if (kb_backlight_config.level != 0){
    108         kb_backlight_config.enable = true;
    109       }
    110       break;
    111     case ENC_MODE_BRIGHTNESS:
    112       mapped_code = KC_BRIGHTNESS_UP;
    113       break;
    114     case ENC_MODE_CLOCK_SET:
    115       update_time_config(1);
    116       queue_for_send = true;
    117       break;
    118   }
    119   return mapped_code;
    120 }
    121 
    122 uint16_t handle_encoder_ccw(){
    123   uint16_t mapped_code = 0;
    124   switch(encoder_mode){
    125     default:
    126     case ENC_MODE_VOLUME:
    127       mapped_code = KC_VOLD;
    128       break;
    129     case ENC_MODE_MEDIA:
    130       mapped_code = KC_MEDIA_PREV_TRACK;
    131       break;
    132     case ENC_MODE_SCROLL:
    133       mapped_code = KC_WH_U;
    134       break;
    135     case ENC_MODE_BACKLIGHT:
    136       // mapped_code = BL_DEC;
    137       if(kb_backlight_config.level != 0){
    138         kb_backlight_config.level = kb_backlight_config.level - 1;
    139       }
    140       backlight_set(kb_backlight_config.level);
    141       if (kb_backlight_config.level == 0){
    142         kb_backlight_config.enable = false;
    143       }
    144       break;
    145     case ENC_MODE_BRIGHTNESS:
    146       mapped_code = KC_BRIGHTNESS_DOWN;
    147       break;
    148     case ENC_MODE_CLOCK_SET:
    149       update_time_config(-1);
    150       queue_for_send = true;
    151       break;
    152   }
    153   return mapped_code;
    154 }
    155 
    156 uint16_t handle_encoder_press(){
    157   uint16_t mapped_code = 0;
    158   switch(encoder_mode){
    159     case ENC_MODE_VOLUME:
    160       mapped_code = KC_MUTE;
    161       break;
    162     case ENC_MODE_MEDIA:
    163       mapped_code = KC_MEDIA_PLAY_PAUSE;
    164       break;
    165     case ENC_MODE_SCROLL:
    166       mapped_code = KC_BTN3;
    167       break;
    168     case ENC_MODE_BACKLIGHT:
    169       // mapped_code = BL_TOGG;
    170       kb_backlight_config.breathing = !kb_backlight_config.breathing;
    171       if(!kb_backlight_config.breathing){
    172         breathing_disable();
    173       } else{
    174         breathing_enable();
    175       }
    176       break;
    177     case ENC_MODE_CLOCK_SET:
    178       time_config_idx = (time_config_idx + 1) % 5;
    179     default:
    180     case ENC_MODE_BRIGHTNESS:
    181       break;
    182   }
    183   return mapped_code;
    184 }