This repository has been archived by the owner on Jan 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accelerometer.py
57 lines (45 loc) · 1.55 KB
/
accelerometer.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
import threading
import time
import math
from plyer import accelerometer
from kivy.clock import Clock, mainthread
FREQUENCY = 1.0 / 1
class Accelerometer:
stop = threading.Event()
def __init__(self):
self._delegate = None
def configure_with(self, delegate):
self._delegate = delegate
def start(self):
try:
accelerometer.enable()
self.start_second_thread()
except Exception as e:
self._delegate.update("accelerometer.start: %s" % e)
@mainthread
def update(self, msg):
self._delegate.update(msg)
def start_second_thread(self):
try:
threading.Thread(target=self.second_thread, name="Little Sparrow").start()
except Exception as e:
self._delegate.update("accelerometer.start_second_thread: %s" % e)
def second_thread(self):
try:
while True:
if self.stop.is_set():
break
msg = Accelerometer.accelerometer_representation()
self.update(msg)
time.sleep(1.0 / 24)
except Exception as e:
self.update("accelerometer.second_thread: %s" % e)
@staticmethod
def accelerometer_representation():
x = float(accelerometer.acceleration[0] or 0)
y = float(accelerometer.acceleration[1] or 0)
z = float(accelerometer.acceleration[2] or 0)
# return math.arcsin(z / (pow(pow(z, 2) + pow(y, 2) + pow(x, 2)), 0.5))
if x == 0:
x = 0.0001
return math.atan(y / x)