Compiling Teensy Sketches with VisualCode (Win10)

luni

Well-known member
Inspired by this thread, I did a quick and pragmatic experiment compiling a blink sketch for Teensy 3.2 with Visual Code (Win10).
First of all: it works perfectly including Intellisense and all the convenience functions from a modern build environment. Build time is about 30s for a complete rebuild and below 1s if you only touch the sketch. Uploading with the teensy uploader as well as with TyCommander is included.


The place to start :: VisualCode projects for PJRC Teensy boards >> github::luni64/VisualTeensy
VisualTeensy
VisualCode is a modern, open source code editor which can easily be used to compile and upload sketches for the PJRC Teensy Boards using VisualTeensy. VisualTeensy is a small Windows utility which fully automates the generation of Teensy projects for VisualCode. Precompiled binaries can be downloaded here.

Here a screenshot of the environment:
build1.jpg^

Getting it going is quite simple and needs to be done only once.
  • Download make.exe and copy it to some convenient place. You can download it from e.g. http://www.equation.com/servlet/equation.cmd?fa=make
  • Download and install VSCode
  • The first time you open a c++ file it ask you to install supporting extensions. Install the extension "c/c++ Intellisense, debugging, and code browsing".
  • I attached the "blink workspace" to this post, extract it to some convenient place and open the file "test.code-workspace" using "File | Open Workspace.
  • Open the file c_cpp_properties.json in .vscode and adjust the path to match your Arduino installation.
  • Open the file tasks.json in .vscode and adjust the path to your make.exe. Note: this has to be done for all the build tasks in the file (didn't find a way how to centralise that setting)
  • Open the makefile and adjust the path to your Arduino installation (ARDUINO_BASE) and the path to TyCommander (UPL_BASE_TYCMD) if you want to use it.
  • The file flags.mk contains the board specific settings, you can change that manually or use Board2Make for auto generating it (https://github.com/luni64/Board2Make)
That's all.

You can compile / upload with CTRL + SHIFT + B (or via Tasks|Run Build Tasks) which gives you the following options:

  • Build -> this will build the sketch
  • Clean -> deletes all generated binaries -> the next build will start from scratch
  • Rebuild User Code -> rebuilds everything but the teensy core.
  • Upload with Teensy uploader
  • Upload with TyCommander

Tested this with Arduino 1.8.5

Hope somebody can use this as a starter

View attachment test.zip
 
Last edited by a moderator:
Glad that worked! Does the Arduino extension add/break anything? :: marketplace.visualstudio.com vscode.vscode-arduino With the automated Make editor - would it be possible to add compile/link flags as well? For instance those that emit asm or custom flags for libraries use or testing?
The setup can not interfere with anything in VSCode. All the configuration I did is restricted to the used project folder. Regarding custom compile flags: That's a good idea, I'll add that.

I also installed the Arduino extension you linked to. It does work but as already mentioned in this thread it currently does not work for Teensy. Additionally I didn't get intellisense working correctly with it, seems like they point it to a wrong include folder but maybe I misconfigured it.

Personal opinion: The extension tries to mimic the Arduino build system which is one of the main reasons why I went to another IDE (VisualStudio) in the first place. So I'm probably not in the target group for that extension. Anyway, I think I'll switch from VisualStudio to VSCode since I really like the lean approach of it. But I won't use the extension and stick with my flexible, makefile based builds as posted above. Anyway, I try to extend Board2Make to autogenerate all the necessary settings for VSCode to make setting up Teensy projects much simpler.
 
Last edited:
If you dont mind, Ill steal the approach from Board2Make and make a command line python tool out of it :)
 
If you dont mind, Ill steal the approach from Board2Make and make a command line python tool out of it :)

Sure, good idea. Might be even better to make a vscode extension out of it but I'm quite illiterate regarding javascript :-/
 
Sure, good idea. Might be even better to make a vscode extension out of it but I'm quite illiterate regarding javascript :-/

illiterate at javascript is a feature, not a bug.

im avoiding loading up VSCode with too many extensions - command line tools with instant access via the builtin terminal is the trick, i dont want to slow down the startup speed i love :)
 
No need to adopt the makefile. It is sufficient to copy the library sources to a subfolder of the project src folder. Something like this:

Code:
src
|__ main.cpp
|__ anotherProjectFile.cpp
|__ [B]someLib[/B]
     |__ [B]someLib.h[/B]
     |__ [B]someLib.cpp[/B]
     |__ [B]anotherLibFile.cpp[/B]

You'd use it like this in the sketch (includepath is relative to the src folder)
Code:
#include "Arduino.h"
#include "someLib/someLib.h"

void setup()
{
}
.....
You can of course tweak the makefile to use the libraries directly but I personally prefer to have all the sources of a project in the project folder.

I'm currently working on an improved version of the project generator. It now generates the complete project including the makefile, c_cpp_properties.json, tasks.json and a sample "blink" automatically. Drop me a note if you want to test a beta version.
 
Hi,

I am using Visual Studio in combination with PlatformIO to program my Teensy. Works without problems. Easy to select libraries etc.

regards
Cor
 
VisualTeensy

I finally finished the first beta version of my project generator for VSCode and published it on GitHub: https://github.com/luni64/VisualTeensy. The repo contains a quick usage guide. Here a screenshot of the setup screen:

quickSetupExample.PNG

