BLE temperature profile using digital DS1820 or analog LM35 sensors

Dependencies:   DS1820

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AnalogOut.h Source File

AnalogOut.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #ifndef MBED_ANALOGOUT_H
00017 #define MBED_ANALOGOUT_H
00018 
00019 #include "platform.h"
00020 
00021 #if DEVICE_ANALOGOUT
00022 
00023 #include "analogout_api.h"
00024 
00025 namespace mbed {
00026 
00027 /** An analog output, used for setting the voltage on a pin
00028  *
00029  * Example:
00030  * @code
00031  * // Make a sawtooth output
00032  *
00033  * #include "mbed.h"
00034  *
00035  * AnalogOut tri(p18);
00036  * int main() {
00037  *     while(1) {
00038  *         tri = tri + 0.01;
00039  *         wait_us(1);
00040  *         if(tri == 1) {
00041  *             tri = 0;
00042  *         }
00043  *     }
00044  * }
00045  * @endcode
00046  */
00047 class AnalogOut {
00048 
00049 public:
00050 
00051     /** Create an AnalogOut connected to the specified pin
00052      *
00053      *  @param AnalogOut pin to connect to (18)
00054      */
00055     AnalogOut(PinName pin) {
00056         analogout_init(&_dac, pin);
00057     }
00058 
00059     /** Set the output voltage, specified as a percentage (float)
00060      *
00061      *  @param value A floating-point value representing the output voltage,
00062      *    specified as a percentage. The value should lie between
00063      *    0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%).
00064      *    Values outside this range will be saturated to 0.0f or 1.0f.
00065      */
00066     void write(float value) {
00067         analogout_write(&_dac, value);
00068     }
00069 
00070     /** Set the output voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
00071      *
00072      *  @param value 16-bit unsigned short representing the output voltage,
00073      *            normalised to a 16-bit value (0x0000 = 0v, 0xFFFF = 3.3v)
00074      */
00075     void write_u16(unsigned short value) {
00076         analogout_write_u16(&_dac, value);
00077     }
00078 
00079     /** Return the current output voltage setting, measured as a percentage (float)
00080      *
00081      *  @returns
00082      *    A floating-point value representing the current voltage being output on the pin,
00083      *    measured as a percentage. The returned value will lie between
00084      *    0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%).
00085      *
00086      *  @note
00087      *    This value may not match exactly the value set by a previous write().
00088      */
00089     float read() {
00090         return analogout_read(&_dac);
00091     }
00092 
00093 #ifdef MBED_OPERATORS
00094     /** An operator shorthand for write()
00095      */
00096     AnalogOut& operator= (float percent) {
00097         write(percent);
00098         return *this;
00099     }
00100 
00101     AnalogOut& operator= (AnalogOut& rhs) {
00102         write(rhs.read());
00103         return *this;
00104     }
00105 
00106     /** An operator shorthand for read()
00107      */
00108     operator float() {
00109         return read();
00110     }
00111 #endif
00112 
00113 protected:
00114     dac_t _dac;
00115 };
00116 
00117 } // namespace mbed
00118 
00119 #endif
00120 
00121 #endif