Mistake on this page?
Report an issue in GitHub or email us
AnalogIn.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 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 /** \addtogroup drivers */
30 
31 /** An analog input, used for reading the voltage on a pin
32  *
33  * @note Synchronization level: Thread safe
34  *
35  * Example:
36  * @code
37  * // Print messages when the AnalogIn is greater than 50%
38  *
39  * #include "mbed.h"
40  *
41  * AnalogIn temperature(p20);
42  *
43  * int main() {
44  * while(1) {
45  * if(temperature > 0.5) {
46  * printf("Too hot! (%f)", temperature.read());
47  * }
48  * }
49  * }
50  * @endcode
51  * @ingroup drivers
52  */
53 class AnalogIn {
54 
55 public:
56 
57  /** Create an AnalogIn, connected to the specified pin
58  *
59  * @param pin AnalogIn pin to connect to
60  */
61  AnalogIn(PinName pin)
62  {
63  lock();
64  analogin_init(&_adc, pin);
65  unlock();
66  }
67 
68  /** Read the input voltage, represented as a float in the range [0.0, 1.0]
69  *
70  * @returns A floating-point value representing the current input voltage, measured as a percentage
71  */
72  float read()
73  {
74  lock();
75  float ret = analogin_read(&_adc);
76  unlock();
77  return ret;
78  }
79 
80  /** Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
81  *
82  * @returns
83  * 16-bit unsigned short representing the current input voltage, normalized to a 16-bit value
84  */
85  unsigned short read_u16()
86  {
87  lock();
88  unsigned short ret = analogin_read_u16(&_adc);
89  unlock();
90  return ret;
91  }
92 
93  /** An operator shorthand for read()
94  *
95  * The float() operator can be used as a shorthand for read() to simplify common code sequences
96  *
97  * Example:
98  * @code
99  * float x = volume.read();
100  * float x = volume;
101  *
102  * if(volume.read() > 0.25) { ... }
103  * if(volume > 0.25) { ... }
104  * @endcode
105  */
106  operator float()
107  {
108  // Underlying call is thread safe
109  return read();
110  }
111 
112  virtual ~AnalogIn()
113  {
114  // Do nothing
115  }
116 
117 protected:
118 #if !defined(DOXYGEN_ONLY)
119  virtual void lock()
120  {
121  _mutex->lock();
122  }
123 
124  virtual void unlock()
125  {
126  _mutex->unlock();
127  }
128 
129  analogin_t _adc;
130  static SingletonPtr<PlatformMutex> _mutex;
131 #endif //!defined(DOXYGEN_ONLY)
132 };
133 
134 } // namespace mbed
135 
136 #endif
137 
138 #endif
139 
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:53
float read()
Read the input voltage, represented as a float in the range [0.0, 1.0].
Definition: AnalogIn.h:72
unsigned short read_u16()
Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF].
Definition: AnalogIn.h:85
uint16_t analogin_read_u16(analogin_t *obj)
Read the value from analogin pin, represented as an unsigned 16bit value.
AnalogIn(PinName pin)
Create an AnalogIn, connected to the specified pin.
Definition: AnalogIn.h:61
float analogin_read(analogin_t *obj)
Read the input voltage, represented as a float in the range [0.0, 1.0].
Definition: AnalogIn.h:28
void analogin_init(analogin_t *obj, PinName pin)
Initialize the analogin peripheral.
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.