Development mbed library for MAX32630FTHR
Dependents: blinky_max32630fthr
drivers/AnalogOut.h@0:5c4d7b2438d3, 2016-11-11 (annotated)
- Committer:
- switches
- Date:
- Fri Nov 11 20:59:50 2016 +0000
- Revision:
- 0:5c4d7b2438d3
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
switches | 0:5c4d7b2438d3 | 1 | /* mbed Microcontroller Library |
switches | 0:5c4d7b2438d3 | 2 | * Copyright (c) 2006-2013 ARM Limited |
switches | 0:5c4d7b2438d3 | 3 | * |
switches | 0:5c4d7b2438d3 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
switches | 0:5c4d7b2438d3 | 5 | * you may not use this file except in compliance with the License. |
switches | 0:5c4d7b2438d3 | 6 | * You may obtain a copy of the License at |
switches | 0:5c4d7b2438d3 | 7 | * |
switches | 0:5c4d7b2438d3 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
switches | 0:5c4d7b2438d3 | 9 | * |
switches | 0:5c4d7b2438d3 | 10 | * Unless required by applicable law or agreed to in writing, software |
switches | 0:5c4d7b2438d3 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
switches | 0:5c4d7b2438d3 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
switches | 0:5c4d7b2438d3 | 13 | * See the License for the specific language governing permissions and |
switches | 0:5c4d7b2438d3 | 14 | * limitations under the License. |
switches | 0:5c4d7b2438d3 | 15 | */ |
switches | 0:5c4d7b2438d3 | 16 | #ifndef MBED_ANALOGOUT_H |
switches | 0:5c4d7b2438d3 | 17 | #define MBED_ANALOGOUT_H |
switches | 0:5c4d7b2438d3 | 18 | |
switches | 0:5c4d7b2438d3 | 19 | #include "platform/platform.h" |
switches | 0:5c4d7b2438d3 | 20 | |
switches | 0:5c4d7b2438d3 | 21 | #if DEVICE_ANALOGOUT |
switches | 0:5c4d7b2438d3 | 22 | |
switches | 0:5c4d7b2438d3 | 23 | #include "hal/analogout_api.h" |
switches | 0:5c4d7b2438d3 | 24 | #include "platform/PlatformMutex.h" |
switches | 0:5c4d7b2438d3 | 25 | |
switches | 0:5c4d7b2438d3 | 26 | namespace mbed { |
switches | 0:5c4d7b2438d3 | 27 | /** \addtogroup drivers */ |
switches | 0:5c4d7b2438d3 | 28 | /** @{*/ |
switches | 0:5c4d7b2438d3 | 29 | |
switches | 0:5c4d7b2438d3 | 30 | /** An analog output, used for setting the voltage on a pin |
switches | 0:5c4d7b2438d3 | 31 | * |
switches | 0:5c4d7b2438d3 | 32 | * @Note Synchronization level: Thread safe |
switches | 0:5c4d7b2438d3 | 33 | * |
switches | 0:5c4d7b2438d3 | 34 | * Example: |
switches | 0:5c4d7b2438d3 | 35 | * @code |
switches | 0:5c4d7b2438d3 | 36 | * // Make a sawtooth output |
switches | 0:5c4d7b2438d3 | 37 | * |
switches | 0:5c4d7b2438d3 | 38 | * #include "mbed.h" |
switches | 0:5c4d7b2438d3 | 39 | * |
switches | 0:5c4d7b2438d3 | 40 | * AnalogOut tri(p18); |
switches | 0:5c4d7b2438d3 | 41 | * int main() { |
switches | 0:5c4d7b2438d3 | 42 | * while(1) { |
switches | 0:5c4d7b2438d3 | 43 | * tri = tri + 0.01; |
switches | 0:5c4d7b2438d3 | 44 | * wait_us(1); |
switches | 0:5c4d7b2438d3 | 45 | * if(tri == 1) { |
switches | 0:5c4d7b2438d3 | 46 | * tri = 0; |
switches | 0:5c4d7b2438d3 | 47 | * } |
switches | 0:5c4d7b2438d3 | 48 | * } |
switches | 0:5c4d7b2438d3 | 49 | * } |
switches | 0:5c4d7b2438d3 | 50 | * @endcode |
switches | 0:5c4d7b2438d3 | 51 | */ |
switches | 0:5c4d7b2438d3 | 52 | class AnalogOut { |
switches | 0:5c4d7b2438d3 | 53 | |
switches | 0:5c4d7b2438d3 | 54 | public: |
switches | 0:5c4d7b2438d3 | 55 | |
switches | 0:5c4d7b2438d3 | 56 | /** Create an AnalogOut connected to the specified pin |
switches | 0:5c4d7b2438d3 | 57 | * |
switches | 0:5c4d7b2438d3 | 58 | * @param AnalogOut pin to connect to (18) |
switches | 0:5c4d7b2438d3 | 59 | */ |
switches | 0:5c4d7b2438d3 | 60 | AnalogOut(PinName pin) { |
switches | 0:5c4d7b2438d3 | 61 | analogout_init(&_dac, pin); |
switches | 0:5c4d7b2438d3 | 62 | } |
switches | 0:5c4d7b2438d3 | 63 | |
switches | 0:5c4d7b2438d3 | 64 | /** Set the output voltage, specified as a percentage (float) |
switches | 0:5c4d7b2438d3 | 65 | * |
switches | 0:5c4d7b2438d3 | 66 | * @param value A floating-point value representing the output voltage, |
switches | 0:5c4d7b2438d3 | 67 | * specified as a percentage. The value should lie between |
switches | 0:5c4d7b2438d3 | 68 | * 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%). |
switches | 0:5c4d7b2438d3 | 69 | * Values outside this range will be saturated to 0.0f or 1.0f. |
switches | 0:5c4d7b2438d3 | 70 | */ |
switches | 0:5c4d7b2438d3 | 71 | void write(float value) { |
switches | 0:5c4d7b2438d3 | 72 | lock(); |
switches | 0:5c4d7b2438d3 | 73 | analogout_write(&_dac, value); |
switches | 0:5c4d7b2438d3 | 74 | unlock(); |
switches | 0:5c4d7b2438d3 | 75 | } |
switches | 0:5c4d7b2438d3 | 76 | |
switches | 0:5c4d7b2438d3 | 77 | /** Set the output voltage, represented as an unsigned short in the range [0x0, 0xFFFF] |
switches | 0:5c4d7b2438d3 | 78 | * |
switches | 0:5c4d7b2438d3 | 79 | * @param value 16-bit unsigned short representing the output voltage, |
switches | 0:5c4d7b2438d3 | 80 | * normalised to a 16-bit value (0x0000 = 0v, 0xFFFF = 3.3v) |
switches | 0:5c4d7b2438d3 | 81 | */ |
switches | 0:5c4d7b2438d3 | 82 | void write_u16(unsigned short value) { |
switches | 0:5c4d7b2438d3 | 83 | lock(); |
switches | 0:5c4d7b2438d3 | 84 | analogout_write_u16(&_dac, value); |
switches | 0:5c4d7b2438d3 | 85 | unlock(); |
switches | 0:5c4d7b2438d3 | 86 | } |
switches | 0:5c4d7b2438d3 | 87 | |
switches | 0:5c4d7b2438d3 | 88 | /** Return the current output voltage setting, measured as a percentage (float) |
switches | 0:5c4d7b2438d3 | 89 | * |
switches | 0:5c4d7b2438d3 | 90 | * @returns |
switches | 0:5c4d7b2438d3 | 91 | * A floating-point value representing the current voltage being output on the pin, |
switches | 0:5c4d7b2438d3 | 92 | * measured as a percentage. The returned value will lie between |
switches | 0:5c4d7b2438d3 | 93 | * 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%). |
switches | 0:5c4d7b2438d3 | 94 | * |
switches | 0:5c4d7b2438d3 | 95 | * @note |
switches | 0:5c4d7b2438d3 | 96 | * This value may not match exactly the value set by a previous write(). |
switches | 0:5c4d7b2438d3 | 97 | */ |
switches | 0:5c4d7b2438d3 | 98 | float read() { |
switches | 0:5c4d7b2438d3 | 99 | lock(); |
switches | 0:5c4d7b2438d3 | 100 | float ret = analogout_read(&_dac); |
switches | 0:5c4d7b2438d3 | 101 | unlock(); |
switches | 0:5c4d7b2438d3 | 102 | return ret; |
switches | 0:5c4d7b2438d3 | 103 | } |
switches | 0:5c4d7b2438d3 | 104 | |
switches | 0:5c4d7b2438d3 | 105 | /** An operator shorthand for write() |
switches | 0:5c4d7b2438d3 | 106 | */ |
switches | 0:5c4d7b2438d3 | 107 | AnalogOut& operator= (float percent) { |
switches | 0:5c4d7b2438d3 | 108 | // Underlying write call is thread safe |
switches | 0:5c4d7b2438d3 | 109 | write(percent); |
switches | 0:5c4d7b2438d3 | 110 | return *this; |
switches | 0:5c4d7b2438d3 | 111 | } |
switches | 0:5c4d7b2438d3 | 112 | |
switches | 0:5c4d7b2438d3 | 113 | AnalogOut& operator= (AnalogOut& rhs) { |
switches | 0:5c4d7b2438d3 | 114 | // Underlying write call is thread safe |
switches | 0:5c4d7b2438d3 | 115 | write(rhs.read()); |
switches | 0:5c4d7b2438d3 | 116 | return *this; |
switches | 0:5c4d7b2438d3 | 117 | } |
switches | 0:5c4d7b2438d3 | 118 | |
switches | 0:5c4d7b2438d3 | 119 | /** An operator shorthand for read() |
switches | 0:5c4d7b2438d3 | 120 | */ |
switches | 0:5c4d7b2438d3 | 121 | operator float() { |
switches | 0:5c4d7b2438d3 | 122 | // Underlying read call is thread safe |
switches | 0:5c4d7b2438d3 | 123 | return read(); |
switches | 0:5c4d7b2438d3 | 124 | } |
switches | 0:5c4d7b2438d3 | 125 | |
switches | 0:5c4d7b2438d3 | 126 | virtual ~AnalogOut() { |
switches | 0:5c4d7b2438d3 | 127 | // Do nothing |
switches | 0:5c4d7b2438d3 | 128 | } |
switches | 0:5c4d7b2438d3 | 129 | |
switches | 0:5c4d7b2438d3 | 130 | protected: |
switches | 0:5c4d7b2438d3 | 131 | |
switches | 0:5c4d7b2438d3 | 132 | virtual void lock() { |
switches | 0:5c4d7b2438d3 | 133 | _mutex.lock(); |
switches | 0:5c4d7b2438d3 | 134 | } |
switches | 0:5c4d7b2438d3 | 135 | |
switches | 0:5c4d7b2438d3 | 136 | virtual void unlock() { |
switches | 0:5c4d7b2438d3 | 137 | _mutex.unlock(); |
switches | 0:5c4d7b2438d3 | 138 | } |
switches | 0:5c4d7b2438d3 | 139 | |
switches | 0:5c4d7b2438d3 | 140 | dac_t _dac; |
switches | 0:5c4d7b2438d3 | 141 | PlatformMutex _mutex; |
switches | 0:5c4d7b2438d3 | 142 | }; |
switches | 0:5c4d7b2438d3 | 143 | |
switches | 0:5c4d7b2438d3 | 144 | } // namespace mbed |
switches | 0:5c4d7b2438d3 | 145 | |
switches | 0:5c4d7b2438d3 | 146 | #endif |
switches | 0:5c4d7b2438d3 | 147 | |
switches | 0:5c4d7b2438d3 | 148 | #endif |
switches | 0:5c4d7b2438d3 | 149 | |
switches | 0:5c4d7b2438d3 | 150 | /** @}*/ |