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 lock(); 00062 analogin_init(&_adc, pin); 00063 unlock(); 00064 } 00065 00066 /** Read the input voltage, represented as a float in the range [0.0, 1.0] 00067 * 00068 * @returns A floating-point value representing the current input voltage, measured as a percentage 00069 */ 00070 float read() { 00071 lock(); 00072 float ret = analogin_read(&_adc); 00073 unlock(); 00074 return ret; 00075 } 00076 00077 /** Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF] 00078 * 00079 * @returns 00080 * 16-bit unsigned short representing the current input voltage, normalised to a 16-bit value 00081 */ 00082 unsigned short read_u16() { 00083 lock(); 00084 unsigned short ret = analogin_read_u16(&_adc); 00085 unlock(); 00086 return ret; 00087 } 00088 00089 /** An operator shorthand for read() 00090 * 00091 * The float() operator can be used as a shorthand for read() to simplify common code sequences 00092 * 00093 * Example: 00094 * @code 00095 * float x = volume.read(); 00096 * float x = volume; 00097 * 00098 * if(volume.read() > 0.25) { ... } 00099 * if(volume > 0.25) { ... } 00100 * @endcode 00101 */ 00102 operator float() { 00103 // Underlying call is thread safe 00104 return read(); 00105 } 00106 00107 virtual ~AnalogIn() { 00108 // Do nothing 00109 } 00110 00111 protected: 00112 00113 virtual void lock() { 00114 _mutex->lock(); 00115 } 00116 00117 virtual void unlock() { 00118 _mutex->unlock(); 00119 } 00120 00121 analogin_t _adc; 00122 static SingletonPtr<PlatformMutex> _mutex; 00123 }; 00124 00125 } // namespace mbed 00126 00127 #endif 00128 00129 #endif 00130
Generated on Tue Jul 12 2022 12:43:28 by
