![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
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/AnalogOut.h@2:c69117fc79da, 2017-03-08 (annotated)
- Committer:
- 16038618
- Date:
- Wed Mar 08 10:24:56 2017 +0000
- Revision:
- 2:c69117fc79da
- Parent:
- 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_ANALOGOUT_H |
16038618 | 1:4bdfa7d7e8bf | 17 | #define MBED_ANALOGOUT_H |
16038618 | 1:4bdfa7d7e8bf | 18 | |
16038618 | 1:4bdfa7d7e8bf | 19 | #include "platform.h" |
16038618 | 1:4bdfa7d7e8bf | 20 | |
16038618 | 1:4bdfa7d7e8bf | 21 | #if DEVICE_ANALOGOUT |
16038618 | 1:4bdfa7d7e8bf | 22 | |
16038618 | 1:4bdfa7d7e8bf | 23 | #include "analogout_api.h" |
16038618 | 1:4bdfa7d7e8bf | 24 | |
16038618 | 1:4bdfa7d7e8bf | 25 | namespace mbed { |
16038618 | 1:4bdfa7d7e8bf | 26 | |
16038618 | 1:4bdfa7d7e8bf | 27 | /** An analog output, used for setting the voltage on a pin |
16038618 | 1:4bdfa7d7e8bf | 28 | * |
16038618 | 1:4bdfa7d7e8bf | 29 | * Example: |
16038618 | 1:4bdfa7d7e8bf | 30 | * @code |
16038618 | 1:4bdfa7d7e8bf | 31 | * // Make a sawtooth output |
16038618 | 1:4bdfa7d7e8bf | 32 | * |
16038618 | 1:4bdfa7d7e8bf | 33 | * #include "mbed.h" |
16038618 | 1:4bdfa7d7e8bf | 34 | * |
16038618 | 1:4bdfa7d7e8bf | 35 | * AnalogOut tri(p18); |
16038618 | 1:4bdfa7d7e8bf | 36 | * int main() { |
16038618 | 1:4bdfa7d7e8bf | 37 | * while(1) { |
16038618 | 1:4bdfa7d7e8bf | 38 | * tri = tri + 0.01; |
16038618 | 1:4bdfa7d7e8bf | 39 | * wait_us(1); |
16038618 | 1:4bdfa7d7e8bf | 40 | * if(tri == 1) { |
16038618 | 1:4bdfa7d7e8bf | 41 | * tri = 0; |
16038618 | 1:4bdfa7d7e8bf | 42 | * } |
16038618 | 1:4bdfa7d7e8bf | 43 | * } |
16038618 | 1:4bdfa7d7e8bf | 44 | * } |
16038618 | 1:4bdfa7d7e8bf | 45 | * @endcode |
16038618 | 1:4bdfa7d7e8bf | 46 | */ |
16038618 | 1:4bdfa7d7e8bf | 47 | class AnalogOut { |
16038618 | 1:4bdfa7d7e8bf | 48 | |
16038618 | 1:4bdfa7d7e8bf | 49 | public: |
16038618 | 1:4bdfa7d7e8bf | 50 | |
16038618 | 1:4bdfa7d7e8bf | 51 | /** Create an AnalogOut connected to the specified pin |
16038618 | 1:4bdfa7d7e8bf | 52 | * |
16038618 | 1:4bdfa7d7e8bf | 53 | * @param AnalogOut pin to connect to (18) |
16038618 | 1:4bdfa7d7e8bf | 54 | */ |
16038618 | 1:4bdfa7d7e8bf | 55 | AnalogOut(PinName pin) { |
16038618 | 1:4bdfa7d7e8bf | 56 | analogout_init(&_dac, pin); |
16038618 | 1:4bdfa7d7e8bf | 57 | } |
16038618 | 1:4bdfa7d7e8bf | 58 | |
16038618 | 1:4bdfa7d7e8bf | 59 | /** Set the output voltage, specified as a percentage (float) |
16038618 | 1:4bdfa7d7e8bf | 60 | * |
16038618 | 1:4bdfa7d7e8bf | 61 | * @param value A floating-point value representing the output voltage, |
16038618 | 1:4bdfa7d7e8bf | 62 | * specified as a percentage. The value should lie between |
16038618 | 1:4bdfa7d7e8bf | 63 | * 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%). |
16038618 | 1:4bdfa7d7e8bf | 64 | * Values outside this range will be saturated to 0.0f or 1.0f. |
16038618 | 1:4bdfa7d7e8bf | 65 | */ |
16038618 | 1:4bdfa7d7e8bf | 66 | void write(float value) { |
16038618 | 1:4bdfa7d7e8bf | 67 | analogout_write(&_dac, value); |
16038618 | 1:4bdfa7d7e8bf | 68 | } |
16038618 | 1:4bdfa7d7e8bf | 69 | |
16038618 | 1:4bdfa7d7e8bf | 70 | /** Set the output voltage, represented as an unsigned short in the range [0x0, 0xFFFF] |
16038618 | 1:4bdfa7d7e8bf | 71 | * |
16038618 | 1:4bdfa7d7e8bf | 72 | * @param value 16-bit unsigned short representing the output voltage, |
16038618 | 1:4bdfa7d7e8bf | 73 | * normalised to a 16-bit value (0x0000 = 0v, 0xFFFF = 3.3v) |
16038618 | 1:4bdfa7d7e8bf | 74 | */ |
16038618 | 1:4bdfa7d7e8bf | 75 | void write_u16(unsigned short value) { |
16038618 | 1:4bdfa7d7e8bf | 76 | analogout_write_u16(&_dac, value); |
16038618 | 1:4bdfa7d7e8bf | 77 | } |
16038618 | 1:4bdfa7d7e8bf | 78 | |
16038618 | 1:4bdfa7d7e8bf | 79 | /** Return the current output voltage setting, measured as a percentage (float) |
16038618 | 1:4bdfa7d7e8bf | 80 | * |
16038618 | 1:4bdfa7d7e8bf | 81 | * @returns |
16038618 | 1:4bdfa7d7e8bf | 82 | * A floating-point value representing the current voltage being output on the pin, |
16038618 | 1:4bdfa7d7e8bf | 83 | * measured as a percentage. The returned value will lie between |
16038618 | 1:4bdfa7d7e8bf | 84 | * 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%). |
16038618 | 1:4bdfa7d7e8bf | 85 | * |
16038618 | 1:4bdfa7d7e8bf | 86 | * @note |
16038618 | 1:4bdfa7d7e8bf | 87 | * This value may not match exactly the value set by a previous write(). |
16038618 | 1:4bdfa7d7e8bf | 88 | */ |
16038618 | 1:4bdfa7d7e8bf | 89 | float read() { |
16038618 | 1:4bdfa7d7e8bf | 90 | return analogout_read(&_dac); |
16038618 | 1:4bdfa7d7e8bf | 91 | } |
16038618 | 1:4bdfa7d7e8bf | 92 | |
16038618 | 1:4bdfa7d7e8bf | 93 | #ifdef MBED_OPERATORS |
16038618 | 1:4bdfa7d7e8bf | 94 | /** An operator shorthand for write() |
16038618 | 1:4bdfa7d7e8bf | 95 | */ |
16038618 | 1:4bdfa7d7e8bf | 96 | AnalogOut& operator= (float percent) { |
16038618 | 1:4bdfa7d7e8bf | 97 | write(percent); |
16038618 | 1:4bdfa7d7e8bf | 98 | return *this; |
16038618 | 1:4bdfa7d7e8bf | 99 | } |
16038618 | 1:4bdfa7d7e8bf | 100 | |
16038618 | 1:4bdfa7d7e8bf | 101 | AnalogOut& operator= (AnalogOut& rhs) { |
16038618 | 1:4bdfa7d7e8bf | 102 | write(rhs.read()); |
16038618 | 1:4bdfa7d7e8bf | 103 | return *this; |
16038618 | 1:4bdfa7d7e8bf | 104 | } |
16038618 | 1:4bdfa7d7e8bf | 105 | |
16038618 | 1:4bdfa7d7e8bf | 106 | /** An operator shorthand for read() |
16038618 | 1:4bdfa7d7e8bf | 107 | */ |
16038618 | 1:4bdfa7d7e8bf | 108 | operator float() { |
16038618 | 1:4bdfa7d7e8bf | 109 | return read(); |
16038618 | 1:4bdfa7d7e8bf | 110 | } |
16038618 | 1:4bdfa7d7e8bf | 111 | #endif |
16038618 | 1:4bdfa7d7e8bf | 112 | |
16038618 | 1:4bdfa7d7e8bf | 113 | protected: |
16038618 | 1:4bdfa7d7e8bf | 114 | dac_t _dac; |
16038618 | 1:4bdfa7d7e8bf | 115 | }; |
16038618 | 1:4bdfa7d7e8bf | 116 | |
16038618 | 1:4bdfa7d7e8bf | 117 | } // namespace mbed |
16038618 | 1:4bdfa7d7e8bf | 118 | |
16038618 | 1:4bdfa7d7e8bf | 119 | #endif |
16038618 | 1:4bdfa7d7e8bf | 120 | |
16038618 | 1:4bdfa7d7e8bf | 121 | #endif |