Replace your arduino.py (in the folder /plugins/arduino) with this.This version 
checks for ttyUSB and ttyACM
I don't have an arduino to check it, but I think that must work.
I'm not sure why your board no is listed like a serial device..

> To: [email protected]
> CC: [email protected]; [email protected]
> From: [email protected]
> Date: Fri, 5 Oct 2012 15:01:29 +1000
> Subject: Re: Re: [IAEP] Arduino and XO-1
> 
> > Alan,
> > 
> > Upon doing some research, apparently /dev/ttyACM0 is  the identifier for
> > the board.
> > 
> > Do I have to modify the plugin? If so, how do I do this?
> 
> in my blogpost i give a link to a dirty hacked version for ttyACM0
> 
> This version http://www.box.com/shared/bsf8tmj6al is hard coded to 
> dev/ttyACM0, that means it only works the first time the Leostick is plugged 
> in and not at all for older Arduino boards. It is patched and works on Sugar 
> 0.94.
> 
> I cant remember where or what I patched exactly but somewhere in the plugin 
> code it looks for ttyUSBn, n=1,2,3 ..... and I hacked that
> 
> It would be good if a more competent programmer than me had it search the 
> ttyUSBn and ttyACMn
> 
> Tony
                                          
#!/usr/bin/env python
# Copyright (c) 2012, Alan Aguiar <[email protected]>
#
# 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

import os
from time import time

from gettext import gettext as _
from plugins.plugin import Plugin

from TurtleArt.tapalette import make_palette
from TurtleArt.talogo import media_blocks_dictionary, primitive_dictionary, logoerror

import firmata
import commands

VALUE = {_('HIGH'): firmata.HIGH, _('LOW'): firmata.LOW}
MODE = {_('INPUT'): firmata.INPUT, _('OUTPUT'): firmata.OUTPUT,
        _('PWM'): firmata.PWM, _('SERVO'): firmata.SERVO}

ERROR = _('ERROR: Check the Arduino and the number of port.')
ERROR_VALUE_A = _('ERROR: Value must be a number from 0 to 255.')
ERROR_VALUE_D = _('ERROR: Value must be either HIGH or LOW.')
ERROR_MODE = _('ERROR: The mode must be either INPUT, OUTPUT, PWM or SERVO.')


