Evolving Train of Thought for C/C++ Library's and Header Files

So, In the last few weeks’s I have been converting my Large ESP8266 project to multiple .h Module files so I can carry functions over from project to project and at the same time have them centrally managed to update code / fix bugs so they carry over to all of my projects.

One important detail on how I do this is to be able to trigger function code from within the Main project as needed so I don’t have to handle user specific events within the Header file where it does not belong. So, I do this by using TypeDef and declaring a Prototype for my Callback function. In this case,

Then set the callback function,

Set the callback in the Header file

Then in my Header file I then check to see if the value is set != NULL. If so then I call it.

The Main Function will get triggered and I can then access the variables stored in the STACK/HEAP depending on how I pass it to the caller from the callee.

One thing to note for the LOVE OF ORANGE JUICE WATCH OUT FOR USE AFTER FREE ISSUES. If your doing this from an ASync call just be aware to Lock the thread/variables so you don’t crash or use Data that get’s falsely injected!

With this method you can just Include the Handler, Call the Coordinating Init Function and pass the &Address of a function matching the prototype and your off to the races!

Full Header and Main.C example
DHT.H
#ifndef dhtthermastat
#define dhtthermastat

Main.C

Leave a comment

Your email address will not be published. Required fields are marked *