Implemented first Hangar-Service

Dependencies:   CalibrateMagneto QuaternionMath

Fork of SML2 by TobyRich GmbH

Revision:
6:c12cea26842d
Child:
7:604a8369b801
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sensor.h	Thu Feb 12 19:00:28 2015 +0000
@@ -0,0 +1,49 @@
+#ifndef _H_SENSOR_H
+#define _H_SENSOR_H
+
+/// Base class for I2C-connected sensors. Defines functionality supported by all sensors.
+class Sensor {
+public:
+    /// Base class for sensor data frame. Sensors may derive from this and add their own data fields.
+    class Data {
+    public:
+        int16_t x; ///< x-axis sensor data
+        int16_t y; ///< y-axis sensor data
+        int16_t z; ///< z-axis sensor data
+        int16_t reserved; ///< (reserved, do not use)
+        float timestamp; ///< Timestamp in seconds.
+    };
+    
+    /// Defines protocol used to send data back to owner. Derive from this class and use Sensor.setDelegate() to receive sensor updates.
+    class Delegate {
+    public:
+        /// A new sensor data frame, might be called several (hundred) times a second.
+        virtual void sensorUpdate(Sensor* source, Data frame) = 0;
+    };
+    
+    virtual void setDelegate(Delegate &d) {
+        _delegate = &d;
+    }
+    
+    /// Power on a sensor and make it ready for use.
+    /// @return true if power-up was successful, false otherwise.
+    virtual bool powerOn() = 0;
+    
+    /// Power off a sensor. This will generally only put the sensor into deep sleep.
+    virtual void powerOff() = 0;
+    
+    virtual void start() = 0; ///< Start continuous data capture. If a delegate is set, its sensorUpdate() method will be called for each data frame.
+    virtual void stop() = 0; ///< Stop capturing data.
+        
+    virtual Data read() = 0; ///< Read and return instantaneous (current) sensor data. No need to start the sensor.
+    
+protected:
+    /// Derived classes should use this method to pass sensor data to their owner. Delegate existence check is automatically done before dispatching.
+    void sendData(Data frame) {
+        if (_delegate)
+            _delegate->sensorUpdate(this, frame);
+    }
+    Delegate * _delegate;
+};
+
+#endif//_H_SENSOR_H
\ No newline at end of file