Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/plugins/wedo_plugin/wedo_plugin.py
blob: c0cbe126484a743f5ca963a5b9540968abb797ac (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#Copyright (c) 2012, Tony Forster, Ian Daniher, Walter Bender
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

from gettext import gettext as _

from plugins.plugin import Plugin
from WeDoMore import WeDo, scan_for_devices, UNAVAILABLE

from TurtleArt.tapalette import make_palette
from TurtleArt.taconstants import XO1, XO15
from TurtleArt.talogo import primitive_dictionary

import logging
_logger = logging.getLogger('turtleart-activity WeDo plugin')


class Wedo_plugin(Plugin):

    def __init__(self, parent):
        ''' Scan for WeDo devices '''
        self.WeDos = []
        device_list = scan_for_devices()
        for i, dev in enumerate(device_list):
            self.WeDos.append(WeDo(device=dev))
            self.WeDos[-1].number = i
        self._parent = parent
        if len(self.WeDos) == 0:
            self._status = False  # no WeDo devices found
            self.active_wedo = None
        else:
            self._status = True
            self.active_wedo = 0

    def setup(self):
        ''' Setup palettes '''
        palette = make_palette('WeDo', colors=["#FF6060", "#A06060"],
			       help_string=_('Palette of WeDo blocks'))

        primitive_dictionary['wedoselect'] = self.wedo_select
        palette.add_block('wedoselect',
                          style='basic-style-1arg',
                          default = 1,
                          label=_('WeDo'),
                          help_string=_('set current WeDo device'),
                          prim_name = 'wedoselect')

        self._parent.lc.def_prim(
            'wedoselect', 1,
            lambda self, n: primitive_dictionary['wedoselect'](n))

        primitive_dictionary['wedocount'] = self.wedo_count
        palette.add_block('wedogetcount',
                          style='box-style',
                          label=_('number of WeDos'),
                          help_string=_('number of WeDo devices'),
                          prim_name = 'wedocount')

        self._parent.lc.def_prim(
            'wedocount', 0, lambda self: primitive_dictionary['wedocount']())

        primitive_dictionary['wedotilt'] = self.wedo_gettilt
        palette.add_block('tilt',
                        style='box-style',
                        label=_('tilt'),
                        help_string=_('tilt sensor output: (-1 == no tilt,\
 0 == tilt forward, 3 == tilt back, 1 == tilt left, 2 == tilt right)'),
                        value_block=True,
                        prim_name = 'wedotilt')
        self._parent.lc.def_prim(
            'wedotilt', 0, lambda self: primitive_dictionary['wedotilt']())

        primitive_dictionary['wedodistance'] = self.wedo_getdistance
        palette.add_block('wedodistance',
                        style='box-style',
                        label=_('distance'),
                        help_string=_('distance sensor output'),
                        value_block=True,
                        prim_name = 'wedodistance')
        self._parent.lc.def_prim(
            'wedodistance', 0,
            lambda self: primitive_dictionary['wedodistance']())
        
        primitive_dictionary['wedogetMotorA'] = self.wedo_getmotora
        palette.add_block('wedogetMotorA',
                        style='box-style',
                        label=_('Motor A'),
                        help_string=_('returns the current value of Motor A'),
                        value_block=True,
                        prim_name = 'wedogetMotorA')

        self._parent.lc.def_prim(
            'wedogetMotorA', 0,
            lambda self: primitive_dictionary['wedogetMotorA']())

        primitive_dictionary['wedogetMotorB'] = self.wedo_getmotorb
        palette.add_block('wedogetMotorB',
                        style='box-style',
                        label=_('Motor B'),
                        help_string=_('returns the current value of Motor B'),
                        value_block=True,
                        prim_name = 'wedogetMotorB')
        self._parent.lc.def_prim(
            'wedogetMotorB', 0,
            lambda self: primitive_dictionary['wedogetMotorB']())

        primitive_dictionary['wedosetMotorA'] = self.wedo_setmotora
        palette.add_block('wedosetMotorA',
                        style = 'basic-style-1arg',
                        label = _('Motor A'),
                        default = 30,
                        prim_name = 'wedosetMotorA',
                        help_string = _('set the value for Motor A'))
        self._parent.lc.def_prim(
            'wedosetMotorA', 1,
            lambda self, a: primitive_dictionary['wedosetMotorA'](a))

        primitive_dictionary['wedosetMotorB'] = self.wedo_setmotorb
        palette.add_block('wedosetMotorB',
                        style = 'basic-style-1arg',
                        label = _('Motor B'),
                        default = 30,
                        prim_name = 'wedosetMotorB',
                        help_string = _('set the value for Motor B'))
        self._parent.lc.def_prim(
            'wedosetMotorB', 1,
            lambda self, b: primitive_dictionary['wedosetMotorB'](b))

    def wedo_select(self, i):
        ''' Select current device '''
        if self.wedo_count() == 0:
            self.active_wedo = None
            self._parent.showlabel(
                'status', _('WeDo is unavailable'))
            return
        if type(i) == unicode:
            i = str(i.encode('ascii', 'replace'))
        if type(i) == float or type(i) == str:
            i = int(i)
        if type(i) != int:
            i = 1
        if i < 0:
            i = -i
        if i < 0 or i > self.wedo_count() - 1:
            self._parent.showlabel(
                'status', _('WeDo %d is unavailable; defaulting to 1') % (i))
        i -= 1  # Userspace counts from 1; internally, we count from 0
        if i > self.wedo_count() - 1:
            i = 0
        self.active_wedo = i

    def wedo_count(self):
        ''' How many devices are available? '''
        n = len(self.WeDos)
        for wedo in self.WeDos:
            if wedo.dev is None:
                n -= 1
        return n

    def wedo_find(self):
        ''' Find an available device '''
        for wedo in self.WeDos:
            if wedo.dev is not None:
                return self.WeDos.index(wedo)
        return None

    def wedo_gettilt(self):
        if self.active_wedo is None:
            self.active_wedo = self.wedo_find()
            if self.active_wedo is None:
                self._parent.showlabel('status', _('WeDo is unavailable'))
                return -1
        tilt = self.WeDos[self.active_wedo].getTilt()
        if tilt == UNAVAILABLE:
            # Should we look for tilt on another WeDo?
            self._parent.showlabel(
                'status', _('%s is unavailable on WeDo %d') % (
                    _('tilt'), self.active_wedo + 1))
            return -1
        else:
            return tilt

    def wedo_getdistance(self):
        if self.active_wedo is None:
            self.active_wedo = self.wedo_find()
            if self.active_wedo is None:
                self._parent.showlabel('status', _('WeDo is unavailable'))
                return 0
        distance = self.WeDos[self.active_wedo].getDistance()
        if distance == UNAVAILABLE:
            # Should we look for distance on another WeDo?
            self._parent.showlabel(
                'status', _('%s is unavailable on WeDo %d') % (
                    _('distance'), self.active_wedo + 1))
            return 0
        else:
            return distance

    def wedo_getmotora(self):
        if self.active_wedo is None:
            self.active_wedo = self.wedo_find()
            if self.active_wedo is None:
                self._parent.showlabel('status', _('WeDo is unavailable'))
                return 0
        speed = self.WeDos[self.active_wedo].getMotorA()
        if speed == UNAVAILABLE:
            self._parent.showlabel(
                'status', _('%s is unavailable on WeDo %d') % (
                    _('Motor A'), self.active_wedo + 1))
            return 0
        else:
            return speed

    def wedo_getmotorb(self):
        if self.active_wedo is None:
            self.active_wedo = self.wedo_find()
            if self.active_wedo is None:
                self._parent.showlabel('status', _('WeDo is unavailable'))
                return 0
        speed = self.WeDos[self.active_wedo].getMotorB()
        if speed == UNAVAILABLE:
            self._parent.showlabel(
                'status', _('%s is unavailable on WeDo %d') % (
                    _('Motor B'), self.active_wedo + 1))
            return 0
        else:
            return speed

    def wedo_setmotora(self, speed):
        if self.active_wedo is None:
            self.active_wedo = self.wedo_find()
        if self.active_wedo is None:
            self._parent.showlabel('status', _('WeDo is unavailable'))
            return
        status = self.WeDos[self.active_wedo].setMotorA(speed)
        if status == UNAVAILABLE:
            self._parent.showlabel(
                'status', _('%s is unavailable on WeDo %d') % (
                    _('Motor A'), self.active_wedo + 1))

    def wedo_setmotorb(self, speed):
        if self.active_wedo is None:
            self.active_wedo = self.wedo_find()
        if self.active_wedo is None:
            self._parent.showlabel('status', _('WeDo is unavailable'))
            return
        status = self.WeDos[self.active_wedo].setMotorB(speed)
        if status == UNAVAILABLE:
            self._parent.showlabel(
                'status', _('%s is unavailable on WeDo %d') % (
                    _('Motor B'), self.active_wedo + 1))

    def start(self):
        ''' Each time program starts, scan for devices and reset status '''
        for wedo in self.WeDos:
            wedo.dev = None
        self.active_wedo = None
        device_list = scan_for_devices()
        if len(device_list) > 0:
            self.status = True
            if self.active_wedo is None:
                self.active_wedo = 0
        else:
            self.status = False
        for i, dev in enumerate(device_list):
            if i < len(self.WeDos):
                self.WeDos[i].dev = dev
                self.WeDos[i].number = i
                status = self.WeDos[i].init_device()  # Reinitial device
                if status == UNAVAILABLE:
                    self._parent.showlabel('status', _('WeDo is unavailable'))
            else:
                self.WeDos.append(WeDo(device=dev))
                self.WeDos[i].number = i

    def stop(self):
        if self._status:
            for wedo in self.WeDos:
                if wedo.dev is not None:
                    wedo.setMotors(0, 0)

    def goto_background(self):
        pass

    def return_to_foreground(self):
        pass

    def quit(self):
        if self._status:
            for wedo in self.WeDos:
                if wedo.dev is not None:
                    wedo.setMotors(0, 0)