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