Web · Wiki · Activities · Blog · Lists · Chat · Meeting · Bugs · Git · Translate · Archive · People · Donate
1
#!/usr/bin/env python
2
#Copyright (c) 2011 Walter Bender
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18
import os
19
20
from gettext import gettext as _
21
22
from plugins.plugin import Plugin
23
24
from TurtleArt.tapalette import make_palette
25
from TurtleArt.tautils import debug_output
26
from TurtleArt.taprimitive import Primitive
27
28
import logging
29
_logger = logging.getLogger('turtleart-activity accelerometer plugin')
30
31
32
ACCELEROMETER_DEVICE = '/sys/devices/platform/lis3lv02d/position'
33
34
35
class Accelerometer(Plugin):
36
37
    def __init__(self, parent):
38
        Plugin.__init__(self)
39
        self._parent = parent
40
        if os.path.exists(ACCELEROMETER_DEVICE):
41
            self._status = True
42
        else:
43
            self._status = False
44
        self.running_sugar = self._parent.running_sugar
45
46
    def setup(self):
47
        # set up accelerometer specific blocks
48
        palette = make_palette('sensor',
49
                               colors=["#FF6060", "#A06060"],
50
                               help_string=_('Palette of sensor blocks'),
51
                               position=6)
52
53
        if self._status:
54
            palette.add_block('xyz',
55
                              style='basic-style-extended-vertical',
56
                              label=_('acceleration'),
57
                              help_string=\
58
                                  _('push acceleration in x, y, z to heap'),
59
                              prim_name='xyz')
60
        else:
61
            palette.add_block('xyz',
62
                              style='basic-style-extended-vertical',
63
                              label=_('acceleration'),
64
                              help_string=\
65
                                  _('push acceleration in x, y, z to heap'),
66
                              hidden=True,
67
                              prim_name='xyz')
68
69
        self._parent.lc.def_prim(
70
            'xyz', 0,
71
            Primitive(self.prim_xyz))
72
73
    def _status_report(self):
74
        debug_output('Reporting accelerator status: %s' % (str(self._status)))
75
        return self._status
76
77
    # Block primitives used in talogo
78
79
    def prim_xyz(self):
80
        ''' push accelerometer xyz to stack '''
81
        if not self._status:
82
            self._parent.lc.heap.append(0)
83
            self._parent.lc.heap.append(0)
84
            self._parent.lc.heap.append(0)
85
        else:
86
            fh = open(ACCELEROMETER_DEVICE)
87
            string = fh.read()
88
            xyz = string[1:-2].split(',')
89
            self._parent.lc.heap.append(float(xyz[2]) / 18)
90
            self._parent.lc.heap.append(float(xyz[1]) / 18)
91
            self._parent.lc.heap.append(float(xyz[0]) / 18)
92
            fh.close()