IoT Home Alarm System

Dependents:   IoTBurglar_and_Fire_AlarmSystem

Committer:
kbrahmbhatt6
Date:
Fri Apr 29 06:59:59 2016 +0000
Revision:
0:2f388b030837
1

Who changed what in which revision?

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