brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #ifndef AOS_CRIO_SHARED_LIBS_INTERRUPT_NOTIFIER_H_ |
| 2 | #define AOS_CRIO_SHARED_LIBS_INTERRUPT_NOTIFIER_H_ |
| 3 | |
| 4 | #include "WPILib/InterruptableSensorBase.h" |
| 5 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 6 | namespace aos { |
| 7 | namespace crio { |
| 8 | |
| 9 | // An InterruptBridge that notifies based on interrupts from a WPILib |
| 10 | // InterruptableSensorBase object (which DigitalInput is an example of). |
brians | 5603268 | 2013-03-02 21:00:17 +0000 | [diff] [blame^] | 11 | // Not actually a subclass because WPILib appears to not really use actual |
| 12 | // interrupts. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 13 | template<typename T> |
brians | 5603268 | 2013-03-02 21:00:17 +0000 | [diff] [blame^] | 14 | class InterruptNotifier { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 15 | public: |
brians | 5603268 | 2013-03-02 21:00:17 +0000 | [diff] [blame^] | 16 | typedef void (*Handler)(T *param); |
| 17 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 18 | // This object will hold a reference to sensor, but will not free it. This |
| 19 | // object will take ownership of everything related to interrupts for sensor |
| 20 | // (ie this constructor will call sensor->RequestInterrupts). |
| 21 | // Interrupts should be cancelled (the state InterruptableSensorBases are |
| 22 | // constructed in) when this constructor is called. |
| 23 | // Any setup of sensor that is required should happen before Start() is |
| 24 | // called, but after this constructor (ie SetUpSourceEdge). |
brians | 5603268 | 2013-03-02 21:00:17 +0000 | [diff] [blame^] | 25 | InterruptNotifier(Handler handler, |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 26 | InterruptableSensorBase *sensor, |
brians | 5603268 | 2013-03-02 21:00:17 +0000 | [diff] [blame^] | 27 | T *param = NULL); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 28 | virtual ~InterruptNotifier(); |
| 29 | |
| 30 | // Starts calling the handler whenever the interrupt triggers. |
| 31 | void Start(); |
| 32 | |
| 33 | private: |
| 34 | // The only docs that seem to exist on the first arg is that it's named |
brians | 5603268 | 2013-03-02 21:00:17 +0000 | [diff] [blame^] | 35 | // interruptAssertedMask in WPILib/ChipObject/tInterruptManager.h. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 36 | // The second arg is the general callback parameter which will be a pointer to |
| 37 | // an instance. This function calls Notify() on that instance. |
| 38 | static void StaticNotify(uint32_t, void *self_in); |
| 39 | virtual void StopNotifications(); |
| 40 | |
brians | 5603268 | 2013-03-02 21:00:17 +0000 | [diff] [blame^] | 41 | const Handler handler_; |
| 42 | T *const param_; |
| 43 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 44 | InterruptableSensorBase *const sensor_; |
| 45 | DISALLOW_COPY_AND_ASSIGN(InterruptNotifier<T>); |
| 46 | }; |
| 47 | |
| 48 | } // namespace crio |
| 49 | } // namespace aos |
| 50 | |
| 51 | #include "aos/crio/shared_libs/interrupt_notifier-tmpl.h" |
| 52 | |
| 53 | #endif // AOS_CRIO_SHARED_LIBS_INTERRUPT_NOTIFIER_H_ |