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