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