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

I have an accelerometer on my tablet that I can read from javascript.

How can I access this data in python? Is there any ctypes trick that I can use to call a Windows 8 API function?

+4
source share
1 answer

Awful hack - run the web server 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 like:

import server

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

server.go()

Opening localhost:8080/in a browser

0
source

All Articles