]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/api/AnalogOut.h
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / api / AnalogOut.h
1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #ifndef MBED_ANALOGOUT_H
17 #define MBED_ANALOGOUT_H
18
19 #include "platform.h"
20
21 #if DEVICE_ANALOGOUT
22
23 #include "analogout_api.h"
24
25 namespace mbed {
26
27 /** An analog output, used for setting the voltage on a pin
28 *
29 * Example:
30 * @code
31 * // Make a sawtooth output
32 *
33 * #include "mbed.h"
34 *
35 * AnalogOut tri(p18);
36 * int main() {
37 * while(1) {
38 * tri = tri + 0.01;
39 * wait_us(1);
40 * if(tri == 1) {
41 * tri = 0;
42 * }
43 * }
44 * }
45 * @endcode
46 */
47 class AnalogOut {
48
49 public:
50
51 /** Create an AnalogOut connected to the specified pin
52 *
53 * @param AnalogOut pin to connect to (18)
54 */
55 AnalogOut(PinName pin) {
56 analogout_init(&_dac, pin);
57 }
58
59 /** Set the output voltage, specified as a percentage (float)
60 *
61 * @param value A floating-point value representing the output voltage,
62 * specified as a percentage. The value should lie between
63 * 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%).
64 * Values outside this range will be saturated to 0.0f or 1.0f.
65 */
66 void write(float value) {
67 analogout_write(&_dac, value);
68 }
69
70 /** Set the output voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
71 *
72 * @param value 16-bit unsigned short representing the output voltage,
73 * normalised to a 16-bit value (0x0000 = 0v, 0xFFFF = 3.3v)
74 */
75 void write_u16(unsigned short value) {
76 analogout_write_u16(&_dac, value);
77 }
78
79 /** Return the current output voltage setting, measured as a percentage (float)
80 *
81 * @returns
82 * A floating-point value representing the current voltage being output on the pin,
83 * measured as a percentage. The returned value will lie between
84 * 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%).
85 *
86 * @note
87 * This value may not match exactly the value set by a previous write().
88 */
89 float read() {
90 return analogout_read(&_dac);
91 }
92
93 #ifdef MBED_OPERATORS
94 /** An operator shorthand for write()
95 */
96 AnalogOut& operator= (float percent) {
97 write(percent);
98 return *this;
99 }
100
101 AnalogOut& operator= (AnalogOut& rhs) {
102 write(rhs.read());
103 return *this;
104 }
105
106 /** An operator shorthand for read()
107 */
108 operator float() {
109 return read();
110 }
111 #endif
112
113 protected:
114 dac_t _dac;
115 };
116
117 } // namespace mbed
118
119 #endif
120
121 #endif
Imprint / Impressum