-
Notifications
You must be signed in to change notification settings - Fork 8
/
UnderMeSensi.py
66 lines (51 loc) · 1.77 KB
/
UnderMeSensi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
# Be sure to install python-qtmobility and run on a N900 :)
import sys
from PySide import QtCore, QtGui, QtDeclarative, QtOpenGL
from QtMobility import Sensors
class Listener(QtCore.QObject):
def __init__(self):
QtCore.QObject.__init__(self)
self._initial = True
self._rotation = 0.
self._tilt = 0.
def get_rotation(self):
return self._rotation
def set_rotation(self, rotation):
if self._initial:
self._rotation = rotation
self._initial = False
else:
# Smooth the accelermeter input changes
self._rotation *= .8
self._rotation += .2*rotation
self.on_rotation.emit()
def get_tilt(self):
return self._tilt
def set_tilt(self, tilt):
self._tilt = tilt
self.on_rotation.emit()
on_rotation = QtCore.Signal()
rotation = QtCore.Property(float, get_rotation, set_rotation, \
notify=on_rotation)
tilt = QtCore.Property(float, get_tilt, set_tilt, \
notify=on_rotation)
@QtCore.Slot()
def on_reading_changed(self):
accel = self.sender()
# Scale the x axis reading to keep the image roughly steady
self.rotation = accel.reading().x()*7
self.tilt = (accel.reading().y()-10)*8
app = QtGui.QApplication(sys.argv)
accel = Sensors.QAccelerometer()
listener = Listener()
accel.readingChanged.connect(listener.on_reading_changed)
accel.start()
view = QtDeclarative.QDeclarativeView()
glw = QtOpenGL.QGLWidget()
view.setViewport(glw)
view.setResizeMode(QtDeclarative.QDeclarativeView.SizeRootObjectToView)
view.rootContext().setContextProperty('listener', listener)
view.setSource(__file__.replace('.py', '.qml'))
view.showFullScreen()
app.exec_()