From 6f19839920d0134bdd3fd900139c0667c047def4 Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Wed, 4 Mar 2015 21:21:10 +0200 Subject: [PATCH] Split PlatformIO build tool to "core" and "upload" --- platformio/builder/main.py | 5 +- platformio/builder/tools/platformio.py | 65 +--------------------- platformio/builder/tools/upload.py | 76 ++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 65 deletions(-) create mode 100644 platformio/builder/tools/upload.py diff --git a/platformio/builder/main.py b/platformio/builder/main.py index 5fcc2d51..d384aa4a 100644 --- a/platformio/builder/main.py +++ b/platformio/builder/main.py @@ -50,7 +50,10 @@ commonvars.AddVariables( ) DefaultEnvironment( - tools=["gcc", "g++", "as", "ar", "gnulink", "platformio"], + tools=[ + "gcc", "g++", "as", "ar", "gnulink", + "platformio", "upload" + ], toolpath=[join("$PIOBUILDER_DIR", "tools")], variables=commonvars, diff --git a/platformio/builder/tools/platformio.py b/platformio/builder/tools/platformio.py index 79dd62a6..a50b566f 100644 --- a/platformio/builder/tools/platformio.py +++ b/platformio/builder/tools/platformio.py @@ -2,17 +2,12 @@ # See LICENSE for details. import atexit -import platform import re from os import getenv, listdir, remove, sep, walk from os.path import basename, dirname, isdir, isfile, join, normpath -from time import sleep -from SCons.Script import Exit, SConscript, SConscriptChdir +from SCons.Script import SConscript, SConscriptChdir from SCons.Util import case_sensitive_suffixes -from serial import Serial - -from platformio.util import get_serialports def ProcessGeneral(env): @@ -303,60 +298,6 @@ def ConvertInoToCpp(env): atexit.register(delete_tmpcpp, tmpcpp) -def FlushSerialBuffer(env, port): - s = Serial(env.subst(port)) - s.flushInput() - s.setDTR(False) - s.setRTS(False) - sleep(0.1) - s.setDTR(True) - s.setRTS(True) - s.close() - - -def TouchSerialPort(env, port, baudrate): - s = Serial(port=env.subst(port), baudrate=baudrate) - s.close() - if platform.system() != "Darwin": - sleep(0.3) - - -def WaitForNewSerialPort(_, before): - new_port = None - elapsed = 0 - while elapsed < 10: - now = [i['port'] for i in get_serialports()] - diff = list(set(now) - set(before)) - if diff: - new_port = diff[0] - break - - before = now - sleep(0.25) - elapsed += 0.25 - - if not new_port: - Exit("Error: Couldn't find a board on the selected port. " - "Check that you have the correct port selected. " - "If it is correct, try pressing the board's reset " - "button after initiating the upload.") - - return new_port - - -def AutodetectUploadPort(env): - if "UPLOAD_PORT" not in env: - for item in get_serialports(): - if "VID:PID" in item['hwid']: - print "Auto-detected UPLOAD_PORT: %s" % item['port'] - env.Replace(UPLOAD_PORT=item['port']) - break - - if "UPLOAD_PORT" not in env: - Exit("Error: Please specify `upload_port` for environment or use " - "global `--upload-port` option.\n") - - def exists(_): return True @@ -369,8 +310,4 @@ def generate(env): env.AddMethod(BuildLibrary) env.AddMethod(BuildDependentLibraries) env.AddMethod(ConvertInoToCpp) - env.AddMethod(FlushSerialBuffer) - env.AddMethod(TouchSerialPort) - env.AddMethod(WaitForNewSerialPort) - env.AddMethod(AutodetectUploadPort) return env diff --git a/platformio/builder/tools/upload.py b/platformio/builder/tools/upload.py new file mode 100644 index 00000000..130fd22f --- /dev/null +++ b/platformio/builder/tools/upload.py @@ -0,0 +1,76 @@ +# Copyright (C) Ivan Kravets +# See LICENSE for details. + +import platform +from time import sleep + +from SCons.Script import Exit +from serial import Serial + +from platformio.util import get_serialports + + +def FlushSerialBuffer(env, port): + s = Serial(env.subst(port)) + s.flushInput() + s.setDTR(False) + s.setRTS(False) + sleep(0.1) + s.setDTR(True) + s.setRTS(True) + s.close() + + +def TouchSerialPort(env, port, baudrate): + s = Serial(port=env.subst(port), baudrate=baudrate) + s.close() + if platform.system() != "Darwin": + sleep(0.3) + + +def WaitForNewSerialPort(_, before): + new_port = None + elapsed = 0 + while elapsed < 10: + now = [i['port'] for i in get_serialports()] + diff = list(set(now) - set(before)) + if diff: + new_port = diff[0] + break + + before = now + sleep(0.25) + elapsed += 0.25 + + if not new_port: + Exit("Error: Couldn't find a board on the selected port. " + "Check that you have the correct port selected. " + "If it is correct, try pressing the board's reset " + "button after initiating the upload.") + + return new_port + + +def AutodetectUploadPort(env): + if "UPLOAD_PORT" not in env: + for item in get_serialports(): + if "VID:PID" in item['hwid']: + print "Auto-detected UPLOAD_PORT: %s" % item['port'] + env.Replace(UPLOAD_PORT=item['port']) + break + + if "UPLOAD_PORT" not in env: + Exit("Error: Please specify `upload_port` for environment or use " + "global `--upload-port` option.\n") + + +def exists(_): + return True + + +def generate(env): + env.AddMethod(FlushSerialBuffer) + env.AddMethod(TouchSerialPort) + env.AddMethod(WaitForNewSerialPort) + env.AddMethod(AutodetectUploadPort) + return env