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 pinmap reference to structure which holds static pinmap.
73  */
74  AnalogIn(const PinMap &pinmap);
75  AnalogIn(const PinMap &&) = delete; // prevent passing of temporary objects
76 
77  /** Create an AnalogIn, connected to the specified pin
78  *
79  * @param pin AnalogIn pin to connect to
80  */
81  AnalogIn(PinName pin);
82 
83  /** Read the input voltage, represented as a float in the range [0.0, 1.0]
84  *
85  * @returns A floating-point value representing the current input voltage, measured as a percentage
86  */
87  float read();
88 
89  /** Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
90  *
91  * @returns
92  * 16-bit unsigned short representing the current input voltage, normalized to a 16-bit value
93  */
94  unsigned short read_u16();
95 
96  /** An operator shorthand for read()
97  *
98  * The float() operator can be used as a shorthand for read() to simplify common code sequences
99  *
100  * Example:
101  * @code
102  * float x = volume.read();
103  * float x = volume;
104  *
105  * if(volume.read() > 0.25) { ... }
106  * if(volume > 0.25) { ... }
107  * @endcode
108  */
109  operator float()
110  {
111  // Underlying call is thread safe
112  return read();
113  }
114 
115  virtual ~AnalogIn()
116  {
117  // Do nothing
118  }
119 
120 protected:
121 #if !defined(DOXYGEN_ONLY)
122  virtual void lock()
123  {
124  _mutex->lock();
125  }
126 
127  virtual void unlock()
128  {
129  _mutex->unlock();
130  }
131 
132  analogin_t _adc;
133  static SingletonPtr<PlatformMutex> _mutex;
134 #endif //!defined(DOXYGEN_ONLY)
135 
136 };
137 
138 /** @}*/
139 
140 } // namespace mbed
141 
142 #endif
143 
144 #endif
struct analogin_s analogin_t
Analogin hal structure.
Definition: analogin_api.h:34
AnalogIn(const PinMap &pinmap)
Create an AnalogIn, connected to the specified pin.
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].
Definition: pinmap.h:31
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.