0

I'm coming from a web environment and not that fluent in C++. About to start a moderately complex dev on an ESP32 device.

I know that there is hundreds of arduino libraries out there but I haven't found a lot of complex actual application examples.

I'm mostly interested in the file structure, separation of concerns and other C++ tweaks that should be considered best practices when coding on embedded devices in a more robust way.

I found the ardupilot GitHub project to be very interseting to look into, but its quite big, and I'm not sure if it would be considered well written code as I can't judge myself.

Can you recommend other great projects to look into?

4
  • 1
    Welcome to Arduino:SE. You might find it helpful to review the site tour and Help Centre and, in particular, How to Ask. Nov 28 '17 at 20:59
  • arduinos cannot do that much at once, instead they should be setup to do one thing well. Nov 28 '17 at 21:37
  • C++ is an hard lenguage to learn by itself. Arduino make it harder, because you have to debug your program without any help or support from the non-existent OS. Finally, Arduino IDE organize code in his own peculiar way, which introduce all kind of nasty compilation errores. Welcome!
    – user31481
    Nov 28 '17 at 22:07
  • Not sufficient to answer your question, but certainly an essential reference when you're talking about best practices is the Arduino Library specification: github.com/arduino/Arduino/wiki/…. I see people publish what they claim to be an "Arduino library" that doesn't even work with the Arduino IDE because they're using some alternate IDE and never bothered to test with the official IDE. If it doesn't work with the Arduino IDE it's not an Arduino library.
    – per1234
    Nov 29 '17 at 12:10
1

I have not discovered a best practice guide anywhere for multiple file, complex projects, however with similar needs I found the following useful:

  • While Arduino does support C++ syntax it does not have support for the C++ standard library. It links against avr-libc, so the only standard library functions available are those that are contained in this library (which is far closer to ANSI C).

    For modules see: http://www.nongnu.org/avr-libc/user-manual/modules.html

    For discussion on C++ see: http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_cplusplus

  • Arduino.h implements some abstracted classes, functions and types. Many are useful (particularly for hardware functions) through I found the String class to be problematic. It was very easy to use but consumed way too many resources for serious consideration in a larger / complex project (where ram resource limits are a problem). Instead, use NULL terminated strings and the standard C library string.h functions for manipulation.

    Likewise, using char strings as literals consumes heaps of memory. It is useful to isolate all string literals into a single header file and poke them into PROGMEM using macros and/or enums to reference them.

  • I found eclipse IDE to be great for development of complex projects. However it is missing some of the Arduino IDE functions.

    This plugin provides the functions found in the Arduino IDE (use of .ino main file, library management, board upload, serial monitor etc) as an eclipse plugin. http://eclipse.baeyens.it/

    It can be downloaded as a full eclipse package or added as a plugin to an existing package. It has active development and the developer is very response. Nightly build plugin here: https://github.com/Sloeber/arduino-eclipse-plugin

  • Although there are some serial debuggers available for Arduino, I have not looked into them. Instead (for complex projects) I found it useful to use pre-processor directives to specify a SIMULATOR mode (where the Arduino specific code is stubbed, returning test/dummy data where useful), thereby allowing a debug build on the development platform (with integrated debugging) before changing a #define and flicking back to the Arduino plugin for board build and upload.

  • Though far from representing anything approaching best practice you are welcome to peruse a project of mine which demonstrates all of the above principles in a complex example.

    Under src/ see ff_string_consts.h for the PROGMEM examples
    See ff_sys_config.h for various directives.

    https://github.com/winginitau/FodderFactory

0

If you are used to OO (which I guess since you like separation of concerns), do not use the Arduino IDE, since it is very hard to work with multiple files. Instead use e.g. vMicro for Visual Studio.

Separation of concerns (using C++) for the Arduino is not much different than in other languages. C++ has the notion of classes, and fields/functions can be made private, protected and public (simplified).

However, C++ itself is not so easy to learn, mostly because of pointers and related issues. However, it's something to dive in when you want to learn something new.

Not the answer you're looking for? Browse other questions tagged or ask your own question.