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