简体   繁体   English

如何使用python读取我的Windows平板电脑中的加速度计?

[英]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.

相关问题 如何通过 Python 访问数位板笔数据? - How can I access tablet pen data via Python? 如何在 Windows 上用 Python 读取系统信息? - How can I read system information in Python on Windows? 如何在 Windows 中读取 Python 中另一个进程的内存? - How can I read the memory of another process in Python in Windows? Python:如何读取 csv 并在循环中清理我的数据 - Python: How can I read csv and clean my data in a loop 如何在Windows上将Anaconda3 python设置为我的默认设置 - How can I set Anaconda3 python my default on windows 如何在Cygwin上使用Windows Python环境? - How can I use my windows Python environment on Cygwin? Python 无法读取我的 Python 项目中的文件。 我该如何解决? - Python can't read a file in my python project. How can I solve it? 如何读取我的 exel 表中的特定数据并从读取的每个数据集创建一个图? (Python) - How can I read specific data in my exel sheet and create a plot from each dataset that is read? (Python) 如何在Windows和Python 3.7+中的asyncio StreamReader中检查是否要读取某些内容? - How can I check if something to read in asyncio StreamReader in Windows and Python 3.7+? 如何使用python从远程计算机读取Windows事件日志? - How can I use python to read windows eventlog from a remote machine?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM