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