├── setup.cfg ├── requirements.txt ├── .travis.yml ├── pluto ├── exceptions.py ├── boards.py ├── utils.py └── pluto.py ├── examples ├── blink.py ├── led.py └── sine_led.py ├── .gitignore ├── LICENSE ├── setup.py ├── README.md └── tests.py /setup.cfg: -------------------------------------------------------------------------------- 1 | [metadata] 2 | description-file = README.md 3 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | pyserial 2 | pyFirmata 3 | unittest2 4 | mock 5 | future 6 | 7 | 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | 3 | python: 4 | - "2.6" 5 | - "2.7" 6 | - "3.2" 7 | - "3.3" 8 | - "3.4" 9 | 10 | install: 11 | - pip install . --use-mirrors 12 | - pip install -r requirements.txt --use-mirrors 13 | 14 | script: nosetests 15 | -------------------------------------------------------------------------------- /pluto/exceptions.py: -------------------------------------------------------------------------------- 1 | #!/usr/env/bin python 2 | 3 | """Custom exceptions for Pluto""" 4 | 5 | class PinError(Exception): 6 | def __init__(self, value): 7 | self.value = value 8 | 9 | def __str__(self): 10 | return repr(self.value) 11 | -------------------------------------------------------------------------------- /examples/blink.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Blink 3 | Turns on an on-board LED on for one second, then off for one second repeatedly. 4 | 5 | Most Arduinos have an on-board LED you can control. On the Uno and Leonardo, 6 | it is attached to digital pin 13. If you're unsure what pin the on-board LED 7 | is connected to on your Arduino model, check the doc at http://arduino.cc 8 | 9 | This example code is in the public domain. 10 | 11 | modified June 13, 2015 12 | by Joe Chasinga 13 | ''' 14 | 15 | #!/usr/bin/env python 16 | 17 | import sys, os 18 | sys.path.append('../pluto') 19 | 20 | from pluto import * 21 | import time 22 | 23 | def main(): 24 | # Invoke a general board 25 | board = Board() 26 | board.led(13).blink() 27 | 28 | if __name__ == '__main__': 29 | main() 30 | 31 | -------------------------------------------------------------------------------- /examples/led.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Blink 3 | Turns on an on-board LED on for one second, and then off. 4 | 5 | Most Arduinos have an on-board LED you can control. On the Uno and Leonardo, 6 | it is attached to digital pin 13. If you're unsure what pin the on-board LED 7 | is connected to on your Arduino model, check the doc at http://arduino.cc 8 | 9 | This example code is in the public domain. 10 | 11 | modified July 12, 2015 12 | by Joe Chasinga 13 | ''' 14 | 15 | #!/usr/bin/env python 16 | 17 | import sys, os 18 | sys.path.append('../pluto') 19 | 20 | from pluto import * 21 | import time 22 | 23 | def main(): 24 | # Invoke a general board 25 | board = Board() 26 | board.led(13).on() 27 | time.sleep(1) 28 | # the board remembers the on-board led 29 | board.led.off() 30 | 31 | if __name__ == '__main__': 32 | main() 33 | 34 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Mac backup files 2 | *~ 3 | *# 4 | .#* 5 | *.bak 6 | .DS_Store 7 | 8 | # Byte-compiled / optimized / DLL files 9 | __pycache__/ 10 | *.py[cod] 11 | *$py.class 12 | 13 | # C extensions 14 | *.so 15 | 16 | # Distribution / packaging 17 | .Python 18 | env/ 19 | build/ 20 | develop-eggs/ 21 | dist/ 22 | downloads/ 23 | eggs/ 24 | .eggs/ 25 | lib/ 26 | lib64/ 27 | parts/ 28 | sdist/ 29 | var/ 30 | *.egg-info/ 31 | .installed.cfg 32 | *.egg 33 | 34 | # PyInstaller 35 | # Usually these files are written by a python script from a template 36 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 37 | *.manifest 38 | *.spec 39 | 40 | # Installer logs 41 | pip-log.txt 42 | pip-delete-this-directory.txt 43 | 44 | # Unit test / coverage reports 45 | htmlcov/ 46 | .tox/ 47 | .coverage 48 | .coverage.* 49 | .cache 50 | nosetests.xml 51 | coverage.xml 52 | *,cover 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | 61 | # Sphinx documentation 62 | docs/_build/ 63 | 64 | # PyBuilder 65 | target/ 66 | -------------------------------------------------------------------------------- /examples/sine_led.py: -------------------------------------------------------------------------------- 1 | ''' 2 | LED 3 | Turns on and off (blink) an on-board LED with decremental intervals. 4 | 5 | Pluto has collect some number of Arduino boards with on-board LED attached to pin 13. For these boards, Pluto can recognize automatically through the use of the board's class. If unsure, consult the doc at http://arduino.cc and use general Board class, then supply the pin number as the argument to led callable. 6 | 7 | This example code is in the public domain. 8 | 9 | modified June 22, 2015 10 | by Joe Chasinga 11 | ''' 12 | 13 | #!/usr/bin/env python 14 | 15 | from __future__ import division 16 | import sys, os, math 17 | sys.path.append('../pluto') 18 | 19 | from pluto import * 20 | import time 21 | 22 | board = Board() 23 | 24 | def blink(): 25 | angle = 0 26 | 27 | while True: 28 | t = abs(math.sin(angle)) 29 | 30 | board.led(13).on() 31 | time.sleep(t/12) 32 | board.led(13).off() 33 | time.sleep(t/12) 34 | 35 | angle = angle + 0.02 36 | 37 | if __name__ == '__main__': 38 | blink() 39 | 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Joe Chasinga 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from distutils.core import setup 4 | 5 | with open('README.md') as f: 6 | long_description = f.read() 7 | 8 | setup( 9 | name='pluto', 10 | version='0.1.0', 11 | description="A Python interface for the Firmata protocol", 12 | long_description=long_description, 13 | author='Joe Chasinga', 14 | author_email='jo.chasinga@gmail.com', 15 | packages=['pluto'], 16 | include_package_data=True, 17 | install_requires=[ 18 | 'pyserial', 19 | 'pyfirmata', 20 | 'unittest2', 21 | 'future', 22 | 'mock', 23 | ], 24 | zip_safe=False, 25 | url='https://github.com/jochasinga/pluto', 26 | download_url = 'https://github.com/jochasinga/pluto/tarball/0.1.0', 27 | keywords = ['arduino', 'robotics', 'diy', 'iot', 'electronics'], 28 | classifiers=[ 29 | 'Development Status :: 4 - Beta', 30 | 'Environment :: Other Environment', 31 | 'Intended Audience :: Developers', 32 | 'License :: OSI Approved :: MIT License', 33 | 'Operating System :: OS Independent', 34 | 'Programming Language :: Python', 35 | 'Programming Language :: Python :: 2.6', 36 | 'Programming Language :: Python :: 2.7', 37 | 'Topic :: Utilities', 38 | 'Topic :: Home Automation', 39 | ], 40 | ) 41 | 42 | -------------------------------------------------------------------------------- /pluto/boards.py: -------------------------------------------------------------------------------- 1 | from builtins import range 2 | BOARDS = { 3 | 'arduino' : { 4 | 'digital' : tuple(x for x in range(14)), 5 | 'analog' : tuple(x for x in range(6)), 6 | 'pwm' : (3, 5, 6, 9, 10, 11), 7 | 'use_ports' : True, 8 | 'disabled' : (0, 1) # Rx, Tx, Crystal 9 | }, 10 | 'arduino_mega' : { 11 | 'digital' : tuple(x for x in range(54)), 12 | 'analog' : tuple(x for x in range(16)), 13 | 'pwm' : tuple(x for x in range(2,14)), 14 | 'use_ports' : True, 15 | 'disabled' : (0, 1) # Rx, Tx, Crystal 16 | }, 17 | 'arduino_due': { 18 | 'digital': tuple(x for x in range(54)), 19 | 'analog': tuple(x for x in range(12)), 20 | 'pwm': tuple(x for x in range(2, 14)), 21 | 'use_ports': True, 22 | 'disabled': (0, 1) # Rx, Tx, Crystal 23 | }, 24 | 'arduino_yun' : { 25 | 'digital' : tuple(x for x in range(20)), 26 | 'analog' : tuple(x for x in range(14, 19)), 27 | 'pwm' : (3, 5, 6, 9, 10, 11), 28 | 'use_ports' : True, 29 | 'disabled' : (0, 1) # Rx, Tx, Crystal 30 | }, 31 | 'spark_core' : { 32 | 'digital' : tuple(x for x in range(7)), 33 | 'analog' : tuple(x for x in range(10, 17)), 34 | 'pwm' : (0, 1, 10, 11, 14, 15, 16, 17), 35 | 'use_ports' : True, 36 | 'disabled' : (18, 19) 37 | }, 38 | 'arduino_lilypad' : { 39 | 'digital' : tuple(x for x in range(4)), 40 | 'analog' : tuple(x for x in range(16, 21)), 41 | 'pwm' : (3, 7, 8, 11, 12, 13), 42 | 'use_ports' : True, 43 | 'disabled' : (0, 1) # Rx, Tx, Crystal 44 | }, 45 | 'arduino_lilypad_usb': { 46 | 'digital' : tuple(x for x in range(9)), 47 | 'analog' : tuple(x for x in range(4)), 48 | 'pwm' : (3, 9, 10, 11, 13), 49 | 'use_ports' : True, 50 | 'disabled' : (0, 1) # Rx, Tx, Crystal 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /pluto/utils.py: -------------------------------------------------------------------------------- 1 | from builtins import object 2 | import pluto 3 | import re 4 | import serial.tools.list_ports 5 | import time 6 | from pyfirmata import util, OUTPUT, INPUT, PWM 7 | from pluto import LOW, HIGH, LED_BUILTIN 8 | import exceptions 9 | 10 | class ArduinoUtil(object): 11 | """ 12 | A utility class containing all the Arduino-esque functions 13 | """ 14 | @staticmethod 15 | def digitalWrite(board, pin_number, value): 16 | if isinstance(board, pluto.Board): 17 | if board.digital[pin_number].mode != OUTPUT: 18 | board.digital[pin_number].mode = OUTPUT 19 | else: 20 | pass 21 | board.digital[pin_number].write(value) 22 | else: 23 | raise TypeError("The object isn't an instance of 'pluto.Board'") 24 | 25 | @staticmethod 26 | def digitalRead(board, pin_number): 27 | if isinstance(board, pluto.Board): 28 | if board.digital[pin_number].mode != INPUT: 29 | board.digital[pin_number].mode = INPUT 30 | else: 31 | pass 32 | board.digital[pin_number].read() 33 | else: 34 | raise TypeError("The object isn't an instance of 'pluto.Board'.") 35 | 36 | @staticmethod 37 | def analogWrite(board, pin_number, value): 38 | if isinstance(board, pluto.Board): 39 | if board.digital[pin_number].PWM_CAPABLE: 40 | if board.digital[pin_number].mode != 3: 41 | board.digital[pin_number]._set_mode(PWM) 42 | else: 43 | pass 44 | board.digital[pin_number].write(value) 45 | else: 46 | raise exceptions.PinError("This pin is not PWM capable.") 47 | else: 48 | raise TypeError("The object isn't an instance of 'pluto.Board'.") 49 | 50 | @staticmethod 51 | def analogRead(board, pin_number): 52 | if isinstance(board, pluto.Board): 53 | board.analog[pin_number].read() 54 | else: 55 | raise TypeError("The object isn't an instance of 'pluto.Board'.") 56 | 57 | class PortUtil(object): 58 | """Helper class that scan serial port automatically""" 59 | comports = [p[0] for p in serial.tools.list_ports.comports()] 60 | num_ports = len(comports) 61 | auto_port = None 62 | keywords = [] 63 | patterns = [] 64 | 65 | @classmethod 66 | def count_ports(cls): 67 | return cls.num_ports 68 | 69 | @classmethod 70 | def scan(cls, *args, **kwargs): 71 | if len(args) == 0: 72 | cls.keywords = ['usb', 'serial'] 73 | else: 74 | for index, val in enumerate(args): 75 | cls.keywords.append(val) 76 | 77 | for keyword in cls.keywords: 78 | p = re.compile('(/dev/)((tty)|(cu)|.*).({0})\w*[\d]'.format(keyword)) 79 | cls.patterns.append(p) 80 | 81 | for port in cls.comports: 82 | for pattern in cls.patterns: 83 | m = pattern.match(port) 84 | if m: 85 | cls.auto_port = m.group() 86 | else: 87 | pass 88 | 89 | return cls.auto_port 90 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | pluto 2 | ===== 3 | The Python Arduino framework built on top of [pyFirmata](https://github.com/tino/pyFirmata). 4 | 5 | Intention 6 | ========= 7 | A high-level Python interface to control Arduino boards for easy prototyping. 8 | Compatible with Python >= 2.6 and 3.x. 9 | 10 | 11 | Test status: 12 | 13 | ![build-status](https://travis-ci.org/jochasinga/pluto.svg?branch=master) 14 | 15 | Installation 16 | ============ 17 | 18 | ```bash 19 | 20 | git clone https://github.com/jochasinga/pluto 21 | cd pluto 22 | python setup.py install 23 | 24 | ``` 25 | setuptools: [https://pypi.python.org/pypi/setuptools](https://pypi.python.org/pypi/setuptools) 26 | 27 | It is currently not on Pypi, so `pip` installation is not available yet. 28 | 29 | Upload Firmata 30 | ============== 31 | + Fire up Arduino software 32 | + Choose the appropriate board and port name 33 | + Go to File > Examples > Firmata > StandardFirmata 34 | + Click upload 35 | 36 | Download the Arduino software here: [http://www.arduino.cc/en/Main/Software](http://www.arduino.cc/en/Main/Software) 37 | 38 | Examples 39 | ======== 40 | 41 | All code snippets here are using Python interactive shell. To use it, open the terminal app and on the command line type `python` to enter the `>>>` interpreter mode. 42 | 43 | Creating a Board 44 | ---------------- 45 | >>> from pluto import * # import everything 46 | >>> board = Uno('/dev/cu.usbmodem1411') # for Arduino Uno 47 | 48 | *note*: `from import *` is not necessary and not a good idea if you will be doing any development, but for simple usage it imports convenient constants like `LOW`, `HIGH`, and `LED_BUILTIN`. 49 | 50 | **pluto** has `PortUtil` class which use **regex** to search for a relevant port. 51 | 52 | >>> PortUtil.scan() # -> '/dev/cu.usbmodem1411' 53 | 54 | Every board instance has a `PortUtil` object created. This makes it possible to create an instance of any board without explicitly providing the port name. 55 | 56 | >>> board = Board() # Create a generic board 57 | >>> my_lily = LilypadUSB() # Create a Lilypad USB board 58 | 59 | Control an LED 60 | -------------- 61 | 62 | Controlling an on-board LED is very easy. 63 | 64 | >>> board.led.on() 65 | >>> board.led.off() 66 | >>> board.led.blink() 67 | 68 | However, to control an on-board LED on a "generic" board created by the `Board` class, you should provide a pin number of the on-board LED **for the first time**: 69 | 70 | >>> board.led(9).on() # This board has a built-in LED on pin 9 71 | >>> board.led.on() # The pin number is injected into `led` attribute. 72 | 73 | To control an LED connected to an arbitrary pin: 74 | 75 | >>> board.led(pin_number).on() 76 | 77 | Note that the `led` attribute of the board does not change to that latest pin number but stay as the on-board LED. 78 | 79 | You can also create Led instances separately 80 | 81 | >>> led = Led(board) # This creates Led instance at pin 13 82 | >>> led.on() 83 | >>> led.off() 84 | >>> led_1 = Led(board, 9) # Led instance at pin 9 85 | >>> led_1.strobe() 86 | 87 | Control a Pin 88 | ------------- 89 | 90 | In Pluto, `Led` inherits from `Pin`, therefore you can do more basic things like writing and reading from a pin. 91 | 92 | >>> pin = Pin(board, 10) # Initiate a `Pin` instance at pin 10 93 | >>> pin.high() # Guess what this does! 94 | >>> pin.low() 95 | >>> pin.pulse() # Use PWM just like Led's strobe. 96 | 97 | To read the pin value: 98 | 99 | >>> pin.read() # Default to digitalRead 100 | >>> pin.read(mode=ANALOG) # Switch to analogRead (If it's an analog pin) 101 | 102 | Just like `Led` you can control pins as the board's attribute. 103 | 104 | >>> board = Board() 105 | >>> board.pin(10).high() 106 | 107 | Arduino-style APIs 108 | ------------------ 109 | 110 | `ArduinoUtil` instance is instantiated with the board to allow Arduino C-style APIs for arduino users. 111 | 112 | >>> board = Board() 113 | >>> board.digitalWrite(13, HIGH) 114 | >>> board.digitalWrite(13, LOW) 115 | >>> board.analogWrite(9, 0.5) 116 | >>> my_val = board.pin(13).digitalRead() 117 | 118 | It's not Pythonic, with camelCase methods but it's a set of APIs for Arduino users. Maybe will consider using under_score. 119 | 120 | Run code in *examples/* to find out more. 121 | 122 | TODO 123 | ==== 124 | 125 | It's just a start, and there's a lot more to do. Here is a few ideas: 126 | 127 | + Implement PWM. (ONGOING) 128 | + Implement digitalRead and analogRead. 129 | + Optimize the code and divide into several modules. 130 | + Make *pluto* a fork of *pyFirmata* instead of relying on the library. 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | -------------------------------------------------------------------------------- /tests.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import sys, os 4 | import unittest2 as unittest 5 | import serial 6 | import pyfirmata 7 | import re 8 | import serial.tools.list_ports 9 | from pyfirmata import mockup 10 | from mock import Mock, patch 11 | 12 | sys.path.append('./pluto') 13 | import pluto 14 | from boards import BOARDS 15 | from utils import ArduinoUtil, PortUtil 16 | 17 | class BoardBaseTest(unittest.TestCase): 18 | """Base Class for generic boards""" 19 | def setUp(self): 20 | pluto.serial.Serial = mockup.MockupSerial 21 | pluto.BOARD_SETUP_WAIT_TIME = 0 22 | self.board = pluto.Board() 23 | 24 | def tearDown(self): 25 | self.board.destroy() 26 | 27 | class BoardUnoTest(BoardBaseTest): 28 | """Base Class for Arduino Uno""" 29 | def setUp(self): 30 | super(BoardUnoTest, self).setUp() 31 | self.board = pluto.Uno() 32 | 33 | class BoardLilypadUSBTest(BoardBaseTest): 34 | """Base Class for Arduino LilypadUSB""" 35 | def setUp(self): 36 | super(BoardLilypadUSBTest, self).setUp() 37 | self.board = pluto.LilypadUSB() 38 | 39 | class BoardInitTest(BoardBaseTest): 40 | """Test for generic board""" 41 | def test_board_is_initiated(self): 42 | self.assertIsInstance(self.board, pluto.Board) 43 | 44 | def test_board_name_defined(self): 45 | self.assertEqual(self.board.name, None) 46 | 47 | def test_board_can_control_pin(self): 48 | self.assertIs(self.board.pin(13).high(), self.board.digital[13].write(1)) 49 | self.assertIs(self.board.pin(13).low(), self.board.digital[13].write(0)) 50 | self.assertIs(self.board.pin(10).high(), self.board.digital[10].write(1)) 51 | self.assertIs(self.board.pin(10).low(), self.board.digital[10].write(0)) 52 | 53 | def test_board_remember_onboard_led(self): 54 | self.board.led(13).on() 55 | self.assertIs(self.board.led.off(), self.board.digital[13].write(0)) 56 | self.assertIs(self.board.led.on(), self.board.digital[13].write(1)) 57 | 58 | def test_board_can_control_onboard_led(self): 59 | self.assertIs(self.board.led(13).on(), self.board.digital[13].write(1)) 60 | self.assertIs(self.board.led(13).off(), self.board.digital[13].write(0)) 61 | 62 | def test_board_can_control_arbitrary_led(self): 63 | self.assertIs(self.board.led(10).on(), self.board.digital[10].write(1)) 64 | self.assertIs(self.board.led(10).off(), self.board.digital[10].write(0)) 65 | 66 | # TODO: Test blink method 67 | # TODO: Test strope method 68 | 69 | class BoardUnoInitTest(BoardUnoTest, BoardInitTest): 70 | """Test for Uno""" 71 | def test_board_is_initiated(self): 72 | self.assertIsInstance(self.board, pluto.Uno) 73 | 74 | def test_board_name_defined(self): 75 | self.assertEqual(self.board.name, 'arduino') 76 | 77 | class PortUtilityTest(unittest.TestCase): 78 | """Test for PortUtil methods""" 79 | def setUp(self): 80 | self.mock_portutil = Mock(spec=PortUtil) 81 | self.mock_portutil.num_ports = 3 82 | self.mock_portutil.auto_ports = '/dev/cu.usbmodem7321' 83 | self.mock_portutil.count_ports.return_value = self.mock_portutil.num_ports 84 | self.mock_portutil.scan.return_value = self.mock_portutil.auto_ports 85 | 86 | def test_auto_port_count(self): 87 | self.assertEqual(self.mock_portutil.count_ports(), self.mock_portutil.num_ports) 88 | 89 | def test_auto_port_discovery(self): 90 | self.assertEqual(self.mock_portutil.scan(), '/dev/cu.usbmodem7321') 91 | 92 | class ArduinoUtilityTest(BoardBaseTest): 93 | """Test for ArduinoUtil methods""" 94 | def test_digital_write(self): 95 | self.assertIs(self.board.digitalWrite(13, 1), self.board.digital[13].write(1)) 96 | 97 | def test_digital_read(self): 98 | self.assertIs(self.board.digitalRead(13), self.board.digital[13].read()) 99 | 100 | def test_analog_write(self): 101 | self.assertIs(self.board.analogWrite(9, 0.5), self.board.digital[9].write(0.5)) 102 | 103 | def test_analog_read(self): 104 | self.assertIs(self.board.analogRead(3), self.board.analog[3].read()) 105 | 106 | class PinTest(unittest.TestCase): 107 | """Test for pins""" 108 | def setUp(self): 109 | self.board = pluto.Board() 110 | self.pin = pluto.Pin(self.board, 13) 111 | 112 | def test_pin_initiated(self): 113 | self.assertIsInstance(self.pin, pluto.Pin) 114 | 115 | def test_write_pin(self): 116 | self.assertIs(self.pin.high(), self.board.digital[13].write(1)) 117 | self.assertIs(self.pin.low(), self.board.digital[13].write(0)) 118 | 119 | def test_read_pin(self): 120 | # TODO: Write test for reading analog and digital pins 121 | pass 122 | 123 | class TestBoardMessages(BoardBaseTest): 124 | def test_assert_serial(self, *incoming_bytes): 125 | serial_msg = bytearray() 126 | res = self.board.sp.read() 127 | while res: 128 | serial_msg += res 129 | res = self.board.sp.read() 130 | self.assertEqual(bytearray(incoming_bytes), serial_msg) 131 | 132 | if __name__ == '__main__': 133 | unittest.main(verbosity=2) 134 | -------------------------------------------------------------------------------- /pluto/pluto.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | from __future__ import division 3 | 4 | from pyfirmata import * 5 | from boards import BOARDS 6 | import time 7 | 8 | __version__ = '0.1.0' 9 | 10 | # convenient contants 11 | LOW = 0 12 | HIGH = 1 13 | LED_BUILTIN = 13 14 | DIGITAL = 0 15 | ANALOG = 1 16 | 17 | from utils import ArduinoUtil, PortUtil 18 | 19 | class Board(pyfirmata.Board): 20 | """The Base class for any board.""" 21 | # TODO: auto-scan for type of board 22 | def __init__(self, port=None, layout=BOARDS['arduino'], baudrate=57600, name=None, timeout=None): 23 | """ 24 | default_leds = [ 25 | 'arduino', 26 | 'arduino_leonardo', 27 | 'arduino_mega', 28 | 'arduino_due', 29 | 'arduino_yun', 30 | 'arduino_zero', 31 | 'arduino_micro', 32 | 'arduino_lilypad_usb', 33 | ] 34 | """ 35 | # Arduino boards with on-board digital LED @ pin 13 36 | # TODO: Move callable hooks to children Board classes to avoid using class's name 37 | self.DEFAULT_LEDS = [ 38 | 'Uno', 39 | 'LilypadUSB', 40 | ] 41 | 42 | def led_hook(pin_number): 43 | led = Led(self, pin_number) 44 | self.led = led 45 | return led 46 | 47 | def pin_hook(pin_number): 48 | pin = Pin(self, pin_number) 49 | self.pin = pin 50 | return pin 51 | 52 | self.name = name 53 | self.util = ArduinoUtil() 54 | self.pin = (lambda pin_number: pin_hook(pin_number)) 55 | #self.led = Led(self) if self.name in default_leds else (lambda pin_number: led_hook(pin_number)) 56 | # Use class's name instead of name attribute 57 | self.led = Led(self) if self.__class__.__name__ in self.DEFAULT_LEDS else (lambda pin_number: led_hook(pin_number)) 58 | auto_port = PortUtil.scan() 59 | self.sp = serial.Serial(auto_port, baudrate, timeout=timeout) 60 | # Allow 5 secs for Arduino's auto-reset to happen 61 | # Alas, Firmata blinks its version before printing it to serial 62 | # For 2.3, even 5 seconds might not be enough. 63 | # TODO Find a more reliable way to wait until the board is ready 64 | self.pass_time(BOARD_SETUP_WAIT_TIME) 65 | self._layout = layout 66 | self._iterator = False 67 | self.it = util.Iterator(self) 68 | if not self.name: 69 | self.name = port 70 | else: 71 | pass 72 | 73 | if layout: 74 | self.setup_layout(layout) 75 | else: 76 | self.auto_setup() 77 | 78 | if self._iterator: 79 | self.it.start() 80 | 81 | # Iterate over the first messages to get firmware data 82 | while self.bytes_available(): 83 | self.iterate() 84 | # TODO Test whether we got a firmware name and version, otherwise there 85 | # probably isn't any Firmata installed 86 | 87 | def digitalWrite(self, pin_number, value): 88 | self.util.digitalWrite(self, pin_number, value) 89 | 90 | def digitalRead(self, pin_number): 91 | self.util.digitalRead(self, pin_number) 92 | 93 | def analogWrite(self, pin_number, value): 94 | self.util.analogWrite(self, pin_number, value) 95 | 96 | def analogRead(self, pin_number): 97 | self.util.analogRead(self, pin_number) 98 | 99 | def destroy(self): 100 | super(Board, self).exit() 101 | 102 | class Pin(pyfirmata.Pin): 103 | """Pluto's Pin representation""" 104 | def __init__(self, board, pin_number=LED_BUILTIN, type=ANALOG, port=None): 105 | super(Pin, self).__init__(board, pin_number, type, port) 106 | self.pin_number = pin_number 107 | self.util = ArduinoUtil() 108 | 109 | def __call__(self, pin_number): 110 | return Pin(self.board, pin_number) 111 | 112 | def digitalWrite(self, *args, **kwargs): 113 | for index, val in enumerate(args): 114 | if index == 0: 115 | self.pin_number = val 116 | elif index == 1: 117 | self.value = val 118 | 119 | if kwargs is not None: 120 | for key, val in kwargs.items(): 121 | if key == "pin_number": 122 | self.pin_number = val 123 | elif key == "value": 124 | self.value = val 125 | else: 126 | pass 127 | 128 | self.util.digitalWrite(self.board, self.pin_number, self.value) 129 | 130 | def digitalRead(self, *args, **kwargs): 131 | for index, val in enumerate(args): 132 | if index == 0: 133 | self.pin_number = val 134 | elif index == 1: 135 | self.value = val 136 | 137 | if kwargs is not None: 138 | for key, val in kwargs.items(): 139 | if key == "pin_number": 140 | self.pin_number = val 141 | elif key == "value": 142 | self.value = val 143 | else: 144 | pass 145 | 146 | self.util.digitalRead(self.board, self.pin_number) 147 | 148 | def analogWrite(self, *args, **kwargs): 149 | for index, val in enumerate(args): 150 | if index == 0: 151 | self.pin_number = val 152 | elif index == 1: 153 | self.value = val 154 | 155 | if kwargs is not None: 156 | for key, val in kwargs.items(): 157 | if key == "pin_number": 158 | self.pin_number = val 159 | elif key == "value": 160 | self.value = val 161 | else: 162 | pass 163 | 164 | self.util.analogWrite(self.board, self.pin_number, self.value) 165 | 166 | def high(self): 167 | self.digitalWrite(pin_number=self.pin_number, value=HIGH) 168 | 169 | def low(self): 170 | self.digitalWrite(pin_number=self.pin_number, value=LOW) 171 | 172 | def alternate(self, interval=1, forever=True): 173 | while forever: 174 | self.digitalWrite(pin_number=self.pin_number, value=HIGH) 175 | time.sleep(interval) 176 | self.digitalWrite(pin_number=self.pin_number, value=LOW) 177 | time.sleep(interval) 178 | 179 | def pulse(self, step=1, forever=True): 180 | val = 0 181 | while forever: 182 | if val <= 10000: 183 | val = val + step 184 | else: 185 | val = val - step 186 | 187 | self.analogWrite(pin_number=self.pin_number, value=val/10000) 188 | 189 | def read(self, mode=DIGITAL): 190 | if self._iterator: 191 | pass 192 | else: 193 | self._iterator = True 194 | if mode == DIGITAL: 195 | self.board.digitalRead(self.pin_number) 196 | elif mode == ANALOG: 197 | # TODO: Implement analog read 198 | pass 199 | 200 | class Led(Pin): 201 | """Led representation""" 202 | def __init__(self, board, pin_number=LED_BUILTIN, type=ANALOG, port=None): 203 | super(Led, self).__init__(board, pin_number, type, port) 204 | self.blinking = False 205 | 206 | def __call__(self, pin_number): 207 | return Led(self.board, pin_number) 208 | 209 | def on(self): 210 | self.board.digitalWrite(self.pin_number, HIGH) 211 | 212 | def off(self): 213 | self.board.digitalWrite(self.pin_number, LOW) 214 | 215 | def blink(self, interval=1, forever=True): 216 | super(Led, self).alternate(interval, forever) 217 | self.blinking = True 218 | 219 | def strobe(self, step=1, forever=True): 220 | super(Led, self).pulse(step, forever) 221 | self.strobing = True 222 | 223 | class Uno(Board): 224 | """ 225 | A board that will set itself up as an Arduino Uno 226 | """ 227 | def __init__(self, *args, **kwargs): 228 | super(Uno, self).__init__(*args, **kwargs) 229 | self.name = 'arduino' 230 | 231 | def __str__(self): 232 | super(Uno, self).__str__() 233 | 234 | class Mega(Board): 235 | """ 236 | A board that will set itself up as an ArduinoMega 237 | """ 238 | def __init__(self, *args, **kwargs): 239 | args = list(args) 240 | args.append(BOARDS['arduino_mega']) 241 | super(Mega, self).__init__(*args, **kwargs) 242 | 243 | def __str__(self): 244 | super(Mega, self).__str__() 245 | 246 | #TODO: Look at Serial conflict for Yun 247 | ''' 248 | class Yun(Board): 249 | """ 250 | A board that will set itself up as an ArduinoYun 251 | """ 252 | def __init__(self, *args, **kwargs): 253 | args = list(args) 254 | args.append(BOARDS['arduino_yun']) 255 | super(Yun, self).__init__(*args, **kwargs) 256 | ''' 257 | 258 | class SparkCore(Board): 259 | """ 260 | A board that will set itself up as a Spark Core 261 | """ 262 | def __init__(self, *args, **kwargs): 263 | super(SparkCore, self).__init__(*args, **kwargs) 264 | 265 | class LilypadUSB(Board): 266 | """ 267 | A board that will set itself up as an ArduinoLilypadUSB 268 | """ 269 | def __init__(self, *args, **kwargs): 270 | args = list(args) 271 | args.append(BOARDS['arduino_lilypad_usb']) 272 | super(LilypadUSB, self).__init__(*args, **kwargs) 273 | self.name = 'arduino_lilypad_usb' 274 | --------------------------------------------------------------------------------