inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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