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