Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

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