Skip to content

Commit ee48d2a

Browse files
Add safe mode
1 parent 0f9e0df commit ee48d2a

File tree

1 file changed

+48
-11
lines changed
  • tmk_core/protocol/nrf

1 file changed

+48
-11
lines changed

tmk_core/protocol/nrf/bmp.c

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,11 @@ bmp_error_t nus_rcv_callback(const uint8_t* dat, uint32_t len)
255255
#include "config_file_util.h"
256256
#include "apidef.h"
257257
#include "cli.h"
258+
#include "bmp_matrix.h"
259+
260+
#ifndef BMP_FORCE_SAFE_MODE
261+
#define BMP_FORCE_SAFE_MODE false
262+
#endif
258263

259264
char config_string[2048];
260265
char keymap_string[10*1024];
@@ -570,26 +575,24 @@ bmp_error_t bmp_state_change_cb(bmp_api_event_t event)
570575
return BMP_OK;
571576
}
572577

573-
#if defined(ALLOW_MSC_ROW_PIN) && defined(ALLOW_MSC_COL_PIN)
574-
#include "bmp_matrix.h"
575-
static bool checkMscDisableFlag(bmp_api_config_t const * const config)
578+
static bool checkKeyIsPressedOnStartup(bmp_api_config_t const * const config, uint8_t row, uint8_t col)
576579
{
577580
int8_t low_side_pin, high_side_pin;
578581
if (config->matrix.diode_direction == MATRIX_COL2ROW
579582
|| config->matrix.diode_direction == MATRIX_COL2ROW2COL
580583
|| config->matrix.diode_direction == MATRIX_COL2ROW_LPME) {
581-
high_side_pin = ALLOW_MSC_COL_PIN;
582-
low_side_pin = ALLOW_MSC_ROW_PIN;
584+
high_side_pin = col;
585+
low_side_pin = row;
583586
}
584587
else if (config->matrix.diode_direction == MATRIX_ROW2COL
585588
|| config->matrix.diode_direction == MATRIX_ROW2COL2ROW
586589
|| config->matrix.diode_direction == MATRIX_ROW2COL_LPME) {
587-
high_side_pin = ALLOW_MSC_ROW_PIN;
588-
low_side_pin = ALLOW_MSC_COL_PIN;
590+
high_side_pin = row;
591+
low_side_pin = col;
589592
}
590593
else {
591594
// return default value
592-
return DISABLE_MSC;
595+
return false;
593596
}
594597

595598
setPinInput(high_side_pin);
@@ -600,7 +603,20 @@ static bool checkMscDisableFlag(bmp_api_config_t const * const config)
600603

601604
writePinLow(low_side_pin);
602605

603-
if (pin_state == 0) {
606+
return pin_state == 0;
607+
}
608+
609+
static inline bool checkSafemodeFlag(bmp_api_config_t const * const config)
610+
{
611+
return checkKeyIsPressedOnStartup(config, config->matrix.row_pins[0],
612+
config->matrix.col_pins[0]);
613+
}
614+
615+
#if defined(ALLOW_MSC_ROW_PIN) && defined(ALLOW_MSC_COL_PIN)
616+
static inline bool checkMscDisableFlag(bmp_api_config_t const * const config)
617+
{
618+
if (checkKeyIsPressedOnStartup(config,
619+
ALLOW_MSC_ROW_PIN, ALLOW_MSC_COL_PIN) == 0) {
604620
// enable MSC
605621
return false;
606622
} else {
@@ -612,6 +628,7 @@ static bool checkMscDisableFlag(bmp_api_config_t const * const config)
612628

613629
static bool has_ble = true;
614630
static bool has_usb = true;
631+
static bool is_safe_mode = false;
615632

616633
void bmp_init()
617634
{
@@ -649,15 +666,35 @@ void bmp_init()
649666
};
650667

651668
BMPAPI->app.init(&default_config);
669+
652670
const bmp_api_config_t * config = BMPAPI->app.get_config();
671+
672+
673+
if (checkSafemodeFlag(config) || BMP_FORCE_SAFE_MODE)
674+
{
675+
// start in safe mode
676+
BMPAPI->app.set_config(&default_config);
677+
config = &default_config;
678+
is_safe_mode = true;
679+
}
680+
653681
BMPAPI->usb.set_msc_write_cb(msc_write_callback);
654682
BMPAPI->app.set_state_change_cb(bmp_state_change_cb);
655683

684+
if (!is_safe_mode)
685+
{
656686
#if defined(ALLOW_MSC_ROW_PIN) && defined(ALLOW_MSC_COL_PIN)
657-
BMPAPI->usb.init(config, checkMscDisableFlag(config));
687+
BMPAPI->usb.init(config, checkMscDisableFlag(config));
658688
#else
659-
BMPAPI->usb.init(config, DISABLE_MSC);
689+
BMPAPI->usb.init(config, DISABLE_MSC);
660690
#endif
691+
}
692+
else
693+
{
694+
BMPAPI->usb.init(config, DISABLE_MSC);
695+
}
696+
697+
661698
BMPAPI->ble.init(config);
662699

663700
BMPAPI->logger.info("usb init");

0 commit comments

Comments
 (0)