Skip to content

Commit ac71d1d

Browse files
bmp-0.4.0
- Save keyboard and layout names to KEYMAP.JSN - Change EXKC behavior
1 parent 4db335c commit ac71d1d

File tree

9 files changed

+156
-22
lines changed

9 files changed

+156
-22
lines changed

keyboards/ble_micro_pro/config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2929
#define PRODUCT ble_micro_pro
3030
#define DESCRIPTION A development board for wireless split keyboards
3131

32+
#define BMP_BOOTPIN_AS_RESET
33+
3234
#define TAPPING_TERM_PER_KEY
3335
#define PERMISSIVE_HOLD
3436
#define PREVENT_STUCK_MODIFIERS

tmk_core/nrf.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ NRF_VER_DIR = sdk$(NRFSDK_VER)
3535

3636
COMMON_VPATH += $(DRIVER_PATH)/nrf52
3737

38-
GIT_DESCRIBE = $(shell git describe --tags --dirty="\\*")
38+
GIT_DESCRIBE = $(shell git describe --tags --long --dirty="\\*")
3939
CFLAGS += -DGIT_DESCRIBE=$(GIT_DESCRIBE)
4040
CFLAGS += -DTARGET=$(TARGET)
4141

tmk_core/protocol/nrf/bmp.c

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ void bmp_action_exec(keyevent_t event)
8888
}
8989

9090
static int sleep_enter_counter = -1;
91+
int reset_counter = -1;
9192

