diff --git a/HISTORY.rst b/HISTORY.rst index 463e45db..266b83aa 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -9,6 +9,9 @@ Release History * Added support of *Engduino* boards for `atmelavr `__ platform (`issue #38 `_) +* Added ``--board`` option to `platformio init `_ + command which allows to initialise project with the specified embedded boards + (`issue #21 `_) * Added `example with uploading firmware `_ via USB programmer (USBasp) for `atmelavr `_ diff --git a/docs/projectconf.rst b/docs/projectconf.rst index 0c017f4f..eb6ab93c 100644 --- a/docs/projectconf.rst +++ b/docs/projectconf.rst @@ -58,18 +58,24 @@ For example, ``[env:hello_world]``. Options ~~~~~~~ +.. _projectconf_env_platform: + ``platform`` ^^^^^^^^^^^^ :ref:`Platform ` type +.. _projectconf_env_framework: + ``framework`` ^^^^^^^^^^^^^ See ``framework`` type in *Frameworks* section of :ref:`platforms` +.. _projectconf_env_board: + ``board`` ^^^^^^^^^ diff --git a/docs/quickstart.rst b/docs/quickstart.rst index 1fc63923..789ea3f8 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -7,33 +7,18 @@ Quickstart Please read `Get Started `_ article from the official WebSite. -First, :ref:`Install PlatformIO `. +1. :ref:`Install PlatformIO `. -Print all available development platforms for installing +2. Find board ``type`` from :ref:`platforms` (you can choose multiple board + types). + +3. Initialize new PlatformIO based project with the pre-configured + environments for your boards: .. code-block:: bash - $ platformio search - [ ... ] + $ platformio init --board=TYPE1 --board=TYPE2 - -Install new development platform - -.. code-block:: bash - - $ platformio install PLATFORM - Downloading [####################################] 100% - Unpacking [####################################] 100% - Installing ..... - [ ... ] - The platform 'PLATFORM' has been successfully installed! - - -Initialize new PlatformIO based project - -.. code-block:: bash - - $ platformio init The current working directory *** will be used for the new project. You can specify another project directory via `platformio init -d %PATH_TO_THE_PROJECT_DIR%` command. @@ -46,29 +31,23 @@ Initialize new PlatformIO based project Project has been successfully initialized! Now you can process it with `platformio run` command. +More detailed information about this command is here :ref:`cmd_init`. -Setup environments in ``platformio.ini``. For more examples go to -:ref:`Project Configuration File ` - -.. code-block:: ini - - # Simple and base environment - [env:mybaseenv] - platform = %INSTALLED_PLATFORM_NAME_HERE% - - -Process the project's environments +4. Process the project's environments. .. code-block:: bash $ platformio run - # if embedded project then upload firmware + # if you don't have specified `targets = upload` option for environment, + # then you can upload firmware manually with this command: $ platformio run --target upload # clean project $ platformio run --target clean +If you don't have installed required platforms, then *PlatformIO* will propose +you to install them automatically. Further examples can be found in the ``examples/`` directory in the source distribution or `on the web `_. diff --git a/docs/userguide/cmd_init.rst b/docs/userguide/cmd_init.rst index fb2ac331..295d2b89 100644 --- a/docs/userguide/cmd_init.rst +++ b/docs/userguide/cmd_init.rst @@ -32,8 +32,27 @@ Options .. option:: --project-dir, -d -Specified path to the directory where *PlatformIO* will initialize new project. +A path to the directory where *PlatformIO* will initialise new project. +.. option:: + --board, -b + +If you specify board ``type`` (you can pass multiple ``--board`` options), then +*PlatformIO* will automatically generate environment for :ref:`projectconf` and +pre-fill these data: + +* :ref:`projectconf_env_platform` +* :ref:`projectconf_env_framework` +* :ref:`projectconf_env_board` + +The full list with pre-configured boards is available here :ref:`platforms`. + +.. option:: + --disable-auto-uploading + +If you initialise project with the specified ``--board``, then *PlatformIO* +will create environment with enabled firmware auto-uploading. This option +allows you to disable firmware auto-uploading by default. Examples -------- @@ -43,6 +62,7 @@ Examples .. code-block:: bash $ platformio init + The current working directory *** will be used for the new project. You can specify another project directory via `platformio init -d %PATH_TO_THE_PROJECT_DIR%` command. @@ -61,6 +81,21 @@ Examples .. code-block:: bash $ platformio init -d %PATH_TO_DIR% + + The next files/directories will be created in *** + platformio.ini - Project Configuration File + src - a source directory. Put your source code here + lib - a directory for the project specific libraries + Do you want to continue? [y/N]: y + Project has been successfully initialized! + Now you can process it with `platformio run` command. + +3. Initialise project for Arduino Uno + +.. code-block:: bash + + $ platformio init --board uno + The next files/directories will be created in *** platformio.ini - Project Configuration File src - a source directory. Put your source code here diff --git a/platformio/commands/init.py b/platformio/commands/init.py index 986d784c..10e0fb2f 100644 --- a/platformio/commands/init.py +++ b/platformio/commands/init.py @@ -8,15 +8,17 @@ from shutil import copyfile import click from platformio import app -from platformio.exception import ProjectInitialized -from platformio.util import get_source_dir +from platformio.exception import ProjectInitialized, UnknownBoard +from platformio.util import get_boards, get_source_dir @click.command("init", short_help="Initialize new PlatformIO based project") @click.option("--project-dir", "-d", default=getcwd(), type=click.Path(exists=True, file_okay=False, dir_okay=True, writable=True, resolve_path=True)) -def cli(project_dir): +@click.option("--board", "-b", multiple=True, metavar="TYPE") +@click.option('--disable-auto-uploading', is_flag=True) +def cli(project_dir, board, disable_auto_uploading): project_file = join(project_dir, "platformio.ini") src_dir = join(project_dir, "src") @@ -24,6 +26,10 @@ def cli(project_dir): if all([isfile(project_file), isdir(src_dir), isdir(lib_dir)]): raise ProjectInitialized() + builtin_boards = set(get_boards().keys()) + if board and not set(board).issubset(builtin_boards): + raise UnknownBoard(", ".join(set(board).difference(builtin_boards))) + if project_dir == getcwd(): click.secho("The current working directory", fg="yellow", nl=False) click.secho(" %s " % project_dir, fg="cyan", nl=False) @@ -51,6 +57,8 @@ def cli(project_dir): if not isfile(project_file): copyfile(join(get_source_dir(), "projectconftpl.ini"), project_file) + if board: + fill_project_envs(project_file, board, disable_auto_uploading) click.secho( "Project has been successfully initialized!\n" "Now you can process it with `platformio run` command.", @@ -58,3 +66,31 @@ def cli(project_dir): ) else: click.secho("Aborted by user", fg="red") + + +def fill_project_envs(project_file, board_types, disable_auto_uploading): + builtin_boards = get_boards() + content = [] + for type_ in board_types: + if type_ not in builtin_boards: + continue + else: + content.append("") + + data = builtin_boards[type_] + framework = data.get("build", {}).get("core", None) + if framework in ("msp430", "lm4f"): + framework = "energia" + + content.append("[env:autogen_%s]" % type_) + content.append("platform = %s" % data['platform']) + + if framework: + content.append("framework = %s" % framework) + content.append("board = %s" % type_) + + content.append("%stargets = upload" % "# " if disable_auto_uploading + else "") + + with open(project_file, "a") as f: + f.write("\n".join(content)) diff --git a/platformio/exception.py b/platformio/exception.py index bbfc2f01..727a4602 100644 --- a/platformio/exception.py +++ b/platformio/exception.py @@ -30,6 +30,11 @@ class UnknownCLICommand(PlatformioException): " to see all available commands") +class UnknownBoard(PlatformioException): + + MESSAGE = "Unknown board type '%s'" + + class UnknownPackage(PlatformioException): MESSAGE = "Detected unknown package '%s'" diff --git a/platformio/projectconftpl.ini b/platformio/projectconftpl.ini index 21525446..ce94d9de 100644 --- a/platformio/projectconftpl.ini +++ b/platformio/projectconftpl.ini @@ -5,6 +5,14 @@ # http://docs.platformio.ikravets.com/en/latest/projectconf.html # +# A sign `#` at the beginning of the line indicate a comment +# Comment lines are ignored. + # Simple and base environment -#[env:mybaseenv] -#platform = %INSTALLED_PLATFORM_NAME_HERE% +# [env:mybaseenv] +# platform = %INSTALLED_PLATFORM_NAME_HERE% +# framework = +# board = +# +# Automatic targets - enable auto-uploading +# targets = upload