diff options
Diffstat (limited to 'common/motion_lid.c')
-rw-r--r-- | common/motion_lid.c | 42 |
1 files changed, 35 insertions, 7 deletions
diff --git a/common/motion_lid.c b/common/motion_lid.c index 3209492a65..f2c6863b80 100644 --- a/common/motion_lid.c +++ b/common/motion_lid.c @@ -58,6 +58,16 @@ #define TABLET_ZONE_LID_ANGLE FLOAT_TO_FP(300) #define LAPTOP_ZONE_LID_ANGLE FLOAT_TO_FP(240) +/* + * We will change our tablet mode status when we are "convinced" that it has + * changed. This means we will have to consecutively calculate our new tablet + * mode while the angle is stable and come to the same conclusion. The number + * of consecutive calculations is the debounce count with an interval between + * readings set by the motion_sense task. This should avoid spurious forces + * that may trigger false transitions of the tablet mode switch. + */ +#define TABLET_MODE_DEBOUNCE_COUNT 3 +static int tablet_mode_debounce_cnt = TABLET_MODE_DEBOUNCE_COUNT; #endif #ifdef CONFIG_LID_ANGLE_INVALID_CHECK @@ -230,13 +240,31 @@ static int calculate_lid_angle(const vector_3_t base, const vector_3_t lid, *lid_angle = FP_TO_INT(last_lid_angle_fp + FLOAT_TO_FP(0.5)); #ifdef CONFIG_LID_ANGLE_TABLET_MODE - if (last_lid_angle_fp > TABLET_ZONE_LID_ANGLE) - new_tablet_mode = 1; - else if (last_lid_angle_fp < LAPTOP_ZONE_LID_ANGLE) - new_tablet_mode = 0; - if (tablet_get_mode() != new_tablet_mode) { - tablet_set_mode(new_tablet_mode); - hook_notify(HOOK_TABLET_MODE_CHANGE); + if (reliable) { + if (last_lid_angle_fp > TABLET_ZONE_LID_ANGLE) + new_tablet_mode = 1; + else if (last_lid_angle_fp < LAPTOP_ZONE_LID_ANGLE) + new_tablet_mode = 0; + + /* Only change tablet mode if we're sure. */ + if (tablet_get_mode() != new_tablet_mode) { + if (tablet_mode_debounce_cnt == 0) { + /* Alright, we're convinced. */ + tablet_mode_debounce_cnt = + TABLET_MODE_DEBOUNCE_COUNT; + tablet_set_mode(new_tablet_mode); + hook_notify(HOOK_TABLET_MODE_CHANGE); + return reliable; + } + tablet_mode_debounce_cnt--; + return reliable; + } + + /* + * Since it hasn't changed, clear any pending tablet mode + * change. + */ + tablet_mode_debounce_cnt = TABLET_MODE_DEBOUNCE_COUNT; } #endif /* CONFIG_LID_ANGLE_TABLET_MODE */ #else /* CONFIG_LID_ANGLE_INVALID_CHECK */ |