1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef MBED_ANALOGIN_H
17 #define MBED_ANALOGIN_H
23 #include "analogin_api.h"
27 /** An analog input, used for reading the voltage on a pin
31 * // Print messages when the AnalogIn is greater than 50%
35 * AnalogIn temperature(p20);
39 * if(temperature > 0.5) {
40 * printf("Too hot! (%f)", temperature.read());
50 /** Create an AnalogIn, connected to the specified pin
52 * @param pin AnalogIn pin to connect to
53 * @param name (optional) A string to identify the object
55 AnalogIn(PinName pin) {
56 analogin_init(&_adc, pin);
59 /** Read the input voltage, represented as a float in the range [0.0, 1.0]
61 * @returns A floating-point value representing the current input voltage, measured as a percentage
64 return analogin_read(&_adc);
67 /** Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
70 * 16-bit unsigned short representing the current input voltage, normalised to a 16-bit value
72 unsigned short read_u16() {
73 return analogin_read_u16(&_adc);
77 /** An operator shorthand for read()
79 * The float() operator can be used as a shorthand for read() to simplify common code sequences
83 * float x = volume.read();
86 * if(volume.read() > 0.25) { ... }
87 * if(volume > 0.25) { ... }