Skip to content

Commit 3df5eca

Browse files
author
Bogdan Marinescu
committed
Replaced callback type to Event in core classes
In the core classes, the callback event type (used for 'attach'-like functions) was 'Callback<void()>'. This commit changes the type to Event, which allows the callbacks to be used both like regular callbacks, or enqueued in an event queue for deferred execution. The commit preserves backward compatibility (the default behaviour is still to run the callbacks immediately (like 'Callback' does), not defer them). Event and Callback have similar constructors and attach() functions, so it was enough to replace one type with the other, without other code changes. Future similar changes to the API should be able to follow the same pattern.
1 parent 071098d commit 3df5eca

File tree

7 files changed

+19
-19
lines changed

7 files changed

+19
-19
lines changed

hal/api/CAN.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
#include "can_api.h"
2424
#include "can_helper.h"
25-
#include "Callback.h"
25+
#include "Event.h"
2626
#include "PlatformMutex.h"
2727

2828
namespace mbed {
@@ -214,7 +214,7 @@ class CAN {
214214
* @param func A pointer to a void function, or 0 to set as none
215215
* @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, CAN::TxIrq for transmitted or aborted, CAN::EwIrq for error warning, CAN::DoIrq for data overrun, CAN::WuIrq for wake-up, CAN::EpIrq for error passive, CAN::AlIrq for arbitration lost, CAN::BeIrq for bus error)
216216
*/
217-
void attach(Callback<void()> func, IrqType type=RxIrq);
217+
void attach(const Event& func, IrqType type=RxIrq);
218218

219219
/** Attach a member function to call whenever a CAN frame received interrupt
220220
* is generated.
@@ -248,7 +248,7 @@ class CAN {
248248
virtual void lock();
249249
virtual void unlock();
250250
can_t _can;
251-
Callback<void()> _irq[IrqCnt];
251+
Event _irq[IrqCnt];
252252
PlatformMutex _mutex;
253253
};
254254

hal/api/InterruptIn.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
#include "gpio_api.h"
2424
#include "gpio_irq_api.h"
25-
#include "Callback.h"
25+
#include "Event.h"
2626
#include "critical.h"
2727
#include "toolchain.h"
2828

@@ -83,7 +83,7 @@ class InterruptIn {
8383
*
8484
* @param func A pointer to a void function, or 0 to set as none
8585
*/
86-
void rise(Callback<void()> func);
86+
void rise(const Event& func);
8787

8888
/** Attach a member function to call when a rising edge occurs on the input
8989
*
@@ -107,7 +107,7 @@ class InterruptIn {
107107
*
108108
* @param func A pointer to a void function, or 0 to set as none
109109
*/
110-
void fall(Callback<void()> func);
110+
void fall(const Event& func);
111111

112112
/** Attach a member function to call when a falling edge occurs on the input
113113
*
@@ -149,8 +149,8 @@ class InterruptIn {
149149
gpio_t gpio;
150150
gpio_irq_t gpio_irq;
151151

152-
Callback<void()> _rise;
153-
Callback<void()> _fall;
152+
Event _rise;
153+
Event _fall;
154154
};
155155

156156
} // namespace mbed

hal/api/SerialBase.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#if DEVICE_SERIAL
2222

2323
#include "Stream.h"
24-
#include "Callback.h"
24+
#include "Event.h"
2525
#include "serial_api.h"
2626
#include "toolchain.h"
2727

@@ -97,7 +97,7 @@ class SerialBase {
9797
* @param func A pointer to a void function, or 0 to set as none
9898
* @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
9999
*/
100-
void attach(Callback<void()> func, IrqType type=RxIrq);
100+
void attach(const Event& func, IrqType type=RxIrq);
101101

102102
/** Attach a member function to call whenever a serial interrupt is generated
103103
*
@@ -246,7 +246,7 @@ class SerialBase {
246246
#endif
247247

248248
serial_t _serial;
249-
Callback<void()> _irq[IrqCnt];
249+
Event _irq[IrqCnt];
250250
int _baud;
251251

252252
};

hal/api/Ticker.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#define MBED_TICKER_H
1818

1919
#include "TimerEvent.h"
20-
#include "Callback.h"
20+
#include "Event.h"
2121
#include "toolchain.h"
2222

2323
namespace mbed {
@@ -72,7 +72,7 @@ class Ticker : public TimerEvent {
7272
* @param func pointer to the function to be called
7373
* @param t the time between calls in seconds
7474
*/
75-
void attach(Callback<void()> func, float t) {
75+
void attach(const Event& func, float t) {
7676
attach_us(func, t * 1000000.0f);
7777
}
7878

@@ -98,7 +98,7 @@ class Ticker : public TimerEvent {
9898
* @param fptr pointer to the function to be called
9999
* @param t the time between calls in micro-seconds
100100
*/
101-
void attach_us(Callback<void()> func, timestamp_t t) {
101+
void attach_us(const Event& func, timestamp_t t) {
102102
_function.attach(func);
103103
setup(t);
104104
}
@@ -134,7 +134,7 @@ class Ticker : public TimerEvent {
134134

135135
protected:
136136
timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */
137-
Callback<void()> _function; /**< Callback. */
137+
Event _function; /**< Callback. */
138138
};
139139

140140
} // namespace mbed

hal/common/CAN.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ int CAN::filter(unsigned int id, unsigned int mask, CANFormat format, int handle
101101
return ret;
102102
}
103103

104-
void CAN::attach(Callback<void()> func, IrqType type) {
104+
void CAN::attach(const Event& func, IrqType type) {
105105
lock();
106106
if (func) {
107107
_irq[(CanIrqType)type].attach(func);

hal/common/InterruptIn.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void InterruptIn::mode(PinMode pull) {
5050
core_util_critical_section_exit();
5151
}
5252

53-
void InterruptIn::rise(Callback<void()> func) {
53+
void InterruptIn::rise(const Event& func) {
5454
core_util_critical_section_enter();
5555
if (func) {
5656
_rise.attach(func);
@@ -62,7 +62,7 @@ void InterruptIn::rise(Callback<void()> func) {
6262
core_util_critical_section_exit();
6363
}
6464

65-
void InterruptIn::fall(Callback<void()> func) {
65+
void InterruptIn::fall(const Event& func) {
6666
core_util_critical_section_enter();
6767
if (func) {
6868
_fall.attach(func);

hal/common/SerialBase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ int SerialBase::writeable() {
6767
return ret;
6868
}
6969

70-
void SerialBase::attach(Callback<void()> func, IrqType type) {
70+
void SerialBase::attach(const Event& func, IrqType type) {
7171
lock();
7272
// Disable interrupts when attaching interrupt handler
7373
core_util_critical_section_enter();

0 commit comments

Comments
 (0)