Default Arduino Sketch

In the Arduino IDE I have a custom default sketch that includes a default comment block at the top and some boiler plate that I include in most of my projects.

Is it possible to edit the “New Project” sketch in PlatformIO in the same way?

…you mean like creating a template-project (or just template-main.cpp) and just copy, paste and rename it for a new project that should use that template?
…you could also use snippets in what ever text editor you are using?

When I create a new project a main.cpp file is created automatically with the very bare bones of an Arduino sketch. I want to change that default main.cpp so that when I create a new template I get a different main.cpp

…and you could achieve exatly that by creating a template project and instead of using the “create new project” dialog just copy, paste and rename your templates project folder.

But then how would I select the board I wanted to use? I’d need somewhere to have a list of all the options needed to manually update the ini file…

Surely PlatformIO must save that skeleton sketch somewhere?

Right, so from the code for the init command that runs when you do a ‘create new project’, I can see that the folder structure (i.e. lib, test, src) is hard coded, as is the content of the readme files that goes in those folders and the .travis.yml CI file… and how it also hooks into installing the platform files if not already installed, but for life of me I can’t see what is creating main.cpp, although I’m guessing that wold have to be framework dependent… i.e. you wouldn’t do an #include <Arduino.h> on a STM32Cube or mbed starting file! So it must be conditional… ?!

Yes I can’t find it either.

Presumably it is already framework dependent as it only puts the #include <Arduino.h> for boards that use the Arduino Framework already.

Maybe I should be looking to see whether that “skeleton file” (if that is what it is) is part of the Arduino Framework?

Or maybe it’s just not possible and I should just create a file with my default sketch in and copy and paste it manually for each new project? Or maybe the correct way is to look at something in VSCode? Maybe a macro or something.

Forgive the “newbie” type questions. I’m very much a newbie to PlatformIO having developed for many years in vi/nano and then more recently in the ArduinoIDE. I’m still getting my head around what a “proper” IDE can and can’t do.

1 Like

There doesn’t appear to be anything in the ~/.platformio/platforms/atmelavr/ folder and a first look through ~/.platformio/packages/framework-arduinoavr and ~/.platformio/packages/toolchain-atmelavr found nothing either.

1 Like

Forget that! It’s a very good question, as I’m sure some people would find it very handy to be able to customise the ‘default’ main.cpp, etc… i.e. being able to put in code block headers that they might use on all their projects, etc. And that’s good… I looked through those also but nothing was jumping out as the responsible code.

@ivankravets What is responsible for creating main.cpp? And is there any provision already in place for it to be tweaked or a template provided?

For that I would recommend the snipped function. Snippets are “snippets” of reusable code that you can quickly insert into a file. An example would be the “for-loop” snippet that comes with the C/C++ Extension. To use a snippet you just type an identifier in VSCode directly into your file, then Intelli-Sense will recognize the identifier and you can insert it with Tab. e.g. type “for” and then Tab which will result in a inserted code fragment for a for-loop or “forr” for a reversed for-loop. To learn how to create your own take a look at this. You could use that function to insert your custom skelleton.

1 Like

Oh wow… learn something every day! Very nice! :smiley: