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.
AnalogIn.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_ANALOGIN_H 00017 #define MBED_ANALOGIN_H 00018 00019 #include "platform/platform.h" 00020 00021 #if defined (DEVICE_ANALOGIN) || defined(DOXYGEN_ONLY) 00022 00023 #include "hal/analogin_api.h" 00024 #include "platform/SingletonPtr.h" 00025 #include "platform/PlatformMutex.h" 00026 00027 namespace mbed { 00028 /** \addtogroup drivers */ 00029 00030 /** An analog input, used for reading the voltage on a pin 00031 * 00032 * @note Synchronization level: Thread safe 00033 * 00034 * Example: 00035 * @code 00036 * // Print messages when the AnalogIn is greater than 50% 00037 * 00038 * #include "mbed.h" 00039 * 00040 * AnalogIn temperature(p20); 00041 * 00042 * int main() { 00043 * while(1) { 00044 * if(temperature > 0.5) { 00045 * printf("Too hot! (%f)", temperature.read()); 00046 * } 00047 * } 00048 * } 00049 * @endcode 00050 * @ingroup drivers 00051 */ 00052 class AnalogIn { 00053 00054 public: 00055 00056 /** Create an AnalogIn, connected to the specified pin 00057 * 00058 * @param pin AnalogIn pin to connect to 00059 */ 00060 AnalogIn(PinName pin) 00061 { 00062 lock(); 00063 analogin_init(&_adc, pin); 00064 unlock(); 00065 } 00066 00067 /** Read the input voltage, represented as a float in the range [0.0, 1.0] 00068 * 00069 * @returns A floating-point value representing the current input voltage, measured as a percentage 00070 */ 00071 float read() 00072 { 00073 lock(); 00074 float ret = analogin_read(&_adc); 00075 unlock(); 00076 return ret; 00077 } 00078 00079 /** Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF] 00080 * 00081 * @returns 00082 * 16-bit unsigned short representing the current input voltage, normalised to a 16-bit value 00083 */ 00084 unsigned short read_u16() 00085 { 00086 lock(); 00087 unsigned short ret = analogin_read_u16(&_adc); 00088 unlock(); 00089 return ret; 00090 } 00091 00092 /** An operator shorthand for read() 00093 * 00094 * The float() operator can be used as a shorthand for read() to simplify common code sequences 00095 * 00096 * Example: 00097 * @code 00098 * float x = volume.read(); 00099 * float x = volume; 00100 * 00101 * if(volume.read() > 0.25) { ... } 00102 * if(volume > 0.25) { ... } 00103 * @endcode 00104 */ 00105 operator float() 00106 { 00107 // Underlying call is thread safe 00108 return read(); 00109 } 00110 00111 virtual ~AnalogIn() 00112 { 00113 // Do nothing 00114 } 00115 00116 protected: 00117 00118 virtual void lock() 00119 { 00120 _mutex->lock(); 00121 } 00122 00123 virtual void unlock() 00124 { 00125 _mutex->unlock(); 00126 } 00127 00128 analogin_t _adc; 00129 static SingletonPtr<PlatformMutex> _mutex; 00130 }; 00131 00132 } // namespace mbed 00133 00134 #endif 00135 00136 #endif 00137
Generated on Tue Aug 9 2022 00:37:00 by
