#include "tap.h" #ifdef FEATURE_TAP_DETECTION #include "imu.h" #include extern BLEHidAdafruit blehid; // ─── Tap detection setup ────────────────────────────────────────────────────── void setupTapDetection() { imuWriteReg(REG_CTRL1_XL, 0x60); // ODR=416Hz, FS=±2g imuWriteReg(REG_TAP_CFG, 0x8E); // INT_EN + LIR + TAP_Z/Y/X imuWriteReg(REG_TAP_THS_6D, 0x0C); // threshold 750 mg (was 500 mg — too easy to false-trigger) imuWriteReg(REG_INT_DUR2, 0x7A); // DUR=7(538ms), QUIET=2(19ms), SHOCK=2(38ms) imuWriteReg(REG_WAKE_UP_THS, 0x80); // enable double-tap imuWriteReg(REG_MD1_CFG, 0x48); // route taps to INT1 Serial.println("[TAP] Engine configured — single=LEFT, double=RIGHT"); } // ─── Tap processing ─────────────────────────────────────────────────────────── void processTaps(unsigned long now) { // Release held button after CLICK_HOLD_MS if (clickButtonDown && (now - clickDownMs >= CLICK_HOLD_MS)) { blehid.mouseButtonPress(clickButton, false); clickButtonDown = false; clickButton = 0; } if (clickButtonDown) return; // Don't start a new click while one is held // The LSM6DS3 (with D_TAP_EN) already disambiguates at hardware level: // SINGLE_TAP is only set after the DUR window expires with no second tap. // DOUBLE_TAP is set immediately when the second tap arrives within DUR. // We trust this directly — no software delay needed. uint8_t tapSrc = imuReadReg(REG_TAP_SRC); if (!(tapSrc & 0x40)) return; // TAP_IA not set — no event if (tapSrc & 0x10) { // DOUBLE_TAP → right click Serial.println("[TAP] Double → RIGHT"); blehid.mouseButtonPress(MOUSE_BUTTON_RIGHT, true); clickButton = MOUSE_BUTTON_RIGHT; clickButtonDown = true; clickDownMs = now; statRightClicks++; } else if (tapSrc & 0x20) { // SINGLE_TAP → left click Serial.println("[TAP] Single → LEFT"); blehid.mouseButtonPress(MOUSE_BUTTON_LEFT, true); clickButton = MOUSE_BUTTON_LEFT; clickButtonDown = true; clickDownMs = now; statLeftClicks++; } } #endif // FEATURE_TAP_DETECTION