Skip to content

Commit c7b70e1

Browse files
API v11: Sleep mode for Corne ECWL
1 parent 7bbc860 commit c7b70e1

File tree

7 files changed

+87
-9
lines changed

7 files changed

+87
-9
lines changed

keyboards/crkbd_ecwl/bmp/bmp.c

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "crkbd_ecwl.h"
2020
#include "quantum.h"
2121
#include "bmp_matrix.h"
22+
#include "bmp.h"
2223

2324
#include "debug.h"
2425
#include "i2c_master.h"
@@ -35,10 +36,24 @@ const uint8_t row_pins[] = {7, 8, 9};
3536
const uint8_t col_pins[] = {15, 14, 13};
3637
const uint8_t col_sels[] = {6, 4, 3, 0, 1, 2, 5};
3738

39+
// Switch scan threthold
3840
#define ERROR_TH 3000
3941
#define HIGH_TH 1000
4042
#define LOW_TH 800
4143

44+
// Sleep count threthold(x17ms)
45+
#define STANBY_COUNT (10 * 1000 / 17) // 10sec
46+
#define SCAN_RATE_STANBY (2) // 2count
47+
#define SCAN_RATE_DEEP_SLEEP (1 * 1000 / 17) // 1sec
48+
49+
typedef enum {
50+
SCAN_MODE_ACTIVE,
51+
SCAN_MODE_STANBY,
52+
SCAN_MODE_DEEP_SLEEP,
53+
} scan_mode_t;
54+
55+
static scan_mode_t scan_mode = SCAN_MODE_ACTIVE;
56+
4257
#define LEN(x) (sizeof(x) / sizeof(x[0]))
4358

