lib
Fork of mbed-dev by
Embed:
(wiki syntax)
Show/hide line numbers
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 DEVICE_ANALOGIN 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 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 */ 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 * @param name (optional) A string to identify the object 00060 */ 00061 AnalogIn(PinName pin) { 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 lock(); 00073 float ret = analogin_read(&_adc); 00074 unlock(); 00075 return ret; 00076 } 00077 00078 /** Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF] 00079 * 00080 * @returns 00081 * 16-bit unsigned short representing the current input voltage, normalised to a 16-bit value 00082 */ 00083 unsigned short read_u16() { 00084 lock(); 00085 unsigned short ret = analogin_read_u16(&_adc); 00086 unlock(); 00087 return ret; 00088 } 00089 00090 /** An operator shorthand for read() 00091 * 00092 * The float() operator can be used as a shorthand for read() to simplify common code sequences 00093 * 00094 * Example: 00095 * @code 00096 * float x = volume.read(); 00097 * float x = volume; 00098 * 00099 * if(volume.read() > 0.25) { ... } 00100 * if(volume > 0.25) { ... } 00101 * @endcode 00102 */ 00103 operator float() { 00104 // Underlying call is thread safe 00105 return read(); 00106 } 00107 00108 virtual ~AnalogIn() { 00109 // Do nothing 00110 } 00111 00112 protected: 00113 00114 virtual void lock() { 00115 _mutex->lock(); 00116 } 00117 00118 virtual void unlock() { 00119 _mutex->unlock(); 00120 } 00121 00122 analogin_t _adc; 00123 static SingletonPtr<PlatformMutex> _mutex; 00124 }; 00125 00126 } // namespace mbed 00127 00128 #endif 00129 00130 #endif 00131 00132 /** @}*/
Generated on Tue Jul 12 2022 20:00:13 by 1.7.2