Rizky Ardi Maulana / mbed-os
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/platform.h"
00020 
00021 #if DEVICE_ANALOGIN
00022 
00023 #include "hal/analogin_api.h"
00024 #include "platform/SingletonPtr.h"
00025 #include "platform/PlatformMutex.h"
00026 
00027 namespace mbed {
00028 /** \addtogroup drivers */
00029 /** @{*/
00030 
00031 /** An analog input, used for reading the voltage on a pin
00032  *
00033  * @Note Synchronization level: Thread safe
00034  *
00035  * Example:
00036  * @code
00037  * // Print messages when the AnalogIn is greater than 50%
00038  *
00039  * #include "mbed.h"
00040  *
00041  * AnalogIn temperature(p20);
00042  *
00043  * int main() {
00044  *     while(1) {
00045  *         if(temperature > 0.5) {
00046  *             printf("Too hot! (%f)", temperature.read());
00047  *         }
00048  *     }
00049  * }
00050  * @endcode
00051  */
00052  
00053  
00054 class BatteryLevel {
00055  
00056 public:
00057     BatteryLevel() {
00058         battery_init(&_adc);
00059     } 
00060     
00061     unsigned short read_battery_u16() {
00062         unsigned short ret = battery_read_u16(&_adc);
00063         return ret;
00064     }
00065     virtual ~BatteryLevel() {
00066         // Do nothing
00067     }
00068 
00069 protected:  
00070 
00071     analogin_t _adc;
00072     static SingletonPtr<PlatformMutex>  _mutex;
00073 };
00074 
00075  
00076 class AnalogIn {
00077 
00078 public:
00079 
00080     /** Create an AnalogIn, connected to the specified pin
00081      *
00082      * @param pin AnalogIn pin to connect to
00083      * @param name (optional) A string to identify the object
00084      */
00085     AnalogIn(PinName pin) {
00086         lock();
00087         analogin_init(&_adc, pin);
00088         unlock();
00089     }
00090     /** Read the input voltage, represented as a float in the range [0.0, 1.0]
00091      *
00092      * @returns A floating-point value representing the current input voltage, measured as a percentage
00093      */
00094     float read() {
00095         lock();
00096         float ret = analogin_read(&_adc);
00097         unlock();
00098         return ret;
00099     }
00100 
00101     /** Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
00102      *
00103      * @returns
00104      *   16-bit unsigned short representing the current input voltage, normalised to a 16-bit value
00105      */
00106     unsigned short read_u16() {
00107         lock();
00108         unsigned short ret = analogin_read_u16(&_adc);
00109         unlock();
00110         return ret;
00111     }
00112 
00113     /** An operator shorthand for read()
00114      *
00115      * The float() operator can be used as a shorthand for read() to simplify common code sequences
00116      *
00117      * Example:
00118      * @code
00119      * float x = volume.read();
00120      * float x = volume;
00121      *
00122      * if(volume.read() > 0.25) { ... }
00123      * if(volume > 0.25) { ... }
00124      * @endcode
00125      */
00126     operator float() {
00127         // Underlying call is thread safe
00128         return read();
00129     }
00130 
00131     virtual ~AnalogIn() {
00132         // Do nothing
00133     }
00134 
00135 protected:
00136 
00137     virtual void lock() {
00138         _mutex->lock();
00139     }
00140 
00141     virtual void unlock() {
00142         _mutex->unlock();
00143     }
00144 
00145     analogin_t _adc;
00146     static SingletonPtr<PlatformMutex>  _mutex;
00147 };
00148 
00149 } // namespace mbed
00150 
00151 #endif
00152 
00153 #endif
00154 
00155 /** @}*/