Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWalter Bender <walter.bender@gmail.com>2011-02-25 15:34:01 (GMT)
committer Walter Bender <walter.bender@gmail.com>2011-02-25 15:34:01 (GMT)
commitc5eb935834b6957e50abf7c82245d3040ac269b3 (patch)
treeb56636ce8025dcfc14c20ab8cbc3e513bf0e07de
parent01eb74926f7b0e223802675bb8838e52be1ef319 (diff)
made class for block primitives
-rw-r--r--TurtleArt/taprimitive.py127
-rw-r--r--plugins/audio_sensors_plugin.py89
-rw-r--r--plugins/camera_plugin.py61
-rw-r--r--plugins/rfid_plugin.py28
4 files changed, 226 insertions, 79 deletions
diff --git a/TurtleArt/taprimitive.py b/TurtleArt/taprimitive.py
new file mode 100644
index 0000000..47180b8
--- /dev/null
+++ b/TurtleArt/taprimitive.py
@@ -0,0 +1,127 @@
+#!/usr/bin/env python
+#Copyright (c) 2011 Walter Bender
+
+#Permission is hereby granted, free of charge, to any person obtaining a copy
+#of this software and associated documentation files (the "Software"), to deal
+#in the Software without restriction, including without limitation the rights
+#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+#copies of the Software, and to permit persons to whom the Software is
+#furnished to do so, subject to the following conditions:
+
+#The above copyright notice and this permission notice shall be included in
+#all copies or substantial portions of the Software.
+
+#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+#THE SOFTWARE.
+
+from taconstants import EXPANDABLE, EXPANDABLE_BLOCKS, EXPANDABLE_ARGS, \
+ PRIMITIVES, OLD_NAMES, BLOCK_SCALE, BLOCK_NAMES, CONTENT_BLOCKS, \
+ PALETTES, COLORS, BASIC_STYLE_HEAD, BASIC_STYLE_HEAD_1ARG, \
+ BASIC_STYLE_TAIL, BASIC_STYLE, BASIC_STYLE_EXTENDED, BASIC_STYLE_1ARG, \
+ BASIC_STYLE_VAR_ARG, BULLET_STYLE, BASIC_STYLE_2ARG, BOX_STYLE, \
+ BOX_STYLE_MEDIA, NUMBER_STYLE, NUMBER_STYLE_VAR_ARG, NUMBER_STYLE_BLOCK, \
+ NUMBER_STYLE_PORCH, NUMBER_STYLE_1ARG, NUMBER_STYLE_1STRARG, \
+ COMPARE_STYLE, BOOLEAN_STYLE, NOT_STYLE, FLOW_STYLE, FLOW_STYLE_TAIL, \
+ FLOW_STYLE_1ARG, FLOW_STYLE_BOOLEAN, FLOW_STYLE_WHILE, FLOW_STYLE_ELSE, \
+ COLLAPSIBLE_TOP, COLLAPSIBLE_TOP_NO_ARM, COLLAPSIBLE_TOP_NO_LABEL, \
+ COLLAPSIBLE_TOP_NO_ARM_NO_LABEL, COLLAPSIBLE_BOTTOM, PORTFOLIO_STYLE_2x2, \
+ PORTFOLIO_STYLE_1x1, PORTFOLIO_STYLE_2x1, PORTFOLIO_STYLE_1x2, \
+ STANDARD_STROKE_WIDTH, BOX_COLORS, GRADIENT_COLOR, COMPARE_PORCH_STYLE, \
+ BASIC_STYLE_EXTENDED_VERTICAL, CONSTANTS, INVISIBLE, PALETTE_NAMES, \
+ HELP_STRINGS, DEFAULTS, SPECIAL_NAMES
+
+from talogo import VALUE_BLOCKS
+from tautils import debug_output
+
+
+class Primitive():
+ """ a class for defining new block primitives """
+
+ def __init__(self, name):
+ self._name = name
+ self._special_name = None
+ self._palette = None
+ self._style = None
+ self._label = None
+ self._default = None
+ self._help = None
+ self._prim_name = None
+ self._value_block = False
+ self._content_block = False
+
+ def add_prim(self):
+ if self._name is None:
+ debug_output('You must specify a name for your block')
+ return
+
+ if self._style is None:
+ debug_output('You must specify a style for your block')
+ return
+ else:
+ self._style.append(self._name)
+
+ if self._label is not None:
+ BLOCK_NAMES[self._name] = self._label
+
+ if self._palette is not None:
+ PALETTES[PALETTE_NAMES.index(self._palette)].append(self._name)
+
+ if self._help is not None:
+ HELP_STRINGS[self._name] = self._help
+
+ if self._value_block:
+ VALUE_BLOCKS.append(self._name)
+
+ if self._content_block:
+ CONTENT_BLOCKS.append(self._name)
+
+ if self._prim_name is not None:
+ PRIMITIVES[self._name] = self._prim_name
+
+ if self._default is not None:
+ DEFAULTS[self._name] = self._default
+
+ if self._special_name is not None:
+ SPECIAL_NAMES[self._name] = self._special_name
+
+ def set_value_block(self, value=True):
+ self._value_block = value
+
+ def set_content_block(self, value=True):
+ self._content_block = value
+
+ def set_palette(self, palette):
+ if not palette in PALETTE_NAMES:
+ debug_output('Could not find palette %s' % (palette))
+ else:
+ self._palette = palette
+
+ def set_help(self, help):
+ self._help = help
+
+ def set_special_name(self, name):
+ self._special_name = name
+
+ def set_label(self, label):
+ if type(label) == type([]):
+ self._label = label[:]
+ else:
+ self._label = [label]
+
+ def set_default(self, default):
+ if type(default) == type([]):
+ self._default = default[:]
+ else:
+ self._default = [default]
+
+ def set_style(self, style):
+ self._style = style
+
+ def set_prim_name(self, prim_name):
+ self._prim_name = prim_name
+
diff --git a/plugins/audio_sensors_plugin.py b/plugins/audio_sensors_plugin.py
index c99321f..8f0e2d4 100644
--- a/plugins/audio_sensors_plugin.py
+++ b/plugins/audio_sensors_plugin.py
@@ -37,10 +37,9 @@ from audio.audiograb import AudioGrab_Unknown, AudioGrab_XO1, AudioGrab_XO15, \
from audio.ringbuffer import RingBuffer1d
-from TurtleArt.taconstants import PALETTES, PALETTE_NAMES, BOX_STYLE_MEDIA, \
- CONTENT_BLOCKS, BLOCK_NAMES, DEFAULTS, SPECIAL_NAMES, HELP_STRINGS, \
- BOX_STYLE, PRIMITIVES, XO1, XO15
-from TurtleArt.talogo import VALUE_BLOCKS, PLUGIN_DICTIONARY
+from TurtleArt.taprimitive import Primitive
+from TurtleArt.taconstants import BOX_STYLE, XO1, XO15
+from TurtleArt.talogo import PLUGIN_DICTIONARY
from TurtleArt.tautils import get_path
import logging
@@ -85,54 +84,68 @@ class Audio_sensors_plugin(Plugin):
self.voltage_gain = -0.0001471
self.voltage_bias = 1.695
- PALETTES[PALETTE_NAMES.index('sensor')].append('sound')
- BOX_STYLE.append('sound')
- BLOCK_NAMES['sound'] = [_('sound')]
- HELP_STRINGS['sound'] = _('raw microphone input signal')
- VALUE_BLOCKS.append('sound')
- PRIMITIVES['sound'] = 'sound'
+ sound = Primitive('sound')
+ sound.set_palette('sensor')
+ sound.set_style(BOX_STYLE)
+ sound.set_label(_('sound'))
+ sound.set_help(_('raw microphone input signal'))
+ sound.set_value_block(True)
+ sound.set_prim_name('sound')
PLUGIN_DICTIONARY['sound'] = self.prim_sound
self._parent.lc._def_prim('sound', 0,
- lambda self: PLUGIN_DICTIONARY['sound']())
- PALETTES[PALETTE_NAMES.index('sensor')].append('volume')
- BOX_STYLE.append('volume')
- BLOCK_NAMES['volume'] = [_('volume')]
- HELP_STRINGS['volume'] = _('microphone input volume')
- VALUE_BLOCKS.append('volume')
- PRIMITIVES['volume'] = 'volume'
+ lambda self: PLUGIN_DICTIONARY['sound']())
+ sound.add_prim()
+
+ volume = Primitive('volume')
+ volume.set_palette('sensor')
+ volume.set_style(BOX_STYLE)
+ volume.set_label(_('volume'))
+ volume.set_help(_('microphone input volume'))
+ volume.set_value_block(True)
+ volume.set_prim_name('volume')
PLUGIN_DICTIONARY['volume'] = self.prim_volume
self._parent.lc._def_prim('volume', 0,
- lambda self: PLUGIN_DICTIONARY['volume']())
- PALETTES[PALETTE_NAMES.index('sensor')].append('pitch')
- BOX_STYLE.append('pitch')
- BLOCK_NAMES['pitch'] = [_('pitch')]
- HELP_STRINGS['pitch'] = _('microphone input pitch')
- VALUE_BLOCKS.append('pitch')
- PRIMITIVES['pitch'] = 'pitch'
+ lambda self: PLUGIN_DICTIONARY['volume']())
+ volume.add_prim()
+
+ pitch = Primitive('pitch')
+ if PITCH_AVAILABLE:
+ pitch.set_palette('sensor')
+ pitch.set_style(BOX_STYLE)
+ pitch.set_label(_('pitch'))
+ pitch.set_help(_('microphone input pitch'))
+ pitch.set_value_block(True)
+ pitch.set_prim_name('pitch')
PLUGIN_DICTIONARY['pitch'] = self.prim_pitch
self._parent.lc._def_prim('pitch', 0,
- lambda self: PLUGIN_DICTIONARY['pitch']())
+ lambda self: PLUGIN_DICTIONARY['pitch']())
+ pitch.add_prim()
if self.hw in [XO1, XO15]:
- PALETTES[PALETTE_NAMES.index('sensor')].append('resistance')
- BOX_STYLE.append('resistance')
- BLOCK_NAMES['resistance'] = [_('resistance')]
- HELP_STRINGS['resistance'] = _('sensor input resistance')
- VALUE_BLOCKS.append('resistance')
- PRIMITIVES['resistance'] = 'resistance'
+ resistance = Primitive('resistance')
+ resistance.set_palette('sensor')
+ resistance.set_style(BOX_STYLE)
+ resistance.set_label(_('resistance'))
+ resistance.set_help(_('sensor input resistance'))
+ resistance.set_value_block(True)
+ resistance.set_prim_name('resistance')
PLUGIN_DICTIONARY['resistance'] = self.prim_resistance
self._parent.lc._def_prim('resistance', 0,
lambda self: PLUGIN_DICTIONARY['resistance']())
-
- PALETTES[PALETTE_NAMES.index('sensor')].append('voltage')
- BOX_STYLE.append('voltage')
- BLOCK_NAMES['voltage'] = [_('voltage')]
- HELP_STRINGS['voltage'] = _('sensor voltage')
- VALUE_BLOCKS.append('voltage')
- PRIMITIVES['voltage'] = 'voltage'
+ resistance.add_prim()
+
+ voltage = Primitive('voltage')
+ voltage.set_palette('sensor')
+ voltage.set_style(BOX_STYLE)
+ voltage.set_label(_('voltage'))
+ voltage.set_help(_('sensor input voltage'))
+ voltage.set_value_block(True)
+ voltage.set_prim_name('voltage')
PLUGIN_DICTIONARY['voltage'] = self.prim_voltage
self._parent.lc._def_prim('voltage', 0,
lambda self: PLUGIN_DICTIONARY['voltage']())
+ voltage.add_prim()
+
self.audio_started = False
def start(self):
diff --git a/plugins/camera_plugin.py b/plugins/camera_plugin.py
index 3061d39..960dc54 100644
--- a/plugins/camera_plugin.py
+++ b/plugins/camera_plugin.py
@@ -30,11 +30,10 @@ from camera.v4l2 import v4l2_control, V4L2_CID_AUTOGAIN, VIDIOC_G_CTRL, \
VIDIOC_S_CTRL
from plugin import Plugin
-from TurtleArt.taconstants import PALETTES, PALETTE_NAMES, BOX_STYLE_MEDIA, \
- CONTENT_BLOCKS, BLOCK_NAMES, DEFAULTS, SPECIAL_NAMES, HELP_STRINGS, \
- BOX_STYLE, PRIMITIVES
-from TurtleArt.talogo import VALUE_BLOCKS, MEDIA_BLOCKS_DICTIONARY, \
- PLUGIN_DICTIONARY
+
+from TurtleArt.taprimitive import Primitive
+from TurtleArt.taconstants import BOX_STYLE_MEDIA, BOX_STYLE
+from TurtleArt.talogo import MEDIA_BLOCKS_DICTIONARY, PLUGIN_DICTIONARY
from TurtleArt.tautils import get_path
import logging
@@ -62,35 +61,41 @@ class Camera_plugin(Plugin):
def setup(self):
# set up camera-specific blocks
if self._status:
- PALETTES[PALETTE_NAMES.index('sensor')].append('luminance')
- BOX_STYLE.append('luminance')
- BLOCK_NAMES['luminance'] = [_('brightness')]
- HELP_STRINGS['luminance'] = _("light level detected by camera")
- VALUE_BLOCKS.append('luminance')
- PRIMITIVES['luminance'] = 'luminance'
+ luminance = Primitive('luminance')
+ luminance.set_palette('sensor')
+ luminance.set_style(BOX_STYLE)
+ luminance.set_label(_('brightness'))
+ luminance.set_help(_('light level detected by camera'))
+ luminance.set_value_block(True)
+ luminance.set_prim_name('luminance')
PLUGIN_DICTIONARY['luminance'] = self.prim_read_camera
self._parent.lc._def_prim('luminance', 0,
lambda self: PLUGIN_DICTIONARY['luminance'](True))
+ luminance.add_prim()
# Depreciated block
- BOX_STYLE.append('readcamera')
- BLOCK_NAMES['readcamera'] = [_('read camera')]
- HELP_STRINGS['readcamera'] = \
- _("Average RGB color from camera is pushed to the stack")
- VALUE_BLOCKS.append('readcamera')
- PRIMITIVES['readcamera'] = 'readcamera'
- PLUGIN_DICTIONARY['readcamera'] = self.prim_read_camera
- self._parent.lc._def_prim('readcamera', 0,
- lambda self: PLUGIN_DICTIONARY['readcamera'](True))
-
- PALETTES[PALETTE_NAMES.index('sensor')].append('camera')
- BOX_STYLE_MEDIA.append('camera')
- CONTENT_BLOCKS.append('camera')
- BLOCK_NAMES['camera'] = [' ']
- DEFAULTS['camera'] = ['CAMERA']
- SPECIAL_NAMES['camera'] = _('camera')
- HELP_STRINGS['camera'] = _('camera output')
+ read_camera = Primitive('read_camera')
+ read_camera.set_style(BOX_STYLE)
+ read_camera.set_label(_('brightness'))
+ read_camera.set_help(
+ _('Average RGB color from camera is pushed to the stack'))
+ read_camera.set_value_block(True)
+ read_camera.set_prim_name('read_camera')
+ PLUGIN_DICTIONARY['read_camera'] = self.prim_read_camera
+ self._parent.lc._def_prim('read_camera', 0,
+ lambda self: PLUGIN_DICTIONARY['read_camera'](True))
+ read_camera.add_prim()
+
+ camera = Primitive('camera')
+ camera.set_palette('sensor')
+ camera.set_style(BOX_STYLE_MEDIA)
+ camera.set_label([' '])
+ camera.set_help(_('camera output'))
+ camera.set_special_name(_('camera'))
+ camera.set_content_block(True)
+ camera.set_default(['CAMERA'])
MEDIA_BLOCKS_DICTIONARY['camera'] = self.prim_take_picture
+ camera.add_prim()
def start(self):
# This gets called by the start button
diff --git a/plugins/rfid_plugin.py b/plugins/rfid_plugin.py
index e0cfafc..82f431a 100644
--- a/plugins/rfid_plugin.py
+++ b/plugins/rfid_plugin.py
@@ -27,9 +27,10 @@ from gettext import gettext as _
from rfid.rfidutils import strhex2bin, strbin2dec, find_device
from plugin import Plugin
-from TurtleArt.taconstants import PALETTES, PALETTE_NAMES, BLOCK_NAMES, \
- HELP_STRINGS, BOX_STYLE
-from TurtleArt.talogo import VALUE_BLOCKS, PLUGIN_DICTIONARY
+
+from TurtleArt.taprimitive import Primitive
+from TurtleArt.taconstants import BOX_STYLE
+from TurtleArt.talogo import PLUGIN_DICTIONARY
import logging
_logger = logging.getLogger('turtleart-activity RFID plugin')
@@ -83,18 +84,19 @@ class Rfid_plugin(Plugin):
self._status = True
def setup(self):
- # set up camera-specific blocks
+ # set up RFID-specific blocks
if self._status:
- PALETTES[PALETTE_NAMES.index('sensor')].append('rfid')
- BOX_STYLE.append('rfid')
- BLOCK_NAMES['rfid'] = [_('RFID')]
- HELP_STRINGS['rfid'] = _("read value from RFID device")
- PRIMITIVES['rfid'] = 'rfid'
- VALUE_BLOCKS.append('rfid')
- PLUGIN_DICTIONARY['rfid'] = self.prim_read_rfid
+ rfid = Primitive('rfid')
+ rfid.set_palette('sensor')
+ rfid.set_style(BOX_STYLE)
+ rfid.set_label(_('RFID'))
+ rfid.set_help(_('read value from RFID device'))
+ rfid.set_value_block(True)
+ rfid.set_prim_name('rfid')
+ PLUGIN_DICTIONARY['rfid'] = self.prim_read_camera
self._parent.lc._def_prim('rfid', 0,
- lambda self: PLUGIN_DICTIONARY['rfid']())
-
+ lambda self: PLUGIN_DICTIONARY['rfid'](True))
+ rfid.add_prim()
def start(self):
# This gets called by the start button