Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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