Mistake on this page?
Report an issue in GitHub or email us
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 "platform/PlatformMutex.h"
26 
27 namespace 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  */
56 class AnalogOut {
57 
58 public:
59 
60  /** Create an AnalogOut connected to the specified pin
61  *
62  * @param pin AnalogOut pin to connect to
63  */
64  AnalogOut(PinName pin)
65  {
66  analogout_init(&_dac, pin);
67  }
68 
69  /** Create an AnalogOut connected to the specified pin
70  *
71  * @param pinmap reference to structure which holds static pinmap.
72  */
73  AnalogOut(const PinMap &&) = delete; // prevent passing of temporary objects
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 
143 protected:
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  PlatformMutex _mutex;
157 #endif //!defined(DOXYGEN_ONLY)
158 };
159 
160 /** @}*/
161 
162 } // namespace mbed
163 
164 #endif
165 
166 #endif
AnalogOut & operator=(float percent)
An operator shorthand for write()
Definition: AnalogOut.h:110
AnalogOut(PinName pin)
Create an AnalogOut connected to the specified pin.
Definition: AnalogOut.h:64
void analogout_free(dac_t *obj)
Release the analogout object.
struct dac_s dac_t
Analogout hal structure.
Definition: analogout_api.h:34
An analog output, used for setting the voltage on a pin.
Definition: AnalogOut.h:56
float read()
Return the current output voltage setting, measured as a percentage (float)
void write(float value)
Set the output voltage, specified as a percentage (float)
The PlatformMutex class is used to synchronize the execution of threads.
Definition: PlatformMutex.h:47
void analogout_init(dac_t *obj, PinName pin)
Initialize the analogout peripheral.
Definition: pinmap.h:31
virtual ~AnalogOut()
Definition: AnalogOut.h:136
void write_u16(unsigned short value)
Set the output voltage, represented as an unsigned short in the range [0x0, 0xFFFF].
void analogout_init_direct(dac_t *obj, const PinMap *pinmap)
Initialize the analogout peripheral.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.