I am going to explain a bit how both: gpioInitialise() and gpioTerminate() work. The first one is going to make a backup of the pins status at the time this function is called (whatever that status is), gpioTerminate() will return the system to the point it was before by using that backup. So the idea is Initialise makes a backup when you start the library and Terminate recovers that backup and takes the pins to their previous state.
What might happen is, let’s say you have a program like yours setting up the PWM, if you interrupt that program e.g. Control-C when the pin is HIGH, if you don’t reboot the nano the pin will remain HIGH and Terminate() will recover the system to that point when you execute your program next time (it is not going to know the difference, remember that for whatever the reason on a clean/default image some pins are HIGH by default).
Something you can do to verify this is:
- Reboot your nano (pins 32 & 33 should be LOW now)
- Compile & execute a program just calling Initialise() and Terminate() something like this:
int main(int argc, char *argv)
Init = gpioInitialise();
// Terminating library
Pins should be LOW after executing this, if that is not the case, please let me know because that would be very weird.
So in short gpioTerminate() it is not going to setup your pins HIGH, it should be called always to make sure that the pins’s state goes back to before using the library functions.
If you program in C any interruption (like a user doing a Control-C on keyboard) should be captured by using signal() and the function handler should execute gpioTerminate() to ’ clean’ everything before exiting. If using C++ then you should use try catch.
Please reboot your nano and run that little test calling only gpioInitialise() and gpioTerminate() and let me know what happens.
Again, an option would be for the library itself to capture any interruption but I left it to the user as this would have other implications, also Terminate could be resetting the CPU registers but by doing that this could have an impact on other software, those are all design decisions and there is not a clear cut answer to that depending on the situation one could be better than the other.
Thanks for your feedback again and let me know how it goes, my apologies for not having proper documentation yet but I do this on my spare time, that will come.