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