Sample code on how to pass an optional function pointer to a library and set a single pin to either InterruptIn or DigitalOut.

Dependencies:   mbed

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.

Committer:
frankvnk
Date:
Sat May 03 16:15:38 2014 +0000
Revision:
0:0a6e921b085b
Initial release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
frankvnk 0:0a6e921b085b 1 #ifndef DemoClass_H
frankvnk 0:0a6e921b085b 2 #define DemoClass_H
frankvnk 0:0a6e921b085b 3
frankvnk 0:0a6e921b085b 4 #include "mbed.h"
frankvnk 0:0a6e921b085b 5
frankvnk 0:0a6e921b085b 6 class DemoClass {
frankvnk 0:0a6e921b085b 7 public:
frankvnk 0:0a6e921b085b 8 /**
frankvnk 0:0a6e921b085b 9 * \brief Create an object connected to I2C bus, irq input or digital output and user-ISR.
frankvnk 0:0a6e921b085b 10 * \param sda SDA pin.
frankvnk 0:0a6e921b085b 11 * \param scl SCL pin.
frankvnk 0:0a6e921b085b 12 * \param irqsync Interrupt input when called with user-ISR pointer.
frankvnk 0:0a6e921b085b 13 * Digital output when called without user-ISR pointer.
frankvnk 0:0a6e921b085b 14 * \param fptr Pointer to user-ISR.
frankvnk 0:0a6e921b085b 15 * \return none
frankvnk 0:0a6e921b085b 16 */
frankvnk 0:0a6e921b085b 17
frankvnk 0:0a6e921b085b 18 DemoClass(PinName sda, PinName scl, PinName irqsync = NC, void (*fptr)(void) = NULL);
frankvnk 0:0a6e921b085b 19
frankvnk 0:0a6e921b085b 20 /**
frankvnk 0:0a6e921b085b 21 * \brief Example status function.
frankvnk 0:0a6e921b085b 22 * \param none.
frankvnk 0:0a6e921b085b 23 * \return Always 1.
frankvnk 0:0a6e921b085b 24 */
frankvnk 0:0a6e921b085b 25 bool Status(void);
frankvnk 0:0a6e921b085b 26
frankvnk 0:0a6e921b085b 27 private:
frankvnk 0:0a6e921b085b 28 I2C _i2c;
frankvnk 0:0a6e921b085b 29 FunctionPointer _fptr;
frankvnk 0:0a6e921b085b 30 void _sensorISR(void);
frankvnk 0:0a6e921b085b 31 };
frankvnk 0:0a6e921b085b 32
frankvnk 0:0a6e921b085b 33 #endif