summaryrefslogtreecommitdiffstats
path: root/keyboards/ergodox_infinity/ergodox_infinity.c
blob: da8ea311adb6a1f736c1f7a90197ae8cc326d966 (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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include QMK_KEYBOARD_H
#include <ch.h>
#include <hal.h>
#include <string.h>
#include "eeconfig.h"
#include "serial_link/system/serial_link.h"
#ifdef VISUALIZER_ENABLE
#    include "lcd_backlight.h"
#endif

#if (defined(LED_MATRIX_ENABLE) || defined(WPM_ENABLE))
#    include "serial_link/protocol/transport.h"

#    ifdef LED_MATRIX_ENABLE
MASTER_TO_ALL_SLAVES_OBJECT(led_matrix, led_eeconfig_t);
MASTER_TO_ALL_SLAVES_OBJECT(led_suspend_state, bool);
static led_eeconfig_t last_sent_led_matrix;
static uint16_t       led_matrix_sent_timer = 0;

void send_led_suspend_state(void) {
    if (is_serial_link_master()) {
        *begin_write_led_suspend_state() = led_matrix_get_suspend_state();
        end_write_led_suspend_state();
    }
}
#    endif

#    ifdef WPM_ENABLE
#        include "wpm.h"
MASTER_TO_ALL_SLAVES_OBJECT(current_wpm, uint8_t);
static uint8_t last_sent_wpm = 0;
#    endif

static remote_object_t *remote_objects[] = {
#    ifdef LED_MATRIX_ENABLE
    REMOTE_OBJECT(led_matrix),
    REMOTE_OBJECT(led_suspend_state),
#    endif
#    ifdef WPM_ENABLE
    REMOTE_OBJECT(current_wpm),
#    endif
};
#endif

void init_serial_link_hal(void) {
    PORTA->PCR[1] = PORTx_PCRn_PE | PORTx_PCRn_PS | PORTx_PCRn_PFE | PORTx_PCRn_MUX(2);
    PORTA->PCR[2] = PORTx_PCRn_DSE | PORTx_PCRn_SRE | PORTx_PCRn_MUX(2);
    PORTE->PCR[0] = PORTx_PCRn_PE | PORTx_PCRn_PS | PORTx_PCRn_PFE | PORTx_PCRn_MUX(3);
    PORTE->PCR[1] = PORTx_PCRn_DSE | PORTx_PCRn_SRE | PORTx_PCRn_MUX(3);
}

#define RED_PIN 1
#define GREEN_PIN 2
#define BLUE_PIN 3
#define CHANNEL_RED FTM0->CHANNEL[0]
#define CHANNEL_GREEN FTM0->CHANNEL[1]
#define CHANNEL_BLUE FTM0->CHANNEL[2]

#define RGB_PORT PORTC
#define RGB_PORT_GPIO GPIOC

// Base FTM clock selection (72 MHz system clock)
// @ 0xFFFF period, 72 MHz / (0xFFFF * 2) = Actual period
// Higher pre-scalar will use the most power (also look the best)
// Pre-scalar calculations
// 0 -      72 MHz -> 549 Hz
// 1 -      36 MHz -> 275 Hz
// 2 -      18 MHz -> 137 Hz
// 3 -       9 MHz ->  69 Hz (Slightly visible flicker)
// 4 -   4 500 kHz ->  34 Hz (Visible flickering)
// 5 -   2 250 kHz ->  17 Hz
// 6 -   1 125 kHz ->   9 Hz
// 7 - 562 500  Hz ->   4 Hz
// Using a higher pre-scalar without flicker is possible but FTM0_MOD will need to be reduced
// Which will reduce the brightness range
#define PRESCALAR_DEFINE 0
void lcd_backlight_hal_init(void) {
    // Setup Backlight
    SIM->SCGC6 |= SIM_SCGC6_FTM0;
    FTM0->CNT = 0;  // Reset counter

    // PWM Period
    // 16-bit maximum
    FTM0->MOD = 0xFFFF;

    // Set FTM to PWM output - Edge Aligned, Low-true pulses
#define CNSC_MODE FTM_SC_CPWMS | FTM_SC_PS(4) | FTM_SC_CLKS(0)
    CHANNEL_RED.CnSC   = CNSC_MODE;
    CHANNEL_GREEN.CnSC = CNSC_MODE;
    CHANNEL_BLUE.CnSC  = CNSC_MODE;

    // System clock, /w prescalar setting
    FTM0->SC = FTM_SC_CLKS(1) | FTM_SC_PS(PRESCALAR_DEFINE);

    CHANNEL_RED.CnV   = 0;
    CHANNEL_GREEN.CnV = 0;
    CHANNEL_BLUE.CnV  = 0;

    RGB_PORT_GPIO->PDDR |= (1 << RED_PIN);
    RGB_PORT_GPIO->PDDR |= (1 << GREEN_PIN);
    RGB_PORT_GPIO->PDDR |= (1 << BLUE_PIN);

#define RGB_MODE PORTx_PCRn_SRE | PORTx_PCRn_DSE | PORTx_PCRn_MUX(4)
    RGB_PORT->PCR[RED_PIN]   = RGB_MODE;
    RGB_PORT->PCR[GREEN_PIN] = RGB_MODE;
    RGB_PORT->PCR[BLUE_PIN]  = RGB_MODE;
}

static uint16_t cie_lightness(uint16_t v) {
    // The CIE 1931 formula for lightness
    // Y = luminance (output) 0-1
    // L = lightness input 0 - 100

    // Y = (L* / 902.3)           if L* <= 8
    // Y = ((L* + 16) / 116)^3    if L* > 8

    float l = 100.0f * (v / 65535.0f);
    float y = 0.0f;
    if (l <= 8.0f) {
        y = l / 902.3;
    } else {
        y = ((l + 16.0f) / 116.0f);
        y = y * y * y;
        if (y > 1.0f) {
            y = 1.0f;
        }
    }
    return y * 65535.0f;
}

void lcd_backlight_hal_color(uint16_t r, uint16_t g, uint16_t b) {
    CHANNEL_RED.CnV   = cie_lightness(r);
    CHANNEL_GREEN.CnV = cie_lightness(g);
    CHANNEL_BLUE.CnV  = cie_lightness(b);
}

__attribute__ ((weak)) void matrix_init_user(void) {}

__attribute__ ((weak)) void matrix_scan_user(void) {}


void keyboard_pre_init_kb() {
#ifdef LED_MATRIX_ENABLE
    // Turn on LED controller
    setPinOutput(B16);
    writePinHigh(B16);
#endif
    keyboard_pre_init_user();
}

void matrix_init_kb(void) {
    // put your keyboard start-up code here
    // runs once when the firmware starts up

#ifdef LED_MATRIX_ENABLE
    /*
     * Since K20x is stuck with a 32 byte EEPROM (see tmk_core/common/chibios/eeprom_teensy.c),
     * and neither led_matrix_eeconfig.speed or .flags fit in this boundary, just force their values to default on boot.
     */
#    if !defined(LED_MATRIX_STARTUP_SPD)
#        define LED_MATRIX_STARTUP_SPD UINT8_MAX / 2
#    endif
    led_matrix_set_speed(LED_MATRIX_STARTUP_SPD);
    led_matrix_set_flags(LED_FLAG_ALL);
#endif

    matrix_init_user();
    // The backlight always has to be initialized, otherwise it will stay lit
#ifndef VISUALIZER_ENABLE
    lcd_backlight_hal_init();
#endif
#if (defined(LED_MATRIX_ENABLE) || defined(WPM_ENABLE))
    add_remote_objects(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t *));
#endif
}

