Egor Rumjantsev / mbed

Fork of mbed by mbed official

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 
00020 #include "platform.h"
00021 
00022 #if DEVICE_ANALOGIN
00023 
00024 #include "analogin_api.h"
00025 
00026 namespace mbed {
00027 
00028 /** An analog input, used for reading the voltage on a pin
00029  *
00030  * Example:
00031  * @code
00032  * // Print messages when the AnalogIn is greater than 50%
00033  *
00034  * #include "mbed.h"
00035  *
00036  * AnalogIn temperature(p20);
00037  *
00038  * int main() {
00039  *     while(1) {
00040  *         if(temperature > 0.5) {
00041  *             printf("Too hot! (%f)", temperature.read());
00042  *         }
00043  *     }
00044  * }
00045  * @endcode
00046  */
00047 class AnalogIn {
00048 
00049 public:
00050 
00051     /** Create an AnalogIn, connected to the specified pin
00052      *
00053      * @param pin AnalogIn pin to connect to
00054      * @param name (optional) A string to identify the object
00055      */
00056     AnalogIn(PinName pin) {
00057         analogin_init(&_adc, pin);
00058     }
00059 
00060     /** Read the input voltage, represented as a float in the range [0.0, 1.0]
00061      *
00062      * @returns A floating-point value representing the current input voltage, measured as a percentage
00063      */
00064     float read() {
00065         return analogin_read(&_adc);
00066     }
00067 
00068     /** Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
00069      *
00070      * @returns
00071      *   16-bit unsigned short representing the current input voltage, normalised to a 16-bit value
00072      */
00073     unsigned short read_u16() {
00074         return analogin_read_u16(&_adc);
00075     }
00076 
00077 #ifdef MBED_OPERATORS
00078     /** An operator shorthand for read()
00079      *
00080      * The float() operator can be used as a shorthand for read() to simplify common code sequences
00081      *
00082      * Example:
00083      * @code
00084      * float x = volume.read();
00085      * float x = volume;
00086      *
00087      * if(volume.read() > 0.25) { ... }
00088      * if(volume > 0.25) { ... }
00089      * @endcode
00090      */
00091     operator float() {
00092         return read();
00093     }
00094 #endif
00095 
00096 protected:
00097     analogin_t _adc;
00098 };
00099 
00100 } // namespace mbed
00101 
00102 #endif
00103 
00104 #endif