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  /** Set the output voltage, specified as a percentage (float)
70  *
71  * @param value A floating-point value representing the output voltage,
72  * specified as a percentage. The value should lie between
73  * 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%).
74  * Values outside this range will be saturated to 0.0f or 1.0f.
75  */
76  void write(float value);
77 
78  /** Set the output voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
79  *
80  * @param value 16-bit unsigned short representing the output voltage,
81  * normalized to a 16-bit value (0x0000 = 0v, 0xFFFF = 3.3v)
82  */
83  void write_u16(unsigned short value);
84 
85  /** Return the current output voltage setting, measured as a percentage (float)
86  *
87  * @returns
88  * A floating-point value representing the current voltage being output on the pin,
89  * measured as a percentage. The returned value will lie between
90  * 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%).
91  *
92  * @note
93  * This value may not match exactly the value set by a previous write().
94  */
95  float read();
96 
97  /** An operator shorthand for write()
98  * \sa AnalogOut::write()
99  */
100  AnalogOut &operator= (float percent)
101  {
102  // Underlying write call is thread safe
103  write(percent);
104  return *this;
105  }
106 
107  /** An operator shorthand for write()
108  * \sa AnalogOut::write()
109  */
111  {
112  // Underlying write call is thread safe
113  write(rhs.read());
114  return *this;
115  }
116 
117  /** An operator shorthand for read()
118  * \sa AnalogOut::read()
119  */
120  operator float()
121  {
122  // Underlying read call is thread safe
123  return read();
124  }
125 
126  virtual ~AnalogOut()
127  {
128  /** Deinitialize pin configuration.
129  */
130  analogout_free(&_dac);
131  }
132 
133 protected:
134 #if !defined(DOXYGEN_ONLY)
135  virtual void lock()
136  {
137  _mutex.lock();
138  }
139 
140  virtual void unlock()
141  {
142  _mutex.unlock();
143  }
144 
145  dac_t _dac;
146  PlatformMutex _mutex;
147 #endif //!defined(DOXYGEN_ONLY)
148 };
149 
150 /** @}*/
151 
152 } // namespace mbed
153 
154 #endif
155 
156 #endif
AnalogOut & operator=(float percent)
An operator shorthand for write()
Definition: AnalogOut.h:100
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.
virtual ~AnalogOut()
Definition: AnalogOut.h:126
void write_u16(unsigned short value)
Set the output voltage, represented as an unsigned short in the range [0x0, 0xFFFF].
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.