Skip to content

Commit a7f4262

Browse files
gekytheotherjimmy
authored andcommitted
Adopt Callback class in hal
1 parent 2e112f5 commit a7f4262

File tree

10 files changed

+116
-124
lines changed

10 files changed

+116
-124
lines changed

hal/api/CAN.h

Lines changed: 22 additions & 16 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 "FunctionPointer.h"
25+
#include "Callback.h"
2626

2727
namespace mbed {
2828

@@ -206,34 +206,40 @@ class CAN {
206206
/** Attach a function to call whenever a CAN frame received interrupt is
207207
* generated.
208208
*
209-
* @param fptr A pointer to a void function, or 0 to set as none
209+
* @param func A pointer to a void function, or 0 to set as none
210210
* @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)
211211
*/
212-
void attach(void (*fptr)(void), IrqType type=RxIrq);
212+
void attach(Callback<void()> func, IrqType type=RxIrq);
213213

214214
/** Attach a member function to call whenever a CAN frame received interrupt
215215
* is generated.
216216
*
217-
* @param tptr pointer to the object to call the member function on
218-
* @param mptr pointer to the member function to be called
217+
* @param obj pointer to the object to call the member function on
218+
* @param method pointer to the member function to be called
219219
* @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, TxIrq for transmitted or aborted, EwIrq for error warning, DoIrq for data overrun, WuIrq for wake-up, EpIrq for error passive, AlIrq for arbitration lost, BeIrq for bus error)
220220
*/
221-
template<typename T>
222-
void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
223-
if((mptr != NULL) && (tptr != NULL)) {
224-
_irq[type].attach(tptr, mptr);
225-
can_irq_set(&_can, (CanIrqType)type, 1);
226-
}
227-
else {
228-
can_irq_set(&_can, (CanIrqType)type, 0);
229-
}
221+
template<typename T>
222+
void attach(T* obj, void (T::*method)(), IrqType type=RxIrq) {
223+
attach(Callback<void()>(obj, method), type);
224+
}
225+
226+
/** Attach a member function to call whenever a CAN frame received interrupt
227+
* is generated.
228+
*
229+
* @param obj pointer to the object to call the member function on
230+
* @param method pointer to the member function to be called
231+
* @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, TxIrq for transmitted or aborted, EwIrq for error warning, DoIrq for data overrun, WuIrq for wake-up, EpIrq for error passive, AlIrq for arbitration lost, BeIrq for bus error)
232+
*/
233+
template<typename T>
234+
void attach(T* obj, void (*method)(T*), IrqType type=RxIrq) {
235+
attach(Callback<void()>(obj, method), type);
230236
}
231237

232238
static void _irq_handler(uint32_t id, CanIrqType type);
233239

234240
protected:
235-
can_t _can;
236-
FunctionPointer _irq[9];
241+
can_t _can;
242+
Callback<void()> _irq[9];
237243
};
238244

239245
} // namespace mbed

hal/api/CallChain.h

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#ifndef MBED_CALLCHAIN_H
1717
#define MBED_CALLCHAIN_H
1818

19-
#include "FunctionPointer.h"
19+
#include "Callback.h"
2020
#include <string.h>
2121

