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