From 7f70d6edd5610ec71319113b8b3efcf5a5d91779 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Thu, 02 Dec 2010 15:46:26 +0000 Subject: adding rfid support --- (limited to 'TurtleArt') diff --git a/TurtleArt/rfidutils.py b/TurtleArt/rfidutils.py new file mode 100644 index 0000000..f2c74b4 --- /dev/null +++ b/TurtleArt/rfidutils.py @@ -0,0 +1,123 @@ +# utils.py - Helper functions for tis2000.py +# Copyright (C) 2010 Emiliano Pastorino +# +# 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 3 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, see . + +import os +import logging + +def find_device(): + """ + Search for 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: + # logging.error("FIND_DEVICE: %s: %s"%(i, e)) + pass + if device is None: + logging.debug("No RFID device found") + return device + + +def strhex2bin(strhex): + """ + Convert a string representing an hex value into a + string representing the same value in binary format. + """ + dic = { '0':"0000", + '1':"0001", + '2':"0010", + '3':"0011", + '4':"0100", + '5':"0101", + '6':"0110", + '7':"0111", + '8':"1000", + '9':"1001", + 'A':"1010", + 'B':"1011", + 'C':"1100", + 'D':"1101", + 'E':"1110", + 'F':"1111" + } + binstr = "" + for i in strhex: + binstr = binstr + dic[i.upper()] + return binstr + +def strbin2dec(strbin): + """ + Convert a string representing a binary value into a + string representing the same value in decimal format. + """ + strdec = "0" + for i in range(1, strbin.__len__()+1): + strdec = str(int(strdec)+int(strbin[-i])*int(pow(2, i-1))) + return strdec + +def dec2bin(ndec): + """ + Convert a decimal number into a string representing + the same value in binary format. + """ + if ndec < 1: + return "0" + binary = [] + while ndec != 0: + binary.append(ndec%2) + ndec = ndec/2 + strbin = "" + binary.reverse() + for i in binary: + strbin = strbin+str(i) + return strbin + +def bin2hex(strbin): + """ + Convert a string representing a binary number into a string + representing the same value in hexadecimal format. + """ + dic = { "0000":"0", + "0001":"1", + "0010":"2", + "0011":"3", + "0100":"4", + "0101":"5", + "0110":"6", + "0111":"7", + "1000":"8", + "1001":"9", + "1010":"A", + "1011":"B", + "1100":"C", + "1101":"D", + "1110":"E", + "1111":"F" + } + while strbin.__len__()%4 != 0: + strbin = '0' + strbin + strh = "" + for i in range(0, strbin.__len__()/4): + strh = strh + dic[str(strbin[i*4:i*4+4])] + return strh diff --git a/TurtleArt/taconstants.py b/TurtleArt/taconstants.py index c1f69d5..aa869a3 100644 --- a/TurtleArt/taconstants.py +++ b/TurtleArt/taconstants.py @@ -123,7 +123,7 @@ PALETTES = [['clean', 'forward', 'back', 'show', 'left', 'right', 'setcolor', 'setshade', 'setgray', 'color', 'shade', 'gray', 'startfill', 'stopfill'], ['red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'purple', - 'white', 'black'], + 'white', 'black'], ['plus2', 'minus2', 'product2', 'division2', 'identity2', 'remainder2', 'sqrt', 'random', 'number', 'greater2', 'less2', 'equal2', 'not', 'and2', 'or2'], @@ -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'] + 'sound', 'volume', 'pitch', 'voltage', 'resistance', 'gray', 'see', 'rfid'] BOX_STYLE_MEDIA = ['description', 'audio', 'journal', 'video'] NUMBER_STYLE = ['plus2', 'product2', 'myfunc'] NUMBER_STYLE_VAR_ARG = ['myfunc1arg', 'myfunc2arg', 'myfunc3arg'] @@ -394,6 +394,7 @@ BLOCK_NAMES = { 'resistance': [_('resistance')], 'restore': [_('restore last')], 'restoreall': [_('restore all')], + 'rfid': [_('RFID')], 'right': [_('right')], 'rightpos': [_('right')], 'rightx': [_('picture right')], @@ -544,6 +545,7 @@ PRIMITIVES = { 'remainder2': 'mod', 'repeat': 'repeat', 'resistance': 'resistance', + 'rfid': 'rfid', 'right': 'right', 'rightpos': 'rpos', 'rightx': 'rightx', @@ -866,6 +868,7 @@ 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 553d8a0..cad3066 100644 --- a/TurtleArt/talogo.py +++ b/TurtleArt/talogo.py @@ -61,6 +61,32 @@ VALUE_BLOCKS = ['box1', 'box2', 'color', 'shade', 'gray', 'scale', 'pensize', import logging _logger = logging.getLogger('turtleart-activity') +import os + +from dbus.mainloop.glib import DBusGMainLoop +import dbus + +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' + + +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): @@ -389,6 +415,7 @@ 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 61e28fc..c4d4a0f 100644 --- a/TurtleArt/tawindow.py +++ b/TurtleArt/tawindow.py @@ -74,7 +74,7 @@ from tasprite_factory import SVG, svg_str_to_pixbuf, svg_from_file from tagplay import stop_media from sprites import Sprites, Sprite from audiograb import AudioGrab_Unknown, AudioGrab_XO1, AudioGrab_XO15 - +from rfidutils import strhex2bin, strbin2dec, find_device import logging _logger = logging.getLogger('turtleart-activity') @@ -249,6 +249,72 @@ class TurtleArtWindow(): PALETTES[PALETTE_NAMES.index('sensor')].append('voltage') self.audio_started = 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) + + PALETTES[PALETTE_NAMES.index('sensor')].append('rfid') + + 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) -- cgit v0.9.1