*tiny like in: 0 bits of data, 0 code instructions added, 0 clock ticks
scenario: you have a resource that needs to be initialised before use, and deactivated when done. We usually do that by calling some code before the actions, and some code after we're done.
// ... code before you want to activate stepper driver activate_stepper_driver(true); // code where you use the stepper step(15, left); step(185, right); activate_stepper_driver(false); // ... code after you deactivated the driver
We do the bookkeeping ourselves, and take care that we call both functions.
There's a C++ design pattern (RAII) that can handle this for us. You create an object when you need to activate the resource, and it 'll deactivate when it gets out of scope.
Example class - a real world case where my motor burns 0.6 A when the stepper driver is active (motor "hold" current):
class wakeup_drv8711 { // driver out of sleep as long as object in scope public: inline wakeup_drv8711() { gpio_put(nsleep, 1); } inline ~wakeup_drv8711() { gpio_put(nsleep, 0); } };
... and its use:
// ... code before you want to activate stepper driver { // enter scope. w constructor gets called wakeup_drv8711 w; // code where you use the stepper step(15, left); step(185, right); } // leave scope, w destructor gets called // ... code after you deactivated the driver
The driver is activated at line 4 (3 actually), and deactivated at line 8
Thoughts are welcome.