How can I read the accelerometer in my windows tablet with python?
Question: 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?


Answer: Horrible hack - start up a webserver in `server.py`:

    
    
    import bottle
    from threading import Thread
    
    on_data = lambda alpha, beta, gamma: None
    
    @bottle.route('/')
    def handler():
        return bottle.static_file('index.html', '.')
    
    @bottle.post('/data')
    def handler():
        on_data(**bottle.request.json)
    
    def data_handler(f):
        global on_data
        on_data = f
        return f
    
    def go():
        t = Thread(target=lambda: bottle.run(quiet=True))
        t.start()
    

With this `index.html`:

    
    
    <script>
      window.addEventListener('deviceorientation', function(eventData) {
        var d = {};
        ['alpha', 'beta', 'gamma'].forEach(function(prop) {
          d[prop] = eventData[prop];
        })
        var xhr = new XMLHttpRequest();
        xhr.open("POST", "http://localhost:8080/data");
        xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
        xhr.send(JSON.stringify(d));
      }, false);
    </script>
    

And use it as:

    
    
    import server
    
    @server.data_hander
    def on_acc_data(alpha, beta, gamma):
        print alpha, beta, gamma
    
    server.go()
    

After having opened `localhost:8080/` in the browser

