qmk_firmware

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

keymap.c (15198B)


      1 /* Copyright 2020 Sergey Vlasov <sigprof@gmail.com>
      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 QMK_KEYBOARD_H
     18 
     19 enum tap_dances {
     20     TD_OLED,
     21 };
     22 
     23 enum oled_test_modes {
     24     // Modes between TEST_FIRST and TEST_LAST (inclusive) can be switched with a keypress.
     25     TEST_FIRST,
     26     TEST_LOGO = TEST_FIRST,
     27     TEST_CHARACTERS,
     28     TEST_SLOW_UPDATE,
     29     TEST_ALL_ON,
     30     TEST_FRAME,
     31     TEST_ALL_OFF,
     32     TEST_FILL_HORZ_0,
     33     TEST_FILL_HORZ_1,
     34     TEST_FILL_VERT_0,
     35     TEST_FILL_VERT_1,
     36     TEST_FILL_CHECKERBOARD_1,
     37     TEST_FILL_CHECKERBOARD_2,
     38     TEST_FILL_CHECKERBOARD_4,
     39     TEST_LAST = TEST_FILL_CHECKERBOARD_4,
     40 
     41     // Special modes which are not reachable normally.
     42     TEST_DRAW_ALWAYS_ON,
     43     TEST_DRAW_ALWAYS_OFF,
     44 };
     45 
     46 static enum oled_test_modes test_mode = TEST_FIRST;
     47 
     48 static oled_rotation_t rotation = OLED_ROTATION_0;
     49 
     50 static bool     scrolling;
     51 static uint8_t  scrolling_speed;
     52 static bool     need_update = true;
     53 static bool     draw_always;
     54 static bool     update_speed_test;
     55 static uint32_t update_speed_start_timer;
     56 static uint16_t update_speed_count;
     57 static bool     restart_test;
     58 
     59 static void stop_scrolling(void) {
     60     if (scrolling) {
     61         oled_scroll_off();
     62         scrolling = false;
     63     }
     64 }
     65 
     66 static void dance_oled_finished(tap_dance_state_t *state, void *user_data) {
     67     switch (state->count) {
     68         case 1:
     69             if (state->pressed) {
     70                 // single hold - step through rotations
     71                 switch (rotation) {
     72                     case OLED_ROTATION_0:
     73                         rotation = OLED_ROTATION_90;
     74                         break;
     75                     case OLED_ROTATION_90:
     76                         rotation = OLED_ROTATION_180;
     77                         break;
     78                     case OLED_ROTATION_180:
     79                         rotation = OLED_ROTATION_270;
     80                         break;
     81                     default:
     82                         rotation = OLED_ROTATION_0;
     83                         break;
     84                 }
     85                 stop_scrolling();
     86                 oled_init(rotation);
     87             } else {
     88                 // single tap - step through test modes
     89                 if (test_mode < TEST_LAST) {
     90                     ++test_mode;
     91                 } else {
     92                     test_mode = TEST_FIRST;
     93                 }
     94                 stop_scrolling();
     95                 oled_clear();
     96             }
     97             restart_test = true;
     98             need_update  = true;
     99             break;
    100 
    101         case 2:
    102             if (state->pressed) {
    103                 // tap + hold - change scrolling speed
    104                 scrolling_speed = (scrolling_speed + 1) % 8;
    105                 stop_scrolling();
    106                 oled_scroll_set_speed(scrolling_speed);
    107                 // Cannot reactivate scrolling here, because oled_scroll_off()
    108                 // marks the whole display as dirty, and oled_scroll_left()
    109                 // silently does nothing if either the display is dirty or
    110                 // scrolling is already active.
    111             } else {
    112                 // double tap - toggle scrolling
    113                 if (!scrolling) {
    114                     scrolling = true;
    115                     oled_scroll_left();
    116                 } else {
    117                     scrolling = false;
    118                     oled_scroll_off();
    119                 }
    120             }
    121             need_update = true;
    122             break;
    123 
    124         case 3:
    125             if (state->pressed) {
    126                 // double tap + hold - toggle `draw_always`
    127                 draw_always = !draw_always;
    128                 if (draw_always) {
    129                     test_mode = TEST_DRAW_ALWAYS_ON;
    130                 } else {
    131                     test_mode = TEST_DRAW_ALWAYS_OFF;
    132                 }
    133                 stop_scrolling();
    134                 oled_clear();
    135                 restart_test = true;
    136                 need_update  = true;
    137             } else {
    138                 // triple tap - toggle update speed test
    139                 update_speed_test = !update_speed_test;
    140                 if (update_speed_test) {
    141                     stop_scrolling();
    142                     update_speed_start_timer = timer_read32();
    143                     update_speed_count       = 0;
    144                 }
    145             }
    146             break;
    147         case 4:
    148             if (!state->pressed) {
    149                 // quadruple tap - step through brightness levels
    150                 oled_set_brightness(oled_get_brightness() + 0x10);
    151             }
    152             break;
    153         default:
    154             break;
    155     }
    156 }
    157 
    158 tap_dance_action_t tap_dance_actions[] = {[TD_OLED] = ACTION_TAP_DANCE_FN(dance_oled_finished)};
    159 
    160 const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {LAYOUT_ortho_1x1(TD(TD_OLED))};
    161 
    162 // `bool oled_is_dirty(void)` does not exist at the moment
    163 extern OLED_BLOCK_TYPE oled_dirty;
    164 
    165 static inline uint8_t pixel_width(void) {
    166     if (!(rotation & OLED_ROTATION_90)) {
    167         return OLED_DISPLAY_WIDTH;
    168     }
    169     return OLED_DISPLAY_HEIGHT;
    170 }
    171 
    172 static inline uint8_t pixel_height(void) {
    173     if (!(rotation & OLED_ROTATION_90)) {
    174         return OLED_DISPLAY_HEIGHT;
    175     }
    176     return OLED_DISPLAY_WIDTH;
    177 }
    178 
    179 // Draw the QMK logo at the top left corner, clipping if it does not fit.
    180 static void test_logo(void) {
    181     uint8_t lines = oled_max_lines();
    182     if (lines > 3) {
    183         lines = 3;
    184     }
    185     uint8_t chars = oled_max_chars();
    186     if (chars > 21) {
    187         chars = 21;
    188     }
    189     for (uint8_t row = 0; row < lines; ++row) {
    190         oled_set_cursor(0, row);
    191         for (uint8_t col = 0; col < chars; ++col) {
    192             oled_write_char(0x80 + 0x20 * row + col, false);
    193         }
    194     }
    195 }
    196 
    197 static const PROGMEM char fill_ff[OLED_MATRIX_SIZE] = {[0 ... OLED_MATRIX_SIZE - 1] = 0xff};
    198 
    199 // Fill the whole screen with a pattern made from two bytes alternating after the specified number of repeats.
    200 static void test_fill(uint8_t byte0, uint8_t byte1, uint8_t repeats) {
    201     uint8_t  width = pixel_width();
    202     uint8_t  lines = oled_max_lines();
    203     uint16_t index = 0;
    204     for (uint8_t row = 0; row < lines; ++row) {
    205         for (uint8_t col = 0; col < width; ++col) {
    206             uint8_t byte = ((col / repeats) % 2) ? byte1 : byte0;
    207             oled_write_raw_byte(byte, index++);
    208         }
    209     }
    210 }
    211 
    212 // Draw a frame at the edges of the OLED screen.
    213 static void test_frame(void) {
    214     uint8_t width  = pixel_width();
    215     uint8_t height = pixel_height();
    216     for (uint8_t x = 0; x < width; ++x) {
    217         oled_write_pixel(x, 0, true);
    218         oled_write_pixel(x, height - 1, true);
    219     }
    220     for (uint8_t y = 1; y < height - 1; ++y) {
    221         oled_write_pixel(0, y, true);
    222         oled_write_pixel(width - 1, y, true);
    223     }
    224 }
    225 
    226 // Use all 94 visible ASCII characters for testing.
    227 #define TEST_CHAR_COUNT ('~' - '!' + 1)
    228 
    229 static char get_test_char(uint8_t char_index) { return char_index + '!'; }
    230 
    231 // Fill the whole screen with distinct characters (if the display is large enough to show more than 94 characters
    232 // at once, the sequence is repeated the second time with inverted characters).
    233 static void test_characters(void) {
    234     uint8_t cols       = oled_max_chars();
    235     uint8_t rows       = oled_max_lines();
    236     bool    invert     = false;
    237     uint8_t char_index = 0;
    238     for (uint8_t row = 0; row < rows; ++row) {
    239         for (uint8_t col = 0; col < cols; ++col) {
    240             oled_write_char(get_test_char(char_index), invert);
    241             if (++char_index >= TEST_CHAR_COUNT) {
    242                 char_index = 0;
    243                 invert     = !invert;
    244             }
    245         }
    246     }
    247 }
    248 
    249 // Test screen updating after drawing a single character or pixel.
    250 void test_slow_update(void) {
    251     static uint8_t  phase, x, y, char_index, first_char;
    252     static uint16_t timer;
    253     static uint16_t delay = 500;
    254 
    255     if (restart_test) {
    256         // Initialize all state variables before starting the test.
    257         restart_test = false;
    258         phase        = 0;
    259         x            = 0;
    260         y            = 0;
    261         char_index   = 0;
    262         first_char   = 0;
    263         delay        = 500;
    264     } else {
    265         // Wait for the specified time between steps.
    266         if (timer_elapsed(timer) < delay) {
    267             return;
    268         }
    269     }
    270 
    271     timer = timer_read();
    272     switch (phase) {
    273         case 0:
    274             // Phase 0: fill the whole screen with mostly distinct characters, one character at a time.  Here the
    275             // inversion trick is not used, so that the frame which is drawn in subsequent phases would not be
    276             // overlapped by the inverted character background.
    277             oled_set_cursor(x, y);
    278             oled_write_char(get_test_char(char_index), false);
    279             if (++char_index >= TEST_CHAR_COUNT) {
    280                 char_index = 0;
    281             }
    282             if (++x >= oled_max_chars()) {
    283                 x = 0;
    284                 if (++y >= oled_max_lines()) {
    285                     // The whole screen was filled - start the next phase.
    286                     ++phase;
    287                     x = y = 0;
    288                 }
    289             }
    290             delay = 250;
    291             break;
    292 
    293         case 1:
    294             // Phase 1: draw a line along the left edge of the screen, one pixel at a time.
    295             oled_write_pixel(x, y, true);
    296             if (y < pixel_height() - 1) {
    297                 ++y;
    298             } else {
    299                 // The bottom left corner is reached - start the next phase.
    300                 ++phase;
    301                 ++x;
    302             }
    303             delay = 50;
    304             break;
    305 
    306         case 2:
    307             // Phase 2: draw a line along the bottom edge of the screen, one pixel at a time.
    308             oled_write_pixel(x, y, true);
    309             if (x < pixel_width() - 1) {
    310                 ++x;
    311             } else {
    312                 // The bottom right corner was reached - start the next phase.
    313                 ++phase;
    314                 --y;
    315             }
    316             delay = 50;
    317             break;
    318 
    319         case 3:
    320             // Phase 3: draw a line along the right edge of the screen, one pixel at a time.
    321             oled_write_pixel(x, y, true);
    322             if (y > 0) {
    323                 --y;
    324             } else {
    325                 // The top right corner was reached - start the next phase.
    326                 ++phase;
    327                 --x;
    328             }
    329             delay = 50;
    330             break;
    331 
    332         case 4:
    333             // Phase 4: draw a line along the top edge of the screen, one pixel at a time.
    334             oled_write_pixel(x, y, true);
    335             if (x > 0) {
    336                 --x;
    337             } else {
    338                 // The top left corner was reached - start the next phase.
    339                 ++phase;
    340             }
    341             delay = 50;
    342             break;
    343 
    344         default:
    345             // Restart from phase 0, but change the first character of the sequence to make screen updates visible.
    346             if (++first_char >= TEST_CHAR_COUNT) {
    347                 first_char = 0;
    348             }
    349             phase      = 0;
    350             x          = 0;
    351             y          = 0;
    352             char_index = first_char;
    353             delay      = 500;
    354             break;
    355     }
    356 }
    357 
    358 oled_rotation_t oled_init_user(oled_rotation_t rotation) {
    359     oled_scroll_set_area(0, 0);
    360     oled_scroll_set_speed(scrolling_speed);
    361     return rotation;
    362 }
    363 
    364 bool oled_task_user(void) {
    365     if (update_speed_test) {
    366         // Speed test mode - wait for screen update completion.
    367         if (!oled_dirty) {
    368             // Update statistics and send the measurement result to the console.
    369             update_speed_count++;
    370             if (update_speed_count % 256 == 0) {
    371                 uprintf("OLED: %u updates, %lu ms\n", update_speed_count, timer_elapsed32(update_speed_start_timer));
    372             }
    373 
    374             // Toggle between the "all on" and "all off" states and trigger the screen update again.
    375             if (test_mode == TEST_ALL_ON) {
    376                 test_mode = TEST_ALL_OFF;
    377             } else {
    378                 test_mode = TEST_ALL_ON;
    379             }
    380             need_update = true;
    381         }
    382     }
    383 
    384     // The sample implementation of oled_task_user() in the documentation redraws the image after every call, relying on
    385     // the fact that drawing functions check whether the output actually changes anything in the image, and set dirty
    386     // bits only when something has actually changed.  However, redrawing the image only when some of the underlying
    387     // data has changed is more efficient.  Make it possible to test both modes here.
    388     if (!draw_always || update_speed_test) {
    389         // Draw the image only when the `need_update` flag is set, except for the "slow update" test.
    390         // This mode is also forced when the screen update speed test is performed.
    391         if (!need_update) {
    392             if (test_mode != TEST_SLOW_UPDATE) {
    393                 return false;
    394             }
    395         }
    396         need_update = false;
    397     }
    398 
    399     switch (test_mode) {
    400         case TEST_LOGO:
    401             test_logo();
    402             break;
    403         case TEST_CHARACTERS:
    404             test_characters();
    405             break;
    406         case TEST_SLOW_UPDATE:
    407             test_slow_update();
    408             break;
    409         case TEST_ALL_ON:
    410             oled_write_raw_P(fill_ff, sizeof(fill_ff));
    411             break;
    412         case TEST_FRAME:
    413             test_frame();
    414             break;
    415         case TEST_ALL_OFF:
    416             // `oled_clear()` is faster, but cannot be used with `draw_always`, because it does not check the previous
    417             // content of the buffer and always marks the whole buffer as dirty.
    418             if (update_speed_test) {
    419                 oled_clear();
    420             } else {
    421                 test_fill(0x00, 0x00, 1);
    422             }
    423             break;
    424         case TEST_FILL_HORZ_0:
    425             test_fill(0x55, 0x55, 1);
    426             break;
    427         case TEST_FILL_HORZ_1:
    428             test_fill(0xaa, 0xaa, 1);
    429             break;
    430         case TEST_FILL_VERT_0:
    431             test_fill(0xff, 0x00, 1);
    432             break;
    433         case TEST_FILL_VERT_1:
    434             test_fill(0x00, 0xff, 1);
    435             break;
    436         case TEST_FILL_CHECKERBOARD_1:
    437             test_fill(0x55, 0xaa, 1);
    438             break;
    439         case TEST_FILL_CHECKERBOARD_2:
    440             test_fill(0x33, 0xcc, 2);
    441             break;
    442         case TEST_FILL_CHECKERBOARD_4:
    443             test_fill(0x0f, 0xf0, 4);
    444             break;
    445 
    446         case TEST_DRAW_ALWAYS_ON:
    447             oled_write_P(PSTR("Draw Always"), false);
    448             break;
    449         case TEST_DRAW_ALWAYS_OFF:
    450             oled_write_P(PSTR("Draw Once"), false);
    451             break;
    452     }
    453     return false;
    454 }
    455 
    456 void keyboard_post_init_user(void) {
    457     // Console messages are used for update speed test results
    458     debug_enable = true;
    459 }