Nicolas Borla / Mbed OS ROME2_Robot_Firmware
Committer:
boro
Date:
Mon Mar 16 13:12:31 2020 +0000
Revision:
0:4beb2ea291ec
a

Who changed what in which revision?

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