4459
bool bmp_config_overwrite(bmp_api_config_t const *const config_on_storage,
@@ -92,7 +107,30 @@ uint16_t sw_read[32] = {0};
92107
uint32_t ecs_get_device_row() { return MATRIX_ROWS_DEFAULT; }
93108
uint32_t ecs_get_device_col() { return MATRIX_COLS_DEFAULT; }
94109
uint32_t ecs_matrix_scan(matrix_row_t *matrix_raw) {
95-
uint16_t sw_idx = 0;
110+
static uint32_t sleep_count = 0;
111+
uint16_t sw_idx = 0;
112+
113+
if (scan_mode == SCAN_MODE_DEEP_SLEEP) {
114+
if (sleep_count % SCAN_RATE_DEEP_SLEEP == SCAN_RATE_DEEP_SLEEP - 1) {
115+
sleep_count++;
116+
BMPAPI->ecs.schedule_next_scan();
117+
return 0;
118+
} else if (sleep_count % SCAN_RATE_DEEP_SLEEP != 0) {
119+
sleep_count++;
120+
BMPAPI->ecs.shutdown_amp();
121+
return 0;
122+
}
123+
} else if (scan_mode == SCAN_MODE_STANBY) {
124+
if (sleep_count % SCAN_RATE_STANBY == SCAN_RATE_STANBY - 1) {
125+
sleep_count++;
126+
BMPAPI->ecs.schedule_next_scan();
127+
return 0;
128+
} else if (sleep_count % SCAN_RATE_STANBY != 0) {
129+
sleep_count++;
130+
BMPAPI->ecs.shutdown_amp();
131+
return 0;
132+
}
133+
}
96134

97135
for (int col_sel_idx = 0; col_sel_idx < LEN(col_sels); col_sel_idx++) {
98136
BMPAPI->ecs.discharge_capacitor();
@@ -180,6 +218,19 @@ uint32_t ecs_matrix_scan(matrix_row_t *matrix_raw) {
180218
dprintf("\n");
181219
}
182220

221+
if (matrix_changed) {
222+
if (scan_mode == SCAN_MODE_DEEP_SLEEP) {
223+
BMPAPI->app.reset(0);
224+
}
225+
sleep_count = 0;
226+
scan_mode = SCAN_MODE_ACTIVE;
227+
} else {
228+
if ((!is_usb_connected()) && scan_mode == SCAN_MODE_ACTIVE && sleep_count > STANBY_COUNT) {
229+
scan_mode = SCAN_MODE_STANBY;
230+
}
231+
sleep_count++;
232+
}
233+
183234
return matrix_changed;
184235
}
185236

@@ -188,6 +239,11 @@ static const bmp_matrix_func_t matrix_func = {
188239

189240
const bmp_matrix_func_t *get_matrix_func_user() { return &matrix_func; }
190241

242+
void bmp_enter_sleep() {
243+
scan_mode = SCAN_MODE_DEEP_SLEEP;
244+
BMPAPI->ble.disconnect(1);
245+
}
246+
191247
void bmp_before_sleep() {}
192248

193249
bool checkSafemodeFlag(bmp_api_config_t const *const config) { return false; }

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_8_0
3+
version=0_9_0
44

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

tmk_core/protocol/nrf/bmp.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ void bmp_mode_transition_check(void) {
123123
sleep_enter_counter--;
124124
if (sleep_enter_counter == 0)
125125
{
126-
bmp_before_sleep();
127-
BMPAPI->app.enter_sleep_mode();
126+
bmp_enter_sleep();
128127
}
129128
}
130129

@@ -274,6 +273,7 @@ void bmp_keyboard_task(void)
274273
static uint32_t last_event_time = 0;
275274
uint32_t auto_sleep_timeout = BMPAPI->app.get_config()->reserved[2] * 10 *
276275
60 * 1000; // * 10min * 60s/min * 1000ms/s
276+
277277
if (auto_sleep_timeout && key_event_cnt == 0 && !is_usb_connected()) {
278278
if (timer_elapsed32(last_event_time) > auto_sleep_timeout) {
279279
sleep_enter_counter = 1;
@@ -682,4 +682,9 @@ bool process_record_user_bmp(uint16_t keycode, keyrecord_t* record) {
682682

683683
__attribute__((weak)) uint32_t keymaps_len() { return 0; }
684684

685+
__attribute__((weak)) void bmp_enter_sleep(void) {
686+
bmp_before_sleep();
687+
BMPAPI->app.enter_sleep_mode();
688+
}
689+
685690
__attribute__((weak)) void bmp_before_sleep() {}

tmk_core/protocol/nrf/bmp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ bool is_ble_connected();
3232
bool is_usb_connected();
3333

3434
extern int sleep_enter_counter;
35+
void bmp_enter_sleep();
3536
void bmp_before_sleep();
3637

tmk_core/protocol/nrf/bmp_config.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ bmp_error_t msc_write_callback(const uint8_t *dat, uint32_t len) {
308308
}
309309

310310
bmp_error_t webnus_write_callback(const uint8_t *dat, uint32_t len) {
311+
tfp_printf("[web config]%s\n", dat);
311312
if (strnstr((const char *)dat, "show keymap", len) != NULL) {
312313
BMPAPI->web_config.set_send_buffer(
313314
(uint8_t *)keymap_string,

tmk_core/protocol/nrf/sdk15/apidef.h

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

66
#include "error_def.h"
77

8-
#define API_VERSION 10
8+
#define API_VERSION 11
99
#define CONFIG_VERSION 2
1010
#define PINS_MAX 32
1111

@@ -212,8 +212,8 @@ typedef struct {
212212
} bmp_api_keymap_info_t;
213213

214214
typedef struct {
215-
uint8_t tx_pin;
216-
uint8_t rx_pin;
215+
uint8_t tx_pin;
216+
uint8_t rx_pin;
217217
uint32_t baudrate;
218218
void (*rx_callback)(uint8_t recv);
219219
} bmp_uart_config_t;
@@ -266,8 +266,8 @@ typedef struct {
266266
char (*serial_getc)(void);
267267
void (*serial_puts)(uint8_t const*, uint8_t);
268268
int (*serial_byte_to_read)(void);
269-
void (*set_raw_receive_cb)(void (*raw_receive_cb)(const uint8_t *data, uint8_t length));
270-
int (*raw_send)(const uint8_t *data, uint8_t length);
269+
void (*set_raw_receive_cb)(void (*raw_receive_cb)(const uint8_t* data, uint8_t length));
270+
int (*raw_send)(const uint8_t* data, uint8_t length);
271271
bmp_error_t (*create_file)(const char* sfn, const uint8_t* dat, uint32_t size);
272272
bmp_error_t (*set_msc_write_cb)(bmp_api_msc_write_cb_t);
273273
} bmp_api_usb_t;
@@ -276,6 +276,7 @@ typedef struct {
276276
void (*init)(bmp_api_config_t const* const);
277277
void (*advertise)(uint8_t);
278278
void (*scan)(void);
279+
void (*disconnect)(uint8_t disconect_all);
279280
bmp_error_t (*get_bonding_info)(bmp_api_bonding_info_t* info, uint32_t* len);
280281
void (*delete_bond)(uint8_t);
281282
void (*send_key)(bmp_api_key_report_t*);
@@ -392,6 +393,7 @@ typedef struct {
392393
void (*assign_drive_pin)(uint32_t pin);
393394
void (*sw_read)(uint16_t* res);
394395
void (*clear_drive_pins)(bmp_ecs_config_t const* const config);
396+
void (*shutdown_amp)();
395397
void (*schedule_next_scan)();
396398
void (*discharge_capacitor)();
397399
} bmp_api_ecs_t;

tmk_core/protocol/nrf/sdk15/cli.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ void cli_puts(const char *str);
3535
static MSCMD_USER_RESULT usrcmd_version(MSOPT *msopt, MSCMD_USER_OBJECT usrobj);
3636
static MSCMD_USER_RESULT usrcmd_reset(MSOPT *msopt, MSCMD_USER_OBJECT usrobj);
3737
static MSCMD_USER_RESULT usrcmd_advertise(MSOPT *msopt, MSCMD_USER_OBJECT usrobj);
38+
static MSCMD_USER_RESULT usrcmd_disconnect(MSOPT *msopt, MSCMD_USER_OBJECT usrobj);
3839
static MSCMD_USER_RESULT usrcmd_bootloader(MSOPT *msopt, MSCMD_USER_OBJECT usrobj);
3940
static MSCMD_USER_RESULT usrcmd_bonding_information(MSOPT *msopt, MSCMD_USER_OBJECT usrobj);
4041
static MSCMD_USER_RESULT usrcmd_delete_bonding(MSOPT *msopt, MSCMD_USER_OBJECT usrobj);
@@ -63,6 +64,7 @@ static const MSCMD_COMMAND_TABLE table[] = {
6364
{"version", usrcmd_version, "Show version"},
6465
{"reset", usrcmd_reset, "Reset system"},
6566
{"adv", usrcmd_advertise, "Start advertising"},
67+
{"dis", usrcmd_disconnect, "Disconnect BLE"},
6668
{"dfu", usrcmd_bootloader, "Jump to bootloader"},
6769
{"show", usrcmd_bonding_information, "Show bonded devices"},
6870
{"del", usrcmd_delete_bonding, "Delete bond information"},
@@ -135,6 +137,17 @@ static MSCMD_USER_RESULT usrcmd_advertise(MSOPT *msopt, MSCMD_USER_OBJECT usrobj
135137
return 0;
136138
}
137139

140+
static MSCMD_USER_RESULT usrcmd_disconnect(MSOPT *msopt, MSCMD_USER_OBJECT usrobj) {
141+
if (msopt->argc >= 2) {
142+
print("disconnect all\n");
143+
BMPAPI->ble.disconnect(1);
144+
} else {
145+
print("disconnect device\n");
146+
BMPAPI->ble.disconnect(0);
147+
}
148+
return 0;
149+
}
150+
138151
static MSCMD_USER_RESULT usrcmd_bootloader(MSOPT *msopt, MSCMD_USER_OBJECT usrobj) {
139152
// bootloader_jump();
140153
BMPAPI->bootloader_jump();

0 commit comments

Comments
 (0)