takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

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     {
00062         analogout_init(&_dac, pin);
00063     }
00064 
00065     /** Set the output voltage, specified as a percentage (float)
00066      *
00067      *  @param value A floating-point value representing the output voltage,
00068      *    specified as a percentage. The value should lie between
00069      *    0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%).
00070      *    Values outside this range will be saturated to 0.0f or 1.0f.
00071      */
00072     void write(float value)
00073     {
00074         lock();
00075         analogout_write(&_dac, value);
00076         unlock();
00077     }
00078 
00079     /** Set the output voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
00080      *
00081      *  @param value 16-bit unsigned short representing the output voltage,
00082      *            normalised to a 16-bit value (0x0000 = 0v, 0xFFFF = 3.3v)
00083      */
00084     void write_u16(unsigned short value)
00085     {
00086         lock();
00087         analogout_write_u16(&_dac, value);
00088         unlock();
00089     }
00090 
00091     /** Return the current output voltage setting, measured as a percentage (float)
00092      *
00093      *  @returns
00094      *    A floating-point value representing the current voltage being output on the pin,
00095      *    measured as a percentage. The returned value will lie between
00096      *    0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%).
00097      *
00098      *  @note
00099      *    This value may not match exactly the value set by a previous write().
00100      */
00101     float read()
00102     {
00103         lock();
00104         float ret = analogout_read(&_dac);
00105         unlock();
00106         return ret;
00107     }
00108 
00109     /** An operator shorthand for write()
00110      * \sa AnalogOut::write()
00111      */
00112     AnalogOut &operator= (float percent)
00113     {
00114         // Underlying write call is thread safe
00115         write(percent);
00116         return *this;
00117     }
00118 
00119     /** An operator shorthand for write()
00120      * \sa AnalogOut::write()
00121      */
00122     AnalogOut &operator= (AnalogOut &rhs)
00123     {
00124         // Underlying write call is thread safe
00125         write(rhs.read());
00126         return *this;
00127     }
00128 
00129     /** An operator shorthand for read()
00130      * \sa AnalogOut::read()
00131      */
00132     operator float()
00133     {
00134         // Underlying read call is thread safe
00135         return read();
00136     }
00137 
00138     virtual ~AnalogOut()
00139     {
00140         // Do nothing
00141     }
00142 
00143 protected:
00144 
00145     virtual void lock()
00146     {
00147         _mutex.lock();
00148     }
00149 
00150     virtual void unlock()
00151     {
00152         _mutex.unlock();
00153     }
00154 
00155     dac_t _dac;
00156     PlatformMutex _mutex;
00157 };
00158 
00159 } // namespace mbed
00160 
00161 #endif
00162 
00163 #endif