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