mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AnalogIn.h Source File

AnalogIn.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #ifndef MBED_ANALOGIN_H
00017 #define MBED_ANALOGIN_H
00018 
00019 #include "platform.h"
00020 
00021 #if DEVICE_ANALOGIN
00022 
00023 #include "analogin_api.h"
00024 
00025 namespace mbed {
00026 
00027 /** An analog input, used for reading the voltage on a pin
00028  *
00029  * Example:
00030  * @code
00031  * // Print messages when the AnalogIn is greater than 50%
00032  *
00033  * #include "mbed.h"
00034  *
00035  * AnalogIn temperature(p20);
00036  *
00037  * int main() {
00038  *     while(1) {
00039  *         if(temperature > 0.5) {
00040  *             printf("Too hot! (%f)", temperature.read());
00041  *         }
00042  *     }
00043  * }
00044  * @endcode
00045  */
00046 class AnalogIn {
00047 
00048 public:
00049 
00050     /** Create an AnalogIn, connected to the specified pin
00051      *
00052      * @param pin AnalogIn pin to connect to
00053      * @param name (optional) A string to identify the object
00054      */
00055     AnalogIn(PinName pin) {
00056         analogin_init(&_adc, pin);
00057     }
00058 
00059     /** Read the input voltage, represented as a float in the range [0.0, 1.0]
00060      *
00061      * @returns A floating-point value representing the current input voltage, measured as a percentage
00062      */
00063     float read() {
00064         return analogin_read(&_adc);
00065     }
00066 
00067     /** Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
00068      *
00069      * @returns
00070      *   16-bit unsigned short representing the current input voltage, normalised to a 16-bit value
00071      */
00072     unsigned short read_u16() {
00073         return analogin_read_u16(&_adc);
00074     }
00075 
00076 #ifdef MBED_OPERATORS
00077     /** An operator shorthand for read()
00078      *
00079      * The float() operator can be used as a shorthand for read() to simplify common code sequences
00080      *
00081      * Example:
00082      * @code
00083      * float x = volume.read();
00084      * float x = volume;
00085      *
00086      * if(volume.read() > 0.25) { ... }
00087      * if(volume > 0.25) { ... }
00088      * @endcode
00089      */
00090     operator float() {
00091         return read();
00092     }
00093 #endif
00094 
00095 protected:
00096     analogin_t _adc;
00097 };
00098 
00099 } // namespace mbed
00100 
00101 #endif
00102 
00103 #endif