diff --git a/src/platforms/px4_nodehandle.h b/src/platforms/px4_nodehandle.h index 1608868101..97fb25563c 100644 --- a/src/platforms/px4_nodehandle.h +++ b/src/platforms/px4_nodehandle.h @@ -168,7 +168,7 @@ public: std::function callback, unsigned interval) { - SubscriberUORB *sub_px4 = new SubscriberUORB(meta, interval, callback, &_subs); + SubscriberUORBCallback *sub_px4 = new SubscriberUORBCallback(meta, interval, callback, &_subs); /* Check if this is the smallest interval so far and update _sub_min_interval */ if (_sub_min_interval == nullptr || _sub_min_interval->getInterval() > sub_px4->getInterval()) { diff --git a/src/platforms/px4_subscriber.h b/src/platforms/px4_subscriber.h index 5d4e67ad01..8c299c7489 100644 --- a/src/platforms/px4_subscriber.h +++ b/src/platforms/px4_subscriber.h @@ -161,22 +161,6 @@ class SubscriberUORB : public uORB::Subscription { public: - /** - * Construct SubscriberUORB by providing orb meta data - * @param meta orb metadata for the topic which is used - * @param callback Callback, executed on receiving a new message - * @param interval Minimal interval between calls to callback - * @param list subscriber is added to this list - */ - SubscriberUORB(const struct orb_metadata *meta, - unsigned interval, - std::function callback, - List *list) : - Subscriber(), - uORB::Subscription(meta, interval, list), - _callback(callback) - //XXX store callback - {} /** * Construct SubscriberUORB by providing orb meta data without callback @@ -188,19 +172,61 @@ public: unsigned interval, List *list) : Subscriber(), - uORB::Subscription(meta, interval, list), - _callback(nullptr) - //XXX store callback + uORB::Subscription(meta, interval, list) {} ~SubscriberUORB() {}; + /** + * Update Subscription + * Invoked by the list traversal in NodeHandle::spinOnce + */ + virtual void update() + { + if (!uORB::Subscription::updated()) { + /* Topic not updated */ + return; + } + + /* get latest data */ + uORB::Subscription::update(); + }; + + /* Accessors*/ + /** + * Get the last message value + */ + const M& get() { return uORB::Subscription::getData(); } +}; + +template +class SubscriberUORBCallback : + public SubscriberUORB +{ +public: + /** + * Construct SubscriberUORBCallback by providing orb meta data + * @param meta orb metadata for the topic which is used + * @param callback Callback, executed on receiving a new message + * @param interval Minimal interval between calls to callback + * @param list subscriber is added to this list + */ + SubscriberUORBCallback(const struct orb_metadata *meta, + unsigned interval, + std::function callback, + List *list) : + SubscriberUORB(meta, interval, list), + _callback(callback) + {} + + ~SubscriberUORBCallback() {}; + /** * Update Subscription * Invoked by the list traversal in NodeHandle::spinOnce * If new data is available the callback is called */ - void update() + virtual void update() { if (!uORB::Subscription::updated()) { /* Topic not updated, do not call callback */ @@ -221,16 +247,9 @@ public: }; - /* Accessors*/ - /** - * Get the last message value - */ - const M& get() { return uORB::Subscription::getData(); } - protected: std::function _callback; /**< Callback handle, called when new data is available */ - }; #endif