5 Commits
1.0 ... 1.0.2

Author SHA1 Message Date
samhenrigold
d31e8e49e0 Update project.pbxproj 2025-09-06 18:34:57 -04:00
samhenrigold
f608105ce7 fix my screwed up sensor data parsing 2025-09-06 18:34:06 -04:00
samhenrigold
d590174cdd Update project.pbxproj 2025-09-06 18:12:29 -04:00
samhenrigold
82189fa0b3 More narrow device matching to avoid monitoring prompt 2025-09-06 18:08:35 -04:00
samhenrigold
1ee1d846db Update README.md 2025-09-06 16:37:07 -04:00
4 changed files with 42 additions and 30 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/build

View File

@@ -268,7 +268,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 11.5;
MARKETING_VERSION = 1.0;
MARKETING_VERSION = 1.0.2;
PRODUCT_BUNDLE_IDENTIFIER = gold.samhenri.LidAngleSensor;
PRODUCT_NAME = "$(TARGET_NAME)";
REGISTER_APP_GROUPS = YES;
@@ -310,7 +310,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 11.5;
MARKETING_VERSION = 1.0;
MARKETING_VERSION = 1.0.2;
PRODUCT_BUNDLE_IDENTIFIER = gold.samhenri.LidAngleSensor;
PRODUCT_NAME = "$(TARGET_NAME)";
REGISTER_APP_GROUPS = YES;

View File

@@ -48,11 +48,13 @@
return NULL;
}
// Use Generic Desktop + Mouse criteria to find candidate devices
// This broader search allows us to find the lid sensor among other HID devices
// Match specifically for the lid angle sensor to avoid permission prompts
// Target: Sensor page (0x0020), Orientation usage (0x008A)
NSDictionary *matchingDict = @{
@"UsagePage": @(0x0001), // Generic Desktop
@"Usage": @(0x0003), // Mouse
@"VendorID": @(0x05AC), // Apple
@"ProductID": @(0x8104), // Specific product
@"UsagePage": @(0x0020), // Sensor page
@"Usage": @(0x008A), // Orientation usage
};
IOHIDManagerSetDeviceMatching(manager, (__bridge CFDictionaryRef)matchingDict);
@@ -60,33 +62,38 @@
IOHIDDeviceRef device = NULL;
if (devices && CFSetGetCount(devices) > 0) {
NSLog(@"[LidAngleSensor] Found %ld devices, looking for sensor...", CFSetGetCount(devices));
NSLog(@"[LidAngleSensor] Found %ld matching lid angle sensor device(s)", CFSetGetCount(devices));
const void **deviceArray = malloc(sizeof(void*) * CFSetGetCount(devices));
CFSetGetValues(devices, deviceArray);
// Search for the specific lid angle sensor device
// Discovered through reverse engineering: Apple device with Sensor page
// Test each matching device to find the one that actually works
for (CFIndex i = 0; i < CFSetGetCount(devices); i++) {
IOHIDDeviceRef currentDevice = (IOHIDDeviceRef)deviceArray[i];
IOHIDDeviceRef testDevice = (IOHIDDeviceRef)deviceArray[i];
CFNumberRef vendorID = IOHIDDeviceGetProperty(currentDevice, CFSTR("VendorID"));
CFNumberRef productID = IOHIDDeviceGetProperty(currentDevice, CFSTR("ProductID"));
CFNumberRef usagePage = IOHIDDeviceGetProperty(currentDevice, CFSTR("PrimaryUsagePage"));
CFNumberRef usage = IOHIDDeviceGetProperty(currentDevice, CFSTR("PrimaryUsage"));
// Try to open and read from this device
if (IOHIDDeviceOpen(testDevice, kIOHIDOptionsTypeNone) == kIOReturnSuccess) {
uint8_t testReport[8] = {0};
CFIndex reportLength = sizeof(testReport);
int vid = 0, pid = 0, up = 0, u = 0;
if (vendorID) CFNumberGetValue(vendorID, kCFNumberIntType, &vid);
if (productID) CFNumberGetValue(productID, kCFNumberIntType, &pid);
if (usagePage) CFNumberGetValue(usagePage, kCFNumberIntType, &up);
if (usage) CFNumberGetValue(usage, kCFNumberIntType, &u);
IOReturn result = IOHIDDeviceGetReport(testDevice,
kIOHIDReportTypeFeature,
1,
testReport,
&reportLength);
// Target the specific lid angle sensor device
// VID=0x05AC (Apple), PID=0x8104, UsagePage=0x0020 (Sensor), Usage=0x008A (Orientation)
if (vid == 0x05AC && pid == 0x8104 && up == 0x0020 && u == 0x008A) {
device = (IOHIDDeviceRef)CFRetain(currentDevice);
NSLog(@"[LidAngleSensor] Found lid angle sensor device: VID=0x%04X, PID=0x%04X", vid, pid);
if (result == kIOReturnSuccess && reportLength >= 3) {
// This device works! Use it.
device = (IOHIDDeviceRef)CFRetain(testDevice);
NSLog(@"[LidAngleSensor] Successfully found working lid angle sensor device (index %ld)", i);
IOHIDDeviceClose(testDevice, kIOHIDOptionsTypeNone); // Close for now, will reopen in init
break;
} else {
NSLog(@"[LidAngleSensor] Device %ld failed to read (result: %d, length: %ld)", i, result, reportLength);
IOHIDDeviceClose(testDevice, kIOHIDOptionsTypeNone);
}
} else {
NSLog(@"[LidAngleSensor] Failed to open device %ld", i);
}
}
@@ -119,9 +126,9 @@
if (result == kIOReturnSuccess && reportLength >= 3) {
// Data format: [report_id, angle_low, angle_high]
// Example: [01 72 00] = 0x7201 centidegrees = 291.85 degrees
uint16_t rawValue = *(uint16_t*)(report);
double angle = rawValue * 0.01; // Convert centidegrees to degrees
// Parse the 16-bit value from bytes 1-2 (skipping report ID)
uint16_t rawValue = (report[2] << 8) | report[1]; // High byte, low byte
double angle = (double)rawValue; // Raw value is already in degrees
return angle;
}

View File

@@ -33,3 +33,7 @@ I guess.
**How come the audio feels kind of...weird?**
I'm bad at audio.
**Where did the sound effect come from?**
LEGO Batman 3: Beyond Gotham. But you knew that already.