Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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