,,

Fork of Application by Daniel Sygut

Committer:
Zaitsev
Date:
Tue Jan 10 20:42:26 2017 +0000
Revision:
10:41552d038a69
USB Serial bi-directional bridge

Who changed what in which revision?

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