blob: 51225888c9dae66f973d54540c685b5c84c1afc7 (
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
|
/* 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 "report_buffer.h"
#include "bluetooth.h"
#include "lpm.h"
/* The report buffer is mainly used to fix key press lost issue of macro
* when bluetooth module fifo isn't large enough. The maximun macro
* string length is determined by this queue size, and should be
* REPORT_BUFFER_QUEUE_SIZE devided by 2 since each character is implemented
* by sending a key pressing then a key releasing report.
* Please note that it cosume sizeof(report_buffer_t) * REPORT_BUFFER_QUEUE_SIZE
* bytes RAM, with default setting, used RAM size is
* sizeof(report_buffer_t) * 256 = 34* 256 = 8704 bytes
*/
#ifndef REPORT_BUFFER_QUEUE_SIZE
# define REPORT_BUFFER_QUEUE_SIZE 256
#endif
extern bluetooth_transport_t bluetooth_transport;
/* report_interval value should be less than bluetooth connection interval because
* it takes some time for communicating between mcu and bluetooth module. Carefully
* set this value to feed the bt module so that we don't lost the key report nor lost
* the anchor point of bluetooth interval. The bluetooth connection interval varies
* if BLE is used, invoke report_buffer_set_inverval() to update the value
*/
uint8_t report_interval = DEFAULT_REPORT_INVERVAL_MS;
static uint32_t report_timer_buffer = 0;
uint32_t retry_time_buffer = 0;
report_buffer_t report_buffer_queue[REPORT_BUFFER_QUEUE_SIZE];
uint8_t report_buffer_queue_head;
uint8_t report_buffer_queue_tail;
report_buffer_t kb_rpt;
uint8_t retry = 0;
void report_buffer_init(void) {
// Initialise the report queue
memset(&report_buffer_queue, 0, sizeof(report_buffer_queue));
report_buffer_queue_head = 0;
report_buffer_queue_tail = 0;
retry = 0;
report_timer_buffer = sync_timer_read32();
}
bool report_buffer_enqueue(report_buffer_t *report) {
uint8_t next = (report_buffer_queue_head + 1) % REPORT_BUFFER_QUEUE_SIZE;
if (next == report_buffer_queue_tail) {
return false;
}
report_buffer_queue[report_buffer_queue_head] = *report;
report_buffer_queue_head = next;
return true;
}
inline bool report_buffer_dequeue(report_buffer_t *report) {
if (report_buffer_queue_head == report_buffer_queue_tail) {
return false;
}
*report = report_buffer_queue[report_buffer_queue_tail];
report_buffer_queue_tail = (report_buffer_queue_tail + 1) % REPORT_BUFFER_QUEUE_SIZE;
return true;
}
bool report_buffer_is_empty() {
return report_buffer_queue_head == report_buffer_queue_tail;
}
void report_buffer_update_timer(void) {
report_timer_buffer = sync_timer_read32();
}
bool report_buffer_next_inverval(void) {
return sync_timer_elapsed32(report_timer_buffer) > report_interval;
}
void report_buffer_set_inverval(uint8_t interval) {
report_interval = interval;
}
uint8_t report_buffer_get_retry(void) {
return retry;
}
void report_buffer_set_retry(uint8_t times) {
retry = times;
}
void report_buffer_task(void) {
if (bluetooth_get_state() == BLUETOOTH_CONNECTED && (!report_buffer_is_empty() || retry) && report_buffer_next_inverval()) {
bool pending_data = false;
if (!retry) {
if (report_buffer_dequeue(&kb_rpt) && kb_rpt.type != REPORT_TYPE_NONE) {
if (sync_timer_read32() > 2) {
pending_data = true;
retry = RETPORT_RETRY_COUNT;
retry_time_buffer = sync_timer_read32();
}
}
} else {
if (sync_timer_elapsed32(retry_time_buffer) > 7) {
pending_data = true;
--retry;
retry_time_buffer = sync_timer_read32();
}
}
if (pending_data) {
#if defined(NKRO_ENABLE) && defined(BLUETOOTH_NKRO_ENABLE)
if (kb_rpt.type == REPORT_TYPE_NKRO && bluetooth_transport.send_nkro) {
bluetooth_transport.send_nkro(&kb_rpt.keyboard.nkro.mods);
} else if (kb_rpt.type == REPORT_TYPE_KB && bluetooth_transport.send_keyboard)
bluetooth_transport.send_keyboard(&kb_rpt.keyboard.mods);
#else
if (kb_rpt.type == REPORT_TYPE_KB && bluetooth_transport.send_keyboard) bluetooth_transport.send_keyboard(&kb_rpt.keyboard.mods);
#endif
if (kb_rpt.type == REPORT_TYPE_CONSUMER && bluetooth_transport.send_consumer) bluetooth_transport.send_consumer(kb_rpt.consumer);
report_timer_buffer = sync_timer_read32();
lpm_timer_reset();
}
}
}
|