Sample code on how to pass an optional function pointer to a library and set a single pin to either InterruptIn or DigitalOut.
This code can be used as a template to create a library where a single pin can be set to either InterruptIn or DigitalOut.
There are 3 ways to instantiate the library (example using the KL25Z board):
// 2 parameters : Only SDA and SCL are declared. DemoClass sensor(PTE0, PTE1); // 3 parameters : SDA, SCL and a DigitalOut are declared. DemoClass sensor(PTE0, PTE1, PTD7); // SDA, SCL // 4 parameters : SDA, SCL, InterruptIn and a user function pointer are declared. DemoClass sensor(PTE0, PTE1, PTD7, &sensor_irq); // ISR mode
Notice that the 3rd pin declaration switches from DigitalOut to InterruptIn when the user function pointer is added.
Revision 0:0a6e921b085b, committed 2014-05-03
- Comitter:
- frankvnk
- Date:
- Sat May 03 16:15:38 2014 +0000
- Commit message:
- Initial release
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DemoClass/DemoClass.cpp Sat May 03 16:15:38 2014 +0000 @@ -0,0 +1,34 @@ +#include "DemoClass.h" + +InterruptIn *_irqpin; +DigitalOut *_syncpin; + +// Depending on how the ctor is called, irqsync is either set to none, InterruptIn or DigitalOut +DemoClass::DemoClass(PinName sda, PinName scl, PinName irqsync, void (*fptr)(void)) : _i2c(sda, scl) +{ + // When both irqsync and fptr are nonzero, we use InterruptIn: irqsync pin is interrupt input and Attach ISR. + if((irqsync != NC) && (fptr != NULL)) + { + _irqpin = new InterruptIn(irqsync); // Create InterruptIn pin + _irqpin->fall(this, &DemoClass::_sensorISR); // Attach falling interrupt to local ISR + _fptr.attach(fptr); // Attach function pointer to user function + } + + // When fptr is not defined, we use DigitalOut: irqsync pin is digital output. + if((irqsync != NC) && (fptr == NULL)) + { + _syncpin = new DigitalOut(irqsync); // Create DigitalOut pin + _syncpin->write(0); // Set pin to 0 + } +} + +bool DemoClass::Status(void) +{ + return (1); +} + +void DemoClass::_sensorISR(void) +{ + uint8_t statret = Status(); // Read a status register (eg : to clear an interrupt flag). + _fptr.call(); // Call the user-ISR +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DemoClass/DemoClass.h Sat May 03 16:15:38 2014 +0000 @@ -0,0 +1,33 @@ +#ifndef DemoClass_H +#define DemoClass_H + +#include "mbed.h" + +class DemoClass { +public: + /** + * \brief Create an object connected to I2C bus, irq input or digital output and user-ISR. + * \param sda SDA pin. + * \param scl SCL pin. + * \param irqsync Interrupt input when called with user-ISR pointer. + * Digital output when called without user-ISR pointer. + * \param fptr Pointer to user-ISR. + * \return none + */ + + DemoClass(PinName sda, PinName scl, PinName irqsync = NC, void (*fptr)(void) = NULL); + + /** + * \brief Example status function. + * \param none. + * \return Always 1. + */ + bool Status(void); + +private: + I2C _i2c; + FunctionPointer _fptr; + void _sensorISR(void); +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sat May 03 16:15:38 2014 +0000 @@ -0,0 +1,23 @@ +#include "mbed.h" +#include "DemoClass.h" + +bool Overflow = 0; + +// Forward declaration of the user-ISR +void sensor_irq(void); + +// 3 possible constructor calls (current example uses KL25Z pins) +//DemoClass sensor(PTE0, PTE1); // Free running mode +//DemoClass sensor(PTE0, PTE1, PTD7); // Sync mode +DemoClass sensor(PTE0, PTE1, PTD7, &sensor_irq); // ISR mode + +void sensor_irq(void) +{ + Overflow = 1; +} + +int main() +{ + while(1) + if(Overflow) Overflow = 0; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sat May 03 16:15:38 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/8e73be2a2ac1 \ No newline at end of file