void matrix_scan_kb(void) {
    // put your looping keyboard code here
    // runs every cycle (a lot)

#ifdef LED_MATRIX_ENABLE
    if (is_serial_link_master()) {
        if (!led_matrix_get_suspend_state()) {
            if (timer_elapsed(led_matrix_sent_timer) >= 5000 || memcmp((void *)&last_sent_led_matrix, (void *)&led_matrix_eeconfig, sizeof(last_sent_led_matrix))) {
                led_matrix_sent_timer = timer_read();
                memcpy((void *)&last_sent_led_matrix, (void *)&led_matrix_eeconfig, sizeof(last_sent_led_matrix));
                *begin_write_led_matrix() = last_sent_led_matrix;
                end_write_led_matrix();
            }
        }
    } else if (is_serial_link_connected()) {
        bool *new_led_suspend_state = read_led_suspend_state();
        if (new_led_suspend_state) {
            led_matrix_set_suspend_state(*new_led_suspend_state);
        }
        if (!led_matrix_get_suspend_state()) {
            led_eeconfig_t *new_led_matrix = read_led_matrix();
            if (new_led_matrix) {
                memcpy((void *)&led_matrix_eeconfig, (void *)new_led_matrix, sizeof(last_sent_led_matrix));
            }
        }
    }
#endif

#ifdef WPM_ENABLE
    if (is_serial_link_master()) {
        uint8_t current_wpm = get_current_wpm();
        if (current_wpm != last_sent_wpm) {
            *begin_write_current_wpm() = current_wpm;
            end_write_current_wpm();
            last_sent_wpm = current_wpm;
        }
    } else if (is_serial_link_connected()) {
        uint8_t *new_wpm = read_current_wpm();
        if (new_wpm) {
            set_current_wpm(*new_wpm);
        }
    }
#endif

    matrix_scan_user();
}

