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_PWMOUT_H
MrBedfordVan 0:b9164b348919 17 #define MBED_PWMOUT_H
MrBedfordVan 0:b9164b348919 18
MrBedfordVan 0:b9164b348919 19 #include "platform.h"
MrBedfordVan 0:b9164b348919 20
MrBedfordVan 0:b9164b348919 21 #if DEVICE_PWMOUT
MrBedfordVan 0:b9164b348919 22 #include "pwmout_api.h"
MrBedfordVan 0:b9164b348919 23
MrBedfordVan 0:b9164b348919 24 namespace mbed {
MrBedfordVan 0:b9164b348919 25
MrBedfordVan 0:b9164b348919 26 /** A pulse-width modulation digital output
MrBedfordVan 0:b9164b348919 27 *
MrBedfordVan 0:b9164b348919 28 * Example
MrBedfordVan 0:b9164b348919 29 * @code
MrBedfordVan 0:b9164b348919 30 * // Fade a led on.
MrBedfordVan 0:b9164b348919 31 * #include "mbed.h"
MrBedfordVan 0:b9164b348919 32 *
MrBedfordVan 0:b9164b348919 33 * PwmOut led(LED1);
MrBedfordVan 0:b9164b348919 34 *
MrBedfordVan 0:b9164b348919 35 * int main() {
MrBedfordVan 0:b9164b348919 36 * while(1) {
MrBedfordVan 0:b9164b348919 37 * led = led + 0.01;
MrBedfordVan 0:b9164b348919 38 * wait(0.2);
MrBedfordVan 0:b9164b348919 39 * if(led == 1.0) {
MrBedfordVan 0:b9164b348919 40 * led = 0;
MrBedfordVan 0:b9164b348919 41 * }
MrBedfordVan 0:b9164b348919 42 * }
MrBedfordVan 0:b9164b348919 43 * }
MrBedfordVan 0:b9164b348919 44 * @endcode
MrBedfordVan 0:b9164b348919 45 *
MrBedfordVan 0:b9164b348919 46 * @note
MrBedfordVan 0:b9164b348919 47 * On the LPC1768 and LPC2368, the PWMs all share the same
MrBedfordVan 0:b9164b348919 48 * period - if you change the period for one, you change it for all.
MrBedfordVan 0:b9164b348919 49 * Although routines that change the period maintain the duty cycle
MrBedfordVan 0:b9164b348919 50 * for its PWM, all other PWMs will require their duty cycle to be
MrBedfordVan 0:b9164b348919 51 * refreshed.
MrBedfordVan 0:b9164b348919 52 */
MrBedfordVan 0:b9164b348919 53 class PwmOut {
MrBedfordVan 0:b9164b348919 54
MrBedfordVan 0:b9164b348919 55 public:
MrBedfordVan 0:b9164b348919 56
MrBedfordVan 0:b9164b348919 57 /** Create a PwmOut connected to the specified pin
MrBedfordVan 0:b9164b348919 58 *
MrBedfordVan 0:b9164b348919 59 * @param pin PwmOut pin to connect to
MrBedfordVan 0:b9164b348919 60 */
MrBedfordVan 0:b9164b348919 61 PwmOut(PinName pin) {
MrBedfordVan 0:b9164b348919 62 pwmout_init(&_pwm, pin);
MrBedfordVan 0:b9164b348919 63 }
MrBedfordVan 0:b9164b348919 64
MrBedfordVan 0:b9164b348919 65 /** Set the ouput duty-cycle, specified as a percentage (float)
MrBedfordVan 0:b9164b348919 66 *
MrBedfordVan 0:b9164b348919 67 * @param value A floating-point value representing the output duty-cycle,
MrBedfordVan 0:b9164b348919 68 * specified as a percentage. The value should lie between
MrBedfordVan 0:b9164b348919 69 * 0.0f (representing on 0%) and 1.0f (representing on 100%).
MrBedfordVan 0:b9164b348919 70 * Values outside this range will be saturated to 0.0f or 1.0f.
MrBedfordVan 0:b9164b348919 71 */
MrBedfordVan 0:b9164b348919 72 void write(float value) {
MrBedfordVan 0:b9164b348919 73 pwmout_write(&_pwm, value);
MrBedfordVan 0:b9164b348919 74 }
MrBedfordVan 0:b9164b348919 75
MrBedfordVan 0:b9164b348919 76 /** Return the current output duty-cycle setting, measured as a percentage (float)
MrBedfordVan 0:b9164b348919 77 *
MrBedfordVan 0:b9164b348919 78 * @returns
MrBedfordVan 0:b9164b348919 79 * A floating-point value representing the current duty-cycle being output on the pin,
MrBedfordVan 0:b9164b348919 80 * measured as a percentage. The returned value will lie between
MrBedfordVan 0:b9164b348919 81 * 0.0f (representing on 0%) and 1.0f (representing on 100%).
MrBedfordVan 0:b9164b348919 82 *
MrBedfordVan 0:b9164b348919 83 * @note
MrBedfordVan 0:b9164b348919 84 * This value may not match exactly the value set by a previous <write>.
MrBedfordVan 0:b9164b348919 85 */
MrBedfordVan 0:b9164b348919 86 float read() {
MrBedfordVan 0:b9164b348919 87 return pwmout_read(&_pwm);
MrBedfordVan 0:b9164b348919 88 }
MrBedfordVan 0:b9164b348919 89
MrBedfordVan 0:b9164b348919 90 /** Set the PWM period, specified in seconds (float), keeping the duty cycle the same.
MrBedfordVan 0:b9164b348919 91 *
MrBedfordVan 0:b9164b348919 92 * @note
MrBedfordVan 0:b9164b348919 93 * The resolution is currently in microseconds; periods smaller than this
MrBedfordVan 0:b9164b348919 94 * will be set to zero.
MrBedfordVan 0:b9164b348919 95 */
MrBedfordVan 0:b9164b348919 96 void period(float seconds) {
MrBedfordVan 0:b9164b348919 97 pwmout_period(&_pwm, seconds);
MrBedfordVan 0:b9164b348919 98 }
MrBedfordVan 0:b9164b348919 99
MrBedfordVan 0:b9164b348919 100 /** Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same.
MrBedfordVan 0:b9164b348919 101 */
MrBedfordVan 0:b9164b348919 102 void period_ms(int ms) {
MrBedfordVan 0:b9164b348919 103 pwmout_period_ms(&_pwm, ms);
MrBedfordVan 0:b9164b348919 104 }
MrBedfordVan 0:b9164b348919 105
MrBedfordVan 0:b9164b348919 106 /** Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same.
MrBedfordVan 0:b9164b348919 107 */
MrBedfordVan 0:b9164b348919 108 void period_us(int us) {
MrBedfordVan 0:b9164b348919 109 pwmout_period_us(&_pwm, us);
MrBedfordVan 0:b9164b348919 110 }
MrBedfordVan 0:b9164b348919 111
MrBedfordVan 0:b9164b348919 112 /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.
MrBedfordVan 0:b9164b348919 113 */
MrBedfordVan 0:b9164b348919 114 void pulsewidth(float seconds) {
MrBedfordVan 0:b9164b348919 115 pwmout_pulsewidth(&_pwm, seconds);
MrBedfordVan 0:b9164b348919 116 }
MrBedfordVan 0:b9164b348919 117
MrBedfordVan 0:b9164b348919 118 /** Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same.
MrBedfordVan 0:b9164b348919 119 */
MrBedfordVan 0:b9164b348919 120 void pulsewidth_ms(int ms) {
MrBedfordVan 0:b9164b348919 121 pwmout_pulsewidth_ms(&_pwm, ms);
MrBedfordVan 0:b9164b348919 122 }
MrBedfordVan 0:b9164b348919 123
MrBedfordVan 0:b9164b348919 124 /** Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same.
MrBedfordVan 0:b9164b348919 125 */
MrBedfordVan 0:b9164b348919 126 void pulsewidth_us(int us) {
MrBedfordVan 0:b9164b348919 127 pwmout_pulsewidth_us(&_pwm, us);
MrBedfordVan 0:b9164b348919 128 }
MrBedfordVan 0:b9164b348919 129
MrBedfordVan 0:b9164b348919 130 #ifdef MBED_OPERATORS
MrBedfordVan 0:b9164b348919 131 /** A operator shorthand for write()
MrBedfordVan 0:b9164b348919 132 */
MrBedfordVan 0:b9164b348919 133 PwmOut& operator= (float value) {
MrBedfordVan 0:b9164b348919 134 write(value);
MrBedfordVan 0:b9164b348919 135 return *this;
MrBedfordVan 0:b9164b348919 136 }
MrBedfordVan 0:b9164b348919 137
MrBedfordVan 0:b9164b348919 138 PwmOut& operator= (PwmOut& rhs) {
MrBedfordVan 0:b9164b348919 139 write(rhs.read());
MrBedfordVan 0:b9164b348919 140 return *this;
MrBedfordVan 0:b9164b348919 141 }
MrBedfordVan 0:b9164b348919 142
MrBedfordVan 0:b9164b348919 143 /** An operator shorthand for read()
MrBedfordVan 0:b9164b348919 144 */
MrBedfordVan 0:b9164b348919 145 operator float() {
MrBedfordVan 0:b9164b348919 146 return read();
MrBedfordVan 0:b9164b348919 147 }
MrBedfordVan 0:b9164b348919 148 #endif
MrBedfordVan 0:b9164b348919 149
MrBedfordVan 0:b9164b348919 150 protected:
MrBedfordVan 0:b9164b348919 151 pwmout_t _pwm;
MrBedfordVan 0:b9164b348919 152 };
MrBedfordVan 0:b9164b348919 153
MrBedfordVan 0:b9164b348919 154 } // namespace mbed
MrBedfordVan 0:b9164b348919 155
MrBedfordVan 0:b9164b348919 156 #endif
MrBedfordVan 0:b9164b348919 157
MrBedfordVan 0:b9164b348919 158 #endif