USB Serial application

Fork of USBSerial_HelloWorld by Samuel Mokrani

Committer:
Zaitsev
Date:
Sat Dec 16 10:26:48 2017 +0000
Revision:
11:b3f2a8bdac4d
Parent:
10:41552d038a69
A copy for D.S;

Who changed what in which revision?

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