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

View File

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

View File

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

View File

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

View File

@ -17,7 +17,7 @@
#define MBED_TICKER_H #define MBED_TICKER_H
#include "TimerEvent.h" #include "TimerEvent.h"
#include "FunctionPointer.h" #include "Callback.h"
namespace mbed { namespace mbed {
@ -65,22 +65,22 @@ public:
/** Attach a function to be called by the Ticker, specifiying the interval in seconds /** 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 * @param t the time between calls in seconds
*/ */
void attach(void (*fptr)(void), float t) { void attach(Callback<void()> func, float t) {
attach_us(fptr, t * 1000000.0f); attach_us(func, t * 1000000.0f);
} }
/** Attach a member function to be called by the Ticker, specifiying the interval in seconds /** 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 obj pointer to the object to call the member function on
* @param mptr pointer to the member function to be called * @param method pointer to the member function to be called
* @param t the time between calls in seconds * @param t the time between calls in seconds
*/ */
template<typename T> template<typename T, typename M>
void attach(T* tptr, void (T::*mptr)(void), float t) { void attach(T *obj, M method, float t) {
attach_us(tptr, mptr, t * 1000000.0f); attach(Callback<void()>(obj, method), t);
} }
/** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds /** 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 fptr pointer to the function to be called
* @param t the time between calls in micro-seconds * @param t the time between calls in micro-seconds
*/ */
void attach_us(void (*fptr)(void), timestamp_t t) { void attach_us(Callback<void()> func, timestamp_t t) {
_function.attach(fptr); _function.attach(func);
setup(t); setup(t);
} }
@ -99,10 +99,9 @@ public:
* @param mptr pointer to the member function to be called * @param mptr pointer to the member function to be called
* @param t the time between calls in micro-seconds * @param t the time between calls in micro-seconds
*/ */
template<typename T> template<typename T, typename M>
void attach_us(T* tptr, void (T::*mptr)(void), timestamp_t t) { void attach_us(T *obj, M method, timestamp_t t) {
_function.attach(tptr, mptr); attach_us(Callback<void()>(obj, method), t);
setup(t);
} }
virtual ~Ticker() { virtual ~Ticker() {
@ -119,7 +118,7 @@ protected:
protected: protected:
timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */ timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */
FunctionPointer _function; /**< Callback. */ Callback<void()> _function; /**< Callback. */
}; };
} // namespace mbed } // 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); return can_filter(&_can, id, mask, format, handle);
} }
void CAN::attach(void (*fptr)(void), IrqType type) { void CAN::attach(Callback<void()> func, IrqType type) {
if (fptr) { if (func) {
_irq[(CanIrqType)type].attach(fptr); _irq[(CanIrqType)type].attach(func);
can_irq_set(&_can, (CanIrqType)type, 1); can_irq_set(&_can, (CanIrqType)type, 1);
} else { } else {
can_irq_set(&_can, (CanIrqType)type, 0); can_irq_set(&_can, (CanIrqType)type, 0);

View File

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

View File

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

View File

@ -66,13 +66,6 @@ bool InterruptManager::remove_handler(pFunctionPointer_t handler, IRQn_Type irq)
return false; return false;
if (!_chains[irq_pos]->remove(handler)) if (!_chains[irq_pos]->remove(handler))
return false; 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; return true;
} }

View File

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