mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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