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