PIR grove sensor that sends the sensed data to Thingspeak.com through the wifi module ESP 8266

Dependencies:   mbed

Committer:
skrawool
Date:
Mon Dec 05 16:39:27 2016 +0000
Revision:
0:3954a906acc2
PIR grove sensor that sends the sensed data to thingspeak through the wifi module ESP 8266

Who changed what in which revision?

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