blob: 6c86906322e0af86b9cc2a2e9ea5fafdfba3a079 (
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
|
/* Copyright 2022 @ lokher (https://www.keychron.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "quantum.h"
#include "bluetooth.h"
#include "indicator.h"
#include "lpm.h"
#if defined(PROTOCOL_CHIBIOS)
# include <usb_main.h>
#endif
#include "transport.h"
#ifndef REINIT_LED_DRIVER
# define REINIT_LED_DRIVER 1
#endif
#if defined(PROTOCOL_CHIBIOS)
extern host_driver_t chibios_driver;
#endif
extern host_driver_t bluetooth_driver;
extern keymap_config_t keymap_config;
static transport_t transport = TRANSPORT_USB;
#ifdef NKRO_ENABLE
nkro_t nkro = {false, false};
#endif
static void transport_changed(transport_t new_transport);
__attribute__((weak)) void bt_transport_enable(bool enable) {
if (enable) {
if (host_get_driver() != &bluetooth_driver) {
host_set_driver(&bluetooth_driver);
/* Disconnect and reconnect to sync the bluetooth state
* TODO: query bluetooth state to sync
*/
bluetooth_disconnect();
bluetooth_connect();
// TODO: Clear USB report
}
} else {
indicator_stop();
if (bluetooth_get_state() == BLUETOOTH_CONNECTED) {
report_keyboard_t empty_report = {0};
bluetooth_driver.send_keyboard(&empty_report);
}
}
}
/* There is no dedicated pin for USB power on chip such as STM32L432, but USB power
* can be connected and disconnected via registers.
* Overwrite these two functions if such chip is used. */
__attribute__((weak)) void usb_power_connect(void) {}
__attribute__((weak)) void usb_power_disconnect(void) {}
__attribute__((weak)) void usb_transport_enable(bool enable) {
if (enable) {
if (host_get_driver() != &chibios_driver) {
#if !defined(KEEP_USB_CONNECTION_IN_BLUETOOTH_MODE)
usb_power_connect();
usb_start(&USBD1);
#endif
host_set_driver(&chibios_driver);
}
} else {
if (USB_DRIVER.state == USB_ACTIVE) {
report_keyboard_t empty_report = {0};
chibios_driver.send_keyboard(&empty_report);
}
#if !defined(KEEP_USB_CONNECTION_IN_BLUETOOTH_MODE)
usbStop(&USBD1);
usbDisconnectBus(&USBD1);
usb_power_disconnect();
#endif
}
}
void set_transport(transport_t new_transport) {
if (transport != new_transport) {
transport = new_transport;
clear_keyboard();
switch (transport) {
case TRANSPORT_USB:
usb_transport_enable(true);
bt_transport_enable(false);
lpm_timer_stop();
#ifdef NKRO_ENABLE
# if defined(BLUETOOTH_NKRO_ENABLE)
nkro.bluetooth = keymap_config.nkro;
# endif
keymap_config.nkro = nkro.usb;
#endif
break;
case TRANSPORT_BLUETOOTH:
bt_transport_enable(true);
usb_transport_enable(false);
lpm_timer_reset();
#if defined(NKRO_ENABLE)
nkro.usb = keymap_config.nkro;
# if defined(BLUETOOTH_NKRO_ENABLE)
keymap_config.nkro = nkro.bluetooth;
# else
keymap_config.nkro = FALSE;
# endif
#endif
break;
default:
break;
}
transport_changed(transport);
}
}
transport_t get_transport(void) {
return transport;
}
/* Changing transport may cause bronw-out reset of led driver
* withoug MCU reset, which lead backlight to not work,
* reinit the led driver workgound this issue */
static void reinit_led_drvier(void) {
/* Wait circuit to discharge for a while */
systime_t start = chVTGetSystemTime();
while (chTimeI2MS(chVTTimeElapsedSinceX(start)) < 100) {};
#ifdef LED_MATRIX_ENABLE
led_matrix_init();
#endif
#ifdef RGB_MATRIX_ENABLE
rgb_matrix_init();
#endif
}
void transport_changed(transport_t new_transport) {
#if (REINIT_LED_DRIVER)
reinit_led_drvier();
#endif
#if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_TIMEOUT)
# if (RGB_MATRIX_TIMEOUT > 0)
rgb_matrix_disable_timeout_set(RGB_MATRIX_TIMEOUT_INFINITE);
rgb_matrix_disable_time_reset();
# endif
#endif
#if defined(LED_MATRIX_ENABLE) && defined(LED_MATRIX_TIMEOUT)
# if (LED_MATRIX_TIMEOUT > 0)
led_matrix_disable_timeout_set(LED_MATRIX_TIMEOUT_INFINITE);
led_matrix_disable_time_reset();
# endif
#endif
}
void usb_remote_wakeup(void) {
if (USB_DRIVER.state == USB_SUSPENDED) {
while (USB_DRIVER.state == USB_SUSPENDED) {
/* Do this in the suspended state */
suspend_power_down(); // on AVR this deep sleeps for 15ms
/* Remote wakeup */
if (suspend_wakeup_condition()) {
usbWakeupHost(&USB_DRIVER);
usb_wakeup(&USB_DRIVER);
}
}
// palWriteLine(H3, PAL_HIGH);
wait_ms(500);
// palWriteLine(H3, PAL_LOW);
/* Woken up */
// variables has been already cleared by the wakeup hook
send_keyboard_report();
}
}
|