Skip to content

Commit 26b8d65

Browse files
Add feature: Macro
1 parent 3cc9287 commit 26b8d65

File tree

14 files changed

+478
-34
lines changed

14 files changed

+478
-34
lines changed

keyboards/keyboard_quantizer/kqb/config.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424

2525
#define QUANTIZER_REPORT_PARSER REPORT_PARSER_USER
2626

27-
#define VIA_ENABLE
28-
2927
#define MATRIX_ROW_PINS \
3028
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
3129
#define MATRIX_COL_PINS \

release.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/sh
22

3-
version=0_10_0
3+
version=0_10_2
44

55
for keyboard in ble_micro_pro kugel toybox/bmp crkbd_ecwl/bmp keyboard_quantizer/kqb
66
do

tmk_core/nrf.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ NRFCFLAGS += -DMATRIX_ROWS=32
3030
NRFCFLAGS += -DMATRIX_COLS=32
3131
NRFCFLAGS += -DRGBLED_NUM=128
3232
NRFCFLAGS += -DEEPROM_SIZE=32
33+
NRFCFLAGS += -DVIA_ENABLE # Define here(not in rules.mk) to use VIA keycode definitions, and do not complie quantum/via.c
34+
3335

3436
NRF_VER_DIR = sdk$(NRFSDK_VER)
3537

tmk_core/protocol/nrf.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ NRF_DIR = $(PROTOCOL_DIR)/nrf
2424
$(NRF_DIR)/bmp_process_extended_keycode.c \
2525
$(NRF_DIR)/bmp_indicator_led.c \
2626
$(NRF_DIR)/bmp_encoder_actions.c \
27+
$(NRF_DIR)/bmp_macro.c \
28+
$(NRF_DIR)/bmp_macro_parser.c \
2729
$(NRF_DIR)/encoder.c \
2830
$(NRF_DIR)/via.c \
2931

tmk_core/protocol/nrf/bmp.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2222
#include "bmp_custom_keycode.h"
2323
#include "bmp_process_extended_keycode.h"
2424
#include "bmp_indicator_led.h"
25+
#include "bmp_macro.h"
2526
#include "keycode_str_converter.h"
2627

2728
#include <stdint.h>
@@ -265,6 +266,8 @@ void bmp_keyboard_task(void)
265266
if (velocikey_enabled()) { velocikey_decelerate(); }
266267
#endif
267268

