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