How to use enum without include files?

I understand. However, even something like a #pragma that bypass the arguably broken arduino functionality (even if you do fix “all the bugs”) would be a great expert-level improvement:

#pragma yes_I_know_what_I_am_doing

You can keep your arduino users happy, yet still allow the use of regular C++ code. If you ever start allowing the use of other #include files and archive libraries, I suspect this will be important.

I think this makes sense, and it’s a nice feature for expert-users, a no-op define or flag that the pre-processor just watches for and avoids that file entirely when it sees it. I think we can add this without impacting the majority of users, we’ll chat about it today and hopefully include it in the next feature update on that server.

Thanks!
David

Basically the way the pre-processor works now is something like this:

1.) Find all the include statements, and function declarations / definitions in the code
2.) Figure out which functions are missing declarations, and which include statements are missing
3.) Inject the missing ones right before the first statement in the code.

It’s really not rearranging anything, it’s only adding missing statements. The problem is that it’s adding function declarations above stuff like typedefs, which is causing that problem you see above. Wrapping in classes isn’t necessary, just by adding the missing declaration, the pre-processor will see it’s there already, and won’t add its own.

I do think an “leave_me_alone” define or pragma makes sense, but I just wanted to give some background that it’s really not re-arranging anything.

Thanks!
David

Hey Guys,

So I’ve added the following magic pragma:

#pragma SPARK_NO_PREPROCESSOR

If you include exactly that text in your code file, same # of spaces, same capitalization, etc, the pre-processor will ignore your file. Make sure you have a #include “application.h”, otherwise your code won’t compile. You will also need to add any function declarations, etc.

In the meantime, I believe the Arduino compiler will break on the same kind of enum / class usages unless you use multiple files in your project. We’re working on this, but in the meantime you can also work around that problem by just adding your own function declaration in the right place, and it should work fine.

Thanks,
David

1 Like

Wonderful, thanks!

(And thus I must endeavour to append some uselessly contrite and erudite missive, to appease the reply deities, for they are adamant in their refusal to allow replies of fewer than twenty characters. :sunny: )

2 Likes

Is the following still the preferred way to support ENUMs in Photon code:

#pragma PARTICLE_NO_PREPROCESSOR

#include "my_header_with_enum_defs.h"
#include "application.h"

I’m compiling with

particle compile main.ino my_header_with_enum_defs.h
because I assume I can’t compile with the IDE using the above.

Any advice is greatly appreciated.

-J

You can use Particle Build (Web IDE) just that way.
And depending on your scope for the enum, you could leave out the enum-header and just use them inside your ino.

Awesome. Thanks! BTW, the “Compile in Cloud” in the IDE always returns successful for me even on blatantly uncompilable code…that’s why I started using the CLI tools.

That’s odd since CLI/Dev also use the same remote buildfarm as Build does :confused:

Yeah it’s weird… http://www.screencast.com/t/rA6qgXbv

I see, but this is not Web IDE (aka Particle Build) this is Particle Dev.

How is it if you add #pragma SPARK_NO_PREPROCESSOR at the top of your file?

You also need to put any non-code files out of the directory or actively ignore them by putting the file names into a particle.ignore file.
Further you need to open the project folder (Ctrl/Cmd+Shift+O) rather than only a file from that folder in Dev to have it pull in the files from there.
If you just open a file from your project folder, the sources path might not be set correctly to that project folder.
So you might actually be building another project than the one you are editing - confusing, isn’t it :confused:

You might see the actual build path in the caption line of your Dev window, which unfortunately can’t be seen in your video.