mbed library for slider v2

Dependents:   kl46z_slider_v2

Committer:
mturner5
Date:
Wed Sep 14 07:04:27 2016 +0000
Revision:
0:b7116bd48af6
Tried to use the timer.

Who changed what in which revision?

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