The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Revision:
122:f9eeca106725
Parent:
65:5798e58a58b1
Child:
123:b0220dba8be7
--- a/AnalogOut.h	Wed May 25 16:44:06 2016 +0100
+++ b/AnalogOut.h	Thu Jul 07 14:34:11 2016 +0100
@@ -26,6 +26,8 @@
 
 /** An analog output, used for setting the voltage on a pin
  *
+ * @Note Synchronization level: Thread safe
+ *
  * Example:
  * @code
  * // Make a sawtooth output
@@ -64,7 +66,9 @@
      *    Values outside this range will be saturated to 0.0f or 1.0f.
      */
     void write(float value) {
+        lock();
         analogout_write(&_dac, value);
+        unlock();
     }
 
     /** Set the output voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
@@ -73,7 +77,9 @@
      *            normalised to a 16-bit value (0x0000 = 0v, 0xFFFF = 3.3v)
      */
     void write_u16(unsigned short value) {
+        lock();
         analogout_write_u16(&_dac, value);
+        unlock();
     }
 
     /** Return the current output voltage setting, measured as a percentage (float)
@@ -87,18 +93,23 @@
      *    This value may not match exactly the value set by a previous write().
      */
     float read() {
-        return analogout_read(&_dac);
+        lock();
+        float ret = analogout_read(&_dac);
+        unlock();
+        return ret;
     }
 
 #ifdef MBED_OPERATORS
     /** An operator shorthand for write()
      */
     AnalogOut& operator= (float percent) {
+        // Underlying write call is thread safe
         write(percent);
         return *this;
     }
 
     AnalogOut& operator= (AnalogOut& rhs) {
+        // Underlying write call is thread safe
         write(rhs.read());
         return *this;
     }
@@ -106,12 +117,27 @@
     /** An operator shorthand for read()
      */
     operator float() {
+        // Underlying read call is thread safe
         return read();
     }
 #endif
 
+    virtual ~AnalogOut() {
+        // Do nothing
+    }
+
 protected:
+
+    virtual void lock() {
+        _mutex.lock();
+    }
+
+    virtual void unlock() {
+        _mutex.unlock();
+    }
+
     dac_t _dac;
+    PlatformMutex _mutex;
 };
 
 } // namespace mbed