diff options
author | Rafael Ortiz <rafael@activitycentral.com> | 2011-06-14 22:25:01 (GMT) |
---|---|---|
committer | Rafael Ortiz <rafael@activitycentral.com> | 2011-06-14 22:25:01 (GMT) |
commit | fd156531134c4ce2c9b60e5b3ded499eda89fdce (patch) | |
tree | c1746ecd5eec75641c97cfb8c34ee722ec438991 | |
parent | c9b92a2a244430c677b2e27645a23c082c6c8188 (diff) |
Initial arduino plugin implementation
-rw-r--r-- | plugins/arduino/arduino_plugin.py | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/plugins/arduino/arduino_plugin.py b/plugins/arduino/arduino_plugin.py new file mode 100644 index 0000000..3babe66 --- /dev/null +++ b/plugins/arduino/arduino_plugin.py @@ -0,0 +1,74 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2011 Rafael Ortiz rafael@activitycentral.com +# Arduino plugin for TurtleArt http://wiki.sugarlabs.org/go/Activities/TurtleArt +# +# 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 +# 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 + +#Imports for TA plugins +from TurtleArt.tapalette import make_palette, palette_name_to_index +from TurtleArt.talogo import primitive_dictionary +from TurtleArt.taconstants import BLACK, WHITE, CONSTANTS, BOX_COLORS +from TurtleArt.tautils import debug_output + +from gettext import gettext as _ + +# Arduino Setup +import firmata + +class TAArduino(object): + def __init__(self, dev='/dev/ttyUSB0', baud=115200): + object.__init__(self) + self._dev = dev + self._baud = baud + self._arduino = None # Do not initialize this now + + self.HIGH = firmata.HIGH + self.LOW = firmata.LOW + self.INPUT = firmata.INPUT + self.OUTPUT = firmata.OUTPUT + self.PWM = firmata.PWM + self.SERVO = firmata.SERVO + + def _check_init(self): + if self._arduino is None: + self._arduino = firmata.Arduino(port = self._dev, \ + baudrate=self._baud) + self._arduino.parse() + + def delay(self, secs): + # Do not use this. The firmata module uses time.sleep() to + # implement this, which breaks gtk+ (unresponsive window) + self._check_init() + self._arduino.delay(secs) + + def pin_mode(self, pin, mode): + self._check_init() + self._arduino.pin_mode(int(pin), mode) + + def analog_write(self, pin, value): + self._check_init() + self._arduino.analog_write(int(pin), int(value)) + + def digital_write(self, pin, value): + self._check_init() + self._arduino.digital_write(int(pin), value) + + def analog_read(self, pin): + self._check_init() + self._arduino.parse() #XXX: Not sure why I have to do this here. + return self._arduino.analog_read(int(pin)) + + def digital_read(self, pin): + self._check_init() + return self._arduino.digital_read(int(pin)) |