summaryrefslogtreecommitdiffstats
path: root/keyboards/lily58/keymaps/druotoni/boot.c
blob: ba46f8e3616aee65469576c1a782076624ee1bf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright 2021 Nicolas Druoton (druotoni)
// SPDX-License-Identifier: GPL-2.0-or-later

#include QMK_KEYBOARD_H

#include "boot.h"
#include "fast_random.h"
#include "draw_helper.h"
#include "gui_state.h"

// boot
#define ANIM_BOOT_FRAME_DURATION 8
uint16_t anim_boot_timer         = 0;
uint8_t  anim_boot_current_frame = 0;

#define NAVI_DURATION 55

// terminal stuff
#define TERMINAL_DURATION 25
#define TERMINAL_LINE_NUMBER 19
#define TERMINAL_LINE_MAX 14

#define LILY_DURATION 50

// halt
#define ANIM_HALT_FRAME_DURATION 55
uint16_t anim_halt_timer = 0;

void reset_boot(void) {
    // frame zero
    anim_boot_current_frame = 0;
}

static void draw_lily_key(uint8_t x, uint8_t y, uint8_t *key_number, unsigned long key_state, uint8_t color) {
    uint8_t       v    = *key_number;
    unsigned long mask = 1;
    mask               = mask << v;

    // ligth the key according to the mask
    if (((key_state & mask) == mask)) {
        color = !color;
    }

    draw_rectangle_fill(x, y, 3, 3, color);
    *key_number = v + 1;
}

static void draw_lily_key_row(uint8_t x, uint8_t y, int w, uint8_t *key_number, unsigned long key_state, uint8_t color) {
    // row of rectangle
    for (uint8_t i = 0; i < w; i++) {
        draw_lily_key(x + (i * 4), y, key_number, key_state, color);
    }
}

static void draw_lily_render(unsigned long key_state) {
    // different orientation base on side
#if IS_LEFT

    uint8_t x            = 0;
    uint8_t y            = 56;
    uint8_t x_ref        = 10 + x;
    uint8_t y_ref        = 2 + y;
    uint8_t i_key_number = 0;

    for (uint8_t i = 0; i < 4; i++) {
        draw_lily_key_row(x_ref, y_ref + (i * 4), 4, &i_key_number, key_state, true);
        draw_lily_key_row(x_ref - 8, y_ref + 2 + (i * 4), 2, &i_key_number, key_state, true);
    }

    draw_lily_key_row(x_ref + 2, y_ref + (4 * 4), 3, &i_key_number, key_state, true);

    uint8_t x_side = x_ref + (4 * 4);

    draw_lily_key(x_side, y_ref + (2 * 4) + 2, &i_key_number, key_state, true);
    draw_lily_key(x_side, y_ref + (4 * 4), &i_key_number, key_state, true);

    // screen
    draw_rectangle(x_side, y_ref, 4, 8, true);

    // frame
    drawline_hr(x + 1, y + 2, 8, true);
    oled_write_pixel(x + 8, y + 1, true);
    drawline_hr(x + 8, y, 23, true);

    drawline_hr(x + 1, y + 20, 10, true);
    oled_write_pixel(x + 10, y + 21, true);
    drawline_hr(x + 10, y + 22, 16, true);

    drawline_vb(x, y + 3, 17, true);
    drawline_vb(x + 31, y + 1, 20, true);
    oled_write_pixel(x + 30, y + 21, true);
    oled_write_pixel(x + 29, y + 22, true);
    oled_write_pixel(x + 28, y + 23, true);
    oled_write_pixel(x + 27, y + 24, true);
    oled_write_pixel(x + 26, y + 23, true);
#endif

#if IS_RIGHT
    uint8_t i_key_number = 0;

    for (uint8_t i = 0; i < 4; i++) {
        draw_lily_key_row(7, 58 + (i * 4), 4, &i_key_number, key_state, true);
        draw_lily_key_row(23, 60 + (i * 4), 2, &i_key_number, key_state, true);
    }

    draw_lily_key_row(9, 74, 3, &i_key_number, key_state, true);

    draw_lily_key(3, 68, &i_key_number, key_state, true);
    draw_lily_key(3, 74, &i_key_number, key_state, true);

    // screen
    draw_rectangle(2, 58, 4, 8, true);

    // frame
    drawline_hr(23, 58, 8, true);
    oled_write_pixel(23, 57, true);
    drawline_hr(1, 56, 23, true);

    drawline_hr(21, 76, 10, true);
    oled_write_pixel(21, 77, true);
    drawline_hr(6, 78, 16, true);

    drawline_vb(31, 59, 17, true);
    drawline_vb(0, 57, 20, true);
    oled_write_pixel(1, 77, true);
    oled_write_pixel(2, 78, true);
    oled_write_pixel(3, 79, true);
    oled_write_pixel(4, 80, true);
    oled_write_pixel(5, 79, true);
#endif
}

