Two books for C++ savvy Arduino programmers

I thought I was a knowledgeable C++ programmer as I started on my Arduino-based beehive weighing system.  At first, things were easy; I would chug along and get results; how satisfying. But when I started putting things together; when I attempted to get the parts and pieces to talk to each other; things got sour. I progressively dedicated more time, much more time, to debugging than to developing the application. That is when I realized that I should review my approach, question my knowledge, and explore new tools.

To make a long story short, I ended up writing notes which explained C++ mechanisms, created frameworks to modularize functionalities, and looked into new tools, namely AtmelStudio (MicrochipStudio), Visual Studio, Visual Micro, and PlatformIO. I also honed my regular expressions skills and developed a working knowledge of Awk and Perl, two tools which can render countless services.

All these are compiled inside of two books, Pragmatic  C++ Arduino Programming and Defensive  C++ Arduino Programming, which I should be publishing in July/August 2023.

Pragmatic  C++ Arduino Programming

You may download a partial content of this book (Table of contents, first chapters, Index table)  by clicking on the link PragmaticIntro.pdf.

C++ is a complex programming language designed to create large, mission critical applications. You and I, hobbyists, makers, tinkerers, whatever, may use it profitably and not suffer from long debugging times as long as the application remains simple; that is, that it fits comfortably within the context of a single sketch file (one .ino file). But as soon as you add classes, spread the application across several files, exploit C++ resources extensively, you will get caught by the many gotchas C++ can throw at you; you will then dedicate considerably more time to debugging than to developing your application. As soon as you realize that things are getting out of hand, you should sit back, roll up your sleeves, and tell yourself: What is going on? You must undertake some kind of reality check; this is where being pragmatic comes in

To be productive writing C++ code, you must adopt good programming practices, use good tools, and know and understand C++ mechanisms. But, before embarking on the Arduino C++ route, you should ask yourself which microprocessor/programming language combination should you use? Should you go the Raspberry PI/Python route (powerful CPU and fast development time) or the Arduino C++ route (slow development time but fast/compact code hence small, cheap CPU to run your application in)?

C and the Unix operating system go hand in hand. C was created to create Unix (C++, which was created later, being the object-oriented extension to C).  The language is designed to be close to the hardware so that programmers and the compiler could create fast, compact code. But this comes at a price, you really must know what you are doing as C++ does not provide built-in safeguards to protect you from shooting yourself in the foot such as Python and other languages do. That is why even seasoned programmers will spend more time writing code in C++ than doing so in other languages. But the reward is compact, fast executables which fit in small CPUs.

You could have chosen Raspberry Pi and Python and gotten the job done sooner than with C++ but the code would have been larger and the application slower, hence a more expensive microprocessor to run your application in. If that is OK, by all means switch to the Raspberry Pi/Python tandem but if you want a small code footprint which runs fast such as required for signal processing, Arduino or some other microcontroller such as Raspberry Pi Pico programmed in C++ is the way to go; but the going will be tougher. Much to your dismay, you will soon spend much more time debugging than developing your algorithm. This is the problem this book and its companion, Defensive C++ Arduino Programming, address.

Having chosen the Arduino C++ route, be prepared for long frustrating debugging sessions. This is what happened to me as I developed my Arduino-based beehive weighing system. It got to be 35000 lines of code spread across 35 files.  As I progressed, I was devoting much more time to debugging C++ related issues than to developing the application. I was hit by the many gotchas C++ could throw at me. As I progressed, I told myself “This is ridiculous; it is getting out of hand.”. I then asked myself “What can I do to reduce debugging time?”. The following is what I came up with: good programming practices; improve my C++ skills (it became obvious there was a lot I did not know nor understand); use and/or develop software tools (frameworks); use professional grade IDEs such as AtmelStudio (MicrochipStudio) or Visual Studio with Visual Micr; use tools such as Awk, Perl, regular expressions as programming aids; And do not neglects psychological considerations. These gradually morphed into notes and then into two books: Pragmatic C++ Arduino Programming and Defensive C++ Arduino Programming.

This book is not just another book on learning the C++ language. In writing this book, I address myself to self-taught Arduino programmers who already program in C++ and who want to improve their productivity by writing better code. It doesn’t just say how to do things; it explains the underlying mechanisms since understanding how something works helps write better code. It is a book written by a non-professional for non-professionals who must do professional level work to be successful.

The companion book, Defensive C++ Arduino programming, covers alternatives to the Arduino IDE, AtmelStudio (Microchip) in particular, frameworks to avoid reinventing the wheel, print- and hardware-based debugging, and Awk, Perl and regular expressions, three extremely useful tools.

Defensive C++ Arduino Programming

You may download a partial content of this book (Table of contents, first chapters, Index table)  by clicking on the link DefensiveIntro.pdf.

