Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AnalogIn.h Source File

AnalogIn.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2019 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 /** \defgroup mbed-os-public Public API */
00030 
00031 /** \defgroup drivers-public-api Drivers
00032  * \ingroup mbed-os-public
00033  */
00034 
00035 /** \defgroup drivers-public-api-gpio GPIO
00036  * \ingroup drivers-public-api
00037  */
00038 
00039 /**
00040  * \defgroup drivers_AnalogIn AnalogIn class
00041  * \ingroup drivers-public-api-gpio
00042  * @{
00043  */
00044 
00045 /** An analog input, used for reading the voltage on a pin
00046  *
00047  * @note Synchronization level: Thread safe
00048  *
00049  * Example:
00050  * @code
00051  * // Print messages when the AnalogIn is greater than 50%
00052  *
00053  * #include "mbed.h"
00054  *
00055  * AnalogIn temperature(p20);
00056  *
00057  * int main() {
00058  *     while(1) {
00059  *         if(temperature > 0.5) {
00060  *             printf("Too hot! (%f)", temperature.read());
00061  *         }
00062  *     }
00063  * }
00064  * @endcode
00065  */
00066 class AnalogIn {
00067 
00068 public:
00069 
00070     /** Create an AnalogIn, connected to the specified pin
00071      *
00072      * @param pinmap reference to structure which holds static pinmap.
00073      */
00074     AnalogIn(const PinMap &pinmap);
00075     AnalogIn(const PinMap &&) = delete; // prevent passing of temporary objects
00076 
00077     /** Create an AnalogIn, connected to the specified pin
00078      *
00079      * @param pin AnalogIn pin to connect to
00080      */
00081     AnalogIn(PinName pin);
00082 
00083     /** Read the input voltage, represented as a float in the range [0.0, 1.0]
00084      *
00085      * @returns A floating-point value representing the current input voltage, measured as a percentage
00086      */
00087     float read();
00088 
00089     /** Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
00090      *
00091      * @returns
00092      *   16-bit unsigned short representing the current input voltage, normalized to a 16-bit value
00093      */
00094     unsigned short read_u16();
00095 
00096     /** An operator shorthand for read()
00097      *
00098      * The float() operator can be used as a shorthand for read() to simplify common code sequences
00099      *
00100      * Example:
00101      * @code
00102      * float x = volume.read();
00103      * float x = volume;
00104      *
00105      * if(volume.read() > 0.25) { ... }
00106      * if(volume > 0.25) { ... }
00107      * @endcode
00108      */
00109     operator float()
00110     {
00111         // Underlying call is thread safe
00112         return read();
00113     }
00114 
00115     virtual ~AnalogIn()
00116     {
00117         // Do nothing
00118     }
00119 
00120 protected:
00121 #if !defined(DOXYGEN_ONLY)
00122     virtual void lock()
00123     {
00124         _mutex->lock();
00125     }
00126 
00127     virtual void unlock()
00128     {
00129         _mutex->unlock();
00130     }
00131 
00132     analogin_t _adc;
00133     static SingletonPtr<PlatformMutex>  _mutex;
00134 #endif //!defined(DOXYGEN_ONLY)
00135 
00136 };
00137 
00138 /** @}*/
00139 
00140 } // namespace mbed
00141 
00142 #endif
00143 
00144 #endif