Skip to content

Program the Tympan using VS Code and Platform IO

eyuan-creare edited this page Dec 28, 2021 · 9 revisions

Program the Tympan Using VS Code + Platform IO

Here is how to program the Tympan using VS code, as an alternate to using Arduino as your coding environment.

The benefits of VS Code include:

  • Easily jump to the source code: Let's say you want to use a function from a library you found. Rather than dig through a bunch of files to find out how to use that function, you can simply write out the function, then CTRL+Click on the name to go to straight to its definition in the source code.
  • Directly manage external libraries: When bringing in external libraries, rather than dumping them all in an Arduino folder, you can specify exactly which ones each project uses, and if you like, keep them updated or lock them into a specific version.

Install VS Code with Platform IO

Create a Platform IO Project

(For reference, see Platform IO's QuickStart Guide)

  • Click on the Home button, then start a "New Project". Or alternatively, you can "Import an Arduino Project" or "Open Project" that already exists.

  • Name your project and select the board you are using. Choose "Teensy 4.1" for the Tympan Rev-E, or "Teensy 3.6" for the Tympan Rev-D. You can also choose a custom file location, or let it default to saving the project in your Documents folder.

Adding the Tympan Library

For this tutorial, we are going to add the Tympan library by editing the platform.ino file. This downloads the library to your specific project folder, and allows you to choose which version of the library to include in your project. You could add the library globally so that all your projects can access it (by clicking the PlatformIO "home" button, then "libraries"). But I find that locking down which version you use prevents future updates from breaking your project.

  • The platformio.ini file describes how your project will be built: What platform, board, and framework you are using. When you selected the Teensy, this is all filled out for you. It's just missing one thing... the Tympan library.

  • Open platformio.ini and add Tympan to the list of library dependencies. When you build your code, it will pull the library into your project folder.

lib_deps = https://github.com/Tympan/Tympan_Library.git



  • If you want to go a bit further, you can add a # to specify which "commit" of the library to use:
lib_deps = 
    https://github.com/Tympan/Tympan_Library.git#2d29cb8edbb281584668dd799f95eada11826b38

or which branch of the library to use:

lib_deps = 
    https://github.com/Tympan/Tympan_Library.git#main

Building the Audio Pass-Thru Example

In the AudioPassThru example, audio from the onboard microphones is sent to the Tympan processor, then passed back out to the headphone jack.

  • The only change you need for VSCode is to rename the sketch from .ino to .cpp, as VSCode does not recognize Arduino .ino files. You can paste the code from the AudioPassThru example into the main.cpp file that was created with the new Platform IO project. (Or you can choose to "Import Arduino Project", then rename the example sketch to .cpp).

  • Time to upload your first sketch. First, let's review the PlatformIO buttons at the bottom of your screen:

    • Compile: This builds your code. You can skip this if you plan to upload the code right after compiling.
    • Upload: This compiles your code, then uploads it to your hardware.
    • Clean: This cleans out your compiled files so that your build is fresh. Choose this if you are running into issues.
    • Serial Monitor: This opens a serial terminal, which is useful for seeing printf statements during debugging, or for sending serial commands to control the Tympan.
  • Connect your Tympan, click the upload button, then check that it was successful.

Notes

  • In simple projects, you can write all your code in main.cpp. However, as projects get more complex, you may want to separate the code into several files. To do this, you'll put the source code in the src folder, then expose parts of that code in a header file .h stored in the inc folder.

  • For a tutorial on setting up VS Code and Platform IO for Arduino hardware, check out this YouTube video.