Key features:

  • Once you did the initial setup, generating a new Teensy project can be as quick as choosing a project folder, the used Teensy board and a click on "Generate". The generation of the required makefile, task.json and c_cpp_properities.json is done automatically.
  • You can upload firmware using the standard PJRC uploader and/or TyCommander using VSCode.
  • VisualTeensy doesn't need any installation and doesn't change any system configuration. Just copy VisualTeensy.exe (attached) to a convenient place and start it by double clicking.
  • VisualTeensy does not install any extension to VSCode and does not change any VSCode configuration outside of the project folder.
  • If you want you can generate self contained projects: On request VisualTeensy copies the Teensiduino core library to the VSCode project folder and uses this copy for building. You can also use a standard gcc compiler to make the project completely independend of the current Arduino installation.
  • Intellisense, code completion and all the other goodies of a modern IDE work perfectly out of the box.

Maybe someone has time and inclination to give it a try and do a quick test. Any input, bug reports, additional requirements etc. are very welcome.


View attachment VisualTeensy.zip
 
Last edited:
I did some opimization of the code and added default values for most of the settings. If you have a standard Arduino / Teensyduino installation and if vsCode is installed, generating a new project, compiling and uploading to a Teensy now is a matter of a few clicks only.

Here a short quickstart video showing the usage:

 
I did some experiments with the parallel option for GNU make (-j -Otarget). The results are just amazing. On my garden-variety PC (Intel i5, 2.9Ghz, 6GB Ram) a complete rebuild of Teensyduino including a simple blink example usually took about 30s . Activating parallel make accelerates the process to a about 5s for a build from scratch including the upload to the board with TyCommander. A normal rebuild of the user files only (including upload) is done in less than one second.

Current VisualTeensy version including GNU make for Windows can be downloaded here: https://github.com/luni64/VisualTeensy/releases/tag/v0.4.1

 
I did some opimization of the code and added default values for most of the settings. If you have a standard Arduino / Teensyduino installation and if vsCode is installed, generating a new project, compiling and uploading to a Teensy now is a matter of a few clicks only.

And what about existing projects? how should one handle Arduino libraries that are usually searched for by Arduino and also VisualMicro?
 
Last edited:
VisualTeensy only genreates/updates a project skeleton containing the required files for vsCode and generates a makefile containing the specific teensy settings (board type, optimizer settings....) . If you want to complile an exisiting project you basically copy your files into the generated src folder and rename *.ino to*.cpp (don't forget to delete the autogenerated make.cpp first). Alternatively you can point VisualTeensy to an existing folder and it will generate the vsCode files in that folder. (again, don't forget to rename *.ino to *.cpp...)

If you want to use libraries other then those included in the Teensyduino core you can simply copy them to the scr folder and they will be compiled without any change in the makefile (see here). I can generate an example containing some libraries if that helps understanding the workflow.

I'm currently working on a library manager for VisualTeensy to make using libraries easier for those used to do it the Arduino way.
 
I'm currently working on a library manager for VisualTeensy to make using libraries easier for those used to do it the Arduino way.

great, looking forward to it.
BTW, is there a makefile template that one could customize? say only list of libraries, or special files to be used always?
 
BTW, is there a makefile template that one could customize? say only list of libraries, or special files to be used always?
Good idea. Currently I generate the makefile from a built in template. I could make that configurable, i.e. you could provide your own makefile instead of the build in one.
 
This has evolved really well! Especially using TyCommander - by the way that was just updated to v0.8.10 yesterday. NICE fast parallel build times!

I often run the same sketch on multiple of the Teensy's LC,3.1, 3.5, 3.6 from the same INO sketch. Would there be a way to have each build in it's 'own place'? With the IDE this takes multiple instances to have them active at the same time - it seems this would take duplicate 'project solution' areas with a unique make file?
 
I often run the same sketch on multiple of the Teensy's LC,3.1, 3.5, 3.6 from the same INO sketch. Would there be a way to have each build in it's 'own place'? With the IDE this takes multiple instances to have them active at the same time - it seems this would take duplicate 'project solution' areas with a unique make file?

Sure, that can be done. Just tried it, works great. It needs a little manual tweaking of course:

  • Generate a project for say a T3.6. Then rename the generated makefile to makefile_36.mk (or any other name you prefer)
  • Go back to VisualTeensy, change the board to a Teensy 3.2 and update the project. Rename the newly generated makefile to makefile_32.mk.

multimake.PNG

  • To avoid the need for deleting all binaries and rebuilding for the new board whenever you switch boards, we let both makefiles generate their own seperated binaries. To achieve this you need to open the makefiles and adjust the BIN variable from BIN := bin to BIN := bin32 and to BIN = bin36 respectively. Now all binaries go into those folders instead of the standard ./bin folder.

bin.PNG

  • Last but not least we need two build commands. One for T3.2 and one for T3.6. So, lets open tasks.json and rename the build command to "Build T3.2" and add a "--file=.\\makefile_32.mk" to the args. After that, copy the complete command, rename it to "Build T3.6" and change the file argument to "--file=.\\makefile_36.mk".

tasks.PNG

That's it!. You now have a project with 2 build commands one for the T3.2 build the other for the T3.1 build. Both commands build from the same source of course. The corresponding hex files live bin32 and bin36 respectively. This can be improved of course, but for a quick test it should be good enough. BTW: as always, it sounds much more complicated than it really is.
 
Nice! { though would be cool if that naming was the default - the IDE seems to do that under the covers to some degree 'DebugMin.ino.TEENSY35.hex' } I don't have any of this started yet … does Post #1 still show the right links? That project is VS then links on github to VisualCode variant?

If there is a desire for updates to replace Post#1 and you can post it - I'll migrate it to Post #1 - or update with a link to that new New Post and give it a try myself.
 
That project is VS then links on github to VisualCode variant?
VisualTeensy is written using VisualStudio (C#). I don't see links to any VisualCode variant in #1?. VisualTeensy uses a stock visual code installation and does not change anything in that installation. Anyway #1 is not up to date anymore but I don't think that this is a problem, the links are still valid. Thanks for asking anyway.
 
Back
Top