From 07b0ad83713224a55c74fc6ee0fa6f59035afa9d Mon Sep 17 00:00:00 2001 From: Simon John Date: Mon, 24 Mar 2014 13:27:36 +0000 Subject: [PATCH] replace perl reset script with python one --- README.md | 22 ++-- bin/ard-reset-arduino | 174 ++++++------------------------- packaging/debian/README.md | 2 +- packaging/fedora/README.md | 2 +- packaging/fedora/arduino-mk.spec | 9 +- 5 files changed, 52 insertions(+), 157 deletions(-) diff --git a/README.md b/README.md index d658789..5441f66 100644 --- a/README.md +++ b/README.md @@ -34,30 +34,36 @@ package and can be installed using `apt-get` or `aptitude`. You need to have the Arduino IDE. You can either install it through the installer or download the distribution zip file and extract it. -The Makefile also delegates resetting the board to a short Perl program. -You'll need to install `Device::SerialPort` to use it though. +The Makefile also delegates resetting the board to a short Python program. +You'll need to install `pySerial` to use it though. On Debian or Ubuntu: - apt-get install libdevice-serialport-perl + apt-get install python-serial On Fedora: - yum install perl-Device-SerialPort + yum install pyserial On openSUSE: - zypper install perl-Device-SerialPort + zypper install python-serial On Mac using MacPorts: - sudo port install p5-device-serialport + sudo port install py27-serial - and use /opt/local/bin/perl5 instead of /usr/bin/perl +On Windows: + + pySerial can be downloaded from PyPi On other systems: - cpan Device::SerialPort + pip install pyserial + + or + + easy_install -U pyserial ## Usage diff --git a/bin/ard-reset-arduino b/bin/ard-reset-arduino index bb52736..ec23023 100755 --- a/bin/ard-reset-arduino +++ b/bin/ard-reset-arduino @@ -1,147 +1,37 @@ -#!/usr/bin/env perl +#!/usr/bin/python -use strict; -use warnings; +import serial +import os.path +import argparse +from time import sleep -use Device::SerialPort; -use Getopt::Long; -use Pod::Usage; +parser = argparse.ArgumentParser(description='Reset an Arduino') +parser.add_argument('--caterina', action='store_true', help='Reset a Leonardo, Micro, Robot or LilyPadUSB.') +parser.add_argument('--verbose', action='store_true', help="Watch what's going on on STDERR.") +parser.add_argument('--period', default=0.1, help='Specify the DTR pulse width in seconds.') +parser.add_argument('port', nargs=1, help='Serial device e.g. /dev/ttyACM0') +args = parser.parse_args() -my %Opt = - ( - period => 0.1, - ); - -GetOptions(\%Opt, - "period=f", # width of reset pulse in seconds - "verbose!", - "help!", - "info!", - "caterina!", - ); - -if ($Opt{help} || $Opt{info}) - { - usage(); - } - -die "No Arduinos found!\n" - unless @ARGV; - -foreach my $dev (@ARGV) - { - my $p = Device::SerialPort->new($dev) - or die "Unable to open $dev: $!\n"; - - if ($Opt{caterina}) - { - $p->baudrate(1200); - $p->write_settings; - $p->close; - - print STDERR "Forcing reset using 1200bps open/close on port $dev\n" - if $Opt{verbose}; - - # wait for it to come back - sleep 1; - while( ! -e $dev ) { - print STDERR "Waiting for $dev to come back\n" - if $Opt{verbose}; - sleep 1; - } - - print STDERR "$dev has come back after reset\n" - if $Opt{verbose}; - } - else - { - my $dt = $Opt{period}; - - print STDERR "Setting DTR high for ${dt}s on $dev\n" - if $Opt{verbose}; - - die "Invalid pulse width ($dt), " - unless $dt > 0.0; - - $p->pulse_dtr_on($dt * 1000.0); - } - } - -## here endeth the main - -sub usage - { - pod2usage(-verbose => 2); - } - -__END__ - -=head1 NAME - -ard-reset-arduino - Reset an Arduino - -=head1 USAGE - - $ ard-reset-arduino /dev/cu.usb* - - $ ard-reset-arduino --verbose --period=0.1 /dev/cu.usb* - - $ ard-reset-arduino --verbose --caterina /dev/ttyUSB0 - -=head1 DESCRIPTION - -To reset (most) Arduinos, it's enough to just pulse the DTR line. - -You can do that from the shell with stty, but there's an interesting -diversity of command flags. This little program gives a uniform interface -at the cost of requiring C. - -=head1 OPTIONS - -=over - -=item --verbose - -Watch what's going on on STDERR. - -=item --period=0.25 - -Specify the DTR pulse width in seconds. - -=item --caterina - -Reset a Leonardo, Micro, Robot or LilyPadUSB. - -=back - -=head1 BUGS AND LIMITATIONS - -There are no known bugs in this application. - -Please report problems to the author. - -Patches are welcome. - -=head1 AUTHOR - -Martin Oldfield, ex-atelier@mjo.tc - -Support for Leonardo/Micro added by sej7278, https://github.com/sej7278 - -Thanks to Daniele Vergini who suggested this to me, and supplied -a command line version. - -=head1 LICENCE AND COPYRIGHT - -Copyright (c) 2012, Martin Oldfield. All rights reserved. - -This file is free software; you can redistribute it and/or modify it -under the terms of the GNU Lesser General Public License as published -by the Free Software Foundation; either version 2.1 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. +if args.caterina: + if args.verbose: print 'Forcing reset using 1200bps open/close on port %s' % args.port[0] + ser = serial.Serial(args.port[0], 57600) + ser.close() + ser.open() + ser.close() + ser.setBaudrate(1200) + ser.open() + ser.close() + sleep(1) + while not os.path.exists(args.port[0]): + if args.verbose: print 'Waiting for %s to come back' % args.port[0] + sleep(1) + if args.verbose: print '%s has come back after reset' % args.port[0] +else: + if args.verbose: print 'Setting DTR high on %s for %ss' % (args.port[0],args.period) + ser = serial.Serial(args.port[0], 115200) + ser.setDTR(False) + sleep(args.period) + ser.setDTR(True) + ser.close() diff --git a/packaging/debian/README.md b/packaging/debian/README.md index 26efa5a..c89bb43 100644 --- a/packaging/debian/README.md +++ b/packaging/debian/README.md @@ -7,7 +7,7 @@ or [Ubuntu](https://launchpad.net/ubuntu/+source/arduino-mk) or use apt. First install the dependencies for building/running the package, as root: apt-get build-dep arduino-mk - apt-get install arduino-core libdevice-serialport-perl help2man build-essential dpkg-dev fakeroot perl-doc devscripts + apt-get install arduino-core build-essential dpkg-dev fakeroot devscripts python-serial Fetch the Debian source: diff --git a/packaging/fedora/README.md b/packaging/fedora/README.md index 18c5955..37219cb 100644 --- a/packaging/fedora/README.md +++ b/packaging/fedora/README.md @@ -2,7 +2,7 @@ First install the dependencies as root: - yum install arduino-core perl-Device-SerialPort help2man rpm-build + yum install arduino-core rpm-build pyserial From the top-level Arduino-Makefile directory you've checked out of github, run the following (as unprivileged user) to create a compressed tarball using the naming conventions required by rpmbuild: diff --git a/packaging/fedora/arduino-mk.spec b/packaging/fedora/arduino-mk.spec index f8d6a07..88f4083 100644 --- a/packaging/fedora/arduino-mk.spec +++ b/packaging/fedora/arduino-mk.spec @@ -9,8 +9,8 @@ Group: Development/Tools License: LGPLv2+ BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) BuildArch: noarch -Requires: arduino-core, perl-Device-SerialPort -BuildRequires: arduino-core, perl-Device-SerialPort, help2man +Requires: arduino-core pyserial +BuildRequires: arduino-core pyserial %description Arduino is an open-source electronics prototyping platform based on @@ -27,7 +27,6 @@ Arduino platform. %install mkdir -p %{buildroot}/%{_datadir}/arduino mkdir -p %{buildroot}/%{_bindir} -mkdir -p %{buildroot}/%{_mandir}/man1 mkdir -p %{buildroot}/%{_docdir}/%{name}/examples install -m 755 -d %{buildroot}/%{_docdir}/%{name} install -m 755 -d %{buildroot}/%{_docdir}/%{name}/examples @@ -36,7 +35,6 @@ for file in `find examples -type f ! -name .gitignore` ; do install -m 644 $file install -m 644 *.mk arduino-mk-vars.md %{buildroot}/%{_datadir}/arduino install -m 644 licence.txt %{buildroot}/%{_docdir}/%{name} install -m 755 bin/ard-reset-arduino %{buildroot}/%{_bindir}/ard-reset-arduino -help2man %{buildroot}/%{_bindir}/ard-reset-arduino -n "Reset Arduino board" -s 1 -m "Arduino CLI Reset" --version-string=%{version} -N -o %{buildroot}/%{_mandir}/man1/ard-reset-arduino.1 %clean rm -rf %{buildroot} @@ -44,7 +42,6 @@ rm -rf %{buildroot} %files %defattr(-,root,root,-) %{_bindir}/ard-reset-arduino -%{_mandir}/man1/ard-reset-arduino.1* %{_datadir}/arduino/*.mk %{_datadir}/arduino/arduino-mk-vars.md %doc %{_docdir}/%{name}/licence.txt @@ -52,6 +49,8 @@ rm -rf %{buildroot} %{_docdir}/%{name}/examples %changelog +* Mon Mar 24 2014 Simon John +- Replaced perl/help2man with pyserial for reset script. * Tue Feb 04 2014 Simon John - Added arduino-mk-vars.md to the files to be installed/packaged. * Sat Feb 01 2014 Simon John