From 676d559cd62e35d7718d15a821b43aa7c4cfd28d Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Fri, 18 Feb 2011 14:19:14 +0000 Subject: moved rfid code to plugin --- diff --git a/TurtleArt/taconstants.py b/TurtleArt/taconstants.py index 54fc099..4cf6ee1 100644 --- a/TurtleArt/taconstants.py +++ b/TurtleArt/taconstants.py @@ -223,7 +223,7 @@ BOX_STYLE = ['number', 'xcor', 'ycor', 'heading', 'pensize', 'color', 'shade', 'toppos', 'rightpos', 'bottompos', 'width', 'height', 'pop', 'keyboard', 'red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'purple', 'white', 'black', 'titlex', 'titley', 'leftx', 'topy', 'rightx', 'bottomy', - 'sound', 'volume', 'pitch', 'voltage', 'resistance', 'gray', 'see', 'rfid', + 'sound', 'volume', 'pitch', 'voltage', 'resistance', 'gray', 'see', 'time'] BOX_STYLE_MEDIA = ['description', 'audio', 'journal', 'video'] NUMBER_STYLE = ['plus2', 'product2', 'myfunc'] @@ -396,7 +396,6 @@ BLOCK_NAMES = { 'resistance': [_('resistance')], 'restore': [_('restore last')], 'restoreall': [_('restore all')], - 'rfid': [_('RFID')], 'right': [_('right')], 'rightpos': [_('right')], 'rightx': [_('picture right')], @@ -520,7 +519,6 @@ PRIMITIVES = { 'leftx': 'leftx', 'less2': 'less?', 'list': 'bulletlist', - 'luminance': 'luminance', 'mediawait': 'mediawait', 'minus2': 'minus', 'myfunc': 'myfunction', @@ -550,7 +548,6 @@ PRIMITIVES = { 'remainder2': 'mod', 'repeat': 'repeat', 'resistance': 'resistance', - 'rfid': 'rfid', 'right': 'right', 'rightpos': 'rpos', 'rightx': 'rightx', @@ -880,7 +877,6 @@ HELP_STRINGS = { 'reskin': _("put a custom 'shell' on the turtle"), 'restore': _("restores most recent blocks from trash"), 'restoreall': _("restore all blocks from trash"), - 'rfid': _("RFID"), 'rightpos': _("xcor of right of screen"), 'right': _("turns turtle clockwise (angle in degrees)"), 'run-fastoff': _("Run"), diff --git a/TurtleArt/talogo.py b/TurtleArt/talogo.py index 38f2586..953c296 100644 --- a/TurtleArt/talogo.py +++ b/TurtleArt/talogo.py @@ -61,23 +61,6 @@ import logging _logger = logging.getLogger('turtleart-activity') -def find_device(): - """ Search for RFID devices. Return a device instance or None. """ - device = None - for i in os.listdir(os.path.join('.', 'devices')): - if not os.path.isdir(os.path.join('.', 'devices', i)): - try: - _tempmod = __import__('devices.%s' % i.split('.')[0], - globals(), locals(), ['RFIDReader'], -1) - devtemp = _tempmod.RFIDReader() - if devtemp.get_present() == True: - device = devtemp - except Exception, e: - _logger.error("FIND_DEVICE: %s: %s" % (i, e)) - pass - return device - - class noKeyError(UserDict): __missing__ = lambda x, y: 0 @@ -398,7 +381,6 @@ class LogoCode: 'red': [0, lambda self: CONSTANTS['red']], 'repeat': [2, self._prim_repeat, True], 'resistance': [0, lambda self: self._get_resistance()], - 'rfid': [0, lambda self: self.tw.rfid_idn], 'right': [1, lambda self, x: self._prim_right(x)], 'rightx': [0, lambda self: CONSTANTS['rightx']], 'rpos': [0, lambda self: CONSTANTS['rightpos']], diff --git a/TurtleArt/tawindow.py b/TurtleArt/tawindow.py index 73894e4..589e60c 100644 --- a/TurtleArt/tawindow.py +++ b/TurtleArt/tawindow.py @@ -2,7 +2,6 @@ #Copyright (c) 2007, Playful Invention Company #Copyright (c) 2008-11, Walter Bender #Copyright (c) 2009-11 Raúl Gutiérrez Segalés -#Copyright (C) 2010 Emiliano Pastorino #Copyright (c) 2011 Collabora Ltd. #Permission is hereby granted, free of charge, to any person obtaining a copy @@ -84,20 +83,12 @@ from tautils import magnitude, get_load_name, get_save_name, data_from_file, \ get_hardware from tasprite_factory import SVG, svg_str_to_pixbuf, svg_from_file from sprites import Sprites, Sprite -from rfidutils import strhex2bin, strbin2dec, find_device from dbus.mainloop.glib import DBusGMainLoop if GST_AVAILABLE: from tagplay import stop_media from audiograb import AudioGrab_Unknown, AudioGrab_XO1, AudioGrab_XO15 -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]' - import logging _logger = logging.getLogger('turtleart-activity') @@ -288,40 +279,6 @@ class TurtleArtWindow(): self.saved_pictures = [] self.block_operation = '' - """ - 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) - - PALETTES[PALETTE_NAMES.index('sensor')].append('rfid') - #### def _get_plugin_home(self): @@ -364,50 +321,20 @@ class TurtleArtWindow(): f = "def f(self): from plugins.%s import %s; return %s(self)" \ % (p, P, P) plugin = {} + exec f in globals(), plugin + self._plugins.append(plugin.values()[0](self)) + ''' try: exec f in globals(), plugin self._plugins.append(plugin.values()[0](self)) except ImportError: print 'failed to import %s' % (P) + ''' def _run_plugins(self): for p in self._plugins: p.setup() - #### - - 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 - def new_buffer(self, buf): """ Append a new buffer to the ringbuffer """ self.lc.ringbuffer.append(buf) diff --git a/plugins/rfid_plugin.py b/plugins/rfid_plugin.py new file mode 100644 index 0000000..acf0cad --- /dev/null +++ b/plugins/rfid_plugin.py @@ -0,0 +1,144 @@ +#!/usr/bin/env python +#Copyright (C) 2010 Emiliano Pastorino +#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, BOX_STYLE_MEDIA, \ + CONTENT_BLOCKS, BLOCK_NAMES, DEFAULTS, SPECIAL_NAMES, HELP_STRINGS, \ + BOX_STYLE +from TurtleArt.talogo import VALUE_BLOCKS, MEDIA_BLOCKS_DICTIONARY, \ + 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._self_test(): + 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'](True)) + + def stop(self): + # Ths gets called by the stop button + pass + + def _self_test(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._self_test(): + return self.rfid_idn diff --git a/rfid/rfidutils.py b/rfid/rfidutils.py index f2c74b4..be79dd5 100644 --- a/rfid/rfidutils.py +++ b/rfid/rfidutils.py @@ -23,10 +23,10 @@ def find_device(): Return a device instance or None. """ device = None - for i in os.listdir(os.path.join('.', 'devices')): - if not os.path.isdir(os.path.join('.', 'devices', i)): + for i in os.listdir(os.path.join('.', 'rfid')): + if not os.path.isdir(os.path.join('.', 'rfid', i)): try: - _tempmod = __import__('devices.%s'%i.split('.')[0], globals(), + _tempmod = __import__('rfid.%s'%i.split('.')[0], globals(), locals(), ['RFIDReader'], -1) devtemp = _tempmod.RFIDReader() if devtemp.get_present() == True: -- cgit v0.9.1