Basic fading in and out of leds with light sensor.

Dependencies:   mbed

Committer:
mturner5
Date:
Mon Sep 19 03:24:58 2016 +0000
Revision:
0:cf7af2656659
Basic v1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mturner5 0:cf7af2656659 1 /* mbed Microcontroller Library
mturner5 0:cf7af2656659 2 * Copyright (c) 2006-2013 ARM Limited
mturner5 0:cf7af2656659 3 *
mturner5 0:cf7af2656659 4 * Licensed under the Apache License, Version 2.0 (the "License");
mturner5 0:cf7af2656659 5 * you may not use this file except in compliance with the License.
mturner5 0:cf7af2656659 6 * You may obtain a copy of the License at
mturner5 0:cf7af2656659 7 *
mturner5 0:cf7af2656659 8 * http://www.apache.org/licenses/LICENSE-2.0
mturner5 0:cf7af2656659 9 *
mturner5 0:cf7af2656659 10 * Unless required by applicable law or agreed to in writing, software
mturner5 0:cf7af2656659 11 * distributed under the License is distributed on an "AS IS" BASIS,
mturner5 0:cf7af2656659 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mturner5 0:cf7af2656659 13 * See the License for the specific language governing permissions and
mturner5 0:cf7af2656659 14 * limitations under the License.
mturner5 0:cf7af2656659 15 */
mturner5 0:cf7af2656659 16 #ifndef MBED_PWMOUT_H
mturner5 0:cf7af2656659 17 #define MBED_PWMOUT_H
mturner5 0:cf7af2656659 18
mturner5 0:cf7af2656659 19 #include "platform.h"
mturner5 0:cf7af2656659 20
mturner5 0:cf7af2656659 21 #if DEVICE_PWMOUT
mturner5 0:cf7af2656659 22 #include "pwmout_api.h"
mturner5 0:cf7af2656659 23
mturner5 0:cf7af2656659 24 namespace mbed {
mturner5 0:cf7af2656659 25
mturner5 0:cf7af2656659 26 /** A pulse-width modulation digital output
mturner5 0:cf7af2656659 27 *
mturner5 0:cf7af2656659 28 * Example
mturner5 0:cf7af2656659 29 * @code
mturner5 0:cf7af2656659 30 * // Fade a led on.
mturner5 0:cf7af2656659 31 * #include "mbed.h"
mturner5 0:cf7af2656659 32 *
mturner5 0:cf7af2656659 33 * PwmOut led(LED1);
mturner5 0:cf7af2656659 34 *
mturner5 0:cf7af2656659 35 * int main() {
mturner5 0:cf7af2656659 36 * while(1) {
mturner5 0:cf7af2656659 37 * led = led + 0.01;
mturner5 0:cf7af2656659 38 * wait(0.2);
mturner5 0:cf7af2656659 39 * if(led == 1.0) {
mturner5 0:cf7af2656659 40 * led = 0;
mturner5 0:cf7af2656659 41 * }
mturner5 0:cf7af2656659 42 * }
mturner5 0:cf7af2656659 43 * }
mturner5 0:cf7af2656659 44 * @endcode
mturner5 0:cf7af2656659 45 *
mturner5 0:cf7af2656659 46 * @note
mturner5 0:cf7af2656659 47 * On the LPC1768 and LPC2368, the PWMs all share the same
mturner5 0:cf7af2656659 48 * period - if you change the period for one, you change it for all.
mturner5 0:cf7af2656659 49 * Although routines that change the period maintain the duty cycle
mturner5 0:cf7af2656659 50 * for its PWM, all other PWMs will require their duty cycle to be
mturner5 0:cf7af2656659 51 * refreshed.
mturner5 0:cf7af2656659 52 */
mturner5 0:cf7af2656659 53 class PwmOut {
mturner5 0:cf7af2656659 54
mturner5 0:cf7af2656659 55 public:
mturner5 0:cf7af2656659 56
mturner5 0:cf7af2656659 57 /** Create a PwmOut connected to the specified pin
mturner5 0:cf7af2656659 58 *
mturner5 0:cf7af2656659 59 * @param pin PwmOut pin to connect to
mturner5 0:cf7af2656659 60 */
mturner5 0:cf7af2656659 61 PwmOut(PinName pin) {
mturner5 0:cf7af2656659 62 pwmout_init(&_pwm, pin);
mturner5 0:cf7af2656659 63 }
mturner5 0:cf7af2656659 64
mturner5 0:cf7af2656659 65 /** Set the ouput duty-cycle, specified as a percentage (float)
mturner5 0:cf7af2656659 66 *
mturner5 0:cf7af2656659 67 * @param value A floating-point value representing the output duty-cycle,
mturner5 0:cf7af2656659 68 * specified as a percentage. The value should lie between
mturner5 0:cf7af2656659 69 * 0.0f (representing on 0%) and 1.0f (representing on 100%).
mturner5 0:cf7af2656659 70 * Values outside this range will be saturated to 0.0f or 1.0f.
mturner5 0:cf7af2656659 71 */
mturner5 0:cf7af2656659 72 void write(float value) {
mturner5 0:cf7af2656659 73 pwmout_write(&_pwm, value);
mturner5 0:cf7af2656659 74 }
mturner5 0:cf7af2656659 75
mturner5 0:cf7af2656659 76 /** Return the current output duty-cycle setting, measured as a percentage (float)
mturner5 0:cf7af2656659 77 *
mturner5 0:cf7af2656659 78 * @returns
mturner5 0:cf7af2656659 79 * A floating-point value representing the current duty-cycle being output on the pin,
mturner5 0:cf7af2656659 80 * measured as a percentage. The returned value will lie between
mturner5 0:cf7af2656659 81 * 0.0f (representing on 0%) and 1.0f (representing on 100%).
mturner5 0:cf7af2656659 82 *
mturner5 0:cf7af2656659 83 * @note
mturner5 0:cf7af2656659 84 * This value may not match exactly the value set by a previous <write>.
mturner5 0:cf7af2656659 85 */
mturner5 0:cf7af2656659 86 float read() {
mturner5 0:cf7af2656659 87 return pwmout_read(&_pwm);
mturner5 0:cf7af2656659 88 }
mturner5 0:cf7af2656659 89
mturner5 0:cf7af2656659 90 /** Set the PWM period, specified in seconds (float), keeping the duty cycle the same.
mturner5 0:cf7af2656659 91 *
mturner5 0:cf7af2656659 92 * @note
mturner5 0:cf7af2656659 93 * The resolution is currently in microseconds; periods smaller than this
mturner5 0:cf7af2656659 94 * will be set to zero.
mturner5 0:cf7af2656659 95 */
mturner5 0:cf7af2656659 96 void period(float seconds) {
mturner5 0:cf7af2656659 97 pwmout_period(&_pwm, seconds);
mturner5 0:cf7af2656659 98 }
mturner5 0:cf7af2656659 99
mturner5 0:cf7af2656659 100 /** Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same.
mturner5 0:cf7af2656659 101 */
mturner5 0:cf7af2656659 102 void period_ms(int ms) {
mturner5 0:cf7af2656659 103 pwmout_period_ms(&_pwm, ms);
mturner5 0:cf7af2656659 104 }
mturner5 0:cf7af2656659 105
mturner5 0:cf7af2656659 106 /** Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same.
mturner5 0:cf7af2656659 107 */
mturner5 0:cf7af2656659 108 void period_us(int us) {
mturner5 0:cf7af2656659 109 pwmout_period_us(&_pwm, us);
mturner5 0:cf7af2656659 110 }
mturner5 0:cf7af2656659 111
mturner5 0:cf7af2656659 112 /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.
mturner5 0:cf7af2656659 113 */
mturner5 0:cf7af2656659 114 void pulsewidth(float seconds) {
mturner5 0:cf7af2656659 115 pwmout_pulsewidth(&_pwm, seconds);
mturner5 0:cf7af2656659 116 }
mturner5 0:cf7af2656659 117
mturner5 0:cf7af2656659 118 /** Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same.
mturner5 0:cf7af2656659 119 */
mturner5 0:cf7af2656659 120 void pulsewidth_ms(int ms) {
mturner5 0:cf7af2656659 121 pwmout_pulsewidth_ms(&_pwm, ms);
mturner5 0:cf7af2656659 122 }
mturner5 0:cf7af2656659 123
mturner5 0:cf7af2656659 124 /** Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same.
mturner5 0:cf7af2656659 125 */
mturner5 0:cf7af2656659 126 void pulsewidth_us(int us) {
mturner5 0:cf7af2656659 127 pwmout_pulsewidth_us(&_pwm, us);
mturner5 0:cf7af2656659 128 }
mturner5 0:cf7af2656659 129
mturner5 0:cf7af2656659 130 #ifdef MBED_OPERATORS
mturner5 0:cf7af2656659 131 /** A operator shorthand for write()
mturner5 0:cf7af2656659 132 */
mturner5 0:cf7af2656659 133 PwmOut& operator= (float value) {
mturner5 0:cf7af2656659 134 write(value);
mturner5 0:cf7af2656659 135 return *this;
mturner5 0:cf7af2656659 136 }
mturner5 0:cf7af2656659 137
mturner5 0:cf7af2656659 138 PwmOut& operator= (PwmOut& rhs) {
mturner5 0:cf7af2656659 139 write(rhs.read());
mturner5 0:cf7af2656659 140 return *this;
mturner5 0:cf7af2656659 141 }
mturner5 0:cf7af2656659 142
mturner5 0:cf7af2656659 143 /** An operator shorthand for read()
mturner5 0:cf7af2656659 144 */
mturner5 0:cf7af2656659 145 operator float() {
mturner5 0:cf7af2656659 146 return read();
mturner5 0:cf7af2656659 147 }
mturner5 0:cf7af2656659 148 #endif
mturner5 0:cf7af2656659 149
mturner5 0:cf7af2656659 150 protected:
mturner5 0:cf7af2656659 151 pwmout_t _pwm;
mturner5 0:cf7af2656659 152 };
mturner5 0:cf7af2656659 153
mturner5 0:cf7af2656659 154 } // namespace mbed
mturner5 0:cf7af2656659 155
mturner5 0:cf7af2656659 156 #endif
mturner5 0:cf7af2656659 157
mturner5 0:cf7af2656659 158 #endif