A compilation of some hardware sensors and their shared programming interfaces.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PeriodicSensor.h Source File

PeriodicSensor.h

00001 /* PeriodicSensor.h
00002  * Tested with mbed board: FRDM-KL46Z
00003  * Author: Mark Gottscho
00004  * mgottscho@ucla.edu
00005  */
00006 
00007 #ifndef PERIODICSENSOR_H
00008 #define PERIODICSENSOR_H
00009 
00010 #include "mbed.h"
00011 
00012 /**
00013  * Abstract class from which most sensor classes can be derived.
00014  * This class implements interrupt-driven periodic sampling
00015  */
00016 class PeriodicSensor {
00017     public:
00018         /**
00019          * Constructs a PeriodicSensor object. Default minimum sampling period is set to 0.05 sec.
00020          */
00021         PeriodicSensor();
00022         
00023         /**
00024          * Constructs a PeriodicSensor object with a custom minimum sampling period (must be positive).
00025          */
00026         PeriodicSensor(float min_sampling_period);
00027          
00028         /**
00029          * Destroys the object
00030          */
00031         ~PeriodicSensor();
00032         
00033         /**
00034          * @returns true if there is new data ready to be retrieved.
00035          */
00036         bool isDataReady ();
00037         
00038         /**
00039          * Control the background sampling of the device via interrupts. If disabled, the device can still be sampled using the getter methods directly.
00040          * @param enable if true, enables background sampling with the given sample period.
00041          * @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.
00042          */
00043         void enableBackgroundSampling(bool enable, float sample_period);
00044         
00045         /**
00046          * @returns true if background sampling is enabled.
00047          */
00048         bool isBackgroundSamplingEnabled ();
00049         
00050         /**
00051          * @returns the sample period in seconds, if background sampling is enabled. Else, non-positive return.
00052          */
00053         float getSamplePeriod ();
00054         
00055         /**
00056          * @returns the minimum sample period in seconds
00057          */
00058         float getMinSamplePeriod ();
00059         
00060     protected:
00061         volatile bool __dataReady;
00062         
00063     private:
00064         /**
00065          * This is the interrupt service routine that is called periodically when enabled.
00066          */
00067         virtual void __sample_data_ISR() = 0;
00068         
00069         Ticker __interrupt;
00070         float __sample_period;
00071         bool __background_sampling;
00072         float __min_sample_period;
00073 };
00074 
00075 #endif