From 76fb54403ccd3ebaf1ca49c5172335e3593c5c5c Mon Sep 17 00:00:00 2001 From: Purdea Andrei Date: Tue, 2 Nov 2021 07:54:29 +0200 Subject: haptic: Feature to disable it when usb port is not configured or suspended. (#12692) This also add support for specifying a LED pin to indicate haptic status, and also adds support for a haptic-enable pin, which is useful to turn off the boost converter on the solenoid driver. --- quantum/haptic.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) (limited to 'quantum/haptic.c') diff --git a/quantum/haptic.c b/quantum/haptic.c index 65abcc15fa..f915acf946 100644 --- a/quantum/haptic.c +++ b/quantum/haptic.c @@ -17,6 +17,8 @@ #include "haptic.h" #include "eeconfig.h" #include "debug.h" +#include "usb_device_state.h" +#include "gpio.h" #ifdef DRV2605L # include "DRV2605L.h" #endif @@ -26,6 +28,29 @@ haptic_config_t haptic_config; +static void update_haptic_enable_gpios(void) { + if (haptic_config.enable && ((!HAPTIC_OFF_IN_LOW_POWER) || (usb_device_state == USB_DEVICE_STATE_CONFIGURED))) { +#if defined(HAPTIC_ENABLE_PIN) + HAPTIC_ENABLE_PIN_WRITE_ACTIVE(); +#endif +#if defined(HAPTIC_ENABLE_STATUS_LED) + HAPTIC_ENABLE_STATUS_LED_WRITE_ACTIVE(); +#endif + } else { +#if defined(HAPTIC_ENABLE_PIN) + HAPTIC_ENABLE_PIN_WRITE_INACTIVE(); +#endif +#if defined(HAPTIC_ENABLE_STATUS_LED) + HAPTIC_ENABLE_STATUS_LED_WRITE_INACTIVE(); +#endif + } +} + +static void set_haptic_config_enable(bool enabled) { + haptic_config.enable = enabled; + update_haptic_enable_gpios(); +} + void haptic_init(void) { if (!eeconfig_is_enabled()) { eeconfig_init(); @@ -44,6 +69,10 @@ void haptic_init(void) { // or the previous firmware didn't have solenoid enabled, // and the current one has solenoid enabled. haptic_reset(); + } else { + // Haptic configuration has been loaded through the "raw" union item. + // This is to execute any side effects of the configuration. + set_haptic_config_enable(haptic_config.enable); } #ifdef SOLENOID_ENABLE solenoid_setup(); @@ -54,6 +83,12 @@ void haptic_init(void) { dprintf("DRV2605 driver initialized\n"); #endif eeconfig_debug_haptic(); +#ifdef HAPTIC_ENABLE_PIN + setPinOutput(HAPTIC_ENABLE_PIN); +#endif +#ifdef HAPTIC_ENABLE_STATUS_LED + setPinOutput(HAPTIC_ENABLE_STATUS_LED); +#endif } void haptic_task(void) { @@ -69,13 +104,13 @@ void eeconfig_debug_haptic(void) { } void haptic_enable(void) { - haptic_config.enable = 1; + set_haptic_config_enable(true); xprintf("haptic_config.enable = %u\n", haptic_config.enable); eeconfig_update_haptic(haptic_config.raw); } void haptic_disable(void) { - haptic_config.enable = 0; + set_haptic_config_enable(false); xprintf("haptic_config.enable = %u\n", haptic_config.enable); eeconfig_update_haptic(haptic_config.raw); } @@ -157,7 +192,7 @@ void haptic_dwell_decrease(void) { } void haptic_reset(void) { - haptic_config.enable = true; + set_haptic_config_enable(true); uint8_t feedback = HAPTIC_FEEDBACK_DEFAULT; haptic_config.feedback = feedback; #ifdef DRV2605L @@ -293,3 +328,13 @@ void haptic_shutdown(void) { solenoid_shutdown(); #endif } + +void haptic_notify_usb_device_state_change(void) { + update_haptic_enable_gpios(); +#if defined(HAPTIC_ENABLE_PIN) + setPinOutput(HAPTIC_ENABLE_PIN); +#endif +#if defined(HAPTIC_ENABLE_STATUS_LED) + setPinOutput(HAPTIC_ENABLE_STATUS_LED); +#endif +} -- cgit v1.2.3