@@ -15,10 +15,11 @@ You should have received a copy of the GNU General Public License
1515along 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;
3031static char qmk_config_string [1024 ];
3132static bmp_encoder_config_t bmp_encoder_config ;
3233static char encoder_config_string [1024 ];
34+ static char macro_string [1024 ];
3335
3436bmp_ex_keycode_t bmp_ex_keycodes [BMP_EX_KC_LEN ];
3537uint32_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+
204231const 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
226259static 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
396448static 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
568619uint16_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 ();
0 commit comments