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.cpp@0:0a6e921b085b, 2014-05-03 (annotated)
- Committer:
- frankvnk
- Date:
- Sat May 03 16:15:38 2014 +0000
- Revision:
- 0:0a6e921b085b
Initial release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
frankvnk | 0:0a6e921b085b | 1 | #include "DemoClass.h" |
frankvnk | 0:0a6e921b085b | 2 | |
frankvnk | 0:0a6e921b085b | 3 | InterruptIn *_irqpin; |
frankvnk | 0:0a6e921b085b | 4 | DigitalOut *_syncpin; |
frankvnk | 0:0a6e921b085b | 5 | |
frankvnk | 0:0a6e921b085b | 6 | // Depending on how the ctor is called, irqsync is either set to none, InterruptIn or DigitalOut |
frankvnk | 0:0a6e921b085b | 7 | DemoClass::DemoClass(PinName sda, PinName scl, PinName irqsync, void (*fptr)(void)) : _i2c(sda, scl) |
frankvnk | 0:0a6e921b085b | 8 | { |
frankvnk | 0:0a6e921b085b | 9 | // When both irqsync and fptr are nonzero, we use InterruptIn: irqsync pin is interrupt input and Attach ISR. |
frankvnk | 0:0a6e921b085b | 10 | if((irqsync != NC) && (fptr != NULL)) |
frankvnk | 0:0a6e921b085b | 11 | { |
frankvnk | 0:0a6e921b085b | 12 | _irqpin = new InterruptIn(irqsync); // Create InterruptIn pin |
frankvnk | 0:0a6e921b085b | 13 | _irqpin->fall(this, &DemoClass::_sensorISR); // Attach falling interrupt to local ISR |
frankvnk | 0:0a6e921b085b | 14 | _fptr.attach(fptr); // Attach function pointer to user function |
frankvnk | 0:0a6e921b085b | 15 | } |
frankvnk | 0:0a6e921b085b | 16 | |
frankvnk | 0:0a6e921b085b | 17 | // When fptr is not defined, we use DigitalOut: irqsync pin is digital output. |
frankvnk | 0:0a6e921b085b | 18 | if((irqsync != NC) && (fptr == NULL)) |
frankvnk | 0:0a6e921b085b | 19 | { |
frankvnk | 0:0a6e921b085b | 20 | _syncpin = new DigitalOut(irqsync); // Create DigitalOut pin |
frankvnk | 0:0a6e921b085b | 21 | _syncpin->write(0); // Set pin to 0 |
frankvnk | 0:0a6e921b085b | 22 | } |
frankvnk | 0:0a6e921b085b | 23 | } |
frankvnk | 0:0a6e921b085b | 24 | |
frankvnk | 0:0a6e921b085b | 25 | bool DemoClass::Status(void) |
frankvnk | 0:0a6e921b085b | 26 | { |
frankvnk | 0:0a6e921b085b | 27 | return (1); |
frankvnk | 0:0a6e921b085b | 28 | } |
frankvnk | 0:0a6e921b085b | 29 | |
frankvnk | 0:0a6e921b085b | 30 | void DemoClass::_sensorISR(void) |
frankvnk | 0:0a6e921b085b | 31 | { |
frankvnk | 0:0a6e921b085b | 32 | uint8_t statret = Status(); // Read a status register (eg : to clear an interrupt flag). |
frankvnk | 0:0a6e921b085b | 33 | _fptr.call(); // Call the user-ISR |
frankvnk | 0:0a6e921b085b | 34 | } |