Forráskód Böngészése

Improve boot HID device implementation

Jariullah Safi 4 hónapja
szülő
commit
40850648f0
3 módosított fájl, 57 hozzáadás és 12 törlés
  1. 21 9
      OpenPuck/puck_hid.cpp
  2. 23 2
      OpenPuck/wake_hid.cpp
  3. 13 1
      OpenPuck/wake_hid.h

+ 21 - 9
OpenPuck/puck_hid.cpp

@@ -6,6 +6,7 @@
 #include "rf_link.h"
 #include "triton.h"
 #include "mode_lizard.h"
+#include "wake_hid.h"
 #include <Adafruit_TinyUSB.h>
 #include <Arduino.h>
 #include <string.h>
@@ -447,20 +448,31 @@ static void wakeNudgeTask()
 	} // bus never resumed -> drop the nudge
 	if (USBDevice.suspended())
 		return; // wait for resume; reports can't cross a suspended bus
-	if (g_connSlot < 0 || g_connSlot >= NSLOT || !hid[g_connSlot].ready())
+	// gate on the interface we'll actually send on: boot mouse normally, slot HID only on the debug-CDC fallback
+	if (wakeHidPresent()) {
+		if (!wakeHidReady())
+			return;
+	} else if (g_connSlot < 0 || g_connSlot >= NSLOT ||
+		   !hid[g_connSlot].ready())
 		return;
 	static unsigned long stepMs = 0;
 	if (millis() - stepMs < 15)
 		return; // pace the edges
 	stepMs = millis();
-	hid_mouse_report_t m;
-	m.buttons = 0;
-	m.x = (g_nudgeStep == 1) ? NUDGE_JIGGLE_PX : -NUDGE_JIGGLE_PX;
-	m.y = 0;
-	m.wheel = 0;
-	m.pan = 0;
-	hid[g_connSlot].sendReport(0x40, &m,
-				   sizeof m); // jiggle right, then back
+	int8_t dx = (g_nudgeStep == 1) ? NUDGE_JIGGLE_PX : -NUDGE_JIGGLE_PX;
+	// Ride the boot mouse -- the interface the host armed as the wake source. Only fall back to the gamepad slot
+	// on the debug-CDC boot, where the wake mouse isn't registered (and the host can't wake from it anyway).
+	if (wakeHidPresent())
+		wakeHidMove(dx, 0); // jiggle right, then back
+	else {
+		hid_mouse_report_t m;
+		m.buttons = 0;
+		m.x = dx;
+		m.y = 0;
+		m.wheel = 0;
+		m.pan = 0;
+		hid[g_connSlot].sendReport(0x40, &m, sizeof m);
+	}
 	g_nudgeStep = (g_nudgeStep >= 2) ? 0 : (uint8_t)(g_nudgeStep + 1);
 }
 

+ 23 - 2
OpenPuck/wake_hid.cpp

@@ -1,11 +1,13 @@
 #include "wake_hid.h"
 #include <Adafruit_TinyUSB.h>
 
-// Boot MOUSE descriptor -- proven to enumerate and wake Windows. We never send a report on it; it exists so the
-// host enumerates a "HID-compliant mouse" child and grants the device wake-from-sleep privileges. (A boot
+// Boot MOUSE descriptor -- proven to enumerate and wake Windows. The host enumerates a "HID-compliant mouse"
+// child and arms IT as the wake source, so the wake nudge (wakeHidMove, driven from the puck wake path) must
+// ride THIS interface -- a report on the gamepad slot lands on an interface the host never allow-listed. (A boot
 // keyboard didn't enumerate on Windows and suppressed the wake the mouse class provides.)
 static const uint8_t WAKE_HID_DESC[] = { TUD_HID_REPORT_DESC_MOUSE() };
 static Adafruit_USBD_HID g_wakeHid;
+static bool g_wakeHidPresent = false;
 
 void wakeHidBegin()
 {
@@ -15,4 +17,23 @@ void wakeHidBegin()
 	g_wakeHid.setReportDescriptor(WAKE_HID_DESC, sizeof WAKE_HID_DESC);
 	g_wakeHid.setPollInterval(10);
 	g_wakeHid.begin();
+	g_wakeHidPresent = true;
+}
+
+bool wakeHidPresent()
+{
+	return g_wakeHidPresent;
+}
+
+bool wakeHidReady()
+{
+	return g_wakeHidPresent && g_wakeHid.ready();
+}
+
+bool wakeHidMove(int8_t dx, int8_t dy)
+{
+	if (!wakeHidReady())
+		return false;
+	// boot mouse descriptor has no report ID -> report_id 0; buttons=0 so we move but never click
+	return g_wakeHid.mouseReport(0, 0, dx, dy, 0, 0);
 }

+ 13 - 1
OpenPuck/wake_hid.h

@@ -14,5 +14,17 @@
 // CDC and skips this interface (no endpoint room for both) -- see config.h (g_debugCdcThisBoot).
 #pragma once
 
-// register the boot-keyboard wake interface (call from setup() for clean modes)
+#include <stdint.h>
+
+// register the boot-mouse wake interface (call from setup() for clean modes + normal puck boot)
 void wakeHidBegin();
+
+// true once wakeHidBegin() has registered the boot mouse this boot (false on the debug-CDC boot, which omits it)
+bool wakeHidPresent();
+
+// boot mouse enumerated and ready to accept a report (false while the bus is suspended/unconfigured)
+bool wakeHidReady();
+
+// send one boot-mouse movement report (buttons=0). This is the input that actually wakes the host: it rides the
+// interface Windows armed as the wake source, unlike a gamepad-slot report. Returns false if not ready.
+bool wakeHidMove(int8_t dx, int8_t dy);