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