added prescaler for 16 bit pwm in LPC1347 target

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