class Arduino(Plugin):

    def __init__(self, parent):
        self.tw = parent

        self._dev = '/dev/ttyUSB0'
        self._baud = 57600
        self._arduino = None

        status,output_usb = commands.getstatusoutput("ls /dev/ | grep ttyUSB")
        output_usb_parsed = output_usb.split('\n')

        status,output_acm = commands.getstatusoutput("ls /dev/ | grep ttyACM")
        output_acm_parsed = output_acm.split('\n')

        
        output = output_usb_parsed
        output.extend(output_acm_parsed)
        
        for i in output:
	        status,aux=commands.getstatusoutput("udevinfo -a -p /class/tty/%s | grep ftdi_sio > /dev/null" % i)
	        if (not status):
		        self._dev='/dev/%s' % i
		        break


    def setup(self):
        self._arduino = firmata.Arduino(port = self._dev, baudrate = self._baud)

        palette = make_palette('arduino', ["#00FFFF","#00A0A0"], _('Palette of Arduino blocks'))

        primitive_dictionary['pinmode'] = self._prim_pin_mode
        palette.add_block('pinmode',
                  style='basic-style-2arg',
                  label=[_('pin mode'),_('pin'),_('mode')],
                  help_string=_('Select the pin function (INPUT, OUTPUT, PWM, SERVO).'),
                  prim_name='pinmode')
        self.tw.lc.def_prim('pinmode', 2,
            lambda self, x, y:
            primitive_dictionary['pinmode'](x, y))

        primitive_dictionary['analogwrite'] = self._prim_analog_write
        palette.add_block('analogwrite',
                  style='basic-style-2arg',
                  label=[_('analog write'),_('pin'),_('value')],
                  default=[0, 255],
                  help_string=_('Write analog value in specified port.'),
                  prim_name='analogwrite')
        self.tw.lc.def_prim('analogwrite', 2,
            lambda self, x, y:
            primitive_dictionary['analogwrite'](x, y))

        primitive_dictionary['analogread'] = self._prim_analog_read
        palette.add_block('analogread',
                  style='basic-style-1arg',
                  label=[_('analog read')],
                  default=[0],
                  help_string=_('Read value from analog port. Value may be between 0 and 1023. Use Vref \
to determine voltage. For USB, volt=((read)*5)/1024) approximately.'),
                  prim_name='analogread')
        self.tw.lc.def_prim('analogread', 1,
            lambda self, x:
            primitive_dictionary['analogread'](x))

        primitive_dictionary['digitalwrite'] = self._prim_digital_write
        palette.add_block('digitalwrite',
                  style='basic-style-2arg',
                  label=[_('digital write'),_('pin'),_('value')],
                  default=[13],
                  help_string=_('Write digital value to specified port.'),
                  prim_name='digitalwrite')
        self.tw.lc.def_prim('digitalwrite', 2,
            lambda self, x, y:
            primitive_dictionary['digitalwrite'](x, y))

        primitive_dictionary['digitalread'] = self._prim_digital_read
        palette.add_block('digitalread',
                  style='basic-style-1arg',
                  label=[_('digital read')],
                  default=[13],
                  help_string=_('Read value from digital port.'),
                  prim_name='digitalread')
        self.tw.lc.def_prim('digitalread', 1,
            lambda self, x:
            primitive_dictionary['digitalread'](x))

        primitive_dictionary['high'] = self._prim_high
        palette.add_block('high',
                  style='box-style',
                  label=_('HIGH'),
                  help_string=_('Set HIGH value for digital port.'),
                  prim_name='high')
        self.tw.lc.def_prim('high', 0,
            lambda self: primitive_dictionary['high']())

        primitive_dictionary['input'] = self._prim_input
        palette.add_block('input',
                  style='box-style',
                  label=_('INPUT'),
                  help_string=_('Configure Arduino port for digital input.'),
                  prim_name='input')
        self.tw.lc.def_prim('input', 0,
            lambda self: primitive_dictionary['input']())

        primitive_dictionary['servo'] = self._prim_servo
        palette.add_block('servo',
                  style='box-style',
                  label=_('SERVO'),
                  help_string=_('Configure Arduino port to drive a servo.'),
                  prim_name='servo')
        self.tw.lc.def_prim('servo', 0,
            lambda self: primitive_dictionary['servo']())

        primitive_dictionary['low'] = self._prim_low
        palette.add_block('low',
                  style='box-style',
                  label=_('LOW'),
                  help_string=_('Set LOW value for digital port.'),
                  prim_name='low')
        self.tw.lc.def_prim('low', 0,
            lambda self: primitive_dictionary['low']())

        primitive_dictionary['output'] = self._prim_output
        palette.add_block('output',
                  style='box-style',
                  label=_('OUTPUT'),
                  help_string=_('Configure Arduino port for digital output.'),
                  prim_name='output')
        self.tw.lc.def_prim('output', 0,
            lambda self: primitive_dictionary['output']())

        primitive_dictionary['pwm'] = self._prim_pwm
        palette.add_block('pwm',
                  style='box-style',
                  label=_('PWM'),
                  help_string=_('Configure Arduino port for PWM (pulse-width modulation).'),
                  prim_name='pwm')
        self.tw.lc.def_prim('pwm', 0,
            lambda self: primitive_dictionary['pwm']())

    def start(self):
        pass

    def quit(self):
        pass

    def stop(self):
        pass

    def clear(self):
        pass

    def _check_init(self):
        if self._arduino:
            self._arduino.parse()

    def _prim_pin_mode(self, pin, mode):
        self._check_init()
        mode = MODE[mode]
        self._arduino.pin_mode(int(pin), mode)
            
    def _prim_analog_write(self, pin, value):
        self._check_init()
        value = int(value)
        self._arduino.analog_write(int(pin), value)

    def _prim_digital_write(self, pin, value):
        self._check_init()
        value = VALUE[value]
        self._arduino.digital_write(int(pin), value)
        
    def _prim_analog_read(self, pin):
        self._check_init()
        self._arduino.parse()
        res = -1
        res = self._arduino.analog_read(int(pin))
        return res

    def _prim_digital_read(self, pin):
        self._check_init()
        self._arduino.parse()
        res = -1
        res = self._arduino.digital_read(int(pin))

        if res == VALUE[_('HIGH')]:
            res = _('HIGH')
        elif res == VALUE[_('LOW')]:
            res = _('LOW')

        return res
            
    def _prim_high(self):
        return _('HIGH')

    def _prim_low(self):
        return _('LOW')

    def _prim_input(self):
        return _('INPUT')

    def _prim_output(self):
        return _('OUTPUT')

    def _prim_pwm(self):
        return _('PWM')

    def _prim_servo(self):
        return _('SERVO')


_______________________________________________
IAEP -- It's An Education Project (not a laptop project!)
[email protected]
http://lists.sugarlabs.org/listinfo/iaep

Reply via email to