bool is_keyboard_master(void) { return is_serial_link_master(); }

bool is_keyboard_left(void) {
#if defined(EE_HANDS)
    return eeconfig_read_handedness();
#elif defined(MASTER_IS_ON_RIGHT)
    return !is_keyboard_master();
#else
    return is_keyboard_master();
#endif
}

__attribute__ ((weak)) void ergodox_board_led_on(void) {}

__attribute__ ((weak)) void ergodox_right_led_1_on(void) {}

__attribute__ ((weak)) void ergodox_right_led_2_on(void) {}

__attribute__ ((weak)) void ergodox_right_led_3_on(void) {}

__attribute__ ((weak)) void ergodox_board_led_off(void) {}

__attribute__ ((weak)) void ergodox_right_led_1_off(void) {}

__attribute__ ((weak)) void ergodox_right_led_2_off(void) {}

__attribute__ ((weak)) void ergodox_right_led_3_off(void) {}

__attribute__ ((weak)) void ergodox_right_led_1_set(uint8_t n) {}

__attribute__ ((weak)) void ergodox_right_led_2_set(uint8_t n) {}

__attribute__ ((weak)) void ergodox_right_led_3_set(uint8_t n) {}

void suspend_power_down_kb(void) {
#ifdef LED_MATRIX_ENABLE
    send_led_suspend_state();
#endif
    suspend_power_down_user();
}

void suspend_wakeup_init_kb(void) {
#ifdef LED_MATRIX_ENABLE
    send_led_suspend_state();
#endif
    suspend_wakeup_init_user();
}

