A Python library for reading MacBook lid angle sensor data on macOS. This library provides real-time access to the built-in lid angle sensor available on modern MacBooks.
Install from PyPI:
pip install pybooklid
Or install with audio features for creak sound generation:
pip install pybooklid[audio]
The library automatically handles the required DYLD_LIBRARY_PATH setup for hidapi on macOS.
git clone <repository-url>
cd pybooklid
pip install -e .
# Or with audio features
pip install -e .[audio]
from pybooklid import read_lid_angle
angle = read_lid_angle()
if angle is not None:
print(f"Current lid angle: {angle:.1f}°")
else:
print("Sensor not available")
from pybooklid import LidSensor
with LidSensor() as sensor:
for angle in sensor.monitor(interval=0.1):
print(f"Angle: {angle:.1f}°")
if angle < 10: # Nearly closed
break
from pybooklid import LidSensor
# Manual connection management
sensor = LidSensor(auto_connect=False)
try:
sensor.connect()
# Wait for significant movement
new_angle = sensor.wait_for_change(threshold=5.0, timeout=10.0)
if new_angle:
print(f"Lid moved to {new_angle:.1f}°")
# Monitor with callback
def on_angle_change(angle):
print(f"Callback: {angle:.1f}°")
for angle in sensor.monitor(callback=on_angle_change, max_duration=30):
# Process angle data
pass
finally:
sensor.disconnect()
LidSensor ClassThe main sensor interface class.
__init__(auto_connect=True) - Initialize sensor, optionally auto-connectingconnect() - Connect to the lid angle sensordisconnect() - Disconnect from the sensoris_connected() - Check connection statusread_angle() - Get current angle in degrees (0-180°)monitor(interval=0.1, callback=None, max_duration=None) - Continuous monitoring iteratorwait_for_change(threshold=1.0, timeout=None) - Wait for angle to changeread_lid_angle() - Quick one-shot readingis_sensor_available() - Check if sensor is availableAfter installation, you can use the command line tools:
# Run basic sensor demo
pybooklid-demo
# Run advanced monitoring app
pybooklid-monitor
The lid angle sensor reports values in degrees:
The sensor uses Apple's HID interface:
The library includes comprehensive error handling:
LidSensorError - Base exception for sensor-related errorsTested on:
Should work on most modern MacBooks with lid angle sensors.
examples/simple_usage.py)Demonstrates basic sensor usage patterns and gesture detection.
examples/monitor_app.py)Full-featured monitoring application with statistics, logging, and CSV export.
sudo if permission issues occurpip show hidapibrew install hidapiBased on reverse engineering of the IOKit HID interface used by Apple's internal sensor framework.
This library was made possible by the reverse engineering work done by Sam Henri Gold in the LidAngleSensor project. The key insights about the HID Feature Reports and data format were discovered through that original research.
[report_id, angle_low_byte, angle_high_byte]MIT License - see LICENSE file for details.
Contributions welcome! Please test on different MacBook models and report compatibility.
If this project saved you time, you can support my work via PayPal.
5 commits
Python
100.0%
A Python library for reading MacBook lid angle sensor data on macOS. This library provides real-time access to the built-in lid angle sensor available on modern MacBooks.
Install from PyPI:
pip install pybooklid
Or install with audio features for creak sound generation:
pip install pybooklid[audio]
The library automatically handles the required DYLD_LIBRARY_PATH setup for hidapi on macOS.
git clone <repository-url>
cd pybooklid
pip install -e .
# Or with audio features
pip install -e .[audio]
from pybooklid import read_lid_angle
angle = read_lid_angle()
if angle is not None:
print(f"Current lid angle: {angle:.1f}°")
else:
print("Sensor not available")
from pybooklid import LidSensor
with LidSensor() as sensor:
for angle in sensor.monitor(interval=0.1):
print(f"Angle: {angle:.1f}°")
if angle < 10: # Nearly closed
break
from pybooklid import LidSensor
# Manual connection management
sensor = LidSensor(auto_connect=False)
try:
sensor.connect()
# Wait for significant movement
new_angle = sensor.wait_for_change(threshold=5.0, timeout=10.0)
if new_angle:
print(f"Lid moved to {new_angle:.1f}°")
# Monitor with callback
def on_angle_change(angle):
print(f"Callback: {angle:.1f}°")
for angle in sensor.monitor(callback=on_angle_change, max_duration=30):
# Process angle data
pass
finally:
sensor.disconnect()
LidSensor ClassThe main sensor interface class.
__init__(auto_connect=True) - Initialize sensor, optionally auto-connectingconnect() - Connect to the lid angle sensordisconnect() - Disconnect from the sensoris_connected() - Check connection statusread_angle() - Get current angle in degrees (0-180°)monitor(interval=0.1, callback=None, max_duration=None) - Continuous monitoring iteratorwait_for_change(threshold=1.0, timeout=None) - Wait for angle to changeread_lid_angle() - Quick one-shot readingis_sensor_available() - Check if sensor is availableAfter installation, you can use the command line tools:
# Run basic sensor demo
pybooklid-demo
# Run advanced monitoring app
pybooklid-monitor
The lid angle sensor reports values in degrees:
The sensor uses Apple's HID interface:
The library includes comprehensive error handling:
LidSensorError - Base exception for sensor-related errorsTested on:
Should work on most modern MacBooks with lid angle sensors.
examples/simple_usage.py)Demonstrates basic sensor usage patterns and gesture detection.
examples/monitor_app.py)Full-featured monitoring application with statistics, logging, and CSV export.
sudo if permission issues occurpip show hidapibrew install hidapiBased on reverse engineering of the IOKit HID interface used by Apple's internal sensor framework.
This library was made possible by the reverse engineering work done by Sam Henri Gold in the LidAngleSensor project. The key insights about the HID Feature Reports and data format were discovered through that original research.
[report_id, angle_low_byte, angle_high_byte]MIT License - see LICENSE file for details.
Contributions welcome! Please test on different MacBook models and report compatibility.
If this project saved you time, you can support my work via PayPal.
5 commits
Python
100.0%