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