Mistake on this page?
Report an issue in GitHub or email us
AnalogIn.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2019 ARM Limited
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #ifndef MBED_ANALOGIN_H
18 #define MBED_ANALOGIN_H
19 
20 #include "platform/platform.h"
21 
22 #if DEVICE_ANALOGIN || defined(DOXYGEN_ONLY)
23 
24 #include "hal/analogin_api.h"
25 #include "platform/SingletonPtr.h"
26 #include "platform/PlatformMutex.h"
27 
28 namespace mbed {
29 /** \defgroup mbed-os-public Public API */
30 
31 /** \defgroup drivers-public-api Drivers
32  * \ingroup mbed-os-public
33  */
34 
35 /** \defgroup drivers-public-api-gpio GPIO
36  * \ingroup drivers-public-api
37  */
38 
39 /**
40  * \defgroup drivers_AnalogIn AnalogIn class
41  * \ingroup drivers-public-api-gpio
42  * @{
43  */
44 
45 /** An analog input, used for reading the voltage on a pin
46  *
47  * @note Synchronization level: Thread safe
48  *
49  * Example:
50  * @code
51  * // Print messages when the AnalogIn is greater than 50%
52  *
53  * #include "mbed.h"
54  *
55  * AnalogIn temperature(p20);
56  *
57  * int main() {
58  * while(1) {
59  * if(temperature > 0.5) {
60  * printf("Too hot! (%f)", temperature.read());
61  * }
62  * }
63  * }
64  * @endcode
65  */
66 class AnalogIn {
67 
68 public:
69 
70  /** Create an AnalogIn, connected to the specified pin
71  *
72  * @param pin AnalogIn pin to connect to
73  */
74  AnalogIn(PinName pin);
75 
76  /** Read the input voltage, represented as a float in the range [0.0, 1.0]
77  *
78  * @returns A floating-point value representing the current input voltage, measured as a percentage
79  */
80  float read();
81 
82  /** Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
83  *
84  * @returns
85  * 16-bit unsigned short representing the current input voltage, normalized to a 16-bit value
86  */
87  unsigned short read_u16();
88 
89  /** An operator shorthand for read()
90  *
91  * The float() operator can be used as a shorthand for read() to simplify common code sequences
92  *
93  * Example:
94  * @code
95  * float x = volume.read();
96  * float x = volume;
97  *
98  * if(volume.read() > 0.25) { ... }
99  * if(volume > 0.25) { ... }
100  * @endcode
101  */
102  operator float()
103  {
104  // Underlying call is thread safe
105  return read();
106  }
107 
108  virtual ~AnalogIn()
109  {
110  // Do nothing
111  }
112 
113 protected:
114 #if !defined(DOXYGEN_ONLY)
115  virtual void lock()
116  {
117  _mutex->lock();
118  }
119 
120  virtual void unlock()
121  {
122  _mutex->unlock();
123  }
124 
125  analogin_t _adc;
126  static SingletonPtr<PlatformMutex> _mutex;
127 #endif //!defined(DOXYGEN_ONLY)
128 
129 };
130 
131 /** @}*/
132 
133 } // namespace mbed
134 
135 #endif
136 
137 #endif
138 
struct analogin_s analogin_t
Analogin hal structure.
Definition: analogin_api.h:34
An analog input, used for reading the voltage on a pin.
Definition: AnalogIn.h:66
float read()
Read the input voltage, represented as a float in the range [0.0, 1.0].
unsigned short read_u16()
Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF].
AnalogIn(PinName pin)
Create an AnalogIn, connected to the specified pin.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.