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