2222
namespace mbed {
@@ -57,7 +57,7 @@ namespace mbed {
5757
* @endcode
5858
*/
5959

60-
typedef FunctionPointer* pFunctionPointer_t;
60+
typedef Callback<void()> *pFunctionPointer_t;
6161

6262
class CallChain {
6363
public:
@@ -70,34 +70,34 @@ class CallChain {
7070

7171
/** Add a function at the end of the chain
7272
*
73-
* @param function A pointer to a void function
73+
* @param func A pointer to a void function
7474
*
7575
* @returns
76-
* The function object created for 'function'
76+
* The function object created for 'func'
7777
*/
78-
pFunctionPointer_t add(void (*function)(void));
78+
pFunctionPointer_t add(Callback<void()> func);
7979

8080
/** Add a function at the end of the chain
8181
*
82-
* @param tptr pointer to the object to call the member function on
83-
* @param mptr pointer to the member function to be called
82+
* @param obj pointer to the object to call the member function on
83+
* @param method pointer to the member function to be called
8484
*
8585
* @returns
86-
* The function object created for 'tptr' and 'mptr'
86+
* The function object created for 'obj' and 'method'
8787
*/
88-
template<typename T>
89-
pFunctionPointer_t add(T *tptr, void (T::*mptr)(void)) {
90-
return common_add(new FunctionPointer(tptr, mptr));
88+
template<typename T, typename M>
89+
pFunctionPointer_t add(T *obj, M method) {
90+
return add(Callback<void()>(obj, method));
9191
}
9292

9393
/** Add a function at the beginning of the chain
9494
*
95-
* @param function A pointer to a void function
95+
* @param func A pointer to a void function
9696
*
9797
* @returns
98-
* The function object created for 'function'
98+
* The function object created for 'func'
9999
*/
100-
pFunctionPointer_t add_front(void (*function)(void));
100+
pFunctionPointer_t add_front(Callback<void()> func);
101101

102102
/** Add a function at the beginning of the chain
103103
*
@@ -107,9 +107,9 @@ class CallChain {
107107
* @returns
108108
* The function object created for 'tptr' and 'mptr'
109109
*/
110-
template<typename T>
111-
pFunctionPointer_t add_front(T *tptr, void (T::*mptr)(void)) {
112-
return common_add_front(new FunctionPointer(tptr, mptr));
110+
template<typename T, typename M>
111+
pFunctionPointer_t add_front(T *obj, M method) {
112+
return add_front(Callback<void()>(obj, method));
113113
}
114114

115115
/** Get the number of functions in the chain
@@ -162,8 +162,6 @@ class CallChain {
162162

163163
private:
164164
void _check_size();
165-
pFunctionPointer_t common_add(pFunctionPointer_t pf);
166-
pFunctionPointer_t common_add_front(pFunctionPointer_t pf);
167165

168166
pFunctionPointer_t* _chain;
169167
int _size;

hal/api/InterruptIn.h

Lines changed: 17 additions & 19 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 "FunctionPointer.h"
25+
#include "Callback.h"
2626

2727
namespace mbed {
2828

@@ -70,36 +70,34 @@ class InterruptIn {
7070

7171
/** Attach a function to call when a rising edge occurs on the input
7272
*
73-
* @param fptr A pointer to a void function, or 0 to set as none
73+
* @param func A pointer to a void function, or 0 to set as none
7474
*/
75-
void rise(void (*fptr)(void));
75+
void rise(Callback<void()> func);
7676

7777
/** Attach a member function to call when a rising edge occurs on the input
7878
*
79-
* @param tptr pointer to the object to call the member function on
80-
* @param mptr pointer to the member function to be called
79+
* @param obj pointer to the object to call the member function on
80+
* @param method pointer to the member function to be called
8181
*/
82-
template<typename T>
83-
void rise(T* tptr, void (T::*mptr)(void)) {
84-
_rise.attach(tptr, mptr);
85-
gpio_irq_set(&gpio_irq, IRQ_RISE, 1);
82+
template<typename T, typename M>
83+
void rise(T *obj, M method) {
84+
rise(Callback<void()>(obj, method));
8685
}
8786

8887
/** Attach a function to call when a falling edge occurs on the input
8988
*
90-
* @param fptr A pointer to a void function, or 0 to set as none
89+
* @param func A pointer to a void function, or 0 to set as none
9190
*/
92-
void fall(void (*fptr)(void));
91+
void fall(Callback<void()> func);
9392

9493
/** Attach a member function to call when a falling edge occurs on the input
9594
*
96-
* @param tptr pointer to the object to call the member function on
97-
* @param mptr pointer to the member function to be called
95+
* @param obj pointer to the object to call the member function on
96+
* @param method pointer to the member function to be called
9897
*/
99-
template<typename T>
100-
void fall(T* tptr, void (T::*mptr)(void)) {
101-
_fall.attach(tptr, mptr);
102-
gpio_irq_set(&gpio_irq, IRQ_FALL, 1);
98+
template<typename T, typename M>
99+
void fall(T *obj, M method) {
100+
fall(Callback<void()>(obj, method));
103101
}
104102

105103
/** Set the input pin mode
@@ -124,8 +122,8 @@ class InterruptIn {
124122
gpio_t gpio;
125123
gpio_irq_t gpio_irq;
126124

127-
FunctionPointer _rise;
128-
FunctionPointer _fall;
125+
Callback<void()> _rise;
126+
Callback<void()> _fall;
129127
};
130128

131129
} // namespace mbed

hal/api/SerialBase.h

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

2323
#include "Stream.h"
24-
#include "FunctionPointer.h"
24+
#include "Callback.h"
2525
#include "serial_api.h"
2626

2727
#if DEVICE_SERIAL_ASYNCH
@@ -89,25 +89,31 @@ class SerialBase {
8989

9090
/** Attach a function to call whenever a serial interrupt is generated
9191
*
92-
* @param fptr A pointer to a void function, or 0 to set as none
92+
* @param func A pointer to a void function, or 0 to set as none
9393
* @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
9494
*/
95-
void attach(void (*fptr)(void), IrqType type=RxIrq);
95+
void attach(Callback<void()> func, IrqType type=RxIrq);
9696

9797
/** Attach a member function to call whenever a serial interrupt is generated
9898
*
99-
* @param tptr pointer to the object to call the member function on
100-
* @param mptr pointer to the member function to be called
99+
* @param obj pointer to the object to call the member function on
100+
* @param method pointer to the member function to be called
101101
* @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
102102
*/
103103
template<typename T>
104-
void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
105-
if((mptr != NULL) && (tptr != NULL)) {
106-
_irq[type].attach(tptr, mptr);
107-
serial_irq_set(&_serial, (SerialIrq)type, 1);
108-
} else {
109-
serial_irq_set(&_serial, (SerialIrq)type, 0);
110-
}
104+
void attach(T *obj, void (T::*method)(), IrqType type=RxIrq) {
105+
attach(Callback<void()>(obj, method), type);
106+
}
107+
108+
/** Attach a member function to call whenever a serial interrupt is generated
109+
*
110+
* @param obj pointer to the object to call the member function on
111+
* @param method pointer to the member function to be called
112+
* @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
113+
*/
114+
template<typename T>
115+
void attach(T *obj, void (*method)(T*), IrqType type=RxIrq) {
116+
attach(Callback<void()>(obj, method), type);
111117
}
112118

113119
/** Generate a break condition on the serial line
@@ -210,9 +216,9 @@ class SerialBase {
210216
DMAUsage _rx_usage;
211217
#endif
212218

213-
serial_t _serial;
214-
FunctionPointer _irq[2];
215-
int _baud;
219+
serial_t _serial;
220+
Callback<void()> _irq[2];
221+
int _baud;
216222

217223
};
218224

hal/api/Ticker.h

Lines changed: 16 additions & 17 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 "FunctionPointer.h"
20+
#include "Callback.h"
2121

2222
namespace mbed {
2323

@@ -65,31 +65,31 @@ class Ticker : public TimerEvent {
6565

6666
/** Attach a function to be called by the Ticker, specifiying the interval in seconds
6767
*
68-
* @param fptr pointer to the function to be called
68+
* @param func pointer to the function to be called
6969
* @param t the time between calls in seconds
7070
*/
71-
void attach(void (*fptr)(void), float t) {
72-
attach_us(fptr, t * 1000000.0f);
71+
void attach(Callback<void()> func, float t) {
72+
attach_us(func, t * 1000000.0f);
7373
}
7474

7575
/** Attach a member function to be called by the Ticker, specifiying the interval in seconds
7676
*
77-
* @param tptr pointer to the object to call the member function on
78-
* @param mptr pointer to the member function to be called
77+
* @param obj pointer to the object to call the member function on
78+
* @param method pointer to the member function to be called
7979
* @param t the time between calls in seconds
8080
*/
81-
template<typename T>
82-
void attach(T* tptr, void (T::*mptr)(void), float t) {
83-
attach_us(tptr, mptr, t * 1000000.0f);
81+
template<typename T, typename M>
82+
void attach(T *obj, M method, float t) {
83+
attach(Callback<void()>(obj, method), t);
8484
}
8585

8686
/** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds
8787
*
8888
* @param fptr pointer to the function to be called
8989
* @param t the time between calls in micro-seconds
9090
*/
91-
void attach_us(void (*fptr)(void), timestamp_t t) {
92-
_function.attach(fptr);
91+
void attach_us(Callback<void()> func, timestamp_t t) {
92+
_function.attach(func);
9393
setup(t);
9494
}
9595

@@ -99,10 +99,9 @@ class Ticker : public TimerEvent {
9999
* @param mptr pointer to the member function to be called
100100
* @param t the time between calls in micro-seconds
101101
*/
102-
template<typename T>
103-
void attach_us(T* tptr, void (T::*mptr)(void), timestamp_t t) {
104-
_function.attach(tptr, mptr);
105-
setup(t);
102+
template<typename T, typename M>
103+
void attach_us(T *obj, M method, timestamp_t t) {
104+
attach_us(Callback<void()>(obj, method), t);
106105
}
107106

108107
virtual ~Ticker() {
@@ -118,8 +117,8 @@ class Ticker : public TimerEvent {
118117
virtual void handler();
119118

120119
protected:
121-
timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */
122-
FunctionPointer _function; /**< Callback. */
120+
timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */
121+
Callback<void()> _function; /**< Callback. */
123122
};
124123

125124
} // namespace mbed

hal/common/CAN.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ int CAN::filter(unsigned int id, unsigned int mask, CANFormat format, int handle
6767
return can_filter(&_can, id, mask, format, handle);
6868
}
6969

70-
void CAN::attach(void (*fptr)(void), IrqType type) {
71-
if (fptr) {
72-
_irq[(CanIrqType)type].attach(fptr);
70+
void CAN::attach(Callback<void()> func, IrqType type) {
71+
if (func) {
72+
_irq[(CanIrqType)type].attach(func);
7373
can_irq_set(&_can, (CanIrqType)type, 1);
7474
} else {
7575
can_irq_set(&_can, (CanIrqType)type, 0);

0 commit comments

Comments
 (0)