Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

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