9293
/** \brief Keyboard task: Do keyboard routine jobs
9394
*
@@ -229,6 +230,16 @@ void bmp_keyboard_task(void)
229230
BMPAPI->app.enter_sleep_mode();
230231
}
231232
}
233+
234+
// reset flag check
235+
if (reset_counter > 0)
236+
{
237+
reset_counter--;
238+
if (reset_counter == 0)
239+
{
240+
BMPAPI->app.reset();
241+
}
242+
}
232243
}
233244

234245
bmp_error_t nus_rcv_callback(const uint8_t* dat, uint32_t len)
@@ -277,6 +288,20 @@ char *strnstr(const char *haystack, const char *needle, size_t len) {
277288
return NULL;
278289
}
279290

291+
const char *strnchr(const char *haystack, char needle, size_t len) {
292+
int i;
293+
for (i=0; i < len; i++)
294+
{
295+
if (haystack[i] == needle) {
296+
return &haystack[i];
297+
}
298+
else if (haystack[i] == '\0') {
299+
return NULL;
300+
}
301+
}
302+
return NULL;
303+
}
304+
280305
void parse_and_save_config(void)
281306
{
282307
bmp_api_config_t config;
@@ -315,7 +340,8 @@ void parse_and_save_keymap(void)
315340
{
316341
xprintf("Update keymap. length:%d\r\n", inst.keymap_idx);
317342
xprintf("%d extended keycodes are found\r\n", inst.ek_num);
318-
BMPAPI->app.set_keymap(keymap, inst.keymap_idx);
343+
xprintf("keyboard:%s\r\n", inst.layout_name);
344+
BMPAPI->app.set_keymap(keymap, inst.keymap_idx, inst.layout_name);
319345
bmp_ex_keycode_num = inst.ek_num;
320346
BMPAPI->app.save_file(1);
321347

@@ -460,9 +486,36 @@ static inline void update_keymap_string(bmp_api_config_t const * config,
460486
keymap_conv_inst.use_ascii = config->keymap.use_ascii;
461487
keymap_conv_inst.bmp_ek = bmp_ex_keycodes;
462488

463-
strcpy(str, "{\"layers\":\r\n");
464-
keymap_to_json_conv_layout(&keymap_conv_inst, str + 12,
465-
len-12, config->matrix.layout);
489+
char keyboard_name[32] = {0};
490+
char layout_name[32] = {0};
491+
const char* delimiter = strnchr(keymap_info->layout_name, ':', 32);
492+
493+
if (delimiter != NULL)
494+
{
495+
memcpy(keyboard_name, keymap_info->layout_name,
496+
delimiter - keymap_info->layout_name);
497+
498+
strncpy(layout_name, delimiter + 1, sizeof(layout_name));
499+
}
500+
else
501+
{
502+
strcpy(keyboard_name, config->device_info.name);
503+
strcpy(layout_name, keymap_info->layout_name);
504+
}
505+
506+
507+
strcpy(str, "{\"keyboard\":\"");
508+
strcat(str, keyboard_name);
509+
510+
strcat(str, "\",\r\n\"keymap\":\"");
511+
strcat(str, "\",\r\n\"layout\":\"");
512+
strcat(str, layout_name);
513+
514+
strcat(str, "\",\r\n\"layers\":\r\n");
515+
uint32_t header_len = strlen(str);
516+
517+
keymap_to_json_conv_layout(&keymap_conv_inst, str + header_len,
518+
len - header_len, config->matrix.layout);
466519
strcat(str, "}");
467520
BMPAPI->usb.create_file("KEYMAP JSN", (uint8_t*)str, strlen(str));
468521
}
@@ -567,7 +620,7 @@ void bmp_init()
567620
BMPAPI->app.get_keymap_info(&keymap_info);
568621
if (keymap_info.len == 0)
569622
{
570-
BMPAPI->app.set_keymap((uint16_t*)keymaps, keymaps_len()); // load default keymap
623+
BMPAPI->app.set_keymap((uint16_t*)keymaps, keymaps_len(), keymap_info.layout_name); // load default keymap
571624
BMPAPI->app.get_keymap_info(&keymap_info);
572625
}
573626

tmk_core/protocol/nrf/bmp_extended_keycode_converter.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ uint8_t bmp_ex_keycode2str_locale(bmp_ex_keycode_t const* const ek, char* str, u
5353
}
5454

5555
uint8_t str2bmp_ex_keycode_locale(bmp_ex_keycode_t* ek_res, const char* str, uint32_t len, KEYMAP_LOCALE locale) {
56+
// Delete ANY() if contained
57+
if (str[0] == 'A' && str[1] == 'N' && str[2] == 'Y') {
58+
len = get_inner_element(&str, len);
59+
}
60+
5661
// Delete EX()
5762
len = get_inner_element(&str, len);
5863

tmk_core/protocol/nrf/bmp_process_extended_keycode.c

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ static void state_transition_common(uint16_t exkc_idx);
8888
bool bmp_process_extended_keycode(uint16_t *const p_keycode, keyrecord_t *const record);
8989

9090
static int pending_exkc_num = 0;
91+
static bool pending_normal_kc_flag;
92+
static bool process_normal_kc_flag;
9193

9294
bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
9395
dprintf("Process KC:%d\n", keycode);
@@ -155,9 +157,9 @@ void bmp_action_exec_impl(keyevent_t event) {
155157
bool bmp_process_extended_keycode(uint16_t *const p_keycode, keyrecord_t *const record) {
156158
uint16_t keycode = *p_keycode;
157159
static bool skip_next_call = false;
160+
bool continue_process = true;
158161

159162
if (skip_next_call) {
160-
skip_next_call = false;
161163
return true;
162164
}
163165

@@ -170,42 +172,59 @@ bool bmp_process_extended_keycode(uint16_t *const p_keycode, keyrecord_t *const
170172
}
171173

172174
if (pending_exkc_num > 0) {
173-
// queueing keyevent
174-
//// return false;
175-
return send_pending_buffer(&record->event) ? false : true;
176-
} else {
175+
pending_normal_kc_flag = true;
176+
}
177+
178+
if (pending_normal_kc_flag) {
179+
if ( EXKC_START >= *p_keycode || *p_keycode >= EXKC_END) {
180+
// queueing keyevent
181+
continue_process = send_pending_buffer(&record->event) ? false : true;
182+
}
183+
}
184+
185+
if (pending_exkc_num <= 0) {
186+
pending_normal_kc_flag = false;
187+
if (pending_buffer_cnt[PRESS_BUF] + pending_buffer_cnt[RELEASE_BUF] > 0) {
188+
process_normal_kc_flag = true;
189+
}
190+
}
191+
192+
if (process_normal_kc_flag) {
177193
// process events
178-
keyrecord_t record;
194+
keyevent_t keyevent;
195+
skip_next_call = true;
179196
for (int idx = 0; idx < pending_buffer_max_idx; idx++) {
180197
if (pending_buffer[idx][PRESS_BUF].pressed) {
181-
skip_next_call = true;
182-
record.event = pending_buffer[idx][PRESS_BUF];
198+
keyevent = pending_buffer[idx][PRESS_BUF];
183199
pending_buffer[idx][PRESS_BUF].pressed = false;
184200
dprintf("[PDB] proc(p):%d\n", idx);
185-
process_record(&record);
201+
action_exec(keyevent);
186202
}
187203

188204
if (pending_buffer[idx][RELEASE_BUF].pressed) {
189-
skip_next_call = true;
190-
record.event = pending_buffer[idx][RELEASE_BUF];
205+
keyevent = pending_buffer[idx][RELEASE_BUF];
191206
pending_buffer[idx][RELEASE_BUF].pressed = false;
192-
record.event.pressed = false;
207+
keyevent.pressed = false;
193208
dprintf("[PDB] proc(r):%d\n", idx);
194-
process_record(&record);
209+
action_exec(keyevent);
195210
}
196211
}
197212

213+
skip_next_call = false;
214+
process_normal_kc_flag = false;
198215
pending_buffer_max_idx = 0;
199216
pending_buffer_cnt[PRESS_BUF] = 0;
200217
pending_buffer_cnt[RELEASE_BUF] = 0;
218+
219+
return false;
201220
}
202221

203222
// if ( keycode < EXKC_START || keycode > EXKC_END)
204223
// {
205224
// return true;
206225
// }
207226

208-
return true;
227+
return continue_process;
209228
}
210229

211230
static void preprocess_exkc_common(keyevent_t const *const keyevent) {

tmk_core/protocol/nrf/config_file_util.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ void json_to_keymap_init(json_keymap_convert_inst_t * const inst,
2626
inst->keymap_len = keymap_len;
2727
inst->locale = LOCALE_US;
2828
memset(inst->tail, 0, sizeof(inst->tail));
29+
memset(inst->layout_name, 0, sizeof(inst->layout_name));
2930
}
3031

3132

@@ -48,6 +49,8 @@ static uint16_t register_ex_kc(json_keymap_convert_inst_t* const inst,
4849
return KC_NO;
4950
}
5051

52+
int32_t get_val_string(const char* json, const char* key,
53+
const char** val, uint32_t* len);
5154

5255
// Return quantum keycode from json strings.
5356
// inst->tail contains partial string if quotation is not closed
@@ -57,11 +60,42 @@ int32_t json_to_keymap_conv(json_keymap_convert_inst_t * const inst,
5760
const char * str;
5861
const char * str2;
5962
const char * str3;
63+
uint32_t name_len;
64+
uint32_t layout_name_len;
6065

6166
inst->ek_num = 0;
6267

6368
if ((inst->keymap_idx == 0) && inst->tail[0]=='\0')
6469
{
70+
str = strstr(json, "keyboard");
71+
if (str != NULL)
72+
{
73+
get_val_string(str, "keyboard", &str2, &name_len);
74+
if (name_len + 1 < 32) {
75+
strncat(inst->layout_name, str2, name_len);
76+
strcat(inst->layout_name, ":");
77+
}
78+
}
79+
80+
str = strstr(json, "layout");
81+
if (str != NULL)
82+
{
83+
get_val_string(str, "layout", &str2, &layout_name_len);
84+
if (layout_name_len == 0 && name_len + 1 + 6 < LAYOUT_NAME_MAXLEN)
85+
{
86+
strcat(inst->layout_name, "LAYOUT");
87+
}
88+
else if (name_len + layout_name_len + 1 < LAYOUT_NAME_MAXLEN)
89+
{
90+
strncat(inst->layout_name, str2, layout_name_len);
91+
inst->layout_name[name_len + layout_name_len + 1] = '\0';
92+
}
93+
}
94+
else
95+
{
96+
strcat(inst->layout_name, "LAYOUT");
97+
}
98+
6599
str = strstr(json, "layers");
66100
if (str == NULL)
67101
{

tmk_core/protocol/nrf/config_file_util.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "bmp_custom_keycode.h"
1313

1414

15+
#define LAYOUT_NAME_MAXLEN 32
1516
typedef struct
1617
{
1718
uint16_t *keymap;
@@ -22,6 +23,8 @@ typedef struct
2223

2324
bmp_ex_keycode_t *bmp_ek;
2425
uint16_t ek_num;
26+
27+
char layout_name[LAYOUT_NAME_MAXLEN];
2528
} json_keymap_convert_inst_t;
2629

2730
typedef struct
@@ -33,6 +36,8 @@ typedef struct
3336
uint8_t use_ascii;
3437

3538
bmp_ex_keycode_t *bmp_ek;
39+
40+
const char *layout_name;
3641
} keymap_json_convert_inst_t;
3742

3843
typedef struct

tmk_core/protocol/nrf/matrix.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ extern const bmp_matrix_func_t matrix_func_col2row_lpme;
5858
extern const bmp_matrix_func_t matrix_func_row2col2row;
5959
extern const bmp_matrix_func_t matrix_func_col2row2col;
6060

61+
extern int reset_counter;
62+
#define BOOTPIN 22
63+
6164
__attribute__ ((weak))
6265
void matrix_init_quantum(void) {
6366
matrix_init_kb();
@@ -145,6 +148,10 @@ void matrix_init(void) {
145148
matrix_func->init();
146149

147150
matrix_init_quantum();
151+
152+
#if defined(BMP_BOOTPIN_AS_RESET)
153+
setPinInputHigh(BOOTPIN);
154+
#endif
148155
}
149156

150157
__attribute__ ((weak))
@@ -211,6 +218,14 @@ uint8_t matrix_scan(void)
211218
{
212219
uint8_t res = matrix_scan_impl(matrix);
213220
matrix_scan_quantum();
221+
222+
#if defined(BMP_BOOTPIN_AS_RESET)
223+
if (readPin(BOOTPIN) == 0 && reset_counter < 0)
224+
{
225+
reset_counter = 10;
226+
}
227+
#endif
228+
214229
return res;
215230
}
216231

tmk_core/protocol/nrf/sdk15/apidef.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include "error_def.h"
77

8-
#define API_VERSION 4
8+
#define API_VERSION 5
99
#define CONFIG_VERSION 2
1010
#define PINS_MAX 32
1111

@@ -212,6 +212,7 @@ typedef struct {
212212
uint16_t len;
213213
uint16_t keynum;
214214
const uint16_t* array;
215+
const char* layout_name;
215216
} bmp_api_keymap_info_t;
216217

217218
typedef bmp_error_t (*bmp_api_msc_write_cb_t)(const uint8_t* dat, uint32_t len);
@@ -227,7 +228,7 @@ typedef struct
227228
bmp_error_t (*push_keystate_change)(bmp_api_key_event_t const * const key);
228229
uint32_t (*pop_keystate_change)(bmp_api_key_event_t* key, uint32_t len, uint8_t burst_threshold);
229230
uint16_t (*keymap_key_to_keycode)(uint8_t layer, bmp_api_keypos_t const * const key);
230-
bmp_error_t (*set_keymap)(const uint16_t* keymap, uint16_t len);
231+
bmp_error_t (*set_keymap)(const uint16_t* keymap, uint16_t len, const char * layout_name);
231232
bmp_error_t (*set_config)(bmp_api_config_t const * const);
232233
bmp_error_t (*get_keymap_info)(bmp_api_keymap_info_t* const keymap_info);
233234
const bmp_api_config_t* (*get_config)(void);

0 commit comments

Comments
 (0)