#ifdef SWAP_HANDS_ENABLE
__attribute__ ((weak))
const keypos_t PROGMEM hand_swap_config[MATRIX_ROWS][MATRIX_COLS] = {
    {{0,  9}, {1,  9}, {2,  9}, {3,  9}, {4,  9}},
    {{0, 10}, {1, 10}, {2, 10}, {3, 10}, {4, 10}},
    {{0, 11}, {1, 11}, {2, 11}, {3, 11}, {4, 11}},
    {{0, 12}, {1, 12}, {2, 12}, {3, 12}, {4, 12}},
    {{0, 13}, {1, 13}, {2, 13}, {3, 13}, {4, 13}},
    {{0, 14}, {1, 14}, {2, 14}, {3, 14}, {4, 14}},
    {{0, 15}, {1, 15}, {2, 15}, {3, 15}, {4, 15}},
    {{0, 16}, {1, 16}, {2, 16}, {3, 16}, {4, 16}},
    {{0, 17}, {1, 17}, {2, 17}, {3, 17}, {4, 17}},
    {{0, 0}, {1, 0}, {2, 0}, {3, 0}, {4, 0}},
    {{0, 1}, {1, 1}, {2, 1}, {3, 1}, {4, 1}},
    {{0, 2}, {1, 2}, {2, 2}, {3, 2}, {4, 2}},
    {{0, 3}, {1, 3}, {2, 3}, {3, 3}, {4, 3}},
    {{0, 4}, {1, 4}, {2, 4}, {3, 4}, {4, 4}},
    {{0, 5}, {1, 5}, {2, 5}, {3, 5}, {4, 5}},
    {{0, 6}, {1, 6}, {2, 6}, {3, 6}, {4, 6}},
    {{0, 7}, {1, 7}, {2, 7}, {3, 7}, {4, 7}},
    {{0, 8}, {1, 8}, {2, 8}, {3, 8}, {4, 8}},
};
#endif

#ifdef LED_MATRIX_ENABLE
const is31_led g_is31_leds[DRIVER_LED_TOTAL] = {
// The numbers in the comments are the led numbers DXX on the PCB
/* Refer to IS31 manual for these locations
 *  driver
 *  |   LED address
 *  |   | */
// Left half
//      45           44           43           42           41           40           39
   { 0, C2_2 }, { 0, C1_2 }, { 0, C5_1 }, { 0, C4_1 }, { 0, C3_1 }, { 0, C2_1 }, { 0, C1_1 },
//      52           51           50           49           48           47           46
   { 0, C4_3 }, { 0, C3_3 }, { 0, C2_3 }, { 0, C1_3 }, { 0, C5_2 }, { 0, C4_2 }, { 0, C3_2 },
//      58           57           56           55           54           53
   { 0, C5_4 }, { 0, C4_4 }, { 0, C3_4 }, { 0, C2_4 }, { 0, C1_4 }, { 0, C5_3 },
//      67           66           65           64           63           62           61
   { 0, C4_6 }, { 0, C3_6 }, { 0, C2_6 }, { 0, C1_6 }, { 0, C5_5 }, { 0, C4_5 }, { 0, C3_5 },
//      76           75           74           73           72
   { 0, C4_8 }, { 0, C3_8 }, { 0, C2_8 }, { 0, C1_8 }, { 0, C4_7 },
//                                                                                    60           59
                                                                                 { 0, C2_5 }, { 0, C1_5 },
//                                                                                                 68
                                                                                              { 0, C5_6 },
//                                                                       71           70           69
                                                                    { 0, C3_7 }, { 0, C2_7 }, { 0, C1_7 },
// Right half (mirrored)
// Due to how LED_MATRIX_SPLIT is implemented, only the first half of g_is31_leds is actually used.
// Luckily, the right half has the same LED pinouts, just mirrored.
//      45           44           43           42           41           40           39
   { 0, C2_2 }, { 0, C1_2 }, { 0, C5_1 }, { 0, C4_1 }, { 0, C3_1 }, { 0, C2_1 }, { 0, C1_1 },
//      52           51           50           49           48           47           46
   { 0, C4_3 }, { 0, C3_3 }, { 0, C2_3 }, { 0, C1_3 }, { 0, C5_2 }, { 0, C4_2 }, { 0, C3_2 },
//      58           57           56           55           54           53
   { 0, C5_4 }, { 0, C4_4 }, { 0, C3_4 }, { 0, C2_4 }, { 0, C1_4 }, { 0, C5_3 },
//      67           66           65           64           63           62           61
   { 0, C4_6 }, { 0, C3_6 }, { 0, C2_6 }, { 0, C1_6 }, { 0, C5_5 }, { 0, C4_5 }, { 0, C3_5 },
//      76           75           74           73           72
   { 0, C4_8 }, { 0, C3_8 }, { 0, C2_8 }, { 0, C1_8 }, { 0, C4_7 },
//                                                                                    60           59
                                                                                 { 0, C2_5 }, { 0, C1_5 },
//                                                                                                 68
                                                                                              { 0, C5_6 },
//                                                                       71           70           69
                                                                    { 0, C3_7 }, { 0, C2_7 }, { 0, C1_7 },
};

