qmk_firmware

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

wave.c (5018B)


      1 /* Copyright 2021 HorrorTroll <https://github.com/HorrorTroll>
      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 extern const unsigned char font[] PROGMEM;
     18 
     19 #define ROW_1 OLED_DISPLAY_WIDTH
     20 #define ROW_2 (OLED_DISPLAY_WIDTH * 2)
     21 
     22 static uint32_t wave_sleep = 0;
     23 
     24 #define FRAME_TIMEOUT (1000/24)
     25 
     26 static uint16_t wave_timer = 0;
     27 
     28 static uint8_t next_bar_val = 0;
     29 static uint8_t next_log_byte[OLED_FONT_WIDTH] = {0};
     30 static uint8_t line1[OLED_DISPLAY_WIDTH] = {0};
     31 static uint8_t line2[OLED_DISPLAY_WIDTH] = {0};
     32 
     33 static const uint8_t PROGMEM bar_lut[8] = {0, 16, 24, 56, 60, 124, 126, 255};
     34 
     35 #define BAR_KEY_WEIGHT 128
     36 #define BAR_KEY_DECAY_MAX 18
     37 
     38 static uint8_t bar_key_decay = BAR_KEY_DECAY_MAX;
     39 
     40 // clang-format off
     41 static const char PROGMEM code_to_name[0xFF] = {
     42 //   0    1    2    3    4    5    6    7    8    9    A    B    C    D    E    F
     43     ' ', ' ', ' ', ' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',  // 0x
     44     'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2',  // 1x
     45     '3', '4', '5', '6', '7', '8', '9', '0', 128, 136, 132, 131,  22, '-', '=', '[',  // 2x
     46     ']','\\', '#', ';','\'', '`', ',', '.', '/', 130,   7,   7,   7,   7,   7,   7,  // 3x
     47       7,   7,   7,   7,   7,   7, 137, 138, 139, 140, 141,  30, 143, 142,  31,  26,  // 4x
     48      27,  25,  24, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',  // 5x
     49     ' ', ' ', ' ', ' ', ' ', 135, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',  // 6x
     50     ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',  // 7x
     51     ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',  // 8x
     52     ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',  // 9x
     53     ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',  // Ax
     54     ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',  // Bx
     55     ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',  // Cx
     56     ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',  // Dx
     57      15, 129, 133,   4,  15, 129, 133, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',  // Ex
     58     ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '        // Fx
     59 };
     60 // clang-format on
     61 
     62 void add_keylog(uint16_t keycode) {
     63     if ((keycode >= QK_MOD_TAP   && keycode <= QK_MOD_TAP_MAX)   ||
     64         (keycode >= QK_LAYER_TAP && keycode <= QK_LAYER_TAP_MAX) ||
     65         (keycode >= QK_MODS      && keycode <= QK_MODS_MAX)) {
     66         keycode = keycode & 0xFF;
     67     } else if (keycode > 0xFF) {
     68         keycode = 0;
     69     }
     70 
     71     if (keycode < ARRAY_SIZE(code_to_name)) {
     72         char log_char = pgm_read_byte(&code_to_name[keycode]);
     73 
     74         for (uint8_t j = 0; j < OLED_FONT_WIDTH; j++) {
     75             const uint8_t glyph_line = pgm_read_byte(&font[log_char * OLED_FONT_WIDTH + j]);
     76             next_log_byte[j] = glyph_line;
     77         }
     78     }
     79 }
     80 
     81 bool process_record_user_oled(uint16_t keycode, keyrecord_t *record) {
     82     if (record->event.pressed) {
     83         wave_sleep = timer_read32();
     84         add_keylog(keycode);
     85 
     86         uint8_t t = next_bar_val + BAR_KEY_WEIGHT;
     87 
     88         if (t < next_bar_val) {
     89             next_bar_val = 255;
     90         } else {
     91             next_bar_val = t;
     92         }
     93 
     94         bar_key_decay = BAR_KEY_DECAY_MAX;
     95     }
     96 
     97     return true;
     98 }
     99 
    100 void render_layer_name(void) {
    101     oled_write_P(PSTR("LAYER: WAVE"), false);
    102 }
    103 
    104 void render_frame(void) {
    105     // rotate line 1, and stick in the next byte of the next char,
    106     // then rotate the next char buffer
    107     memmove(line1+1, line1, OLED_DISPLAY_WIDTH - 1);
    108     line1[0] = next_log_byte[OLED_FONT_WIDTH - 1];
    109     memmove(next_log_byte+1, next_log_byte, OLED_FONT_WIDTH - 1);
    110     next_log_byte[0] = 0;
    111 
    112     // rotate line 2, sticking in the next display value
    113     uint8_t disp_val = pgm_read_byte(&bar_lut[next_bar_val / 32]);
    114     memmove(line2+1, line2, OLED_DISPLAY_WIDTH - 1);
    115     line2[0] = disp_val;
    116 
    117     // draw both bars
    118     for (uint8_t i = 0; i < OLED_DISPLAY_WIDTH; i++) {
    119         oled_write_raw_byte(line1[i], ROW_1 + i);
    120         oled_write_raw_byte(line2[i], ROW_2 + i);
    121     }
    122 
    123     // decay the next bar value
    124     if (next_bar_val > bar_key_decay) {
    125         next_bar_val -= bar_key_decay;
    126     } else {
    127         next_bar_val = 0;
    128     }
    129 
    130     if (bar_key_decay > 1) {
    131         bar_key_decay -= 1;
    132     }
    133 }