Making C++ Code Compatible With Arduino

I want to upgrade the gyroscope in a wiimotion plus, so i need to make an arduino nano act like one. Thankfully, the Dolphin Emulator Project had 4 files written in C++17 perfect for this, so they got shamelessly stolen and had their dolphin-ey parts removed.

Ive heard from multiple sources that since arduino is essentially c++ without the standard libraries, so i pasted my code over, changed main to setup, installed ArduinoSTL, and still got so many errors i cant post them all here. Why is this so? Ill leave a link to my files here.

MotionPlus.zip (12 KB)

Ive heard from multiple sources

A better idea would be to learn a bit about how the Arduino IDE actually works.

The programming language is C++, but some routines and simplifications are provided with the IDE that hide some of the complexity from beginners. You are not required to use any of them.

Looking at the files that you provided, it looks like you need to find Arduino alternatives for far more libraries. I took the includes from BinUtils.h that you provided and dumped them in a sketch.

#include <ArduinoSTL.h>

#include <array>
#include <climits>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <initializer_list>
#include <cmath>
#include <functional>
#include <type_traits>


void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

During compilation, it complained about #include ; so you need to find an Arduino alternative for that one. I did find GitHub - jshaw/arduino-array: Array is a library for the Arduino. It is created to help simplify the handling of raw c++ arrays. that might or might not provide the required functionality. Once that error is out-of-the-way, rinse and repeat for the next compilation error.

After that you might have a chance to get your code compiled and working. If you can’t find an Arduino alternative for one of them, you will have to write it yourself.

Note:
The above linked one has instructions how to install; I would not follow that approach but download the zip and use menu sketch->include library->Add .zip library.

ArduinoSTL doesn't support C++17. IIRC, not even C++11 is fully supported.

IMHO, trying to get this working on an Arduino Nano (at least the AVR one) is a waste of time. ARM chips and ARM-based Arduino boards are really cheap nowadays, and their toolchains have full standard library support.

Pieter

PieterP: ARM chips and ARM-based Arduino boards are really cheap nowadays, and their toolchains have full standard library support.

Im not aware of the differences between ARM and AVR based boards. Would i be able to run actual c++ on an ARM board? If so, could you please point me towards a reputable source for some? They sound very intriguing

You can run “actual C++” on AVR boards, the language used to program AVR-based Arduinos is C++. The avr-gcc toolchain simply doesn't include the C++ standard library, because some parts of the standard library use a lot of dynamic allocations, or include design decisions and optimizations that make sense on a 32-or 64-bit CPU with megabytes or gigabytes of RAM, but not on an 8-bit AVR processor with 2KiB of RAM. Many other parts of the standard library would work just fine on AVR, but there's simply no point in maintaining a standard library implementation for a relatively niche 8-bit microcontroller that's quickly losing its popularity in favor of cheaper and much more powerful 32-bit ARM, RISC-V and Xtensa cores.

If you go to https://www.arduino.cc/en/Main/Products, most boards in the “Enhanced features” and “Internet of Things” categories have an ARM chip. The architecture is mentioned on the product page for each board and in the technical specifications. Do keep in mind that there's a wide range of different ARM cores. A Cortex-M4 will be much more powerful than a Cortex-M0(+), for example, and some chips might have an FPU to perform floating point operations in hardware, etc. I personally like the Teensy boards, the 4.x line has a Cortex-M7 clocked at 600 MHz with a 64-bit FPU, it's (one of) the fastest microcontrollers you can get right now, and a Teensy 4.0 is the same price as an Arduino Nano.

Which board is suitable for your project will depend on your application. If you're not going to do any real-time digital signal processing like audio, video, advanced control algorithms etc. having a 600 MHz microcontroller with an FPU is pretty pointless. If you need wireless communication, it might be worth looking into Nordic or Espressif chips with built-in Bluetooth or WiFi. If you just need a small microcontroller to control some LEDs/motors and read some basic sensors, maybe as an upgrade from an Arduino Nano, a Cortex-M0 will be just fine. On the other hand, for a hobby project that will never be mass-produced, there's nothing wrong with using a microcontroller that's more powerful than strictly needed. Spending a couple of dollars more is a better idea than finding out after weeks of work that the chip you bought doesn't have enough resources for your project.