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