initial

Dependencies:   mbed

Committer:
yihui
Date:
Mon Jan 11 02:32:24 2016 +0000
Revision:
0:638edba3adf6
initial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 0:638edba3adf6 1 /* mbed Microcontroller Library
yihui 0:638edba3adf6 2 * Copyright (c) 2006-2013 ARM Limited
yihui 0:638edba3adf6 3 *
yihui 0:638edba3adf6 4 * Licensed under the Apache License, Version 2.0 (the "License");
yihui 0:638edba3adf6 5 * you may not use this file except in compliance with the License.
yihui 0:638edba3adf6 6 * You may obtain a copy of the License at
yihui 0:638edba3adf6 7 *
yihui 0:638edba3adf6 8 * http://www.apache.org/licenses/LICENSE-2.0
yihui 0:638edba3adf6 9 *
yihui 0:638edba3adf6 10 * Unless required by applicable law or agreed to in writing, software
yihui 0:638edba3adf6 11 * distributed under the License is distributed on an "AS IS" BASIS,
yihui 0:638edba3adf6 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
yihui 0:638edba3adf6 13 * See the License for the specific language governing permissions and
yihui 0:638edba3adf6 14 * limitations under the License.
yihui 0:638edba3adf6 15 */
yihui 0:638edba3adf6 16 #ifndef MBED_ANALOGIN_H
yihui 0:638edba3adf6 17 #define MBED_ANALOGIN_H
yihui 0:638edba3adf6 18
yihui 0:638edba3adf6 19 #include "platform.h"
yihui 0:638edba3adf6 20
yihui 0:638edba3adf6 21 #if DEVICE_ANALOGIN
yihui 0:638edba3adf6 22
yihui 0:638edba3adf6 23 #include "analogin_api.h"
yihui 0:638edba3adf6 24
yihui 0:638edba3adf6 25 namespace mbed {
yihui 0:638edba3adf6 26
yihui 0:638edba3adf6 27 /** An analog input, used for reading the voltage on a pin
yihui 0:638edba3adf6 28 *
yihui 0:638edba3adf6 29 * Example:
yihui 0:638edba3adf6 30 * @code
yihui 0:638edba3adf6 31 * // Print messages when the AnalogIn is greater than 50%
yihui 0:638edba3adf6 32 *
yihui 0:638edba3adf6 33 * #include "mbed.h"
yihui 0:638edba3adf6 34 *
yihui 0:638edba3adf6 35 * AnalogIn temperature(p20);
yihui 0:638edba3adf6 36 *
yihui 0:638edba3adf6 37 * int main() {
yihui 0:638edba3adf6 38 * while(1) {
yihui 0:638edba3adf6 39 * if(temperature > 0.5) {
yihui 0:638edba3adf6 40 * printf("Too hot! (%f)", temperature.read());
yihui 0:638edba3adf6 41 * }
yihui 0:638edba3adf6 42 * }
yihui 0:638edba3adf6 43 * }
yihui 0:638edba3adf6 44 * @endcode
yihui 0:638edba3adf6 45 */
yihui 0:638edba3adf6 46 class AnalogIn {
yihui 0:638edba3adf6 47
yihui 0:638edba3adf6 48 public:
yihui 0:638edba3adf6 49
yihui 0:638edba3adf6 50 /** Create an AnalogIn, connected to the specified pin
yihui 0:638edba3adf6 51 *
yihui 0:638edba3adf6 52 * @param pin AnalogIn pin to connect to
yihui 0:638edba3adf6 53 * @param name (optional) A string to identify the object
yihui 0:638edba3adf6 54 */
yihui 0:638edba3adf6 55 AnalogIn(PinName pin) {
yihui 0:638edba3adf6 56 analogin_init(&_adc, pin);
yihui 0:638edba3adf6 57 }
yihui 0:638edba3adf6 58
yihui 0:638edba3adf6 59 /** Read the input voltage, represented as a float in the range [0.0, 1.0]
yihui 0:638edba3adf6 60 *
yihui 0:638edba3adf6 61 * @returns A floating-point value representing the current input voltage, measured as a percentage
yihui 0:638edba3adf6 62 */
yihui 0:638edba3adf6 63 float read() {
yihui 0:638edba3adf6 64 return analogin_read(&_adc);
yihui 0:638edba3adf6 65 }
yihui 0:638edba3adf6 66
yihui 0:638edba3adf6 67 /** Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
yihui 0:638edba3adf6 68 *
yihui 0:638edba3adf6 69 * @returns
yihui 0:638edba3adf6 70 * 16-bit unsigned short representing the current input voltage, normalised to a 16-bit value
yihui 0:638edba3adf6 71 */
yihui 0:638edba3adf6 72 unsigned short read_u16() {
yihui 0:638edba3adf6 73 return analogin_read_u16(&_adc);
yihui 0:638edba3adf6 74 }
yihui 0:638edba3adf6 75
yihui 0:638edba3adf6 76 #ifdef MBED_OPERATORS
yihui 0:638edba3adf6 77 /** An operator shorthand for read()
yihui 0:638edba3adf6 78 *
yihui 0:638edba3adf6 79 * The float() operator can be used as a shorthand for read() to simplify common code sequences
yihui 0:638edba3adf6 80 *
yihui 0:638edba3adf6 81 * Example:
yihui 0:638edba3adf6 82 * @code
yihui 0:638edba3adf6 83 * float x = volume.read();
yihui 0:638edba3adf6 84 * float x = volume;
yihui 0:638edba3adf6 85 *
yihui 0:638edba3adf6 86 * if(volume.read() > 0.25) { ... }
yihui 0:638edba3adf6 87 * if(volume > 0.25) { ... }
yihui 0:638edba3adf6 88 * @endcode
yihui 0:638edba3adf6 89 */
yihui 0:638edba3adf6 90 operator float() {
yihui 0:638edba3adf6 91 return read();
yihui 0:638edba3adf6 92 }
yihui 0:638edba3adf6 93 #endif
yihui 0:638edba3adf6 94
yihui 0:638edba3adf6 95 protected:
yihui 0:638edba3adf6 96 analogin_t _adc;
yihui 0:638edba3adf6 97 };
yihui 0:638edba3adf6 98
yihui 0:638edba3adf6 99 } // namespace mbed
yihui 0:638edba3adf6 100
yihui 0:638edba3adf6 101 #endif
yihui 0:638edba3adf6 102
yihui 0:638edba3adf6 103 #endif