led_config_t g_led_config = {
    {
        // Key Matrix to LED Index
        // Left half
        { NO_LED, NO_LED, NO_LED,     33,     34 },
        { NO_LED, NO_LED, NO_LED,     32,     37 },
        {      6,     13, NO_LED,     26,     36 },
        {      5,     12,     19,     25,     35 },
        {      4,     11,     18,     24,     31 },
        {      3,     10,     17,     23,     30 },
        {      2,      9,     16,     22,     29 },
        {      1,      8,     15,     21,     28 },
        {      0,      7,     14,     20,     27 },
        // Right half
        { NO_LED, NO_LED, NO_LED,     71,     72 },
        { NO_LED, NO_LED, NO_LED,     70,     75 },
        {     44,     51, NO_LED,     64,     74 },
        {     43,     50,     57,     63,     73 },
        {     42,     49,     56,     62,     69 },
        {     41,     48,     55,     61,     68 },
        {     40,     47,     54,     60,     67 },
        {     39,     46,     53,     59,     66 },
        {     38,     45,     52,     58,     65 },
    }, {
        // LED Index to Physical Position (assumes a reasonable gap between halves)
	// Left half
        {   0,  3 }, {  15,  3 }, {  27,  1 }, {  39,  0 }, {  51,  1 }, {  63,  2 }, {  75,  2 },
        {   0, 13 }, {  15, 13 }, {  27, 11 }, {  39, 10 }, {  51, 11 }, {  63, 12 }, {  78, 17 },
        {   0, 23 }, {  15, 23 }, {  27, 21 }, {  39, 20 }, {  51, 21 }, {  63, 22 },
        {   0, 33 }, {  15, 33 }, {  27, 31 }, {  39, 30 }, {  51, 31 }, {  63, 32 }, {  78, 32 },
        {   4, 43 }, {  15, 43 }, {  27, 41 }, {  39, 40 }, {  51, 41 },
                                                                                      {  89, 41 }, { 100, 46 },
                                                                                                   {  95, 55 },
                                                                         {  72, 54 }, {  83, 59 }, {  90, 64 },
        // Right half (mirrored)
        { 224,  3 }, { 209,  3 }, { 197,  1 }, { 185,  0 }, { 173,  1 }, { 161,  2 }, { 149,  2 },
        { 224, 13 }, { 209, 13 }, { 197, 11 }, { 185, 10 }, { 173, 11 }, { 161, 12 }, { 146, 17 },
        { 224, 23 }, { 209, 23 }, { 197, 21 }, { 185, 20 }, { 173, 21 }, { 161, 22 },
        { 224, 33 }, { 209, 33 }, { 197, 31 }, { 185, 30 }, { 173, 31 }, { 161, 32 }, { 146, 32 },
        { 220, 43 }, { 209, 43 }, { 197, 41 }, { 185, 40 }, { 173, 41 },
                                                                                      { 135, 41 }, { 124, 46 },
                                                                                                   { 129, 55 },
                                                                         { 152, 54 }, { 141, 59 }, { 134, 64 },
    }, {
        // LED Index to Flag
        // Left half
        1, 4, 4, 4, 4, 4, 1,
        1, 4, 4, 4, 4, 4, 1,
        1, 4, 4, 4, 4, 4,
        1, 4, 4, 4, 4, 4, 1,
        1, 1, 1, 1, 1,
                          1, 1,
                             1,
                       1, 1, 1,
        // Right half (mirrored)
        1, 4, 4, 4, 4, 4, 1,
        1, 4, 4, 4, 4, 4, 1,
        1, 4, 4, 4, 4, 4,
        1, 4, 4, 4, 4, 4, 1,
        1, 1, 1, 1, 1,
                          1, 1,
                             1,
                       1, 1, 1,
    }
};
#endif