Adopt Callback class in hal

pull/1793/head
Christopher Haster 2016-05-15 16:17:47 -05:00 committed by Jimmy Brisson
parent 2e112f535a
commit a7f4262858
10 changed files with 116 additions and 124 deletions

View File

@ -22,7 +22,7 @@
#include "can_api.h"
#include "can_helper.h"
#include "FunctionPointer.h"
#include "Callback.h"
namespace mbed {
@ -206,34 +206,40 @@ public:
/** Attach a function to call whenever a CAN frame received interrupt is
* generated.
*
* @param fptr A pointer to a void function, or 0 to set as none
* @param func A pointer to a void function, or 0 to set as none
* @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)
*/
void attach(void (*fptr)(void), IrqType type=RxIrq);
void attach(Callback<void()> func, IrqType type=RxIrq);
/** Attach a member function to call whenever a CAN frame received interrupt
* is generated.
*
* @param tptr pointer to the object to call the member function on
* @param mptr pointer to the member function to be called
* @param obj pointer to the object to call the member function on
* @param method pointer to the member function to be called
* @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)
*/
template<typename T>
void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
if((mptr != NULL) && (tptr != NULL)) {
_irq[type].attach(tptr, mptr);
can_irq_set(&_can, (CanIrqType)type, 1);
}
else {
can_irq_set(&_can, (CanIrqType)type, 0);
}
template<typename T>
void attach(T* obj, void (T::*method)(), IrqType type=RxIrq) {
attach(Callback<void()>(obj, method), type);
}
/** Attach a member function to call whenever a CAN frame received interrupt
* is generated.
*
* @param obj pointer to the object to call the member function on
* @param method pointer to the member function to be called
* @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)
*/
template<typename T>
void attach(T* obj, void (*method)(T*), IrqType type=RxIrq) {
attach(Callback<void()>(obj, method), type);
}
static void _irq_handler(uint32_t id, CanIrqType type);
protected:
can_t _can;
FunctionPointer _irq[9];
can_t _can;
Callback<void()> _irq[9];
};
} // namespace mbed

View File

