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