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