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_DIGITALOUT_H
skrawool 0:3954a906acc2 17 #define MBED_DIGITALOUT_H
skrawool 0:3954a906acc2 18
skrawool 0:3954a906acc2 19 #include "platform/platform.h"
skrawool 0:3954a906acc2 20 #include "hal/gpio_api.h"
skrawool 0:3954a906acc2 21 #include "platform/critical.h"
skrawool 0:3954a906acc2 22
skrawool 0:3954a906acc2 23 namespace mbed {
skrawool 0:3954a906acc2 24 /** \addtogroup drivers */
skrawool 0:3954a906acc2 25 /** @{*/
skrawool 0:3954a906acc2 26
skrawool 0:3954a906acc2 27 /** A digital output, used for setting the state of a pin
skrawool 0:3954a906acc2 28 *
skrawool 0:3954a906acc2 29 * @Note Synchronization level: Interrupt safe
skrawool 0:3954a906acc2 30 *
skrawool 0:3954a906acc2 31 * Example:
skrawool 0:3954a906acc2 32 * @code
skrawool 0:3954a906acc2 33 * // Toggle a LED
skrawool 0:3954a906acc2 34 * #include "mbed.h"
skrawool 0:3954a906acc2 35 *
skrawool 0:3954a906acc2 36 * DigitalOut led(LED1);
skrawool 0:3954a906acc2 37 *
skrawool 0:3954a906acc2 38 * int main() {
skrawool 0:3954a906acc2 39 * while(1) {
skrawool 0:3954a906acc2 40 * led = !led;
skrawool 0:3954a906acc2 41 * wait(0.2);
skrawool 0:3954a906acc2 42 * }
skrawool 0:3954a906acc2 43 * }
skrawool 0:3954a906acc2 44 * @endcode
skrawool 0:3954a906acc2 45 */
skrawool 0:3954a906acc2 46 class DigitalOut {
skrawool 0:3954a906acc2 47
skrawool 0:3954a906acc2 48 public:
skrawool 0:3954a906acc2 49 /** Create a DigitalOut connected to the specified pin
skrawool 0:3954a906acc2 50 *
skrawool 0:3954a906acc2 51 * @param pin DigitalOut pin to connect to
skrawool 0:3954a906acc2 52 */
skrawool 0:3954a906acc2 53 DigitalOut(PinName pin) : gpio() {
skrawool 0:3954a906acc2 54 // No lock needed in the constructor
skrawool 0:3954a906acc2 55 gpio_init_out(&gpio, pin);
skrawool 0:3954a906acc2 56 }
skrawool 0:3954a906acc2 57
skrawool 0:3954a906acc2 58 /** Create a DigitalOut connected to the specified pin
skrawool 0:3954a906acc2 59 *
skrawool 0:3954a906acc2 60 * @param pin DigitalOut pin to connect to
skrawool 0:3954a906acc2 61 * @param value the initial pin value
skrawool 0:3954a906acc2 62 */
skrawool 0:3954a906acc2 63 DigitalOut(PinName pin, int value) : gpio() {
skrawool 0:3954a906acc2 64 // No lock needed in the constructor
skrawool 0:3954a906acc2 65 gpio_init_out_ex(&gpio, pin, value);
skrawool 0:3954a906acc2 66 }
skrawool 0:3954a906acc2 67
skrawool 0:3954a906acc2 68 /** Set the output, specified as 0 or 1 (int)
skrawool 0:3954a906acc2 69 *
skrawool 0:3954a906acc2 70 * @param value An integer specifying the pin output value,
skrawool 0:3954a906acc2 71 * 0 for logical 0, 1 (or any other non-zero value) for logical 1
skrawool 0:3954a906acc2 72 */
skrawool 0:3954a906acc2 73 void write(int value) {
skrawool 0:3954a906acc2 74 // Thread safe / atomic HAL call
skrawool 0:3954a906acc2 75 gpio_write(&gpio, value);
skrawool 0:3954a906acc2 76 }
skrawool 0:3954a906acc2 77
skrawool 0:3954a906acc2 78 /** Return the output setting, represented as 0 or 1 (int)
skrawool 0:3954a906acc2 79 *
skrawool 0:3954a906acc2 80 * @returns
skrawool 0:3954a906acc2 81 * an integer representing the output setting of the pin,
skrawool 0:3954a906acc2 82 * 0 for logical 0, 1 for logical 1
skrawool 0:3954a906acc2 83 */
skrawool 0:3954a906acc2 84 int read() {
skrawool 0:3954a906acc2 85 // Thread safe / atomic HAL call
skrawool 0:3954a906acc2 86 return gpio_read(&gpio);
skrawool 0:3954a906acc2 87 }
skrawool 0:3954a906acc2 88
skrawool 0:3954a906acc2 89 /** Return the output setting, represented as 0 or 1 (int)
skrawool 0:3954a906acc2 90 *
skrawool 0:3954a906acc2 91 * @returns
skrawool 0:3954a906acc2 92 * Non zero value if pin is connected to uc GPIO
skrawool 0:3954a906acc2 93 * 0 if gpio object was initialized with NC
skrawool 0:3954a906acc2 94 */
skrawool 0:3954a906acc2 95 int is_connected() {
skrawool 0:3954a906acc2 96 // Thread safe / atomic HAL call
skrawool 0:3954a906acc2 97 return gpio_is_connected(&gpio);
skrawool 0:3954a906acc2 98 }
skrawool 0:3954a906acc2 99
skrawool 0:3954a906acc2 100 /** A shorthand for write()
skrawool 0:3954a906acc2 101 */
skrawool 0:3954a906acc2 102 DigitalOut& operator= (int value) {
skrawool 0:3954a906acc2 103 // Underlying write is thread safe
skrawool 0:3954a906acc2 104 write(value);
skrawool 0:3954a906acc2 105 return *this;
skrawool 0:3954a906acc2 106 }
skrawool 0:3954a906acc2 107
skrawool 0:3954a906acc2 108 DigitalOut& operator= (DigitalOut& rhs) {
skrawool 0:3954a906acc2 109 core_util_critical_section_enter();
skrawool 0:3954a906acc2 110 write(rhs.read());
skrawool 0:3954a906acc2 111 core_util_critical_section_exit();
skrawool 0:3954a906acc2 112 return *this;
skrawool 0:3954a906acc2 113 }
skrawool 0:3954a906acc2 114
skrawool 0:3954a906acc2 115 /** A shorthand for read()
skrawool 0:3954a906acc2 116 */
skrawool 0:3954a906acc2 117 operator int() {
skrawool 0:3954a906acc2 118 // Underlying call is thread safe
skrawool 0:3954a906acc2 119 return read();
skrawool 0:3954a906acc2 120 }
skrawool 0:3954a906acc2 121
skrawool 0:3954a906acc2 122 protected:
skrawool 0:3954a906acc2 123 gpio_t gpio;
skrawool 0:3954a906acc2 124 };
skrawool 0:3954a906acc2 125
skrawool 0:3954a906acc2 126 } // namespace mbed
skrawool 0:3954a906acc2 127
skrawool 0:3954a906acc2 128 #endif
skrawool 0:3954a906acc2 129
skrawool 0:3954a906acc2 130 /** @}*/