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