@ -16,7 +16,7 @@
#ifndef MBED_CALLCHAIN_H
#define MBED_CALLCHAIN_H
#include "FunctionPointer.h"
#include "Callback.h"
#include <string.h>
namespace mbed {
@ -57,7 +57,7 @@ namespace mbed {
* @endcode
*/
typedef FunctionPointer* pFunctionPointer_t;
typedef Callback<void()> *pFunctionPointer_t;
class CallChain {
public:
@ -70,34 +70,34 @@ public:
/** Add a function at the end of the chain
*
* @param function A pointer to a void function
* @param func A pointer to a void function
*
* @returns
* The function object created for 'function'
* The function object created for 'func'
*/
pFunctionPointer_t add(void (*function)(void));
pFunctionPointer_t add(Callback<void()> func);
/** Add a function at the end of the chain
*
* @param tptr pointer to the object to call the member function on
* @param mptr pointer to the member function to be called
* @param obj pointer to the object to call the member function on
* @param method pointer to the member function to be called
*
* @returns
* The function object created for 'tptr' and 'mptr'
* The function object created for 'obj' and 'method'
*/
template<typename T>
pFunctionPointer_t add(T *tptr, void (T::*mptr)(void)) {
return common_add(new FunctionPointer(tptr, mptr));
template<typename T, typename M>
pFunctionPointer_t add(T *obj, M method) {
return add(Callback<void()>(obj, method));
}
/** Add a function at the beginning of the chain
*
* @param function A pointer to a void function
* @param func A pointer to a void function
*
* @returns
* The function object created for 'function'
* The function object created for 'func'
*/
pFunctionPointer_t add_front(void (*function)(void));
pFunctionPointer_t add_front(Callback<void()> func);
/** Add a function at the beginning of the chain
*
@ -107,9 +107,9 @@ public:
* @returns
* The function object created for 'tptr' and 'mptr'
*/
template<typename T>
pFunctionPointer_t add_front(T *tptr, void (T::*mptr)(void)) {
return common_add_front(new FunctionPointer(tptr, mptr));
template<typename T, typename M>
pFunctionPointer_t add_front(T *obj, M method) {
return add_front(Callback<void()>(obj, method));
}
/** Get the number of functions in the chain
@ -162,8 +162,6 @@ public:
private:
void _check_size();
pFunctionPointer_t common_add(pFunctionPointer_t pf);
pFunctionPointer_t common_add_front(pFunctionPointer_t pf);
pFunctionPointer_t* _chain;
int _size;

View File

@ -22,7 +22,7 @@
#include "gpio_api.h"
#include "gpio_irq_api.h"
#include "FunctionPointer.h"
#include "Callback.h"
namespace mbed {
@ -70,36 +70,34 @@ public:
/** Attach a function to call when a rising edge occurs on the input
*
* @param fptr A pointer to a void function, or 0 to set as none
* @param func A pointer to a void function, or 0 to set as none
*/
void rise(void (*fptr)(void));
void rise(Callback<void()> func);
/** Attach a member function to call when a rising edge occurs on the input
*
* @param tptr pointer to the object to call the member function on
* @param mptr pointer to the member function to be called
* @param obj pointer to the object to call the member function on
* @param method pointer to the member function to be called
*/
template<typename T>
void rise(T* tptr, void (T::*mptr)(void)) {
_rise.attach(tptr, mptr);
gpio_irq_set(&gpio_irq, IRQ_RISE, 1);
template<typename T, typename M>
void rise(T *obj, M method) {
rise(Callback<void()>(obj, method));
}
/** Attach a function to call when a falling edge occurs on the input
*
* @param fptr A pointer to a void function, or 0 to set as none
* @param func A pointer to a void function, or 0 to set as none
*/
void fall(void (*fptr)(void));
void fall(Callback<void()> func);
/** Attach a member function to call when a falling edge occurs on the input
*
* @param tptr pointer to the object to call the member function on
* @param mptr pointer to the member function to be called
* @param obj pointer to the object to call the member function on
* @param method pointer to the member function to be called
*/
template<typename T>
void fall(T* tptr, void (T::*mptr)(void)) {
_fall.attach(tptr, mptr);
gpio_irq_set(&gpio_irq, IRQ_FALL, 1);
template<typename T, typename M>
void fall(T *obj, M method) {
fall(Callback<void()>(obj, method));
}
/** Set the input pin mode
@ -124,8 +122,8 @@ protected:
gpio_t gpio;
gpio_irq_t gpio_irq;
FunctionPointer _rise;
FunctionPointer _fall;
Callback<void()> _rise;
Callback<void()> _fall;
};
} // namespace mbed

View File

@ -21,7 +21,7 @@
#if DEVICE_SERIAL
#include "Stream.h"
#include "FunctionPointer.h"
#include "Callback.h"
#include "serial_api.h"
#if DEVICE_SERIAL_ASYNCH
@ -89,25 +89,31 @@ public:
/** Attach a function to call whenever a serial interrupt is generated
*
* @param fptr A pointer to a void function, or 0 to set as none
* @param func A pointer to a void function, or 0 to set as none
* @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
*/
void attach(void (*fptr)(void), IrqType type=RxIrq);
void attach(Callback<void()> func, IrqType type=RxIrq);
/** Attach a member function to call whenever a serial interrupt is generated
*
* @param tptr pointer to the object to call the member function on
* @param mptr pointer to the member function to be called
* @param obj pointer to the object to call the member function on
* @param method pointer to the member function to be called
* @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
*/
template<typename T>
void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
if((mptr != NULL) && (tptr != NULL)) {
_irq[type].attach(tptr, mptr);
serial_irq_set(&_serial, (SerialIrq)type, 1);
} else {
serial_irq_set(&_serial, (SerialIrq)type, 0);
}
void attach(T *obj, void (T::*method)(), IrqType type=RxIrq) {
attach(Callback<void()>(obj, method), type);
}
/** Attach a member function to call whenever a serial interrupt is generated
*
* @param obj pointer to the object to call the member function on
* @param method pointer to the member function to be called
* @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
*/
template<typename T>
void attach(T *obj, void (*method)(T*), IrqType type=RxIrq) {
attach(Callback<void()>(obj, method), type);
}
/** Generate a break condition on the serial line
@ -210,9 +216,9 @@ protected:
DMAUsage _rx_usage;
#endif
serial_t _serial;
FunctionPointer _irq[2];
int _baud;
serial_t _serial;
Callback<void()> _irq[2];
int _baud;
};

View File

@ -17,7 +17,7 @@
#define MBED_TICKER_H
#include "TimerEvent.h"
#include "FunctionPointer.h"
#include "Callback.h"
namespace mbed {
@ -65,22 +65,22 @@ public:
/** Attach a function to be called by the Ticker, specifiying the interval in seconds
*
* @param fptr pointer to the function to be called
* @param func pointer to the function to be called
* @param t the time between calls in seconds
*/
void attach(void (*fptr)(void), float t) {
attach_us(fptr, t * 1000000.0f);
void attach(Callback<void()> func, float t) {
attach_us(func, t * 1000000.0f);
}
/** Attach a member function to be called by the Ticker, specifiying the interval in seconds
*
* @param tptr pointer to the object to call the member function on
* @param mptr pointer to the member function to be called
* @param obj pointer to the object to call the member function on
* @param method pointer to the member function to be called
* @param t the time between calls in seconds
*/
template<typename T>
void attach(T* tptr, void (T::*mptr)(void), float t) {
attach_us(tptr, mptr, t * 1000000.0f);
template<typename T, typename M>
void attach(T *obj, M method, float t) {
attach(Callback<void()>(obj, method), t);
}
/** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds
@ -88,8 +88,8 @@ public:
* @param fptr pointer to the function to be called
* @param t the time between calls in micro-seconds
*/
void attach_us(void (*fptr)(void), timestamp_t t) {
_function.attach(fptr);
void attach_us(Callback<void()> func, timestamp_t t) {
_function.attach(func);
setup(t);
}
@ -99,10 +99,9 @@ public:
* @param mptr pointer to the member function to be called
* @param t the time between calls in micro-seconds
*/
template<typename T>
void attach_us(T* tptr, void (T::*mptr)(void), timestamp_t t) {
_function.attach(tptr, mptr);
setup(t);
template<typename T, typename M>
void attach_us(T *obj, M method, timestamp_t t) {
attach_us(Callback<void()>(obj, method), t);
}
virtual ~Ticker() {
@ -118,8 +117,8 @@ protected:
virtual void handler();
protected:
timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */
FunctionPointer _function; /**< Callback. */
timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */
Callback<void()> _function; /**< Callback. */
};
} // namespace mbed

View File

@ -67,9 +67,9 @@ int CAN::filter(unsigned int id, unsigned int mask, CANFormat format, int handle
return can_filter(&_can, id, mask, format, handle);
}
void CAN::attach(void (*fptr)(void), IrqType type) {
if (fptr) {
_irq[(CanIrqType)type].attach(fptr);
void CAN::attach(Callback<void()> func, IrqType type) {
if (func) {
_irq[(CanIrqType)type].attach(func);
can_irq_set(&_can, (CanIrqType)type, 1);
} else {
can_irq_set(&_can, (CanIrqType)type, 0);

View File

@ -12,12 +12,19 @@ CallChain::~CallChain() {
delete _chain;
}
pFunctionPointer_t CallChain::add(void (*function)(void)) {
return common_add(new FunctionPointer(function));
pFunctionPointer_t CallChain::add(Callback<void()> func) {
_check_size();
_chain[_elements] = new Callback<void()>(func);
_elements ++;
return _chain[_elements];
}
pFunctionPointer_t CallChain::add_front(void (*function)(void)) {
return common_add_front(new FunctionPointer(function));
pFunctionPointer_t CallChain::add_front(Callback<void()> func) {
_check_size();
memmove(_chain + 1, _chain, _elements * sizeof(pFunctionPointer_t));
_chain[0] = new Callback<void()>(func);
_elements ++;
return _chain[0];
}
int CallChain::size() const {
@ -72,19 +79,4 @@ void CallChain::_check_size() {
_chain = new_chain;
}
pFunctionPointer_t CallChain::common_add(pFunctionPointer_t pf) {
_check_size();
_chain[_elements] = pf;
_elements ++;
return pf;
}
pFunctionPointer_t CallChain::common_add_front(pFunctionPointer_t pf) {
_check_size();
memmove(_chain + 1, _chain, _elements * sizeof(pFunctionPointer_t));
_chain[0] = pf;
_elements ++;
return pf;
}
} // namespace mbed

View File

@ -39,9 +39,9 @@ void InterruptIn::mode(PinMode pull) {
gpio_mode(&gpio, pull);
}
void InterruptIn::rise(void (*fptr)(void)) {
if (fptr) {
_rise.attach(fptr);
void InterruptIn::rise(Callback<void()> func) {
if (func) {
_rise.attach(func);
gpio_irq_set(&gpio_irq, IRQ_RISE, 1);
} else {
_rise.attach(NULL);
@ -49,9 +49,9 @@ void InterruptIn::rise(void (*fptr)(void)) {
}
}
void InterruptIn::fall(void (*fptr)(void)) {
if (fptr) {
_fall.attach(fptr);
void InterruptIn::fall(Callback<void()> func) {
if (func) {
_fall.attach(func);
gpio_irq_set(&gpio_irq, IRQ_FALL, 1);
} else {
_fall.attach(NULL);

View File

@ -66,13 +66,6 @@ bool InterruptManager::remove_handler(pFunctionPointer_t handler, IRQn_Type irq)
return false;
if (!_chains[irq_pos]->remove(handler))
return false;
// If there's a single function left in the chain, swith the interrupt vector
// to call that function directly. This way we save both time and space.
if (_chains[irq_pos]->size() == 1 && NULL != _chains[irq_pos]->get(0)->get_function()) {
NVIC_SetVector(irq, (uint32_t)_chains[irq_pos]->get(0)->get_function());
delete _chains[irq_pos];
_chains[irq_pos] = (CallChain*) NULL;
}
return true;
}

View File

@ -48,9 +48,9 @@ int SerialBase::writeable() {
return serial_writable(&_serial);
}
void SerialBase::attach(void (*fptr)(void), IrqType type) {
if (fptr) {
_irq[type].attach(fptr);
void SerialBase::attach(Callback<void()> func, IrqType type) {
if (func) {
_irq[type].attach(func);
serial_irq_set(&_serial, (SerialIrq)type, 1);
} else {
serial_irq_set(&_serial, (SerialIrq)type, 0);