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.
DemoClass/DemoClass.h
- Committer:
- frankvnk
- Date:
- 2014-05-03
- Revision:
- 0:0a6e921b085b
File content as of revision 0:0a6e921b085b:
#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