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