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