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