Test code for Grove Node BLE

Dependencies:   BLE_API nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

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     ~AnalogIn() {
00060         analogin_free(&_adc);
00061     }
00062 
00063     /** Read the input voltage, represented as a float in the range [0.0, 1.0]
00064      *
00065      * @returns A floating-point value representing the current input voltage, measured as a percentage
00066      */
00067     float read() {
00068         return analogin_read(&_adc);
00069     }
00070 
00071     /** Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
00072      *
00073      * @returns
00074      *   16-bit unsigned short representing the current input voltage, normalised to a 16-bit value
00075      */
00076     unsigned short read_u16() {
00077         return analogin_read_u16(&_adc);
00078     }
00079 
00080 #ifdef MBED_OPERATORS
00081     /** An operator shorthand for read()
00082      *
00083      * The float() operator can be used as a shorthand for read() to simplify common code sequences
00084      *
00085      * Example:
00086      * @code
00087      * float x = volume.read();
00088      * float x = volume;
00089      *
00090      * if(volume.read() > 0.25) { ... }
00091      * if(volume > 0.25) { ... }
00092      * @endcode
00093      */
00094     operator float() {
00095         return read();
00096     }
00097 #endif
00098 
00099 protected:
00100     analogin_t _adc;
00101 };
00102 
00103 } // namespace mbed
00104 
00105 #endif
00106 
00107 #endif