]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/ergodox_infinity/ergodox_infinity.c
several improvements for mitosis:datagrok (#1960)
[qmk_firmware.git] / keyboards / ergodox_infinity / ergodox_infinity.c
1 #include QMK_KEYBOARD_H
2 #include "ch.h"
3 #include "hal.h"
4 #include "serial_link/system/serial_link.h"
5 #ifdef VISUALIZER_ENABLE
6 #include "lcd_backlight.h"
7 #endif
8
9 void init_serial_link_hal(void) {
10     PORTA->PCR[1] = PORTx_PCRn_PE | PORTx_PCRn_PS | PORTx_PCRn_PFE | PORTx_PCRn_MUX(2);
11     PORTA->PCR[2] = PORTx_PCRn_DSE | PORTx_PCRn_SRE | PORTx_PCRn_MUX(2);
12     PORTE->PCR[0] = PORTx_PCRn_PE | PORTx_PCRn_PS | PORTx_PCRn_PFE | PORTx_PCRn_MUX(3);
13     PORTE->PCR[1] = PORTx_PCRn_DSE | PORTx_PCRn_SRE | PORTx_PCRn_MUX(3);
14 }
15
16 #define RED_PIN 1
17 #define GREEN_PIN 2
18 #define BLUE_PIN 3
19 #define CHANNEL_RED FTM0->CHANNEL[0]
20 #define CHANNEL_GREEN FTM0->CHANNEL[1]
21 #define CHANNEL_BLUE FTM0->CHANNEL[2]
22
23 #define RGB_PORT PORTC
24 #define RGB_PORT_GPIO GPIOC
25
26 // Base FTM clock selection (72 MHz system clock)
27 // @ 0xFFFF period, 72 MHz / (0xFFFF * 2) = Actual period
28 // Higher pre-scalar will use the most power (also look the best)
29 // Pre-scalar calculations
30 // 0 -      72 MHz -> 549 Hz
31 // 1 -      36 MHz -> 275 Hz
32 // 2 -      18 MHz -> 137 Hz
33 // 3 -       9 MHz ->  69 Hz (Slightly visible flicker)
34 // 4 -   4 500 kHz ->  34 Hz (Visible flickering)
35 // 5 -   2 250 kHz ->  17 Hz
36 // 6 -   1 125 kHz ->   9 Hz
37 // 7 - 562 500  Hz ->   4 Hz
38 // Using a higher pre-scalar without flicker is possible but FTM0_MOD will need to be reduced
39 // Which will reduce the brightness range
40 #define PRESCALAR_DEFINE 0
41 void lcd_backlight_hal_init(void) {
42         // Setup Backlight
43     SIM->SCGC6 |= SIM_SCGC6_FTM0;
44     FTM0->CNT = 0; // Reset counter
45
46         // PWM Period
47         // 16-bit maximum
48         FTM0->MOD = 0xFFFF;
49
50         // Set FTM to PWM output - Edge Aligned, Low-true pulses
51 #define CNSC_MODE FTM_SC_CPWMS | FTM_SC_PS(4) | FTM_SC_CLKS(0)
52         CHANNEL_RED.CnSC = CNSC_MODE;
53         CHANNEL_GREEN.CnSC = CNSC_MODE;
54         CHANNEL_BLUE.CnSC = CNSC_MODE;
55
56         // System clock, /w prescalar setting
57         FTM0->SC = FTM_SC_CLKS(1) | FTM_SC_PS(PRESCALAR_DEFINE);
58
59         CHANNEL_RED.CnV = 0;
60         CHANNEL_GREEN.CnV = 0;
61         CHANNEL_BLUE.CnV = 0;
62
63         RGB_PORT_GPIO->PDDR |= (1 << RED_PIN);
64         RGB_PORT_GPIO->PDDR |= (1 << GREEN_PIN);
65         RGB_PORT_GPIO->PDDR |= (1 << BLUE_PIN);
66
67 #define RGB_MODE PORTx_PCRn_SRE | PORTx_PCRn_DSE | PORTx_PCRn_MUX(4)
68     RGB_PORT->PCR[RED_PIN] = RGB_MODE;
69     RGB_PORT->PCR[GREEN_PIN] = RGB_MODE;
70     RGB_PORT->PCR[BLUE_PIN] = RGB_MODE;
71 }
72
73 static uint16_t cie_lightness(uint16_t v) {
74     // The CIE 1931 formula for lightness
75     // Y = luminance (output) 0-1
76     // L = lightness input 0 - 100
77
78     // Y = (L* / 902.3)           if L* <= 8
79     // Y = ((L* + 16) / 116)^3    if L* > 8
80
81     float l =  100.0f * (v / 65535.0f);
82     float y = 0.0f;
83     if (l <= 8.0f) {
84        y = l / 902.3;
85     }
86     else {
87         y = ((l + 16.0f) / 116.0f);
88         y = y * y * y;
89         if (y > 1.0f) {
90             y = 1.0f;
91         }
92     }
93     return y * 65535.0f;
94 }
95
96 void lcd_backlight_hal_color(uint16_t r, uint16_t g, uint16_t b) {
97         CHANNEL_RED.CnV = cie_lightness(r);
98         CHANNEL_GREEN.CnV = cie_lightness(g);
99         CHANNEL_BLUE.CnV = cie_lightness(b);
100 }
101
102 __attribute__ ((weak))
103 void matrix_init_user(void) {
104 }
105
106 __attribute__ ((weak))
107 void matrix_scan_user(void) {
108 }
109
110
111 void matrix_init_kb(void) {
112         // put your keyboard start-up code here
113         // runs once when the firmware starts up
114
115         matrix_init_user();
116         // The backlight always has to be initialized, otherwise it will stay lit
117 #ifndef VISUALIZER_ENABLE
118         lcd_backlight_hal_init();
119 #endif
120 }
121
122 void matrix_scan_kb(void) {
123         // put your looping keyboard code here
124         // runs every cycle (a lot)
125
126         matrix_scan_user();
127 }
128
129 bool is_keyboard_master(void) {
130     return is_serial_link_master();
131 }
132
133 __attribute__ ((weak))
134 void ergodox_board_led_on(void){
135 }
136
137 __attribute__ ((weak))
138 void ergodox_right_led_1_on(void){
139 }
140
141 __attribute__ ((weak))
142 void ergodox_right_led_2_on(void){
143 }
144
145 __attribute__ ((weak))
146 void ergodox_right_led_3_on(void){
147 }
148
149 __attribute__ ((weak))
150 void ergodox_board_led_off(void){
151 }
152
153 __attribute__ ((weak))
154 void ergodox_right_led_1_off(void){
155 }
156
157 __attribute__ ((weak))
158 void ergodox_right_led_2_off(void){
159 }
160
161 __attribute__ ((weak))
162 void ergodox_right_led_3_off(void){
163 }
164
165 __attribute__ ((weak))
166 void ergodox_right_led_1_set(uint8_t n) {
167 }
168
169 __attribute__ ((weak))
170 void ergodox_right_led_2_set(uint8_t n) {
171 }
172
173 __attribute__ ((weak))
174 void ergodox_right_led_3_set(uint8_t n) {
175 }
176
177 #ifdef ONEHAND_ENABLE
178 __attribute__ ((weak))
179 const keypos_t hand_swap_config[MATRIX_ROWS][MATRIX_COLS] = {
180     {{0, 9}, {1, 9}, {2, 9}, {3, 9}, {4, 9}},
181     {{0, 10}, {1, 10}, {2, 10}, {3, 10}, {4, 10}},
182     {{0, 11}, {1, 11}, {2, 11}, {3, 11}, {4, 11}},
183     {{0, 12}, {1, 12}, {2, 12}, {3, 12}, {4, 12}},
184     {{0, 13}, {1, 13}, {2, 13}, {3, 13}, {4, 13}},
185     {{0, 14}, {1, 14}, {2, 14}, {3, 14}, {4, 14}},
186     {{0, 15}, {1, 15}, {2, 15}, {3, 15}, {4, 15}},
187     {{0, 16}, {1, 16}, {2, 16}, {3, 16}, {4, 16}},
188     {{0, 17}, {1, 17}, {2, 17}, {3, 17}, {4, 17}},
189     {{0, 0}, {1, 0}, {2, 0}, {3, 0}, {4, 0}},
190     {{0, 1}, {1, 1}, {2, 1}, {3, 1}, {4, 1}},
191     {{0, 2}, {1, 2}, {2, 2}, {3, 2}, {4, 2}},
192     {{0, 3}, {1, 3}, {2, 3}, {3, 3}, {4, 3}},
193     {{0, 4}, {1, 4}, {2, 4}, {3, 4}, {4, 4}},
194     {{0, 5}, {1, 5}, {2, 5}, {3, 5}, {4, 5}},
195     {{0, 6}, {1, 6}, {2, 6}, {3, 6}, {4, 6}},
196     {{0, 7}, {1, 7}, {2, 7}, {3, 7}, {4, 7}},
197     {{0, 8}, {1, 8}, {2, 8}, {3, 8}, {4, 8}},
198 };
199 #endif