summaryrefslogtreecommitdiff
path: root/external/plyer/platforms/linux/accelerometer.py
blob: 7272c3391bd19a5a6c4b5c9603b8deeec8b2503f (plain) (blame)
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
'''
Linux accelerometer
---------------------
'''

from plyer.facades import Accelerometer
import os
import glob
import re


class LinuxAccelerometer(Accelerometer):

    def _enable(self):
        pass

    def _disable(self):
        pass

    def _get_acceleration(self):
        try:
            pos = glob.glob("/sys/devices/platform/*/position")[0]
        except IndexError:
            raise Exception('Could not enable accelerometer!')

        with open(pos, "r") as p:
            t = p.read()
            coords = re.findall(r"[-]?\d+\.?\d*", t)
            # Apparently the acceleration on sysfs goes from -1000 to 1000.
            # I divide it by 100 to make it equivalent to Android.
            # The negative is because the coordinates are inverted on Linux
            return [float(i) / -100 for i in coords]


def instance():
    return LinuxAccelerometer()