Files
air-mouse/source/buttons.cpp
2026-03-19 20:18:17 +01:00

53 lines
1.9 KiB
C++

#include "buttons.h"
#ifdef FEATURE_PHYSICAL_BUTTONS
#include <bluefruit.h>
extern BLEHidAdafruit blehid;
static uint8_t physBtnMask = 0; // bitmask of currently-pressed physical buttons
// Setup
void setupPhysicalButtons() {
// Release any held physical buttons before reconfiguring
if (physBtnMask && Bluefruit.connected()) { blehid.mouseButtonRelease(); }
physBtnMask = 0;
if (BTN_LEFT_PIN != BTN_PIN_NONE) pinMode(BTN_LEFT_PIN, INPUT_PULLUP);
if (BTN_RIGHT_PIN != BTN_PIN_NONE) pinMode(BTN_RIGHT_PIN, INPUT_PULLUP);
if (BTN_MIDDLE_PIN != BTN_PIN_NONE) pinMode(BTN_MIDDLE_PIN, INPUT_PULLUP);
bool any = (BTN_LEFT_PIN != BTN_PIN_NONE) || (BTN_RIGHT_PIN != BTN_PIN_NONE)
|| (BTN_MIDDLE_PIN != BTN_PIN_NONE);
if (any) {
Serial.print("[BTN] L=");
BTN_LEFT_PIN == BTN_PIN_NONE ? Serial.print("--") : Serial.print(BTN_LEFT_PIN);
Serial.print(" R=");
BTN_RIGHT_PIN == BTN_PIN_NONE ? Serial.print("--") : Serial.print(BTN_RIGHT_PIN);
Serial.print(" M=");
BTN_MIDDLE_PIN == BTN_PIN_NONE ? Serial.print("--") : Serial.print(BTN_MIDDLE_PIN);
Serial.println();
}
}
// Poll and report
// Called every loop iteration (before rate limiter) for immediate response.
// Uses active-low logic: INPUT_PULLUP, button connects pin to GND.
void processPhysicalButtons() {
if (!Bluefruit.connected()) return;
uint8_t newMask = 0;
if (BTN_LEFT_PIN != BTN_PIN_NONE && digitalRead(BTN_LEFT_PIN) == LOW) newMask |= MOUSE_BUTTON_LEFT;
if (BTN_RIGHT_PIN != BTN_PIN_NONE && digitalRead(BTN_RIGHT_PIN) == LOW) newMask |= MOUSE_BUTTON_RIGHT;
if (BTN_MIDDLE_PIN != BTN_PIN_NONE && digitalRead(BTN_MIDDLE_PIN) == LOW) newMask |= MOUSE_BUTTON_MIDDLE;
if (newMask != physBtnMask) {
physBtnMask = newMask;
if (physBtnMask) blehid.mouseButtonPress(physBtnMask);
else blehid.mouseButtonRelease();
Serial.print("[BTN] mask=0x"); Serial.println(physBtnMask, HEX);
}
}
#endif // FEATURE_PHYSICAL_BUTTONS