teensyduino headers keep getting in the way

Status
Not open for further replies.

christoph

Well-known member
Hi,

I have a very annoying problem that keeps reappearing. At some point I need to include the teensyduino headers, sometimes just to have NULL available. That also brings up macros like B1 (what's that anyway?) or min and max (which have an STL equivalent). These are processed by the preprocessor and usually get in the way of code that uses the same names in e.g. templates, totally messing up the code.

What's your strategy to circumvent this? I have not yet found a safe recipe.

Regards

Christoph
 
This is purely theoretical as I have no experience with this, but would Namespaces not be a soution to this problem ?
 
No, as macros are expanded by the preprocessor. Namespaces are handled by the compiler. It would be "correct" C++ to turn all those defines into constant expressions for the compiler, but probably a very hard thing to do and certainly not arduino-compatible.
 
First, you can't expect a "pure" C++ environment inside a .ino file. Arduino does support .cpp and .c files inside your sketch, if you want a pure C++ or C environment. You still must have a .ino file, but it can be empty, other than a list of #includes to let Arduino's build system know which libraries you want compiled and linked.

Inside your .cpp files, you can choose which headers to include. Of course, if you use #incluide "Arduino.h", you're bringing in all the Arduino compatible stuff, which includes a lot of short names defined by Arduino and AVR compatibility names, which possibly conflict with other stuff.

There are several headers inside hardware/teensy/cores/teensy3 with only a subset of defines. There's no formal documentation, so you'll have to just look at the .h files to decide which ones meet your needs. For example, you might #include "mk20dx128.h" to get only the hardware registers.

You can also structure your software with several .cpp files. Perhaps if some header is required, but it brings in too many symbols that conflict with names you wish to use, you can isolate only the code which needs stuff from that namespace-polluting header into its own .cpp file. Then the rest of your project can exist in other .cpp files with minimal interference from Arduino and AVR compatibility names.

As a practical matter, this is probably the cleanest solution you'll get. Arduino is designed for novices and an intentional part of Arduino's design is defining a LOT of simple names. In all technical matters, there are a lot of trade-offs, and Arduino has clearly chosen ease of learning for novices over a clean namespace for pros. You get a lot of awesome stuff with Arduino, but there are costs and this is certainly one of them.

Teensy aims for Arduino compatibility. The closest you can get to a clean namespace with Arduino is using .cpp files instead of .ino, where you get to control which headers you want included.
 
Thanks for the explanation Paul, I really appreciate that. I don't use the arduino IDE but Code::Blocks to write my code. I could still use the arduino IDE just to compile my code, but I also changed the linker script a bit, so that would require extra work as well.

I'll try to carefully refactor the code that provides hardware interfaces to see if that helps solving the problems. However, putting the "dangerous" parts into a cpp file is not an option for template classes. Some of my template classes provide hardware interfaces, though. That's going to be a hard nut to crack.

Besides: Shouldn't clean namespaces be mandatory for beginners on their way to being pro? I wouldn't consider myself a pro - not as long as I rely on the indeed awesome classes included in teensyduino and arduino. They are great to build upon!

Regards

Christoph
 
Besides: Shouldn't clean namespaces be mandatory for beginners on their way to being pro? I wouldn't consider myself a pro

That's really a topic for the Arduino Developers Mail List.

But this topic has been brought up there many, many times. If you're going to try again, I'd highly recommend phasing your proposal in terms of the actual code users will write in Arduino sketches and the practical benefits they'll see. Be prepared for serious pushback on any proposal that involves using names with any symbols like ":" or "_", as the Arduino Team feels names with any non-aphlanumeric characters are not accessible for beginners.

Time and time again, this namespace stuff has been discussed in terms of very abstract coding best practices and lacking specific examples of the benefits and the actual coding style. Talking about it in C++ terminology only is doomed to failure.

Please, do not post it here. Teensy follows the de-facto standards set by Arduino for this namespace stuff, so if you want to cause any changes, you must make your case upstream to on the Arduino Developers Mail List.
 
It seems like I've hit a nerve there ;)

To sum it up: I've screwed things up when including WProgram.h instead of only the headers I really need (e.g. just mk20dx128.h and possibly those for I/O, like core_pins.h or usb_serial.h).
 
Status
Not open for further replies.
Back
Top