qmk_firmware

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

wt_main.c (5066B)


      1 /* Copyright 2018 Jason Williams (Wilba)
      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 
     17 #include "quantum.h"
     18 
     19 // Check that no backlight functions are called
     20 #if RGB_BACKLIGHT_ENABLED
     21 #include "keyboards/wilba_tech/wt_rgb_backlight.h"
     22 #endif // RGB_BACKLIGHT_ENABLED
     23 #if MONO_BACKLIGHT_ENABLED
     24 #include "keyboards/wilba_tech/wt_mono_backlight.h"
     25 #endif // MONO_BACKLIGHT_ENABLED
     26 
     27 #include "via.h"
     28 
     29 #ifndef VIA_ENABLE
     30 #include "eeprom.h"
     31 #include "version.h" // for QMK_BUILDDATE used in EEPROM magic
     32 #endif
     33 
     34 // Called from via_init() if VIA_ENABLE
     35 // Called from matrix_init_kb() if not VIA_ENABLE
     36 void wt_main_init(void)
     37 {
     38     // This checks both an EEPROM reset (from bootmagic lite, keycodes)
     39     // and also firmware build date (from via_eeprom_is_valid())
     40     if (eeconfig_is_enabled()) {
     41 #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
     42         backlight_config_load();
     43 #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
     44     } else	{
     45 #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
     46         // If the EEPROM has not been saved before, or is out of date,
     47         // save the default values to the EEPROM. Default values
     48         // come from construction of the backlight_config instance.
     49         backlight_config_save();
     50 #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
     51 
     52         // DO NOT set EEPROM valid here, let caller do this
     53     }
     54 
     55 #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
     56     // Initialize LED drivers for backlight.
     57     backlight_init_drivers();
     58 
     59     backlight_timer_init();
     60     backlight_timer_enable();
     61 #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
     62 }
     63 
     64 void matrix_init_kb(void)
     65 {
     66     // If VIA is disabled, we still need to load backlight settings.
     67     // Call via_init_kb() the same way as via_init_kb() does.
     68 #ifndef VIA_ENABLE
     69     wt_main_init();
     70 #endif // VIA_ENABLE
     71 
     72     matrix_init_user();
     73 }
     74 
     75 void matrix_scan_kb(void)
     76 {
     77 #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
     78     // This only updates the LED driver buffers if something has changed.
     79     backlight_update_pwm_buffers();
     80 #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
     81     matrix_scan_user();
     82 }
     83 
     84 bool process_record_kb(uint16_t keycode, keyrecord_t *record)
     85 {
     86 #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
     87     process_record_backlight(keycode, record);
     88 #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
     89 
     90     return process_record_user(keycode, record);
     91 }
     92 
     93 void suspend_power_down_kb(void)
     94 {
     95 #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
     96     backlight_set_suspend_state(true);
     97 #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
     98 }
     99 
    100 void suspend_wakeup_init_kb(void)
    101 {
    102 #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
    103     backlight_set_suspend_state(false);
    104 #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
    105 }
    106 
    107 // Moving this to the bottom of this source file is a workaround
    108 // for an intermittent compiler error for Atmel compiler.
    109 #ifdef VIA_ENABLE
    110 void via_init_kb(void) {
    111     wt_main_init();
    112 }
    113 
    114 void via_custom_value_command_kb(uint8_t *data, uint8_t length) {
    115     uint8_t *command_id        = &(data[0]);
    116     uint8_t *channel_id        = &(data[1]);
    117     uint8_t *value_id_and_data = &(data[2]);
    118 
    119 #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
    120     if ( *channel_id == id_custom_channel ) {
    121         switch ( *command_id )
    122         {
    123             case id_custom_set_value:
    124             {
    125                 backlight_config_set_value(value_id_and_data);
    126                 break;
    127             }
    128             case id_custom_get_value:
    129             {
    130                 backlight_config_get_value(value_id_and_data);
    131                 break;
    132             }
    133             case id_custom_save:
    134             {
    135                 backlight_config_save();
    136                 break;
    137             }
    138             default:
    139             {
    140                 // Unhandled message.
    141                 *command_id = id_unhandled;
    142                 break;
    143             }
    144         }
    145         return;
    146     }
    147 #else
    148         *command_id = id_unhandled;
    149         *channel_id = *channel_id; // force use of variable
    150         *value_id_and_data = *value_id_and_data; // force use of variable
    151 #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
    152 
    153     // DO NOT call raw_hid_send(data,length) here, let caller do this
    154 }
    155 
    156 void via_set_device_indication(uint8_t value)
    157 {
    158 #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
    159     backlight_device_indication(value);
    160 #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
    161 }
    162 
    163 #endif // VIA_ENABLE