static void draw_lily(uint8_t f) {
    // frame for the events
    uint8_t tres_stroke = 10;
    uint8_t tres_boom   = 30;
    uint8_t y_start     = 56;

    if (f == 0 || f == tres_stroke || f == tres_boom) {
        // clean screen
        oled_clear();
    }

    // simple lily58 with all the keys
    if (f < tres_stroke) {
        draw_lily_render(0);
    }

    // increase number of random keys pressed
    if (f >= tres_stroke && f < tres_boom) {
        int inter_f = interpo_pourcent(tres_stroke, tres_boom, f);

        unsigned long key_state = fastrand_long();
        for (int r = 100 - inter_f; r > 0; r = r - 10) {
            key_state &= fastrand_long();
        }
        draw_lily_render(key_state);
    }

    // statir explosion
    if (f >= tres_boom) {
        oled_clear();
        uint8_t density = (f - tres_boom);
        if (density > 4) density = 4;
        draw_static(0, y_start - 8, 32, 32, true, density);
    }
}

static void draw_startup_navi(uint8_t f) {
    // text
    oled_write_cursor(0, 5, "HELL0", false);
    oled_write_cursor(0, 7, "NAVI.", false);

    // prompt
    if ((f % 8) > 4) {
        oled_write_cursor(0, 12, "> ", false);
    } else {
        oled_write_cursor(0, 12, ">_", false);
    }

    // frame threshold
    uint8_t tres_shell = 15;
    uint8_t tres_load  = 35;

    // rand text to init display
    if (f > tres_shell) {
        int inter_f = interpo_pourcent(tres_shell, tres_load, f);

        draw_random_char(1, 12, 'i', 60 + inter_f, 0);
        draw_random_char(2, 12, 'n', 20 + inter_f, 0);
        draw_random_char(3, 12, 'i', inter_f, 0);
        draw_random_char(4, 12, 't', 20 + inter_f, 0);
    }

    // loading propress bar
    if (f > tres_load) {
        int inter_f = interpo_pourcent(tres_load, 50, f);

        // ease
        float fv = inter_f / 100.00;
        fv       = fv * fv * fv * fv;
        inter_f  = fv * 100;

        draw_rectangle(0, (15 * 8), 32, 8, 1);
        draw_progress(0 + 3, (15 * 8) + 3, 26, 2, inter_f, 0, 1);
    }
}

// text dispayed on terminal
static char *boot_ref[TERMINAL_LINE_NUMBER] = {"LT:", "RT:", "M :", "    ", "cnx:", "A0:", "B0:", "    ", "0x40", "0x60", "0x85", "0x0F", "    ", "> run", "x ", "y ", " 100%", "    ", "> key"};

// prompt style for char in the font
char scan_font[5] = {'>', 1, 1, 1, 1};

static char *get_terminal_line(uint8_t i) {
    // display text
    if (i < TERMINAL_LINE_NUMBER) {
        return boot_ref[i];
    }

    // blank line every 3 lines
    if (i % 3 == 0) {
        return "     ";
    }

    // display consecutive chars in the font
    i = (i - TERMINAL_LINE_NUMBER) * 4;

    scan_font[1] = i;
    scan_font[2] = i + 1;
    scan_font[3] = i + 2;
    scan_font[4] = i + 3;

    return scan_font;
}

static void draw_startup_terminal(uint8_t f) {
    // ease for printing on screen
    f = f * 2;
    f += (f / 5);

    // scroll text
    uint8_t i_start   = 0;
    uint8_t i_nb_char = f;

    if (f > TERMINAL_LINE_MAX) {
        i_start   = f - TERMINAL_LINE_MAX;
        i_nb_char = TERMINAL_LINE_MAX;
    }

    // display lines
    oled_clear();
    for (uint8_t i = 0; i < i_nb_char; i++) {
        char *s = get_terminal_line(i + i_start);
        oled_write_cursor(0, i, s, false);
    }
}

bool render_boot(void) {
    // end of the boot sequence
    if (anim_boot_current_frame >= NAVI_DURATION + TERMINAL_DURATION + LILY_DURATION) {
        anim_boot_current_frame = 0;
        oled_clear();
        return true;
    }

    if (timer_elapsed(anim_boot_timer) > ANIM_BOOT_FRAME_DURATION) {
        anim_boot_timer = timer_read();
        if (anim_boot_current_frame < NAVI_DURATION) {
            // 55 frames
            draw_startup_navi(anim_boot_current_frame);
        } else {
            if (anim_boot_current_frame >= NAVI_DURATION && anim_boot_current_frame < NAVI_DURATION + TERMINAL_DURATION) {
                // 25
                draw_startup_terminal(anim_boot_current_frame - NAVI_DURATION);
            } else {
                if (anim_boot_current_frame >= NAVI_DURATION + TERMINAL_DURATION) {
                    // 25
                    draw_lily(anim_boot_current_frame - NAVI_DURATION - TERMINAL_DURATION);
                }
            }
        }

        anim_boot_current_frame++;
    }
    return false;
}

void render_halt(void) {
    if (timer_elapsed(anim_halt_timer) > ANIM_HALT_FRAME_DURATION) {
        anim_halt_timer = timer_read();

        // comb glitch for all the screen
        draw_glitch_comb(0, 0, 32, 128, 3, true);

        // random moving blocks of pixels
        for (uint8_t i = 0; i < 6; i++) {
            int     r  = fastrand();
            int     rr = fastrand();
            uint8_t x  = 4 + r % 28;
            uint8_t y  = rr % 128;

            uint8_t w = 7 + r % 20;
            uint8_t h = 3 + rr % 10;
            int     s = (fastrand() % 20) - 10;
            move_block(x, y, w, h, s);
        }
    }
}