C, initially designed to create the Unix operating system, morphed into C++ as object-oriented programming mechanisms were added to it. It is a language designed for professionals who write big, mission critical applications. Using C++ requires knowledge, discipline, and a professional approach, otherwise debugging time gets out of hand. Amateurs, such as you and I, will be quickly overwhelmed by the many gotchas the language can throw at you. Simply put, the application stops working and you do not have the slightest clue as to why. This book, a sequel to Pragmatic C++ Arduino Programming, covers defensive measures to shield you from these gotchas. They will improve your productivity and render the application more robust and maintainable.

The first line of defense is use a good tool. I started Arduino development with the Arduino IDE. It is a remarkable tool as its all-in-one approach simplifies programming. Furthermore, it could not be cheaper since it is free although you should consider donating to the Arduino team to support their good work. Arduino boards being based on 8-bit Atmel chips, why not use Atmel’s IDE (AtmelStudio now renamed MicrochipStudio since Microchip bought Atmel). It turns out that AtmelStudio is orders of magnitude more programmer friendly than the already programmer friendly Arduino IDE; and its learning curve is short. ATmelStudio is derived from Microsoft’s Visual Studio (fairly old version). Another incredibly useful feature is its support of the ATmega XMini hardware-based debugging boards which are fully described in the book. And the, for non-hardware-based debugging development, go for Visual Studio 2022 with Visual Micro ($25/yr). This is a fantastic combination, the tool pros use.

Do yourself a favor, switch to Visual Studio 2022 with Visual Micro despite Arduino’s recent release of its version 2 IDE. This is why AtmelStudio and the VS 2022/VM combination are extensively covered in this book. PlatformIO, Code::Blocks, and MPlab are also presented.

The second line of defense is that you should not reinvent the wheel; use software tools, aka frameworks. As I progressed on my Arduino-based beehive weighing system, I had to create frameworks to meet various needs (radio-based data transmission, data storage, program documentation, manage print-based debugging, manage memory, handle errors, …). The book presents seventeen frameworks which alleviate programming by providing out-of-the-box tasks which you would otherwise have to develop (these may be downloaded from Dropbox).

The third line of defense is to print-based debug using the Print-based Debugging Framework (one of the seventeen frameworks presented in this book) to get a bird’s eye view as to what your application is doing and to hardware debug using the AtmelStudio ATmega328P-Xmini combination to get a snapshot of your application’s status. Debugging should not be a haphazard process based on occasional prints to the console to see what is happening; it should be a concerted plan using formal techniques to get to the bottom of things thereby uncovering programming errors. Both print-based debugging and hardware-based debugging should be undertaken; they complement each other.

The fourth line of defense is hardware-based debugging. The fundamental idea is to place breakpoints in your source code, launch the application, and have it stop at breakpoints so that you could examine variables and memory. For some types of work, this is the only practical solution to finding bugs. Otherwise, print-based debugging does the job. You may program with Visual Studio 2022/Visual Micro, in my opinion the best possible Arduino programming environment, and switch to AtmelStudio to hardware debug with the ATmega328P-Xmini board. The book indicates how to achieve interoperability between these two tools and the Arduino IDE on the same set of files.

The fifth line of defense is the use of utilities such as Awk, Perl, and regular expressions. They enable you to accomplish tasks otherwise not feasible. They will improve your productivity. These are introduced and explained so that you can quick start on them.

These five lines of defense are the main domains covered in this book. But there is more. Since memory management and error handling are crucial to an application’s robustness, the book goes into extensive details on overloading operator new to use some of the frameworks to inform on available memory: How much of it is fragmented? Are there memory hogs? Are there memory leaks? And if errors are detected, these may be handled by the setjmp/longjmp mechanism structured as a framework to mimic C++’s non Ardjuino supported exception handling. The array index operator [] overloaded within the context of a safe array class is also presented. It saves you from over or undershooting the array’s limits.

This two-book series, Pragmatic C++ Arduino Programming and Defensive C++ Arduino Programming, constitute a reference from which to acquire the necessary skills to be productive writing fast, compact, robust, and maintainable applications.

Author

Michèle Delsol is an avid Arduino developer. She is an engineer now retired with no specific electrical engineering nor electronics training; she has taught herself enough electronics and programming to put together Arduino based devices, the latest and major one being her Arduino-based beehive weighing system. As she progressed on her project, she was struck by the difficulty of getting practical explanations on C++ mechanisms and tools, and by the lack of ready-made software tools. This led her into putting together notes which morphed into the two books: Pragmatic C++ Arduino Programming and Defensive C++ Arduino Programming. They are scheduled to be published in July/August 2023 in print and digital editions on Amazon.

If you want more info you can contact the  author at cppArduino@md-dsl.fr.