summaryrefslogtreecommitdiff
path: root/keyboards/jpe230/big_knob/big_knob.c
blob: db3dab5ed6cd2600160b7c29562c30aa38c907f2 (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
// Copyright 2023 jpe230 (@jpe230)
// SPDX-License-Identifier: GPL-2.0-or-later

#include "qp.h"
#include "qp_comms.h"
#include "qp_st77xx_opcodes.h"
#include "gfx/logo.qgf.h"

painter_device_t lcd;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Init board:
// - Draw logo

void keyboard_post_init_kb(void) {
    backlight_set(BACKLIGHT_DEFAULT_LEVEL);

    wait_ms(LCD_WAIT_TIME);

    // Initialise the LCD
    lcd = qp_st7735_make_spi_device(LCD_HEIGHT, LCD_WIDTH, LCD_CS_PIN, LCD_DC_PIN, LCD_RST_PIN, LCD_SPI_DIVISOR, 0);
    qp_init(lcd, LCD_ROTATION);

    // Invert Colour
    #ifdef LCD_INVERT_COLOUR
    qp_comms_start(lcd);
    qp_comms_command(lcd, ST77XX_CMD_INVERT_ON);
    qp_comms_stop(lcd);
    #endif

    // Apply Offset
    qp_set_viewport_offsets(lcd, LCD_OFFSET_X, LCD_OFFSET_Y);

    // Turn on the LCD and clear the display
    qp_power(lcd, true);
    qp_rect(lcd, 0, 0, LCD_WIDTH, LCD_HEIGHT, HSV_BLACK, true);

    // Show logo
    painter_image_handle_t logo_image = qp_load_image_mem(gfx_logo);
    qp_drawimage(lcd, 0, 0, logo_image);

    keyboard_post_init_user();
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Lights handling:
//  - Turn off backlight (screen) after timeout or suspend
//  - Turn off rgblight after timeout or suspend

bool lights_off = false;

__attribute__((weak)) void lights_wakeup_user(void) {};
__attribute__((weak)) void lights_suspend_user(void) {};

void backlight_wakeup(void) {
    backlight_set(BACKLIGHT_DEFAULT_LEVEL);
}

void backlight_suspend(void) {
    backlight_set(0);
}

void lights_wakeup(void) {
    lights_off = false;
    rgblight_wakeup();
    backlight_wakeup();
    lights_wakeup_user();
}

void lights_suspend(void) {
    lights_off = true;
    lights_suspend_user();
    rgblight_suspend();
    backlight_suspend();
}

void housekeeping_task_kb(void) {
    if ( lights_off && last_input_activity_elapsed() <= LIGHTS_TIMEOUT)
    {
        lights_wakeup();
    }
    if (!lights_off && last_input_activity_elapsed() > LIGHTS_TIMEOUT) {
        lights_suspend();
    }
}

void suspend_power_down_kb(void) {
    lights_suspend();
    qp_power(lcd, false);
    suspend_power_down_user();
}

void suspend_wakeup_init_kb(void) {
    qp_power(lcd, true);
    lights_wakeup();
    suspend_wakeup_init_user();
}