]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/api/AnalogIn.h
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / api / AnalogIn.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 ARM Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef MBED_ANALOGIN_H
17 #define MBED_ANALOGIN_H
18
19 #include "platform.h"
20
21 #if DEVICE_ANALOGIN
22
23 #include "analogin_api.h"
24
25 namespace mbed {
26
27 /** An analog input, used for reading the voltage on a pin
28  *
29  * Example:
30  * @code
31  * // Print messages when the AnalogIn is greater than 50%
32  *
33  * #include "mbed.h"
34  *
35  * AnalogIn temperature(p20);
36  *
37  * int main() {
38  *     while(1) {
39  *         if(temperature > 0.5) {
40  *             printf("Too hot! (%f)", temperature.read());
41  *         }
42  *     }
43  * }
44  * @endcode
45  */
46 class AnalogIn {
47
48 public:
49
50     /** Create an AnalogIn, connected to the specified pin
51      *
52      * @param pin AnalogIn pin to connect to
53      * @param name (optional) A string to identify the object
54      */
55     AnalogIn(PinName pin) {
56         analogin_init(&_adc, pin);
57     }
58
59     /** Read the input voltage, represented as a float in the range [0.0, 1.0]
60      *
61      * @returns A floating-point value representing the current input voltage, measured as a percentage
62      */
63     float read() {
64         return analogin_read(&_adc);
65     }
66
67     /** Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
68      *
69      * @returns
70      *   16-bit unsigned short representing the current input voltage, normalised to a 16-bit value
71      */
72     unsigned short read_u16() {
73         return analogin_read_u16(&_adc);
74     }
75
76 #ifdef MBED_OPERATORS
77     /** An operator shorthand for read()
78      *
79      * The float() operator can be used as a shorthand for read() to simplify common code sequences
80      *
81      * Example:
82      * @code
83      * float x = volume.read();
84      * float x = volume;
85      *
86      * if(volume.read() > 0.25) { ... }
87      * if(volume > 0.25) { ... }
88      * @endcode
89      */
90     operator float() {
91         return read();
92     }
93 #endif
94
95 protected:
96     analogin_t _adc;
97 };
98
99 } // namespace mbed
100
101 #endif
102
103 #endif