STM32F7 Ethernet interface for nucleo STM32F767

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AnalogOut.h Source File

AnalogOut.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #ifndef MBED_ANALOGOUT_H
00017 #define MBED_ANALOGOUT_H
00018 
00019 #include "platform/platform.h"
00020 
00021 #if defined (DEVICE_ANALOGOUT) || defined(DOXYGEN_ONLY)
00022 
00023 #include "hal/analogout_api.h"
00024 #include "platform/PlatformMutex.h"
00025 
00026 namespace mbed {
00027 /** \addtogroup drivers */
00028 
00029 /** An analog output, used for setting the voltage on a pin
00030  *
00031  * @note Synchronization level: Thread safe
00032  *
00033  * Example:
00034  * @code
00035  * // Make a sawtooth output
00036  *
00037  * #include "mbed.h"
00038  *
00039  * AnalogOut tri(p18);
00040  * int main() {
00041  *     while(1) {
00042  *         tri = tri + 0.01;
00043  *         wait_us(1);
00044  *         if(tri == 1) {
00045  *             tri = 0;
00046  *         }
00047  *     }
00048  * }
00049  * @endcode
00050  * @ingroup drivers
00051  */
00052 class AnalogOut {
00053 
00054 public:
00055 
00056     /** Create an AnalogOut connected to the specified pin
00057      *
00058      * @param pin AnalogOut pin to connect to
00059      */
00060     AnalogOut(PinName pin) {
00061         analogout_init(&_dac, pin);
00062     }
00063 
00064     /** Set the output voltage, specified as a percentage (float)
00065      *
00066      *  @param value A floating-point value representing the output voltage,
00067      *    specified as a percentage. The value should lie between
00068      *    0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%).
00069      *    Values outside this range will be saturated to 0.0f or 1.0f.
00070      */
00071     void write(float value) {
00072         lock();
00073         analogout_write(&_dac, value);
00074         unlock();
00075     }
00076 
00077     /** Set the output voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
00078      *
00079      *  @param value 16-bit unsigned short representing the output voltage,
00080      *            normalised to a 16-bit value (0x0000 = 0v, 0xFFFF = 3.3v)
00081      */
00082     void write_u16(unsigned short value) {
00083         lock();
00084         analogout_write_u16(&_dac, value);
00085         unlock();
00086     }
00087 
00088     /** Return the current output voltage setting, measured as a percentage (float)
00089      *
00090      *  @returns
00091      *    A floating-point value representing the current voltage being output on the pin,
00092      *    measured as a percentage. The returned value will lie between
00093      *    0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%).
00094      *
00095      *  @note
00096      *    This value may not match exactly the value set by a previous write().
00097      */
00098     float read() {
00099         lock();
00100         float ret = analogout_read(&_dac);
00101         unlock();
00102         return ret;
00103     }
00104 
00105     /** An operator shorthand for write()
00106      * \sa AnalogOut::write()
00107      */
00108     AnalogOut& operator= (float percent) {
00109         // Underlying write call is thread safe
00110         write(percent);
00111         return *this;
00112     }
00113 
00114     /** An operator shorthand for write()
00115      * \sa AnalogOut::write()
00116      */
00117     AnalogOut& operator= (AnalogOut& rhs) {
00118         // Underlying write call is thread safe
00119         write(rhs.read());
00120         return *this;
00121     }
00122 
00123     /** An operator shorthand for read()
00124      * \sa AnalogOut::read()
00125      */
00126     operator float() {
00127         // Underlying read call is thread safe
00128         return read();
00129     }
00130 
00131     virtual ~AnalogOut() {
00132         // Do nothing
00133     }
00134 
00135 protected:
00136 
00137     virtual void lock() {
00138         _mutex.lock();
00139     }
00140 
00141     virtual void unlock() {
00142         _mutex.unlock();
00143     }
00144 
00145     dac_t _dac;
00146     PlatformMutex _mutex;
00147 };
00148 
00149 } // namespace mbed
00150 
00151 #endif
00152 
00153 #endif