Search code examples
pythonwindowsaccelerometer

How can I read the accelerometer in my windows tablet with python?


I have an accelerometer in my tablet, that I can read from within javascript.

How can I access this data in python? Is there some ctypes trickery I can use to call a windows 8 Sensor API function?


Solution

  • I know this is old but I just found out that you can use winsdk pip install winsdk (community maintained version of winrt)


    Here's the documentation for the accelerometer class under the windows runtime api https://learn.microsoft.com/en-us/uwp/api/windows.devices.sensors.accelerometer?view=winrt-22621


    Basic script for reading out the accelerometer data
    import time
    
    import winsdk.windows.devices.sensors
    import winsdk.windows.devices.sensors as sensors
    
    accel = sensors.Accelerometer.get_default()
    
    
    def reading_changed_handler(accel, args: winsdk.windows.devices.sensors.AccelerometerReadingChangedEventArgs):
        reading = args.reading
        print(reading.acceleration_x, reading.acceleration_y, reading.acceleration_z)
    
    
    accel.add_reading_changed(reading_changed_handler)
    
    
    while True:
        time.sleep(1)