blob: 8334f86ddbcdd431dc5f5e5963cae405575aebb3 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#ifndef AOS_CRIO_SHARED_LIBS_INTERRUPT_NOTIFIER_H_
2#define AOS_CRIO_SHARED_LIBS_INTERRUPT_NOTIFIER_H_
3
4#include "WPILib/InterruptableSensorBase.h"
5
brians343bc112013-02-10 01:53:46 +00006namespace aos {
7namespace crio {
8
9// An InterruptBridge that notifies based on interrupts from a WPILib
10// InterruptableSensorBase object (which DigitalInput is an example of).
brians56032682013-03-02 21:00:17 +000011// Not actually a subclass because WPILib appears to not really use actual
12// interrupts.
brians343bc112013-02-10 01:53:46 +000013template<typename T>
brians56032682013-03-02 21:00:17 +000014class InterruptNotifier {
brians343bc112013-02-10 01:53:46 +000015 public:
brians56032682013-03-02 21:00:17 +000016 typedef void (*Handler)(T *param);
17
brians343bc112013-02-10 01:53:46 +000018 // 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).
brians56032682013-03-02 21:00:17 +000025 InterruptNotifier(Handler handler,
brians343bc112013-02-10 01:53:46 +000026 InterruptableSensorBase *sensor,
brians56032682013-03-02 21:00:17 +000027 T *param = NULL);
brians343bc112013-02-10 01:53:46 +000028 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
brians56032682013-03-02 21:00:17 +000035 // interruptAssertedMask in WPILib/ChipObject/tInterruptManager.h.
brians343bc112013-02-10 01:53:46 +000036 // 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
brians56032682013-03-02 21:00:17 +000041 const Handler handler_;
42 T *const param_;
43
brians343bc112013-02-10 01:53:46 +000044 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_