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_ANALOGOUT_H
skrawool 0:3954a906acc2 17 #define MBED_ANALOGOUT_H
skrawool 0:3954a906acc2 18
skrawool 0:3954a906acc2 19 #include "platform/platform.h"
skrawool 0:3954a906acc2 20
skrawool 0:3954a906acc2 21 #if DEVICE_ANALOGOUT
skrawool 0:3954a906acc2 22
skrawool 0:3954a906acc2 23 #include "hal/analogout_api.h"
skrawool 0:3954a906acc2 24 #include "platform/PlatformMutex.h"
skrawool 0:3954a906acc2 25
skrawool 0:3954a906acc2 26 namespace mbed {
skrawool 0:3954a906acc2 27 /** \addtogroup drivers */
skrawool 0:3954a906acc2 28 /** @{*/
skrawool 0:3954a906acc2 29
skrawool 0:3954a906acc2 30 /** An analog output, used for setting the voltage on a pin
skrawool 0:3954a906acc2 31 *
skrawool 0:3954a906acc2 32 * @Note Synchronization level: Thread safe
skrawool 0:3954a906acc2 33 *
skrawool 0:3954a906acc2 34 * Example:
skrawool 0:3954a906acc2 35 * @code
skrawool 0:3954a906acc2 36 * // Make a sawtooth output
skrawool 0:3954a906acc2 37 *
skrawool 0:3954a906acc2 38 * #include "mbed.h"
skrawool 0:3954a906acc2 39 *
skrawool 0:3954a906acc2 40 * AnalogOut tri(p18);
skrawool 0:3954a906acc2 41 * int main() {
skrawool 0:3954a906acc2 42 * while(1) {
skrawool 0:3954a906acc2 43 * tri = tri + 0.01;
skrawool 0:3954a906acc2 44 * wait_us(1);
skrawool 0:3954a906acc2 45 * if(tri == 1) {
skrawool 0:3954a906acc2 46 * tri = 0;
skrawool 0:3954a906acc2 47 * }
skrawool 0:3954a906acc2 48 * }
skrawool 0:3954a906acc2 49 * }
skrawool 0:3954a906acc2 50 * @endcode
skrawool 0:3954a906acc2 51 */
skrawool 0:3954a906acc2 52 class AnalogOut {
skrawool 0:3954a906acc2 53
skrawool 0:3954a906acc2 54 public:
skrawool 0:3954a906acc2 55
skrawool 0:3954a906acc2 56 /** Create an AnalogOut connected to the specified pin
skrawool 0:3954a906acc2 57 *
skrawool 0:3954a906acc2 58 * @param AnalogOut pin to connect to (18)
skrawool 0:3954a906acc2 59 */
skrawool 0:3954a906acc2 60 AnalogOut(PinName pin) {
skrawool 0:3954a906acc2 61 analogout_init(&_dac, pin);
skrawool 0:3954a906acc2 62 }
skrawool 0:3954a906acc2 63
skrawool 0:3954a906acc2 64 /** Set the output voltage, specified as a percentage (float)
skrawool 0:3954a906acc2 65 *
skrawool 0:3954a906acc2 66 * @param value A floating-point value representing the output voltage,
skrawool 0:3954a906acc2 67 * specified as a percentage. The value should lie between
skrawool 0:3954a906acc2 68 * 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%).
skrawool 0:3954a906acc2 69 * Values outside this range will be saturated to 0.0f or 1.0f.
skrawool 0:3954a906acc2 70 */
skrawool 0:3954a906acc2 71 void write(float value) {
skrawool 0:3954a906acc2 72 lock();
skrawool 0:3954a906acc2 73 analogout_write(&_dac, value);
skrawool 0:3954a906acc2 74 unlock();
skrawool 0:3954a906acc2 75 }
skrawool 0:3954a906acc2 76
skrawool 0:3954a906acc2 77 /** Set the output voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
skrawool 0:3954a906acc2 78 *
skrawool 0:3954a906acc2 79 * @param value 16-bit unsigned short representing the output voltage,
skrawool 0:3954a906acc2 80 * normalised to a 16-bit value (0x0000 = 0v, 0xFFFF = 3.3v)
skrawool 0:3954a906acc2 81 */
skrawool 0:3954a906acc2 82 void write_u16(unsigned short value) {
skrawool 0:3954a906acc2 83 lock();
skrawool 0:3954a906acc2 84 analogout_write_u16(&_dac, value);
skrawool 0:3954a906acc2 85 unlock();
skrawool 0:3954a906acc2 86 }
skrawool 0:3954a906acc2 87
skrawool 0:3954a906acc2 88 /** Return the current output voltage setting, measured as a percentage (float)
skrawool 0:3954a906acc2 89 *
skrawool 0:3954a906acc2 90 * @returns
skrawool 0:3954a906acc2 91 * A floating-point value representing the current voltage being output on the pin,
skrawool 0:3954a906acc2 92 * measured as a percentage. The returned value will lie between
skrawool 0:3954a906acc2 93 * 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%).
skrawool 0:3954a906acc2 94 *
skrawool 0:3954a906acc2 95 * @note
skrawool 0:3954a906acc2 96 * This value may not match exactly the value set by a previous write().
skrawool 0:3954a906acc2 97 */
skrawool 0:3954a906acc2 98 float read() {
skrawool 0:3954a906acc2 99 lock();
skrawool 0:3954a906acc2 100 float ret = analogout_read(&_dac);
skrawool 0:3954a906acc2 101 unlock();
skrawool 0:3954a906acc2 102 return ret;
skrawool 0:3954a906acc2 103 }
skrawool 0:3954a906acc2 104
skrawool 0:3954a906acc2 105 /** An operator shorthand for write()
skrawool 0:3954a906acc2 106 */
skrawool 0:3954a906acc2 107 AnalogOut& operator= (float percent) {
skrawool 0:3954a906acc2 108 // Underlying write call is thread safe
skrawool 0:3954a906acc2 109 write(percent);
skrawool 0:3954a906acc2 110 return *this;
skrawool 0:3954a906acc2 111 }
skrawool 0:3954a906acc2 112
skrawool 0:3954a906acc2 113 AnalogOut& operator= (AnalogOut& rhs) {
skrawool 0:3954a906acc2 114 // Underlying write call is thread safe
skrawool 0:3954a906acc2 115 write(rhs.read());
skrawool 0:3954a906acc2 116 return *this;
skrawool 0:3954a906acc2 117 }
skrawool 0:3954a906acc2 118
skrawool 0:3954a906acc2 119 /** An operator shorthand for read()
skrawool 0:3954a906acc2 120 */
skrawool 0:3954a906acc2 121 operator float() {
skrawool 0:3954a906acc2 122 // Underlying read call is thread safe
skrawool 0:3954a906acc2 123 return read();
skrawool 0:3954a906acc2 124 }
skrawool 0:3954a906acc2 125
skrawool 0:3954a906acc2 126 virtual ~AnalogOut() {
skrawool 0:3954a906acc2 127 // Do nothing
skrawool 0:3954a906acc2 128 }
skrawool 0:3954a906acc2 129
skrawool 0:3954a906acc2 130 protected:
skrawool 0:3954a906acc2 131
skrawool 0:3954a906acc2 132 virtual void lock() {
skrawool 0:3954a906acc2 133 _mutex.lock();
skrawool 0:3954a906acc2 134 }
skrawool 0:3954a906acc2 135
skrawool 0:3954a906acc2 136 virtual void unlock() {
skrawool 0:3954a906acc2 137 _mutex.unlock();
skrawool 0:3954a906acc2 138 }
skrawool 0:3954a906acc2 139
skrawool 0:3954a906acc2 140 dac_t _dac;
skrawool 0:3954a906acc2 141 PlatformMutex _mutex;
skrawool 0:3954a906acc2 142 };
skrawool 0:3954a906acc2 143
skrawool 0:3954a906acc2 144 } // namespace mbed
skrawool 0:3954a906acc2 145
skrawool 0:3954a906acc2 146 #endif
skrawool 0:3954a906acc2 147
skrawool 0:3954a906acc2 148 #endif
skrawool 0:3954a906acc2 149
skrawool 0:3954a906acc2 150 /** @}*/