aboutsummaryrefslogtreecommitdiff
path: root/keyboards/system76/launch_1/launch_1.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/system76/launch_1/launch_1.c')
-rw-r--r--keyboards/system76/launch_1/launch_1.c240
1 files changed, 240 insertions, 0 deletions
diff --git a/keyboards/system76/launch_1/launch_1.c b/keyboards/system76/launch_1/launch_1.c
new file mode 100644
index 000000000..0250b9d9c
--- /dev/null
+++ b/keyboards/system76/launch_1/launch_1.c
@@ -0,0 +1,240 @@
1/*
2 * Copyright (C) 2021 System76
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 3 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 <https://www.gnu.org/licenses/>.
16 */
17
18#include "launch_1.h"
19
20#include "usb_mux.h"
21
22// clang-format off
23#ifdef RGB_MATRIX_ENABLE
24// LEDs by index
25// 0 1 2 3 4 5 6 7 8 9
26// 00 LM4 LL4 LK4 LJ4 LI4 LH4 LG4 LF4 LE4 LD4
27// 10 LC4 LB4 LA4 LA5 LB5 LC5 LD5 LE5 LG5 LH5
28// 20 LI5 LJ5 LK5 LL5 LM5 LO3 LM3 LL3 LK3 LJ3
29// 30 LI3 LH3 LG3 LF3 LE3 LD3 LC3 LB3 LA3 LA2
30// 40 LB2 LC2 LD2 LE2 LF2 LG2 LH2 LI2 LJ2 LK2
31// 50 LL2 LM2 LN2 LO2 LO1 LN1 LM1 LL1 LK1 LJ1
32// 60 LI1 LH1 LG1 LF1 LE1 LD1 LC1 LB1 LA1 LA0
33// 70 LB0 LC0 LD0 LE0 LF0 LG0 LH0 LI0 LJ0 LK0
34// 80 LL0 LM0 LN0 LO0
35led_config_t g_led_config = { LAYOUT(
36 // Key matrix to LED index
37 /* A B C D E F G H I J K L M N O */
38/* 0 */ 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
39/* 1 */ 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54,
40/* 2 */ 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
41/* 3 */ 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25,
42/* 4 */ 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
43/* 5 */ 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24
44), {
45 // LED index to physical position (see leds.sh in `launch' repo)
46/* 00 */ {209, 51}, {190, 51}, {171, 51}, {156, 51}, {140, 51}, {125, 51}, {110, 51}, {95, 51}, {80, 51}, {65, 51},
47/* 10 */ {49, 51}, {34, 51}, {11, 51}, {8, 64}, {27, 64}, {42, 64}, {57, 64}, {80, 64}, {110, 64}, {133, 64},
48/* 20 */ {148, 64}, {167, 64}, {194, 64}, {209, 64}, {224, 64}, {224, 38}, {197, 38}, {178, 38}, {163, 38}, {148, 38},
49/* 30 */ {133, 38}, {118, 38}, {103, 38}, {87, 38}, {72, 38}, {57, 38}, {42, 38}, {27, 38}, {8, 38}, {4, 26},
50/* 40 */ {23, 26}, {38, 26}, {53, 26}, {68, 26}, {84, 26}, {99, 26}, {114, 26}, {129, 26}, {144, 26}, {159, 26},
51/* 50 */ {175, 26}, {190, 26}, {205, 26}, {224, 26}, {224, 13}, {201, 13}, {182, 13}, {167, 13}, {152, 13}, {137, 13},
52/* 60 */ {121, 13}, {106, 13}, {91, 13}, {76, 13}, {61, 13}, {46, 13}, {30, 13}, {15, 13}, {0, 13}, {0, 0},
53/* 70 */ {15, 0}, {30, 0}, {46, 0}, {61, 0}, {76, 0}, {91, 0}, {106, 0}, {121, 0}, {137, 0}, {152, 0},
54/* 80 */ {167, 0}, {182, 0}, {201, 0}, {224, 0}
55}, {
56 // LED index to flags (set all to LED_FLAG_KEYLIGHT)
57 /* 0 1 2 3 4 5 6 7 8 9 */
58/* 00 */ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
59/* 10 */ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
60/* 20 */ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
61/* 30 */ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
62/* 40 */ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
63/* 50 */ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
64/* 60 */ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
65/* 70 */ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
66/* 80 */ 4, 4, 4, 4
67} };
68#endif // RGB_MATRIX_ENABLE
69
70bool eeprom_is_valid(void) {
71 return (
72 eeprom_read_word(((void *)EEPROM_MAGIC_ADDR)) == EEPROM_MAGIC &&
73 eeprom_read_byte(((void *)EEPROM_VERSION_ADDR)) == EEPROM_VERSION
74 );
75}
76// clang-format on
77
78void eeprom_set_valid(bool valid) {
79 eeprom_update_word(((void *)EEPROM_MAGIC_ADDR), valid ? EEPROM_MAGIC : 0xFFFF);
80 eeprom_update_byte(((void *)EEPROM_VERSION_ADDR), valid ? EEPROM_VERSION : 0xFF);
81}
82
83void bootmagic_lite_reset_eeprom(void) {
84 // Set the keyboard-specific EEPROM state as invalid
85 eeprom_set_valid(false);
86 // Set the TMK/QMK EEPROM state as invalid
87 eeconfig_disable();
88}
89
90// The lite version of TMK's bootmagic based on Wilba.
91// 100% less potential for accidentally making the keyboard do stupid things.
92void bootmagic_lite(void) {
93 // Perform multiple scans because debouncing can't be turned off.
94 matrix_scan();
95#if defined(DEBOUNCE) && DEBOUNCE > 0
96 wait_ms(DEBOUNCE * 2);
97#else
98 wait_ms(30);
99#endif
100 matrix_scan();
101
102 // If the configured key (commonly Esc) is held down on power up,
103 // reset the EEPROM valid state and jump to bootloader.
104 uint8_t row = 0; // BOOTMAGIC_LITE_ROW;
105 uint8_t col = 0; // BOOTMAGIC_LITE_COLUMN;
106
107 if (matrix_get_row(row) & (1 << col)) {
108 bootmagic_lite_reset_eeprom();
109
110 // Jump to bootloader.
111 bootloader_jump();
112 }
113}
114
115void system76_ec_rgb_eeprom(bool write);
116void system76_ec_rgb_layer(layer_state_t layer_state);
117void system76_ec_unlock(void);
118bool system76_ec_is_unlocked(void);
119
120rgb_config_t layer_rgb[DYNAMIC_KEYMAP_LAYER_COUNT];
121
122void matrix_init_kb(void) {
123 usb_mux_init();
124
125 bootmagic_lite();
126 if (!eeprom_is_valid()) {
127 dynamic_keymap_reset();
128 dynamic_keymap_macro_reset();
129 system76_ec_rgb_eeprom(true);
130 eeprom_set_valid(true);
131 } else {
132 system76_ec_rgb_eeprom(false);
133 }
134
135 system76_ec_rgb_layer(layer_state);
136}
137
138void matrix_scan_kb(void) {
139 usb_mux_event();
140
141 matrix_scan_user();
142}
143
144#define LEVEL(value) (uint8_t)(((uint16_t)value) * ((uint16_t)RGB_MATRIX_MAXIMUM_BRIGHTNESS) / ((uint16_t)255))
145
146// clang-format off
147static const uint8_t levels[] = {
148 LEVEL(48),
149 LEVEL(72),
150 LEVEL(96),
151 LEVEL(144),
152 LEVEL(192),
153 LEVEL(255)
154};
155// clang-format on
156
157static uint8_t toggle_level = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
158extern bool input_disabled;
159
160static void set_value_all_layers(uint8_t value) {
161 if (!system76_ec_is_unlocked()) {
162 for (int8_t layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++) {
163 layer_rgb[layer].hsv.v = value;
164 }
165 system76_ec_rgb_layer(layer_state);
166 }
167}
168
169bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
170 if (input_disabled) {
171 return false;
172 }
173
174 if (!process_record_user(keycode, record)) {
175 return false;
176 }
177
178 switch (keycode) {
179 case RESET:
180 if (record->event.pressed) {
181 system76_ec_unlock();
182 }
183#ifdef SYSTEM76_EC
184 return false;
185#else
186 return true;
187#endif
188 case RGB_VAD:
189 if (record->event.pressed) {
190 uint8_t level = rgb_matrix_config.hsv.v;
191 for (int i = sizeof(levels) - 1; i >= 0; i--) {
192 if (levels[i] < level) {
193 level = levels[i];
194 break;
195 }
196 }
197 set_value_all_layers(level);
198 }
199 return false;
200 case RGB_VAI:
201 if (record->event.pressed) {
202 uint8_t level = rgb_matrix_config.hsv.v;
203 for (int i = 0; i < sizeof(levels); i++) {
204 if (levels[i] > level) {
205 level = levels[i];
206 break;
207 }
208 }
209 set_value_all_layers(level);
210 }
211 return false;
212 case RGB_TOG:
213 if (record->event.pressed) {
214 uint8_t level = 0;
215 if (rgb_matrix_config.hsv.v == 0) {
216 level = toggle_level;
217 } else {
218 toggle_level = rgb_matrix_config.hsv.v;
219 }
220 set_value_all_layers(level);
221 }
222 return false;
223 }
224
225 return true;
226}
227
228layer_state_t layer_state_set_kb(layer_state_t layer_state) {
229 system76_ec_rgb_layer(layer_state);
230
231 return layer_state_set_user(layer_state);
232}
233
234#ifdef CONSOLE_ENABLE
235void keyboard_post_init_user(void) {
236 debug_enable = true;
237 debug_matrix = false;
238 debug_keyboard = false;
239}
240#endif // CONSOLE_ENABLE