A compilation of some hardware sensors and their shared programming interfaces.
PeriodicSensor.h@1:15396cab58d1, 2014-03-19 (annotated)
- Committer:
- mgottscho
- Date:
- Wed Mar 19 00:35:31 2014 +0000
- Revision:
- 1:15396cab58d1
- Parent:
- 0:8d34cc2ff388
Updated for most recent UtilityLib.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mgottscho | 0:8d34cc2ff388 | 1 | /* PeriodicSensor.h |
mgottscho | 0:8d34cc2ff388 | 2 | * Tested with mbed board: FRDM-KL46Z |
mgottscho | 0:8d34cc2ff388 | 3 | * Author: Mark Gottscho |
mgottscho | 0:8d34cc2ff388 | 4 | * mgottscho@ucla.edu |
mgottscho | 0:8d34cc2ff388 | 5 | */ |
mgottscho | 0:8d34cc2ff388 | 6 | |
mgottscho | 0:8d34cc2ff388 | 7 | #ifndef PERIODICSENSOR_H |
mgottscho | 0:8d34cc2ff388 | 8 | #define PERIODICSENSOR_H |
mgottscho | 0:8d34cc2ff388 | 9 | |
mgottscho | 0:8d34cc2ff388 | 10 | #include "mbed.h" |
mgottscho | 0:8d34cc2ff388 | 11 | |
mgottscho | 0:8d34cc2ff388 | 12 | /** |
mgottscho | 0:8d34cc2ff388 | 13 | * Abstract class from which most sensor classes can be derived. |
mgottscho | 0:8d34cc2ff388 | 14 | * This class implements interrupt-driven periodic sampling |
mgottscho | 0:8d34cc2ff388 | 15 | */ |
mgottscho | 0:8d34cc2ff388 | 16 | class PeriodicSensor { |
mgottscho | 0:8d34cc2ff388 | 17 | public: |
mgottscho | 0:8d34cc2ff388 | 18 | /** |
mgottscho | 0:8d34cc2ff388 | 19 | * Constructs a PeriodicSensor object. Default minimum sampling period is set to 0.05 sec. |
mgottscho | 0:8d34cc2ff388 | 20 | */ |
mgottscho | 0:8d34cc2ff388 | 21 | PeriodicSensor(); |
mgottscho | 0:8d34cc2ff388 | 22 | |
mgottscho | 0:8d34cc2ff388 | 23 | /** |
mgottscho | 0:8d34cc2ff388 | 24 | * Constructs a PeriodicSensor object with a custom minimum sampling period (must be positive). |
mgottscho | 0:8d34cc2ff388 | 25 | */ |
mgottscho | 0:8d34cc2ff388 | 26 | PeriodicSensor(float min_sampling_period); |
mgottscho | 0:8d34cc2ff388 | 27 | |
mgottscho | 0:8d34cc2ff388 | 28 | /** |
mgottscho | 0:8d34cc2ff388 | 29 | * Destroys the object |
mgottscho | 0:8d34cc2ff388 | 30 | */ |
mgottscho | 0:8d34cc2ff388 | 31 | ~PeriodicSensor(); |
mgottscho | 0:8d34cc2ff388 | 32 | |
mgottscho | 0:8d34cc2ff388 | 33 | /** |
mgottscho | 0:8d34cc2ff388 | 34 | * @returns true if there is new data ready to be retrieved. |
mgottscho | 0:8d34cc2ff388 | 35 | */ |
mgottscho | 0:8d34cc2ff388 | 36 | bool isDataReady(); |
mgottscho | 0:8d34cc2ff388 | 37 | |
mgottscho | 0:8d34cc2ff388 | 38 | /** |
mgottscho | 0:8d34cc2ff388 | 39 | * Control the background sampling of the device via interrupts. If disabled, the device can still be sampled using the getter methods directly. |
mgottscho | 0:8d34cc2ff388 | 40 | * @param enable if true, enables background sampling with the given sample period. |
mgottscho | 0:8d34cc2ff388 | 41 | * @param sample_period sampling period in seconds. Must be at least 0.005 sec unless enable is false, in which case it is don't care. |
mgottscho | 0:8d34cc2ff388 | 42 | */ |
mgottscho | 0:8d34cc2ff388 | 43 | void enableBackgroundSampling(bool enable, float sample_period); |
mgottscho | 0:8d34cc2ff388 | 44 | |
mgottscho | 0:8d34cc2ff388 | 45 | /** |
mgottscho | 0:8d34cc2ff388 | 46 | * @returns true if background sampling is enabled. |
mgottscho | 0:8d34cc2ff388 | 47 | */ |
mgottscho | 0:8d34cc2ff388 | 48 | bool isBackgroundSamplingEnabled(); |
mgottscho | 0:8d34cc2ff388 | 49 | |
mgottscho | 0:8d34cc2ff388 | 50 | /** |
mgottscho | 0:8d34cc2ff388 | 51 | * @returns the sample period in seconds, if background sampling is enabled. Else, non-positive return. |
mgottscho | 0:8d34cc2ff388 | 52 | */ |
mgottscho | 0:8d34cc2ff388 | 53 | float getSamplePeriod(); |
mgottscho | 0:8d34cc2ff388 | 54 | |
mgottscho | 0:8d34cc2ff388 | 55 | /** |
mgottscho | 0:8d34cc2ff388 | 56 | * @returns the minimum sample period in seconds |
mgottscho | 0:8d34cc2ff388 | 57 | */ |
mgottscho | 0:8d34cc2ff388 | 58 | float getMinSamplePeriod(); |
mgottscho | 0:8d34cc2ff388 | 59 | |
mgottscho | 0:8d34cc2ff388 | 60 | protected: |
mgottscho | 0:8d34cc2ff388 | 61 | volatile bool __dataReady; |
mgottscho | 0:8d34cc2ff388 | 62 | |
mgottscho | 0:8d34cc2ff388 | 63 | private: |
mgottscho | 0:8d34cc2ff388 | 64 | /** |
mgottscho | 0:8d34cc2ff388 | 65 | * This is the interrupt service routine that is called periodically when enabled. |
mgottscho | 0:8d34cc2ff388 | 66 | */ |
mgottscho | 0:8d34cc2ff388 | 67 | virtual void __sample_data_ISR() = 0; |
mgottscho | 0:8d34cc2ff388 | 68 | |
mgottscho | 0:8d34cc2ff388 | 69 | Ticker __interrupt; |
mgottscho | 0:8d34cc2ff388 | 70 | float __sample_period; |
mgottscho | 0:8d34cc2ff388 | 71 | bool __background_sampling; |
mgottscho | 0:8d34cc2ff388 | 72 | float __min_sample_period; |
mgottscho | 0:8d34cc2ff388 | 73 | }; |
mgottscho | 0:8d34cc2ff388 | 74 | |
mgottscho | 0:8d34cc2ff388 | 75 | #endif |