zevv / nim-arduino

Trying to get Nim integrated into arduino IDE
54 stars 2 forks source link

Nim for Arduino

Let's start with a warning: this project is only an experimental hack. The Arduino development environment does not support other languages then C++, and the authors of the Arduino IDE have not shown any interest in allowing 3d party langauge integrations. To make this work, we create a fake compiler that looks like a C++ from the perspective of the Arduino IDE, but is actually a wrapper around the Nim compiler.

The current status of this project is "Works For Me, but Don't Complain If It Doesn't For You". I am interested in any improvements, patches or other ideas to make this work better.

There are two distinct parts to this project:

Setup

Note: the below configuration will reconfigure the default toolchain for the Arduino IDE, so you can no longer compile normal sketches. I am looking for a way to make this configurable, but that might require some changes from the upstream Arduino developers. For now, simply remove the platform.local.txt file from the arduino tree to restore the original configuration.

Example

import arduino

setup:
  pinMode LED_BUILTIN, OUTPUT

loop:
  digitalWrite LED_BUILTIN, HIGH
  delay 500
  digitalWrite LED_BUILTIN, LOW  
  delay 500

Misc

When using multiple source files, the Arduino IDE will concatenate all of those and feed them to the compiler, so this will not allow Nim to use the usual import mechanism. Instead, you can enable code reordering feature in Nim to make this work as expected: use the following pragma at the top of your sketch:

`{.experimental: "codeReordering".}`