269+
bmp_macro_task();
270+
268271
// update LED
269272
if (led_status != host_keyboard_leds()) {
270273
led_status = host_keyboard_leds();
@@ -570,6 +573,7 @@ void bmp_init()
570573
load_tapping_term_file();
571574
load_eeprom_emulation_file();
572575
load_encoder_config_file();
576+
bmp_macro_load_file();
573577

574578
if (config->mode == WEBNUS_CONFIG)
575579
{

tmk_core/protocol/nrf/bmp_config.c

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ You should have received a copy of the GNU General Public License
1515
along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

18-
1918
#include "apidef.h"
2019
#include "bmp.h"
2120
#include "bmp_config.h"
21+
#include "bmp_macro.h"
22+
#include "bmp_macro_parser.h"
2223

2324
#ifndef TAPPING_TERM
2425
# define TAPPING_TERM 200
@@ -30,6 +31,7 @@ static bmp_qmk_config_t bmp_qmk_config;
3031
static char qmk_config_string[1024];
3132
static bmp_encoder_config_t bmp_encoder_config;
3233
static char encoder_config_string[1024];
34+
static char macro_string[1024];
3335

3436
bmp_ex_keycode_t bmp_ex_keycodes[BMP_EX_KC_LEN];
3537
uint32_t bmp_ex_keycode_num;
@@ -201,6 +203,31 @@ int save_encoder_config(void) {
201203
return res;
202204
}
203205

206+
int parse_macro_string(void) {
207+
// skip key
208+
char *src = macro_string;
209+
while (*src++ != '\n') {
210+
continue;
211+
}
212+
213+
// parse
214+
bmp_macro_file_parse(bmp_macro_get_file_buffer(), (const uint8_t *)src,
215+
strlen(src));
216+
217+
return 0;
218+
}
219+
220+
int save_macro_string(void) {
221+
int res = BMPAPI->app.save_file(BMP_MACRO_RECORD);
222+
if (res == 0) {
223+
xprintf("Succeed to save macro");
224+
} else {
225+
xprintf("Failed to save macro");
226+
}
227+
228+
return res;
229+
}
230+
204231
const file_string_parser_setting_t file_string_parser_setting[] = {
205232
{.key = "\"config\"",
206233
.string_dst = config_string,
@@ -221,7 +248,13 @@ const file_string_parser_setting_t file_string_parser_setting[] = {
221248
.string_dst = encoder_config_string,
222249
.dst_len = sizeof(encoder_config_string),
223250
parse_encoder_config,
224-
save_encoder_config}};
251+
save_encoder_config},
252+
{.key = "macro\n",
253+
.string_dst = macro_string,
254+
.dst_len = sizeof(macro_string),
255+
parse_macro_string,
256+
save_macro_string},
257+
};
225258

226259
static file_string_parser_t parser = {NULL, 0};
227260

@@ -270,13 +303,20 @@ int stream_write_callback(const uint8_t *dat, uint32_t len) {
270303
parser.write_idx += len;
271304
}
272305

273-
if (parser.setting != NULL) {
306+
if (parser.setting == &file_string_parser_setting[PARSER_MACRO]) {
307+
dst[parser.write_idx] = '\0';
308+
int res = parser.setting->parse();
309+
parser.write_idx = 0;
310+
BMPAPI->logger.info("text received");
311+
312+
return res == 0 ? 0 : 1;
313+
} else if (parser.setting != NULL) {
274314
json_close = is_json_closed((const char *)dst, parser.write_idx);
275315
if (json_close == 0) {
276316
dst[parser.write_idx] = '\0';
277317
int res = parser.setting->parse();
278318
parser.write_idx = 0;
279-
BMPAPI->logger.info("Received json");
319+
BMPAPI->logger.info("json received");
280320

281321
return res == 0 ? 0 : 1;
282322
} else if (json_close == -1) {
@@ -385,12 +425,24 @@ static inline void update_tapping_term_string(bmp_api_config_t const *config,
385425
BMPAPI->usb.create_file("TAPTERM JSN", (uint8_t *)str, strlen(str));
386426
}
387427

388-
static inline void update_ecoder_config_string(bmp_encoder_config_t const *config,
389-
char *str, uint32_t len) {
428+
static inline void update_ecoder_config_string(
429+
bmp_encoder_config_t const *config, char *str, uint32_t len) {
390430
encoder_config_to_json(config, str, len);
391431
BMPAPI->usb.create_file("ENCODER JSN", (uint8_t *)str, strlen(str));
392432
}
393433

434+
static inline void update_macro_string(char *str, uint32_t len) {
435+
// put key
436+
uint32_t toklen = strlen(file_string_parser_setting[PARSER_MACRO].key);
437+
strcpy(str, file_string_parser_setting[PARSER_MACRO].key);
438+
439+
// stringify macro
440+
uint8_t *macro = bmp_macro_get_file_buffer();
441+
bmp_macro_file_stringify(macro, (uint8_t *)str + toklen, len - toklen);
442+
443+
BMPAPI->usb.create_file("MACRO TXT", (uint8_t *)str, strlen(str));
444+
}
445+
394446
__attribute__((weak)) void create_user_file() {}
395447

396448
static const char bmp_version_info[] = "API version: " STR(
@@ -561,8 +613,7 @@ int load_encoder_config_file() {
561613

562614
__attribute__((weak)) uint16_t keymap_key_to_keycode_bmp(uint8_t layer,
563615
keypos_t key) {
564-
return BMPAPI->app.keymap_key_to_keycode(layer,
565-
(bmp_api_keypos_t *)&key);
616+
return BMPAPI->app.keymap_key_to_keycode(layer, (bmp_api_keypos_t *)&key);
566617
}
567618

568619
uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key) {
@@ -602,6 +653,7 @@ void update_config_files() {
602653
sizeof(qmk_config_string));
603654
update_ecoder_config_string(&bmp_encoder_config, encoder_config_string,
604655
sizeof(encoder_config_string));
656+
update_macro_string(macro_string, sizeof(macro_string));
605657
create_info_file();
606658
create_index_html();
607659
create_user_file();

tmk_core/protocol/nrf/bmp_config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ typedef enum {
3535
PARSER_KEYMAP,
3636
PARSER_QMK,
3737
PARSER_ENCODER,
38+
PARSER_MACRO,
3839
PARSER_NONE,
3940
} parser_type_t;
4041

tmk_core/protocol/nrf/bmp_macro.c

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
2+
#include "bmp_macro.h"
3+
4+
#include <string.h>
5+
6+
#include "send_string.h"
7+
#include "debug.h"
8+
9+
#include "apidef.h"
10+
11+
static uint8_t* macro_file;
12+
static uint32_t macro_file_length;
13+
static int8_t active_macro = -1;
14+
static uint16_t active_macro_offset;
15+
16+
#define ASSERT_MACRO_FILE() \
17+
if (macro_file == NULL) { \
18+
bmp_macro_load_file(); \
19+
if (macro_file == NULL) { \
20+
return; \
21+
} \
22+
}
23+
24+
void bmp_macro_task(void) {
25+
if (active_macro < 0) {
26+
return;
27+
}
28+
29+
char data[4] = {0, 0, 0, 0};
30+
data[0] = macro_file[active_macro_offset++];
31+
data[1] = 0;
32+
33+
if (data[0] == 0) {
34+
active_macro = -1;
35+
return;
36+
}
37+
38+
if (data[0] == SS_TAP_CODE || data[0] == SS_DOWN_CODE ||
39+
data[0] == SS_UP_CODE) {
40+
data[1] = data[0];
41+
data[0] = SS_QMK_PREFIX;
42+
data[2] = macro_file[active_macro_offset++];
43+
if (data[2] == 0) {
44+
active_macro = -1;
45+
return;
46+
}
47+
}
48+
49+
send_string(data);
50+
}
51+
52+
uint8_t* bmp_macro_get_file_buffer(void) { return macro_file; }
53+
54+
void bmp_macro_load_file(void) {
55+
BMPAPI->app.get_file(BMP_MACRO_RECORD, &macro_file, &macro_file_length);
56+
if (macro_file == NULL) {
57+
BMPAPI->app.save_file(BMP_MACRO_RECORD); // create file
58+
}
59+
}
60+
61+
void bmp_macro_save_file(void) { BMPAPI->app.save_file(BMP_MACRO_RECORD); }
62+
63+
void bmp_macro_send(uint8_t id) {
64+
ASSERT_MACRO_FILE();
65+
66+
if (id >= BMP_MACRO_COUNT) {
67+
dprintf("[Macro] Invalid ID:%d\n", id);
68+
return;
69+
}
70+
71+
const uint8_t* end_addr = macro_file + BMP_MACRO_FILE_LEN - 1;
72+
73+
if (*end_addr != 0) {
74+
dprintf("[Macro] Invalid macro\n");
75+
return;
76+
}
77+
78+
if (active_macro >= 0) {
79+
dprintf("[Macro] Other macro running\n");
80+
return;
81+
}
82+
83+
dprintf("[Macro] Activate %d\n", id);
84+
85+
active_macro_offset = 0;
86+
87+
for (active_macro = 0; active_macro < id; active_macro++) {
88+
while (macro_file[active_macro_offset++] != 0) {
89+
continue;
90+
}
91+
92+
if (active_macro_offset >= BMP_MACRO_FILE_LEN - 1) {
93+
dprintf("Invalid macro file\n");
94+
active_macro = -1;
95+
return;
96+
}
97+
}
98+
}
99+
100+
void bmp_macro_get_buffer(uint16_t offset, uint16_t size, uint8_t* buffer) {
101+
ASSERT_MACRO_FILE();
102+
103+
memcpy(buffer, macro_file + offset, size);
104+
}
105+
106+
void bmp_macro_set_buffer(uint16_t offset, uint16_t size, uint8_t* buffer) {
107+
ASSERT_MACRO_FILE();
108+
109+
if (offset + size >= BMP_MACRO_FILE_LEN) {
110+
dprintf("[Macro] Invalid size: %d\n", offset + size);
111+
return;
112+
}
113+
114+
memcpy(macro_file + offset, buffer, size);
115+
}
116+
117+
void bmp_macro_reset(void) {
118+
ASSERT_MACRO_FILE();
119+
120+
memset(macro_file, 0, BMP_MACRO_FILE_LEN);
121+
}
122+

tmk_core/protocol/nrf/bmp_macro.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
#pragma once
3+
4+
#include <stdint.h>
5+
6+
#define BMP_MACRO_COUNT (16)
7+
#define BMP_MACRO_FILE_LEN (1024)
8+
9+
void bmp_macro_task(void);
10+
uint8_t* bmp_macro_get_file_buffer(void);
11+
void bmp_macro_load_file(void);
12+
void bmp_macro_save_file(void);
13+
void bmp_macro_send(uint8_t id);
14+
void bmp_macro_get_buffer(uint16_t offset, uint16_t size, uint8_t* buffer);
15+
void bmp_macro_set_buffer(uint16_t offset, uint16_t size, uint8_t* buffer);
16+
void bmp_macro_reset(void);

0 commit comments

Comments
 (0)