1

I'm attempting to create a new Arduino project using Sloeber (Eclipse). Assume it's a newly-created project whose main entry file is ".\ArduinoMainClass.ino"

Now... assume I want to create a half dozen new classes:

  • ExecutionState-- a C++ class that's intended to be functionally equivalent to a Java Interface. If you're wondering, I'm attempting to implement the 'State' pattern.

  • GoodState, BadState, UglyState, WeirdState-- C++ classes that all extend ExecutionState. If they were Java, they'd technically be implementing an interface... but since this is C++ and I'm still taking baby steps, for now ExecutionState is just a class with one method that does nothing, and all of THESE classes override it to do the actual work.

  • StateContainer-- a C++ class whose only purpose in life is to hold single instances of GoodState, BadState, UglyState, and WeirdState. If this were Java, they'd be Singletons... GoodState (and the others) would have something like:

    private static ExecutionState instance = new GoodState();
    // ...
    public static ExecutionState getInstance() { return instance; }

... but it's obviously not Java, and I'm nowhere close to knowing enough about C++ to properly implement static singletons yet, so for now, I'm just going to create an instance of StateContainer to create and hold those single instances.

So... I now have Sloeber/Eclipse loaded, and I've created my project. If this were Java, I'd create a subdirectory like .\src\java\com\packagename\executionstate and stick the .cpp and .hpp files for ExecutionState, GoodState, BadState, UglyState, WeirdState, and StateContainer in it, and add "include com.packagename.executionstate.*" to it.

  • I know C++ namespaces have no direct connection to directory names
  • I've gotten the impression that "Arduino"-ish C++ auto-generates the #include files for things it regards as libraries. I have a hunch that I probably want to put all of the classes I mentioned above in a directory somewhere and present them to the IDE as an Arduino "library" so they'll all get implicitly included in one go within the .ino file.
  • I'm completely confused about what I need to #include in the classes I mentioned above so they can mutually find each other... and whether those #includes rightfully belong in the .hpp or .cpp files.

So... getting back to the big picture. If my project is in c:\users\moi\Documents\Slober-workspace\FirstSloeberArduinoApp...

  1. Where do the .cpp and .hpp files for ExecutionState, GoodState, BadState, UglyState, WeirdState, and StateContainer go? (Assume I'd be inclined to put them all in a directory named "executionstates" somewhere in the project's directory hierarchy, but I'm not sure where that exact directory should go).

  2. What #includes do I need to put in ExecutionState, Good/Bad/Ugly/WeirdState, and StateContainer... and do they go in the .hpp file, or the .cpp file for their respective class?

  3. What do I have to do so the .ino file can find StateContainer?

In case you're totally confused at this point, my actual .ino file will look something like this:

void setup() {
    // executionStates is a global variable declared higher up
    executionStates = new StateContainer();
    executionState = executionStates.getGoodState();
    // StateContainer's constructor itself creates instances of the various subclasses of ExecutionState,
   // and passes references to itself to their constructors so they can
   // get the references to the others states as they need to return them.
}

void loop() {
    executionState = executionState->doSomething();
    /* executionState holds a pointer to an instance of an ExecutionState subclass.
       Each time doSomething() executes, it returns a pointer to its new state
     */
}

... and in reality, there will be dozens of subclasses of ExecutionState, each representing a unique execution state.

  • ''Arduino'-ish C++ auto-generates the #include files for things it regards as libraries." The Arduino IDE, being a learning tool for new programmers, does do this, but it is unique to that environment. Having graduate to a professional level tool (Eclipse/Sloeber) which will save you a tremendous amount of tedium - but not fixing your code for you - be prepared to write standards-compliant C++, even if using the Arduino IDE. You will, in the long run, make fewer opportunities for error, come to understand the language better, and your code will be portable to standards-compliant environments. – JRobert Oct 7 at 17:19
0

With Sloeber Eclipse plugin the ino files go to root of the project and the cpp, c and h files can be in root or in subfolders. example:

enter image description here

Folders created by Sloeber: core subfolder is a virtual folder with links to Arduino core files and variant files of the selected board. libraries folder is a virtual folder with links to libraries selected for the project. Release folder contains the generated make file, the .o files and the resulting elf, hex, bin files. (And sloeber.ino.cpp is a file generated by Sloeber)

Project folders: data folder in example picture is a convention for a folder with esp8266 SPIFFS files. utility is a subfolder with additional source files of the project.

0

I put my class files, [class].cpp and [class].h, in the same folder as my project.ino file, at least during development. Then, if they're sufficiently general, I'll move them to [workspace]/libraries/[class]/ for future use.

But even if they're only for this project (so they might as well stay in the project's source code folder), it's good organization to keep them in their own files, since by nature, classes only expose their interfaces, not their "guts".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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