Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorWalter Bender <walter.bender@gmail.com>2011-02-21 23:00:50 (GMT)
committer Walter Bender <walter.bender@gmail.com>2011-02-21 23:00:50 (GMT)
commitaa2d1d42f01f87b5a0a7e533bb6a93d3720b9d1a (patch)
tree7c6a4d820e001530082045875676f44e65280203 /plugins
parente262536e85ce5289bd5eaafa157939ed8657fdab (diff)
parent860754f7e871617df9d101a51dc64a69b742a0ba (diff)
Merge git.sugarlabs.org:~walter/turtleart/collaboration-refactoring
Conflicts: NEWS TurtleArt/talogo.py
Diffstat (limited to 'plugins')
-rw-r--r--plugins/__init__.py0
-rw-r--r--plugins/__init__.pycbin0 -> 143 bytes
-rw-r--r--plugins/audio_sensors_plugin.py272
-rw-r--r--plugins/camera_plugin.py190
-rw-r--r--plugins/camera_plugin.pycbin0 -> 5204 bytes
-rw-r--r--plugins/plugin.py56
-rw-r--r--plugins/plugin.pycbin0 -> 1014 bytes
-rw-r--r--plugins/rfid_plugin.py161
8 files changed, 679 insertions, 0 deletions
diff --git a/plugins/__init__.py b/plugins/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/plugins/__init__.py
diff --git a/plugins/__init__.pyc b/plugins/__init__.pyc
new file mode 100644
index 0000000..ce06608
--- /dev/null
+++ b/plugins/__init__.pyc
Binary files differ
diff --git a/plugins/audio_sensors_plugin.py b/plugins/audio_sensors_plugin.py
new file mode 100644
index 0000000..d8908c8
--- /dev/null
+++ b/plugins/audio_sensors_plugin.py
@@ -0,0 +1,272 @@
+#!/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.
+
+import gst
+try:
+ from numpy import append
+ from numpy.fft import rfft
+ PITCH_AVAILABLE = True
+except:
+ PITCH_AVAILABLE = False
+import gtk
+
+from gettext import gettext as _
+
+from plugin import Plugin
+
+from audio.audiograb import AudioGrab_Unknown, AudioGrab_XO1, AudioGrab_XO15, \
+ SENSOR_DC_NO_BIAS, SENSOR_DC_BIAS
+
+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.tautils import get_path
+
+import logging
+_logger = logging.getLogger('turtleart-activity audio sensors plugin')
+
+
+def _avg(array, abs_value=False):
+ """ Calc. the average value of an array """
+ if len(array) == 0:
+ return 0
+ array_sum = 0
+ if abs_value:
+ for a in array:
+ array_sum += abs(a)
+ else:
+ for a in array:
+ array_sum += a
+ return float(array_sum) / len(array)
+
+
+class Audio_sensors_plugin(Plugin):
+
+ def __init__(self, parent):
+ self._parent = parent
+ self.hw = self._parent.hw
+ self._status = True # TODO: test for audio device
+
+ def setup(self):
+ # set up audio-sensor-specific blocks
+ if not self._status:
+ return
+
+ self.max_samples = 1500
+ self.input_step = 1
+
+ self.ringbuffer = RingBuffer1d(self.max_samples, dtype='int16')
+ if self.hw == XO1:
+ self.voltage_gain = 0.00002225
+ self.voltage_bias = 1.140
+ elif self.hw == XO15:
+ 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'
+ 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'
+ 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'
+ PLUGIN_DICTIONARY['pitch'] = self.prim_pitch
+ self._parent.lc._def_prim('pitch', 0,
+ lambda self: PLUGIN_DICTIONARY['pitch']())
+
+ 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'
+ 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'
+ PLUGIN_DICTIONARY['voltage'] = self.prim_voltage
+ self._parent.lc._def_prim('voltage', 0,
+ lambda self: PLUGIN_DICTIONARY['voltage']())
+ self.audio_started = False
+
+ def start(self):
+ # This gets called by the start button
+ if not self._status:
+ return
+ """ Start grabbing audio if there is an audio block in use """
+ if len(self._parent.block_list.get_similar_blocks('block',
+ ['volume', 'sound', 'pitch', 'resistance', 'voltage'])) > 0:
+ if self.audio_started:
+ self.audiograb.resume_grabbing()
+ else:
+ if self.hw == XO15:
+ self.audiograb = AudioGrab_XO15(self.new_buffer, self)
+ elif self.hw == XO1:
+ self.audiograb = AudioGrab_XO1(self.new_buffer, self)
+ else:
+ self.audiograb = AudioGrab_Unknown(self.new_buffer, self)
+ self.audiograb.start_grabbing()
+ self.audio_started = True
+ self._update_audio_mode()
+
+ def new_buffer(self, buf):
+ """ Append a new buffer to the ringbuffer """
+ self.ringbuffer.append(buf)
+ return True
+
+ def _update_audio_mode(self):
+ """ If there are sensor blocks, set the appropriate audio mode """
+ if not hasattr(self._parent.lc, 'value_blocks'):
+ return
+ for name in ['sound', 'volume', 'pitch']:
+ if name in self._parent.lc.value_blocks:
+ if len(self._parent.lc.value_blocks[name]) > 0:
+ self.audiograb.set_sensor_type()
+ return
+ if 'resistance' in self._parent.lc.value_blocks:
+ if len(self._parent.lc.value_blocks['resistance']) > 0:
+ self.audiograb.set_sensor_type(SENSOR_DC_BIAS)
+ return
+ if 'voltage' in self._parent.lc.value_blocks:
+ if len(self._parent.lc.value_blocks['voltage']) > 0:
+ self.audiograb.set_sensor_type(SENSOR_DC_NO_BIAS)
+ return
+
+ def stop(self):
+ # This gets called by the stop button
+ if self._status:
+ if self.audio_started:
+ self.audiograb.pause_grabbing()
+
+ def goto_background(self):
+ # This gets called when your process is sent to the background
+ pass
+
+ def return_to_foreground(self):
+ # This gets called when your process returns from the background
+ pass
+
+ def quit(self):
+ # This gets called by the quit button
+ self.stop()
+
+ def _status_report(self):
+ print 'Reporting audio sensor status: %s' % (str(self._status))
+ return self._status
+
+ # Block primitives used in talogo
+
+ def prim_volume(self):
+ """ return mic in value """
+ #TODO: Adjust gain for different HW
+ buf = self.ringbuffer.read(None, self.input_step)
+ if len(buf) > 0:
+ volume = float(_avg(buf, abs_value=True))
+ self._parent.lc.update_label_value('volume', volume)
+ return volume
+ else:
+ return 0
+
+ def prim_sound(self):
+ """ return raw mic in value """
+ buf = self.ringbuffer.read(None, self.input_step)
+ if len(buf) > 0:
+ sound = float(buf[0])
+ self._parent.lc.update_label_value('sound', sound)
+ return sound
+ else:
+ return 0
+
+ def prim_pitch(self):
+ """ return index of max value in fft of mic in values """
+ if not PITCH_AVAILABLE:
+ return 0
+ buf = []
+ for i in range(4):
+ buf = append(buf, self.ringbuffer.read(None, self.input_step))
+ if len(buf) > 0:
+ r = []
+ for j in rfft(buf):
+ r.append(abs(j))
+ # Convert output to Hertz
+ pitch = r.index(max(r)) * 48000 / len(buf)
+ self._parent.lc.update_label_value('pitch', pitch)
+ return pitch
+ else:
+ return 0
+
+ def prim_resistance(self):
+ """ return resistance sensor value """
+ buf = self.ringbuffer.read(None, self.input_step)
+ if len(buf) > 0:
+ # See <http://bugs.sugarlabs.org/ticket/552#comment:7>
+ # TODO: test this calibration on XO 1.5
+ if self.hw == XO1:
+ resistance = 2.718 ** ((float(_avg(buf)) * 0.000045788) + \
+ 8.0531)
+ else:
+ avg_buf = float(_avg(buf))
+ if avg_buf > 0:
+ resistance = (420000000 / avg_buf) - 13500
+ else:
+ resistance = 420000000
+ self._parent.lc.update_label_value('resistance', resistance)
+ return resistance
+ else:
+ return 0
+
+ def prim_voltage(self):
+ """ return voltage sensor value """
+ buf = self.ringbuffer.read(None, self.input_step)
+ if len(buf) > 0:
+ # See <http://bugs.sugarlabs.org/ticket/552#comment:7>
+ voltage = float(_avg(buf)) * self.voltage_gain + self.voltage_bias
+ self._parent.lc.update_label_value('voltage', voltage)
+ return voltage
+ else:
+ return 0
diff --git a/plugins/camera_plugin.py b/plugins/camera_plugin.py
new file mode 100644
index 0000000..3061d39
--- /dev/null
+++ b/plugins/camera_plugin.py
@@ -0,0 +1,190 @@
+#!/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.
+
+import gst
+import gtk
+from fcntl import ioctl
+
+from gettext import gettext as _
+
+from camera.tacamera import Camera
+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.tautils import get_path
+
+import logging
+_logger = logging.getLogger('turtleart-activity camera plugin')
+
+
+class Camera_plugin(Plugin):
+
+ def __init__(self, parent):
+ self._parent = parent
+ self._status = False
+
+ v4l2src = gst.element_factory_make('v4l2src')
+ if v4l2src.props.device_name is not None:
+
+ if self._parent.running_sugar:
+ self._imagepath = get_path(self._parent.activity,
+ 'data/turtlepic.png')
+ else:
+ self._imagepath = '/tmp/turtlepic.png'
+ self._camera = Camera(self._imagepath)
+
+ self._status = True
+
+ 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'
+ PLUGIN_DICTIONARY['luminance'] = self.prim_read_camera
+ self._parent.lc._def_prim('luminance', 0,
+ lambda self: PLUGIN_DICTIONARY['luminance'](True))
+
+ # 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')
+ MEDIA_BLOCKS_DICTIONARY['camera'] = self.prim_take_picture
+
+ def start(self):
+ # This gets called by the start button
+ pass
+
+ def stop(self):
+ # This gets called by the stop button
+ if self._status:
+ self._camera.stop_camera_input()
+
+ def goto_background(self):
+ # This gets called when your process is sent to the background
+ pass
+
+ def return_to_foreground(self):
+ # This gets called when your process returns from the background
+ pass
+
+ def quit(self):
+ # This gets called by the quit button
+ pass
+
+ def _status_report(self):
+ print 'Reporting camera status: %s' % (str(self._status))
+ return self._status
+
+ # Block primitives used in talogo
+
+ def prim_take_picture(self):
+ if self._status:
+ ''' method called by media block '''
+ self._camera.save_camera_input_to_file()
+ self._camera.stop_camera_input()
+ self._parent.lc.filepath = self._imagepath
+
+ def prim_read_camera(self, luminance_only=False):
+ """ Read average pixel from camera and push b, g, r to the stack """
+ pixbuf = None
+ array = None
+ w, h = self._parent.lc._w(), self._parent.lc._h()
+ if w > 0 and h > 0 and self._status:
+ try:
+ self._video_capture_device = open('/dev/video0', 'rw')
+ except:
+ self._video_capture_device = None
+ _logger.debug('video capture device not available')
+
+ if self._video_capture_device is not None:
+ self._ag_control = v4l2_control(V4L2_CID_AUTOGAIN)
+ try:
+ ioctl(self._video_capture_device, VIDIOC_G_CTRL,
+ self._ag_control)
+ self._ag_control.value = 0 # disable AUTOGAIN
+ ioctl(self._video_capture_device, VIDIOC_S_CTRL,
+ self._ag_control)
+ except:
+ _logger.debug('AUTOGAIN control not available')
+ pass
+
+ if self._video_capture_device is not None:
+ self._video_capture_device.close()
+
+ self._camera.save_camera_input_to_file()
+ self._camera.stop_camera_input()
+ pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(self._imagepath, w, h)
+ try:
+ array = pixbuf.get_pixels()
+ except:
+ array = None
+
+ if array is not None:
+ length = len(array) / 3
+ r, g, b, i = 0, 0, 0, 0
+ for j in range(length):
+ r += ord(array[i])
+ i += 1
+ g += ord(array[i])
+ i += 1
+ b += ord(array[i])
+ i += 1
+ if luminance_only:
+ lum = int((r * 0.3 + g * 0.6 + b * 0.1) / length)
+ self._parent.lc.update_label_value('luminance', lum)
+ return lum
+ else:
+ self._parent.lc.heap.append(int((b / length)))
+ self._parent.lc.heap.append(int((g / length)))
+ self._parent.lc.heap.append(int((r / length)))
+ else:
+ if luminance_only:
+ return -1
+ else:
+ self._parent.lc.heap.append(-1)
+ self._parent.lc.heap.append(-1)
+ self._parent.lc.heap.append(-1)
+
+
diff --git a/plugins/camera_plugin.pyc b/plugins/camera_plugin.pyc
new file mode 100644
index 0000000..a557a12
--- /dev/null
+++ b/plugins/camera_plugin.pyc
Binary files differ
diff --git a/plugins/plugin.py b/plugins/plugin.py
new file mode 100644
index 0000000..0fe836b
--- /dev/null
+++ b/plugins/plugin.py
@@ -0,0 +1,56 @@
+#!/usr/bin/env python
+#Copyright (c) 2011 Walter Bender
+#Copyright (c) 2011 Collabora Ltd. <http://www.collabora.co.uk/>
+
+#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.
+
+import gobject
+
+
+class Plugin(gobject.GObject):
+ def __init__(self):
+ gobject.GObject.__init__(self)
+
+ def setup(self):
+ """ Setup is called once, when the Turtle Window is created. """
+ raise RuntimeError("You need to define setup for your plugin.")
+
+ def start(self):
+ """ start is called when run button is pressed. """
+ raise RuntimeError("You need to define start for your plugin.")
+
+ def stop(self):
+ """ stop is called when stop button is pressed. """
+ raise RuntimeError("You need to define stop for your plugin.")
+
+ def goto_background(self):
+ """ goto_background is called when the activity is sent to the
+ background. """
+ raise RuntimeError(
+ "You need to define goto_background for your plugin.")
+
+ def return_to_foreground(self):
+ """ return_to_foreground is called when the activity returns to
+ the foreground. """
+ raise RuntimeError(
+ "You need to define return_to_foreground for your plugin.")
+
+ def quit(self):
+ """ cleanup is called when the activity is exiting. """
+ raise RuntimeError("You need to define quit for your plugin.")
diff --git a/plugins/plugin.pyc b/plugins/plugin.pyc
new file mode 100644
index 0000000..cf6e6fd
--- /dev/null
+++ b/plugins/plugin.pyc
Binary files differ
diff --git a/plugins/rfid_plugin.py b/plugins/rfid_plugin.py
new file mode 100644
index 0000000..e0cfafc
--- /dev/null
+++ b/plugins/rfid_plugin.py
@@ -0,0 +1,161 @@
+#!/usr/bin/env python
+#Copyright (C) 2010 Emiliano Pastorino <epastorino@plan.ceibal.edu.uy>
+#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.
+
+import os
+
+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
+
+import logging
+_logger = logging.getLogger('turtleart-activity RFID plugin')
+
+HAL_SERVICE = 'org.freedesktop.Hal'
+HAL_MGR_PATH = '/org/freedesktop/Hal/Manager'
+HAL_MGR_IFACE = 'org.freedesktop.Hal.Manager'
+HAL_DEV_IFACE = 'org.freedesktop.Hal.Device'
+REGEXP_SERUSB = '\/org\/freedesktop\/Hal\/devices\/usb_device['\
+ 'a-z,A-Z,0-9,_]*serial_usb_[0-9]'
+
+
+class Rfid_plugin(Plugin):
+
+ def __init__(self, parent):
+ self._parent = parent
+ self._status = False
+
+ """
+ The following code will initialize a USB RFID reader. Please note that
+ in order to make this initialization function work, it is necessary to
+ set the permission for the ttyUSB device to 0666. You can do this by
+ adding a rule to /etc/udev/rules.d
+
+ As root (using sudo or su), copy the following text into a new file in
+ /etc/udev/rules.d/94-ttyUSB-rules
+
+ KERNEL=="ttyUSB[0-9]",MODE="0666"
+
+ You only have to do this once.
+ """
+
+ self.rfid_connected = False
+ self.rfid_device = find_device()
+ self.rfid_idn = ''
+
+ if self.rfid_device is not None:
+ _logger.info("RFID device found")
+ self.rfid_connected = self.rfid_device.do_connect()
+ if self.rfid_connected:
+ self.rfid_device.connect("tag-read", self._tag_read_cb)
+ self.rfid_device.connect("disconnected", self._disconnected_cb)
+
+ loop = DBusGMainLoop()
+ bus = dbus.SystemBus(mainloop=loop)
+ hmgr_iface = dbus.Interface(bus.get_object(HAL_SERVICE,
+ HAL_MGR_PATH), HAL_MGR_IFACE)
+
+ hmgr_iface.connect_to_signal('DeviceAdded', self._device_added_cb)
+
+ self._status = True
+
+ def setup(self):
+ # set up camera-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
+ self._parent.lc._def_prim('rfid', 0,
+ lambda self: PLUGIN_DICTIONARY['rfid']())
+
+
+ def start(self):
+ # This gets called by the start button
+ if self._status:
+ pass
+
+ def stop(self):
+ # This gets called by the stop button
+ if self._status:
+ pass
+
+ def goto_background(self):
+ # This gets called when your process is sent to the background
+ pass
+
+ def return_to_foreground(self):
+ # This gets called when your process returns from the background
+ pass
+
+ def quit(self):
+ # This gets called by the quit button
+ pass
+
+ def _status_report(self):
+ print 'Reporting RFID status: %s' % (str(self._status))
+ return self._status
+
+ def _device_added_cb(self, path):
+ """
+ Called from hal connection when a new device is plugged.
+ """
+ if not self.rfid_connected:
+ self.rfid_device = find_device()
+ _logger.debug("DEVICE_ADDED: %s" % self.rfid_device)
+ if self.rfid_device is not None:
+ _logger.debug("DEVICE_ADDED: RFID device is not None!")
+ self.rfid_connected = self._device.do_connect()
+ if self.rfid_connected:
+ _logger.debug("DEVICE_ADDED: Connected!")
+ self.rfid_device.connect("tag-read", self._tag_read_cb)
+ self.rfid_device.connect("disconnected", self._disconnected_cb)
+
+ def _disconnected_cb(self, device, text):
+ """
+ Called when the device is disconnected.
+ """
+ self.rfid_connected = False
+ self.rfid_device = None
+
+ def _tag_read_cb(self, device, tagid):
+ """
+ Callback for "tag-read" signal. Receives the read tag id.
+ """
+ idbin = strhex2bin(tagid)
+ self.rfid_idn = strbin2dec(idbin[26:64])
+ while self.rfid_idn.__len__() < 9:
+ self.rfid_idn = '0' + self.rfid_idn
+ print tagid, idbin, self.rfid_idn
+
+ # Block primitives used in talogo
+
+ def prim_read_rfid(self):
+ if self._status:
+ return self.rfid_idn