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