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};
3536const uint8_t col_pins [] = {15 , 14 , 13 };
3637const 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
4459bool bmp_config_overwrite (bmp_api_config_t const * const config_on_storage ,
@@ -92,7 +107,30 @@ uint16_t sw_read[32] = {0};
92107uint32_t ecs_get_device_row () { return MATRIX_ROWS_DEFAULT ; }
93108uint32_t ecs_get_device_col () { return MATRIX_COLS_DEFAULT ; }
94109uint32_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
189240const 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+
191247void bmp_before_sleep () {}
192248
193249bool checkSafemodeFlag (bmp_api_config_t const * const config ) { return false; }
0 commit comments