[英]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. 我的平板电脑里有一个加速计,我可以在javascript中读取。
How can I access this data in python? 如何在python中访问这些数据? Is there some ctypes trickery I can use to call a windows 8 Sensor API function?
是否有一些ctypes技巧我可以用来调用Windows 8传感器API函数?
Horrible hack - start up a webserver in server.py
: 可怕的黑客 - 在
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
: 使用此
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 在浏览器中打开
localhost:8080/
之后
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.