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