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