qmk_firmware

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

eeconfig.h (10442B)


      1 /*
      2 Copyright 2013 Jun Wako <wakojun@gmail.com>
      3 
      4 This program is free software: you can redistribute it and/or modify
      5 it under the terms of the GNU General Public License as published by
      6 the Free Software Foundation, either version 2 of the License, or
      7 (at your option) any later version.
      8 
      9 This program is distributed in the hope that it will be useful,
     10 but WITHOUT ANY WARRANTY; without even the implied warranty of
     11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12 GNU General Public License for more details.
     13 
     14 You should have received a copy of the GNU General Public License
     15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16 */
     17 
     18 #pragma once
     19 
     20 #include <stdint.h>
     21 #include <stdbool.h>
     22 #include <stddef.h>       // offsetof
     23 #include "action_layer.h" // layer_state_t
     24 
     25 // Size of EEPROM dedicated to keyboard- and user-specific data
     26 #ifndef EECONFIG_KB_DATA_SIZE
     27 #    define EECONFIG_KB_DATA_SIZE 0
     28 #endif
     29 #ifndef EECONFIG_KB_DATA_VERSION
     30 #    define EECONFIG_KB_DATA_VERSION (EECONFIG_KB_DATA_SIZE)
     31 #endif
     32 #ifndef EECONFIG_USER_DATA_SIZE
     33 #    define EECONFIG_USER_DATA_SIZE 0
     34 #endif
     35 #ifndef EECONFIG_USER_DATA_VERSION
     36 #    define EECONFIG_USER_DATA_VERSION (EECONFIG_USER_DATA_SIZE)
     37 #endif
     38 
     39 /* debug bit */
     40 #define EECONFIG_DEBUG_ENABLE (1 << 0)
     41 #define EECONFIG_DEBUG_MATRIX (1 << 1)
     42 #define EECONFIG_DEBUG_KEYBOARD (1 << 2)
     43 #define EECONFIG_DEBUG_MOUSE (1 << 3)
     44 
     45 /* keyconf bit */
     46 #define EECONFIG_KEYMAP_SWAP_CONTROL_CAPSLOCK (1 << 0)
     47 #define EECONFIG_KEYMAP_CAPSLOCK_TO_CONTROL (1 << 1)
     48 #define EECONFIG_KEYMAP_SWAP_LALT_LGUI (1 << 2)
     49 #define EECONFIG_KEYMAP_SWAP_RALT_RGUI (1 << 3)
     50 #define EECONFIG_KEYMAP_NO_GUI (1 << 4)
     51 #define EECONFIG_KEYMAP_SWAP_GRAVE_ESC (1 << 5)
     52 #define EECONFIG_KEYMAP_SWAP_BACKSLASH_BACKSPACE (1 << 6)
     53 #define EECONFIG_KEYMAP_NKRO (1 << 7)
     54 
     55 bool eeconfig_is_enabled(void);
     56 bool eeconfig_is_disabled(void);
     57 
     58 void eeconfig_init(void);
     59 void eeconfig_init_quantum(void);
     60 void eeconfig_init_kb(void);
     61 void eeconfig_init_user(void);
     62 
     63 void eeconfig_enable(void);
     64 void eeconfig_disable(void);
     65 
     66 typedef union debug_config_t debug_config_t;
     67 void                         eeconfig_read_debug(debug_config_t *debug_config) __attribute__((nonnull));
     68 void                         eeconfig_update_debug(const debug_config_t *debug_config) __attribute__((nonnull));
     69 
     70 layer_state_t eeconfig_read_default_layer(void);
     71 void          eeconfig_update_default_layer(layer_state_t state);
     72 
     73 typedef union keymap_config_t keymap_config_t;
     74 void                          eeconfig_read_keymap(keymap_config_t *keymap_config) __attribute__((nonnull));
     75 void                          eeconfig_update_keymap(const keymap_config_t *keymap_config) __attribute__((nonnull));
     76 
     77 #ifdef AUDIO_ENABLE
     78 typedef union audio_config_t audio_config_t;
     79 void                         eeconfig_read_audio(audio_config_t *audio_config) __attribute__((nonnull));
     80 void                         eeconfig_update_audio(const audio_config_t *audio_config) __attribute__((nonnull));
     81 #endif // AUDIO_ENABLE
     82 
     83 #ifdef UNICODE_COMMON_ENABLE
     84 typedef union unicode_config_t unicode_config_t;
     85 void                           eeconfig_read_unicode_mode(unicode_config_t *unicode_config) __attribute__((nonnull));
     86 void                           eeconfig_update_unicode_mode(const unicode_config_t *unicode_config) __attribute__((nonnull));
     87 #endif // UNICODE_COMMON_ENABLE
     88 
     89 #ifdef BACKLIGHT_ENABLE
     90 typedef union backlight_config_t backlight_config_t;
     91 void                             eeconfig_read_backlight(backlight_config_t *backlight_config) __attribute__((nonnull));
     92 void                             eeconfig_update_backlight(const backlight_config_t *backlight_config) __attribute__((nonnull));
     93 #endif // BACKLIGHT_ENABLE
     94 
     95 #ifdef STENO_ENABLE
     96 uint8_t eeconfig_read_steno_mode(void);
     97 void    eeconfig_update_steno_mode(uint8_t val);
     98 #endif // STENO_ENABLE
     99 
    100 #ifdef RGB_MATRIX_ENABLE
    101 typedef union rgb_config_t rgb_config_t;
    102 void                       eeconfig_read_rgb_matrix(rgb_config_t *rgb_matrix_config) __attribute__((nonnull));
    103 void                       eeconfig_update_rgb_matrix(const rgb_config_t *rgb_matrix_config) __attribute__((nonnull));
    104 #endif // RGB_MATRIX_ENABLE
    105 
    106 #ifdef LED_MATRIX_ENABLE
    107 typedef union led_eeconfig_t led_eeconfig_t;
    108 void                         eeconfig_read_led_matrix(led_eeconfig_t *led_matrix_config) __attribute__((nonnull));
    109 void                         eeconfig_update_led_matrix(const led_eeconfig_t *led_matrix_config) __attribute__((nonnull));
    110 #endif // LED_MATRIX_ENABLE
    111 
    112 #ifdef RGBLIGHT_ENABLE
    113 typedef union rgblight_config_t rgblight_config_t;
    114 void                            eeconfig_read_rgblight(rgblight_config_t *rgblight_config) __attribute__((nonnull));
    115 void                            eeconfig_update_rgblight(const rgblight_config_t *rgblight_config) __attribute__((nonnull));
    116 #endif // RGBLIGHT_ENABLE
    117 
    118 #if (EECONFIG_KB_DATA_SIZE) == 0
    119 uint32_t eeconfig_read_kb(void);
    120 void     eeconfig_update_kb(uint32_t val);
    121 #endif // (EECONFIG_KB_DATA_SIZE) == 0
    122 
    123 #if (EECONFIG_USER_DATA_SIZE) == 0
    124 uint32_t eeconfig_read_user(void);
    125 void     eeconfig_update_user(uint32_t val);
    126 #endif // (EECONFIG_USER_DATA_SIZE) == 0
    127 
    128 #ifdef HAPTIC_ENABLE
    129 typedef union haptic_config_t haptic_config_t;
    130 void                          eeconfig_read_haptic(haptic_config_t *haptic_config) __attribute__((nonnull));
    131 void                          eeconfig_update_haptic(const haptic_config_t *haptic_config) __attribute__((nonnull));
    132 #endif
    133 
    134 #ifdef CONNECTION_ENABLE
    135 typedef union connection_config_t connection_config_t;
    136 void                              eeconfig_read_connection(connection_config_t *config);
    137 void                              eeconfig_update_connection(const connection_config_t *config);
    138 #endif
    139 
    140 bool eeconfig_read_handedness(void);
    141 void eeconfig_update_handedness(bool val);
    142 
    143 #if (EECONFIG_KB_DATA_SIZE) > 0
    144 bool     eeconfig_is_kb_datablock_valid(void);
    145 uint32_t eeconfig_read_kb_datablock(void *data, uint32_t offset, uint32_t length) __attribute__((nonnull));
    146 uint32_t eeconfig_update_kb_datablock(const void *data, uint32_t offset, uint32_t length) __attribute__((nonnull));
    147 void     eeconfig_init_kb_datablock(void);
    148 #    define eeconfig_read_kb_datablock_field(__object, __field) eeconfig_read_kb_datablock(&(__object.__field), offsetof(typeof(__object), __field), sizeof(__object.__field))
    149 #    define eeconfig_update_kb_datablock_field(__object, __field) eeconfig_update_kb_datablock(&(__object.__field), offsetof(typeof(__object), __field), sizeof(__object.__field))
    150 #endif // (EECONFIG_KB_DATA_SIZE) > 0
    151 
    152 #if (EECONFIG_USER_DATA_SIZE) > 0
    153 bool     eeconfig_is_user_datablock_valid(void);
    154 uint32_t eeconfig_read_user_datablock(void *data, uint32_t offset, uint32_t length) __attribute__((nonnull));
    155 uint32_t eeconfig_update_user_datablock(const void *data, uint32_t offset, uint32_t length) __attribute__((nonnull));
    156 void     eeconfig_init_user_datablock(void);
    157 #    define eeconfig_read_user_datablock_field(__object, __field) eeconfig_read_user_datablock(&(__object.__field), offsetof(typeof(__object), __field), sizeof(__object.__field))
    158 #    define eeconfig_update_user_datablock_field(__object, __field) eeconfig_update_user_datablock(&(__object.__field), offsetof(typeof(__object), __field), sizeof(__object.__field))
    159 #endif // (EECONFIG_USER_DATA_SIZE) > 0
    160 
    161 // Any "checked" debounce variant used requires implementation of:
    162 //    -- bool eeconfig_check_valid_##name(void)
    163 //    -- void eeconfig_post_flush_##name(void)
    164 #define EECONFIG_DEBOUNCE_HELPER_CHECKED(name, config)                  \
    165     static uint8_t dirty_##name = false;                                \
    166                                                                         \
    167     bool eeconfig_check_valid_##name(void);                             \
    168     void eeconfig_post_flush_##name(void);                              \
    169                                                                         \
    170     static inline void eeconfig_init_##name(void) {                     \
    171         dirty_##name = true;                                            \
    172         if (eeconfig_check_valid_##name()) {                            \
    173             eeconfig_read_##name(&config);                              \
    174             dirty_##name = false;                                       \
    175         }                                                               \
    176     }                                                                   \
    177     static inline void eeconfig_flush_##name(bool force) {              \
    178         if (force || dirty_##name) {                                    \
    179             eeconfig_update_##name(&config);                            \
    180             eeconfig_post_flush_##name();                               \
    181             dirty_##name = false;                                       \
    182         }                                                               \
    183     }                                                                   \
    184     static inline void eeconfig_flush_##name##_task(uint16_t timeout) { \
    185         static uint16_t flush_timer = 0;                                \
    186         if (timer_elapsed(flush_timer) > timeout) {                     \
    187             eeconfig_flush_##name(false);                               \
    188             flush_timer = timer_read();                                 \
    189         }                                                               \
    190     }                                                                   \
    191     static inline void eeconfig_flag_##name(bool v) {                   \
    192         dirty_##name |= v;                                              \
    193     }                                                                   \
    194     static inline void eeconfig_write_##name(typeof(config) *conf) {    \
    195         if (memcmp(&config, conf, sizeof(config)) != 0) {               \
    196             memcpy(&config, conf, sizeof(config));                      \
    197             eeconfig_flag_##name(true);                                 \
    198         }                                                               \
    199     }
    200 
    201 #define EECONFIG_DEBOUNCE_HELPER(name, config)     \
    202     EECONFIG_DEBOUNCE_HELPER_CHECKED(name, config) \
    203                                                    \
    204     bool eeconfig_check_valid_##name(void) {       \
    205         return true;                               \
    206     }                                              \
    207     void eeconfig_post_flush_##name(void) {}