Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of TUKS-COURSE-TIMER by
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 DEVICE_ANALOGOUT 00022 00023 #include "hal/analogout_api.h" 00024 #include "platform/PlatformMutex.h" 00025 00026 namespace mbed { 00027 /** \addtogroup drivers */ 00028 /** @{*/ 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 */ 00052 class AnalogOut { 00053 00054 public: 00055 00056 /** Create an AnalogOut connected to the specified pin 00057 * 00058 * @param AnalogOut pin to connect to (18) 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 */ 00107 AnalogOut& operator= (float percent) { 00108 // Underlying write call is thread safe 00109 write(percent); 00110 return *this; 00111 } 00112 00113 AnalogOut& operator= (AnalogOut& rhs) { 00114 // Underlying write call is thread safe 00115 write(rhs.read()); 00116 return *this; 00117 } 00118 00119 /** An operator shorthand for read() 00120 */ 00121 operator float() { 00122 // Underlying read call is thread safe 00123 return read(); 00124 } 00125 00126 virtual ~AnalogOut() { 00127 // Do nothing 00128 } 00129 00130 protected: 00131 00132 virtual void lock() { 00133 _mutex.lock(); 00134 } 00135 00136 virtual void unlock() { 00137 _mutex.unlock(); 00138 } 00139 00140 dac_t _dac; 00141 PlatformMutex _mutex; 00142 }; 00143 00144 } // namespace mbed 00145 00146 #endif 00147 00148 #endif 00149 00150 /** @}*/
Generated on Tue Jul 12 2022 17:38:47 by
