mbed library sources. Supersedes mbed-src.
Dependents: Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more
AnalogOut.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 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 /** \addtogroup drivers */ 00029 00030 /** An analog output, used for setting the voltage on a pin 00031 * 00032 * @note Synchronization level: Thread safe 00033 * 00034 * Example: 00035 * @code 00036 * // Make a sawtooth output 00037 * 00038 * #include "mbed.h" 00039 * 00040 * AnalogOut tri(p18); 00041 * int main() { 00042 * while(1) { 00043 * tri = tri + 0.01; 00044 * wait_us(1); 00045 * if(tri == 1) { 00046 * tri = 0; 00047 * } 00048 * } 00049 * } 00050 * @endcode 00051 * @ingroup drivers 00052 */ 00053 class AnalogOut { 00054 00055 public: 00056 00057 /** Create an AnalogOut connected to the specified pin 00058 * 00059 * @param pin AnalogOut pin to connect to 00060 */ 00061 AnalogOut(PinName pin) 00062 { 00063 analogout_init(&_dac, pin); 00064 } 00065 00066 /** Set the output voltage, specified as a percentage (float) 00067 * 00068 * @param value A floating-point value representing the output voltage, 00069 * specified as a percentage. The value should lie between 00070 * 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%). 00071 * Values outside this range will be saturated to 0.0f or 1.0f. 00072 */ 00073 void write(float value) 00074 { 00075 lock(); 00076 analogout_write(&_dac, value); 00077 unlock(); 00078 } 00079 00080 /** Set the output voltage, represented as an unsigned short in the range [0x0, 0xFFFF] 00081 * 00082 * @param value 16-bit unsigned short representing the output voltage, 00083 * normalized to a 16-bit value (0x0000 = 0v, 0xFFFF = 3.3v) 00084 */ 00085 void write_u16(unsigned short value) 00086 { 00087 lock(); 00088 analogout_write_u16(&_dac, value); 00089 unlock(); 00090 } 00091 00092 /** Return the current output voltage setting, measured as a percentage (float) 00093 * 00094 * @returns 00095 * A floating-point value representing the current voltage being output on the pin, 00096 * measured as a percentage. The returned value will lie between 00097 * 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%). 00098 * 00099 * @note 00100 * This value may not match exactly the value set by a previous write(). 00101 */ 00102 float read() 00103 { 00104 lock(); 00105 float ret = analogout_read(&_dac); 00106 unlock(); 00107 return ret; 00108 } 00109 00110 /** An operator shorthand for write() 00111 * \sa AnalogOut::write() 00112 */ 00113 AnalogOut &operator= (float percent) 00114 { 00115 // Underlying write call is thread safe 00116 write(percent); 00117 return *this; 00118 } 00119 00120 /** An operator shorthand for write() 00121 * \sa AnalogOut::write() 00122 */ 00123 AnalogOut &operator= (AnalogOut &rhs) 00124 { 00125 // Underlying write call is thread safe 00126 write(rhs.read()); 00127 return *this; 00128 } 00129 00130 /** An operator shorthand for read() 00131 * \sa AnalogOut::read() 00132 */ 00133 operator float() 00134 { 00135 // Underlying read call is thread safe 00136 return read(); 00137 } 00138 00139 virtual ~AnalogOut() 00140 { 00141 // Do nothing 00142 } 00143 00144 protected: 00145 #if !defined(DOXYGEN_ONLY) 00146 virtual void lock() 00147 { 00148 _mutex.lock(); 00149 } 00150 00151 virtual void unlock() 00152 { 00153 _mutex.unlock(); 00154 } 00155 00156 dac_t _dac; 00157 PlatformMutex _mutex; 00158 #endif //!defined(DOXYGEN_ONLY) 00159 }; 00160 00161 } // namespace mbed 00162 00163 #endif 00164 00165 #endif
Generated on Tue Jul 12 2022 20:41:14 by 1.7.2