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_SPISLAVE_H
skrawool 0:3954a906acc2 17 #define MBED_SPISLAVE_H
skrawool 0:3954a906acc2 18
skrawool 0:3954a906acc2 19 #include "platform/platform.h"
skrawool 0:3954a906acc2 20
skrawool 0:3954a906acc2 21 #if DEVICE_SPISLAVE
skrawool 0:3954a906acc2 22
skrawool 0:3954a906acc2 23 #include "hal/spi_api.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 SPI slave, used for communicating with a SPI Master device
skrawool 0:3954a906acc2 30 *
skrawool 0:3954a906acc2 31 * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz
skrawool 0:3954a906acc2 32 *
skrawool 0:3954a906acc2 33 * @Note Synchronization level: Not protected
skrawool 0:3954a906acc2 34 *
skrawool 0:3954a906acc2 35 * Example:
skrawool 0:3954a906acc2 36 * @code
skrawool 0:3954a906acc2 37 * // Reply to a SPI master as slave
skrawool 0:3954a906acc2 38 *
skrawool 0:3954a906acc2 39 * #include "mbed.h"
skrawool 0:3954a906acc2 40 *
skrawool 0:3954a906acc2 41 * SPISlave device(p5, p6, p7, p8); // mosi, miso, sclk, ssel
skrawool 0:3954a906acc2 42 *
skrawool 0:3954a906acc2 43 * int main() {
skrawool 0:3954a906acc2 44 * device.reply(0x00); // Prime SPI with first reply
skrawool 0:3954a906acc2 45 * while(1) {
skrawool 0:3954a906acc2 46 * if(device.receive()) {
skrawool 0:3954a906acc2 47 * int v = device.read(); // Read byte from master
skrawool 0:3954a906acc2 48 * v = (v + 1) % 0x100; // Add one to it, modulo 256
skrawool 0:3954a906acc2 49 * device.reply(v); // Make this the next reply
skrawool 0:3954a906acc2 50 * }
skrawool 0:3954a906acc2 51 * }
skrawool 0:3954a906acc2 52 * }
skrawool 0:3954a906acc2 53 * @endcode
skrawool 0:3954a906acc2 54 */
skrawool 0:3954a906acc2 55 class SPISlave {
skrawool 0:3954a906acc2 56
skrawool 0:3954a906acc2 57 public:
skrawool 0:3954a906acc2 58
skrawool 0:3954a906acc2 59 /** Create a SPI slave connected to the specified pins
skrawool 0:3954a906acc2 60 *
skrawool 0:3954a906acc2 61 * mosi or miso can be specfied as NC if not used
skrawool 0:3954a906acc2 62 *
skrawool 0:3954a906acc2 63 * @param mosi SPI Master Out, Slave In pin
skrawool 0:3954a906acc2 64 * @param miso SPI Master In, Slave Out pin
skrawool 0:3954a906acc2 65 * @param sclk SPI Clock pin
skrawool 0:3954a906acc2 66 * @param ssel SPI chip select pin
skrawool 0:3954a906acc2 67 */
skrawool 0:3954a906acc2 68 SPISlave(PinName mosi, PinName miso, PinName sclk, PinName ssel);
skrawool 0:3954a906acc2 69
skrawool 0:3954a906acc2 70 /** Configure the data transmission format
skrawool 0:3954a906acc2 71 *
skrawool 0:3954a906acc2 72 * @param bits Number of bits per SPI frame (4 - 16)
skrawool 0:3954a906acc2 73 * @param mode Clock polarity and phase mode (0 - 3)
skrawool 0:3954a906acc2 74 *
skrawool 0:3954a906acc2 75 * @code
skrawool 0:3954a906acc2 76 * mode | POL PHA
skrawool 0:3954a906acc2 77 * -----+--------
skrawool 0:3954a906acc2 78 * 0 | 0 0
skrawool 0:3954a906acc2 79 * 1 | 0 1
skrawool 0:3954a906acc2 80 * 2 | 1 0
skrawool 0:3954a906acc2 81 * 3 | 1 1
skrawool 0:3954a906acc2 82 * @endcode
skrawool 0:3954a906acc2 83 */
skrawool 0:3954a906acc2 84 void format(int bits, int mode = 0);
skrawool 0:3954a906acc2 85
skrawool 0:3954a906acc2 86 /** Set the spi bus clock frequency
skrawool 0:3954a906acc2 87 *
skrawool 0:3954a906acc2 88 * @param hz SCLK frequency in hz (default = 1MHz)
skrawool 0:3954a906acc2 89 */
skrawool 0:3954a906acc2 90 void frequency(int hz = 1000000);
skrawool 0:3954a906acc2 91
skrawool 0:3954a906acc2 92 /** Polls the SPI to see if data has been received
skrawool 0:3954a906acc2 93 *
skrawool 0:3954a906acc2 94 * @returns
skrawool 0:3954a906acc2 95 * 0 if no data,
skrawool 0:3954a906acc2 96 * 1 otherwise
skrawool 0:3954a906acc2 97 */
skrawool 0:3954a906acc2 98 int receive(void);
skrawool 0:3954a906acc2 99
skrawool 0:3954a906acc2 100 /** Retrieve data from receive buffer as slave
skrawool 0:3954a906acc2 101 *
skrawool 0:3954a906acc2 102 * @returns
skrawool 0:3954a906acc2 103 * the data in the receive buffer
skrawool 0:3954a906acc2 104 */
skrawool 0:3954a906acc2 105 int read(void);
skrawool 0:3954a906acc2 106
skrawool 0:3954a906acc2 107 /** Fill the transmission buffer with the value to be written out
skrawool 0:3954a906acc2 108 * as slave on the next received message from the master.
skrawool 0:3954a906acc2 109 *
skrawool 0:3954a906acc2 110 * @param value the data to be transmitted next
skrawool 0:3954a906acc2 111 */
skrawool 0:3954a906acc2 112 void reply(int value);
skrawool 0:3954a906acc2 113
skrawool 0:3954a906acc2 114 protected:
skrawool 0:3954a906acc2 115 spi_t _spi;
skrawool 0:3954a906acc2 116
skrawool 0:3954a906acc2 117 int _bits;
skrawool 0:3954a906acc2 118 int _mode;
skrawool 0:3954a906acc2 119 int _hz;
skrawool 0:3954a906acc2 120 };
skrawool 0:3954a906acc2 121
skrawool 0:3954a906acc2 122 } // namespace mbed
skrawool 0:3954a906acc2 123
skrawool 0:3954a906acc2 124 #endif
skrawool 0:3954a906acc2 125
skrawool 0:3954a906acc2 126 #endif
skrawool 0:3954a906acc2 127
skrawool 0:3954a906acc2 128 /** @}*/