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