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