Automatically merging main .ino with .h´s into separate .txt?

Hello everybody!

Im trying to build something that's more advanced than my level of programming, a e-bike controller to handle the vehicle lights and sensors, as well as e-motor management (cut-off, throttle and so on). Ive started from scratch (yet again), this time only implementing the main control structures before populating each function with their desired behaviors.

Now, my desire at this stage would be to split up the project code, placing much of the program bulk in their respective header files instead of just having a single long .ino file.

Its getting hard to find my way around almost 1000 lines of code & comments and the project would be much more approachable if i could find each component more easily.

I tried splitting up a earlier version of the project into different files, (tabs in the IDE,) but its a LOT of work, copy/pasting multiple tabs into a long "code"-field of text in the web-emulator every time i change something in the program code..

Now, since i would use #include´s to include those external files anyways, is there a simple way to merge a .ino + multiple .h files into a single "block" of code automagically?

Im thinking something that finds those #include "xx" in the main project .ino, and replaces them with the content of their respective header file, resulting in a single .txt document containing all the code from all the external files mentioned.

I doubt that i can use the .hex build output in the online emulator, but are there any pre-existing applescripts or shell-scripts that could do this search-and-dynamic_replace for me?

i tried searching for it, but the results are pretty meager with such vague descriptions, nothing of practical use for a newbie..

So any hints or suggestions would be greatly appreciated! :)

A main .ino file and multiple named tabs in the IDE would seem to be the perfect solution to your problem.

Why aren't you using the PC based IDE ?

I can't say I understand exactly what your problem is. It seems as if you want to produce a single text file from several separate files - but why?

For my larger projects I have a short .ino file and a number of .h files. The .ino file has lines to #include the .h files. I have never had any need to merge the code into a single file - indeed that would be counter to the whole reason for having separate files.

I use the Geany editor for all my programming.

...R

Robin2: I use the Geany editor for all my programming.

...R

you are on to something...

OP is struggling with the limitations of the IDE.

Another good IDE (at least on my mac) would be ATOM with the plugins for C++ and Arduino.

xarvox: its a LOT of work, copy/pasting multiple tabs into a long "code"-field of text in the web-emulator every time i change something in the program code..

What is this "web-emulator"?

BulldogLowell: you are on to something...

OP is struggling with the limitations of the IDE.

Another good IDE (at least on my mac) would be ATOM with the plugins for C++ and Arduino.

From the OP

I tried splitting up a earlier version of the project into different files, (tabs in the IDE,) but its a LOT of work, copy/pasting multiple tabs into a long "code"-field of text in the web-emulator every time i change something in the program code..

Will any IDE help if he uses the " web emulator"

UKHeliBob: From the OPWill any IDE help if he uses the " web emulator"

right, so the advice was to try a different IDE

pert: What is this "web-emulator"?

create.arduino.cc

i believe

That's certainly not an emulator and the Arduino Web Editor supports multi-file sketches so there's no reason you would need to merge files for it.

BulldogLowell: right, so the advice was to try a different IDE

But he has not said why he is using the "web emulator" and needs to copy code from multiple tabs from the PC based IDE into it when he could just as easily use the PC IDE.

If he shares his reasons for doing as he is then we could maybe provide help in that context instead of just blindly recommending other IDEs

thanks for the input folks, and to clarify the "web emulator" is tinkercad.com, circuit module, witch allows me to test my code (no external files or libraries tho) within a virtual enviroment, saving my real arduinos from accidental shorts and other newbie errors.. I can still test the logic and control structures, as long as i re-route the input button pins and the output light pins to the arduino digital pins, instead of using i2c pin extenders. (mcp23008)

And the reason for merging multiple files is that web emulator, to run a complex project in a simulated enviroment, without having to copy/paste multiple parts of the code manually in very specific positions and order in the main code.

I tend to do work stuff on my laptop, macbook, and only gaming/cad on my pc since i dont really care for windows.

Im already using switched build flags, setting build targets and included hardware (like wether to include display, serial or bluetooth as "output") with a handful of initial #define´s, so eliminating any duplicate code and #extern/#include/#define´s would be eliminated with a bunch of "#if _namedVariable == 3 /codeblock/ #endif"

And to be clear; yes, i do want one short .ino and a bunch of .h files, i just want to re-merge those files into a copy/paste-friendly file for uploading new versions of my code to my simpleton virtual test-bed. :)

It sounds to me as though what you are testing is far removed from the real world and may be of little value considering the hassle involved. If Tinkercad will not allow you to use libraries how do you test anything serious ?

The principles should be easy

If you only use ino files

cat sketch.ino somefile.ino someotherfile.ino > upload.ino

If you use ino, h and cpp files, you can use the same approach but it might require some preparation work. 1) Use grep to generate a file with all needed includes. You might want to clean it out as it can contain duplicates but properly written include files (with guards) should not pose a problem. 2) Use sed to strip all includes from all files (except from the grep result, redirect the resulting output to new versions of the file).

cat greppedresult.txt sketch.stripped.ino somefile.stripped.h someotherfile.stripped.h somefile.stripped.cpp someotherfile.stripped.cpp > upload.ino

Notes 1) Not tested, just the idea. 2) No idea which command line tools are available on a Mac; I think it will not differ from Linux systems.

I don't think it is necessary to strip out the includes. Just put the .h files at the start of the file and then wrap the include statements in conditional preprocessor directives based on macros defined in the .h files (include guards being the obvious choice: foo.h:

#ifndef foo_h
#define foo_h
// declarations
#endif

foo.ino:

#ifndef foo_h
#include "foo.h"
#endif
void setup() {}
void loop() {}

Then your script is quite simple. For extra convenience, figure out how to make the script copy the merged code directly into the clipboard so that you can just run the script, then paste into tinkercad.

Thank you guys for pointing me to cat, i´ll be looking into that! :slight_smile:
I might be able to create a shellscript, somewhat automating the process of merging a split project into a single text file.
I dont think i would be needing a auto-copy-to-clipboard and such fancy stuff, a “search/replace-with” while merging would be enough. :slight_smile:
its just “single use” code that gets pasted over for each version update from the arduino ide, so pieces of “extra”, ignored snippets of code is just fine. :slight_smile:

UKHeliBob:
It sounds to me as though what you are testing is far removed from the real world and may be of little value considering the hassle involved. If Tinkercad will not allow you to use libraries how do you test anything serious ?

Im not sure what you are refering to, but sure, my view of “anything serious” differs from yours. :slight_smile:

My project hardware will be reading a few user inputs and control a whole bunch of digital outputs and that´s hardware-dependent, thus requires libraries. (adafruits mcp23008 lib)

The rest of my project is pure logic, so no libraries or other external files are necessary.

Its just two functions of the code that runs equally exclusively, to read&write local or remote pins.
The rest of the code is the same and un-affected by the build type.

A simple “#define buildType 1” specify that i want the i2c flavor of code, else i select the buildType 0, witch excludes the i2c code with a “#ifdef buildType_sim” (the …_sim and …_i2c flags are defined and undefined as a result of buildType being 1 or 0).

I use the same process to include/exclude the display, bluetooth and other such “extras”, so that i always can build a working, stable version while experimenting with other functions and just exclude beta sections with a “#ifdef devMode” “switch”.

And yes, a emulated system (or simulated, witch is it??) will save me both time and money since im prone to damaging my test environment by shorting out the wrong pins and so on…

And above all, its much more convenient to simply load a browser bookmark, paste in the new code and “instantly” see the results, rather than going for the breadboard and check a bunch of wiring before burning the code…
Its simply lesser components to troubleshoot while developing your code… :slight_smile: