mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Fri Feb 16 16:09:33 2018 +0000
Revision:
181:57724642e740
Parent:
167:e84263d55307
Child:
187:0387e8f68319
mbed-dev library. Release version 159.

Who changed what in which revision?

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