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_CAN_H
skrawool 0:3954a906acc2 17 #define MBED_CAN_H
skrawool 0:3954a906acc2 18
skrawool 0:3954a906acc2 19 #include "platform/platform.h"
skrawool 0:3954a906acc2 20
skrawool 0:3954a906acc2 21 #if DEVICE_CAN
skrawool 0:3954a906acc2 22
skrawool 0:3954a906acc2 23 #include "hal/can_api.h"
skrawool 0:3954a906acc2 24 #include "platform/Callback.h"
skrawool 0:3954a906acc2 25 #include "platform/PlatformMutex.h"
skrawool 0:3954a906acc2 26
skrawool 0:3954a906acc2 27 namespace mbed {
skrawool 0:3954a906acc2 28 /** \addtogroup drivers */
skrawool 0:3954a906acc2 29 /** @{*/
skrawool 0:3954a906acc2 30
skrawool 0:3954a906acc2 31 /** CANMessage class
skrawool 0:3954a906acc2 32 *
skrawool 0:3954a906acc2 33 * @Note Synchronization level: Thread safe
skrawool 0:3954a906acc2 34 */
skrawool 0:3954a906acc2 35 class CANMessage : public CAN_Message {
skrawool 0:3954a906acc2 36
skrawool 0:3954a906acc2 37 public:
skrawool 0:3954a906acc2 38 /** Creates empty CAN message.
skrawool 0:3954a906acc2 39 */
skrawool 0:3954a906acc2 40 CANMessage() : CAN_Message() {
skrawool 0:3954a906acc2 41 len = 8;
skrawool 0:3954a906acc2 42 type = CANData;
skrawool 0:3954a906acc2 43 format = CANStandard;
skrawool 0:3954a906acc2 44 id = 0;
skrawool 0:3954a906acc2 45 memset(data, 0, 8);
skrawool 0:3954a906acc2 46 }
skrawool 0:3954a906acc2 47
skrawool 0:3954a906acc2 48 /** Creates CAN message with specific content.
skrawool 0:3954a906acc2 49 */
skrawool 0:3954a906acc2 50 CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) {
skrawool 0:3954a906acc2 51 len = _len & 0xF;
skrawool 0:3954a906acc2 52 type = _type;
skrawool 0:3954a906acc2 53 format = _format;
skrawool 0:3954a906acc2 54 id = _id;
skrawool 0:3954a906acc2 55 memcpy(data, _data, _len);
skrawool 0:3954a906acc2 56 }
skrawool 0:3954a906acc2 57
skrawool 0:3954a906acc2 58 /** Creates CAN remote message.
skrawool 0:3954a906acc2 59 */
skrawool 0:3954a906acc2 60 CANMessage(int _id, CANFormat _format = CANStandard) {
skrawool 0:3954a906acc2 61 len = 0;
skrawool 0:3954a906acc2 62 type = CANRemote;
skrawool 0:3954a906acc2 63 format = _format;
skrawool 0:3954a906acc2 64 id = _id;
skrawool 0:3954a906acc2 65 memset(data, 0, 8);
skrawool 0:3954a906acc2 66 }
skrawool 0:3954a906acc2 67 };
skrawool 0:3954a906acc2 68
skrawool 0:3954a906acc2 69 /** A can bus client, used for communicating with can devices
skrawool 0:3954a906acc2 70 */
skrawool 0:3954a906acc2 71 class CAN {
skrawool 0:3954a906acc2 72
skrawool 0:3954a906acc2 73 public:
skrawool 0:3954a906acc2 74 /** Creates an CAN interface connected to specific pins.
skrawool 0:3954a906acc2 75 *
skrawool 0:3954a906acc2 76 * @param rd read from transmitter
skrawool 0:3954a906acc2 77 * @param td transmit to transmitter
skrawool 0:3954a906acc2 78 *
skrawool 0:3954a906acc2 79 * Example:
skrawool 0:3954a906acc2 80 * @code
skrawool 0:3954a906acc2 81 * #include "mbed.h"
skrawool 0:3954a906acc2 82 *
skrawool 0:3954a906acc2 83 * Ticker ticker;
skrawool 0:3954a906acc2 84 * DigitalOut led1(LED1);
skrawool 0:3954a906acc2 85 * DigitalOut led2(LED2);
skrawool 0:3954a906acc2 86 * CAN can1(p9, p10);
skrawool 0:3954a906acc2 87 * CAN can2(p30, p29);
skrawool 0:3954a906acc2 88 *
skrawool 0:3954a906acc2 89 * char counter = 0;
skrawool 0:3954a906acc2 90 *
skrawool 0:3954a906acc2 91 * void send() {
skrawool 0:3954a906acc2 92 * if(can1.write(CANMessage(1337, &counter, 1))) {
skrawool 0:3954a906acc2 93 * printf("Message sent: %d\n", counter);
skrawool 0:3954a906acc2 94 * counter++;
skrawool 0:3954a906acc2 95 * }
skrawool 0:3954a906acc2 96 * led1 = !led1;
skrawool 0:3954a906acc2 97 * }
skrawool 0:3954a906acc2 98 *
skrawool 0:3954a906acc2 99 * int main() {
skrawool 0:3954a906acc2 100 * ticker.attach(&send, 1);
skrawool 0:3954a906acc2 101 * CANMessage msg;
skrawool 0:3954a906acc2 102 * while(1) {
skrawool 0:3954a906acc2 103 * if(can2.read(msg)) {
skrawool 0:3954a906acc2 104 * printf("Message received: %d\n\n", msg.data[0]);
skrawool 0:3954a906acc2 105 * led2 = !led2;
skrawool 0:3954a906acc2 106 * }
skrawool 0:3954a906acc2 107 * wait(0.2);
skrawool 0:3954a906acc2 108 * }
skrawool 0:3954a906acc2 109 * }
skrawool 0:3954a906acc2 110 * @endcode
skrawool 0:3954a906acc2 111 */
skrawool 0:3954a906acc2 112 CAN(PinName rd, PinName td);
skrawool 0:3954a906acc2 113 virtual ~CAN();
skrawool 0:3954a906acc2 114
skrawool 0:3954a906acc2 115 /** Set the frequency of the CAN interface
skrawool 0:3954a906acc2 116 *
skrawool 0:3954a906acc2 117 * @param hz The bus frequency in hertz
skrawool 0:3954a906acc2 118 *
skrawool 0:3954a906acc2 119 * @returns
skrawool 0:3954a906acc2 120 * 1 if successful,
skrawool 0:3954a906acc2 121 * 0 otherwise
skrawool 0:3954a906acc2 122 */
skrawool 0:3954a906acc2 123 int frequency(int hz);
skrawool 0:3954a906acc2 124
skrawool 0:3954a906acc2 125 /** Write a CANMessage to the bus.
skrawool 0:3954a906acc2 126 *
skrawool 0:3954a906acc2 127 * @param msg The CANMessage to write.
skrawool 0:3954a906acc2 128 *
skrawool 0:3954a906acc2 129 * @returns
skrawool 0:3954a906acc2 130 * 0 if write failed,
skrawool 0:3954a906acc2 131 * 1 if write was successful
skrawool 0:3954a906acc2 132 */
skrawool 0:3954a906acc2 133 int write(CANMessage msg);
skrawool 0:3954a906acc2 134
skrawool 0:3954a906acc2 135 /** Read a CANMessage from the bus.
skrawool 0:3954a906acc2 136 *
skrawool 0:3954a906acc2 137 * @param msg A CANMessage to read to.
skrawool 0:3954a906acc2 138 * @param handle message filter handle (0 for any message)
skrawool 0:3954a906acc2 139 *
skrawool 0:3954a906acc2 140 * @returns
skrawool 0:3954a906acc2 141 * 0 if no message arrived,
skrawool 0:3954a906acc2 142 * 1 if message arrived
skrawool 0:3954a906acc2 143 */
skrawool 0:3954a906acc2 144 int read(CANMessage &msg, int handle = 0);
skrawool 0:3954a906acc2 145
skrawool 0:3954a906acc2 146 /** Reset CAN interface.
skrawool 0:3954a906acc2 147 *
skrawool 0:3954a906acc2 148 * To use after error overflow.
skrawool 0:3954a906acc2 149 */
skrawool 0:3954a906acc2 150 void reset();
skrawool 0:3954a906acc2 151
skrawool 0:3954a906acc2 152 /** Puts or removes the CAN interface into silent monitoring mode
skrawool 0:3954a906acc2 153 *
skrawool 0:3954a906acc2 154 * @param silent boolean indicating whether to go into silent mode or not
skrawool 0:3954a906acc2 155 */
skrawool 0:3954a906acc2 156 void monitor(bool silent);
skrawool 0:3954a906acc2 157
skrawool 0:3954a906acc2 158 enum Mode {
skrawool 0:3954a906acc2 159 Reset = 0,
skrawool 0:3954a906acc2 160 Normal,
skrawool 0:3954a906acc2 161 Silent,
skrawool 0:3954a906acc2 162 LocalTest,
skrawool 0:3954a906acc2 163 GlobalTest,
skrawool 0:3954a906acc2 164 SilentTest
skrawool 0:3954a906acc2 165 };
skrawool 0:3954a906acc2 166
skrawool 0:3954a906acc2 167 /** Change CAN operation to the specified mode
skrawool 0:3954a906acc2 168 *
skrawool 0:3954a906acc2 169 * @param mode The new operation mode (CAN::Normal, CAN::Silent, CAN::LocalTest, CAN::GlobalTest, CAN::SilentTest)
skrawool 0:3954a906acc2 170 *
skrawool 0:3954a906acc2 171 * @returns
skrawool 0:3954a906acc2 172 * 0 if mode change failed or unsupported,
skrawool 0:3954a906acc2 173 * 1 if mode change was successful
skrawool 0:3954a906acc2 174 */
skrawool 0:3954a906acc2 175 int mode(Mode mode);
skrawool 0:3954a906acc2 176
skrawool 0:3954a906acc2 177 /** Filter out incomming messages
skrawool 0:3954a906acc2 178 *
skrawool 0:3954a906acc2 179 * @param id the id to filter on
skrawool 0:3954a906acc2 180 * @param mask the mask applied to the id
skrawool 0:3954a906acc2 181 * @param format format to filter on (Default CANAny)
skrawool 0:3954a906acc2 182 * @param handle message filter handle (Optional)
skrawool 0:3954a906acc2 183 *
skrawool 0:3954a906acc2 184 * @returns
skrawool 0:3954a906acc2 185 * 0 if filter change failed or unsupported,
skrawool 0:3954a906acc2 186 * new filter handle if successful
skrawool 0:3954a906acc2 187 */
skrawool 0:3954a906acc2 188 int filter(unsigned int id, unsigned int mask, CANFormat format = CANAny, int handle = 0);
skrawool 0:3954a906acc2 189
skrawool 0:3954a906acc2 190 /** Returns number of read errors to detect read overflow errors.
skrawool 0:3954a906acc2 191 */
skrawool 0:3954a906acc2 192 unsigned char rderror();
skrawool 0:3954a906acc2 193
skrawool 0:3954a906acc2 194 /** Returns number of write errors to detect write overflow errors.
skrawool 0:3954a906acc2 195 */
skrawool 0:3954a906acc2 196 unsigned char tderror();
skrawool 0:3954a906acc2 197
skrawool 0:3954a906acc2 198 enum IrqType {
skrawool 0:3954a906acc2 199 RxIrq = 0,
skrawool 0:3954a906acc2 200 TxIrq,
skrawool 0:3954a906acc2 201 EwIrq,
skrawool 0:3954a906acc2 202 DoIrq,
skrawool 0:3954a906acc2 203 WuIrq,
skrawool 0:3954a906acc2 204 EpIrq,
skrawool 0:3954a906acc2 205 AlIrq,
skrawool 0:3954a906acc2 206 BeIrq,
skrawool 0:3954a906acc2 207 IdIrq,
skrawool 0:3954a906acc2 208
skrawool 0:3954a906acc2 209 IrqCnt
skrawool 0:3954a906acc2 210 };
skrawool 0:3954a906acc2 211
skrawool 0:3954a906acc2 212 /** Attach a function to call whenever a CAN frame received interrupt is
skrawool 0:3954a906acc2 213 * generated.
skrawool 0:3954a906acc2 214 *
skrawool 0:3954a906acc2 215 * @param func A pointer to a void function, or 0 to set as none
skrawool 0:3954a906acc2 216 * @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, CAN::TxIrq for transmitted or aborted, CAN::EwIrq for error warning, CAN::DoIrq for data overrun, CAN::WuIrq for wake-up, CAN::EpIrq for error passive, CAN::AlIrq for arbitration lost, CAN::BeIrq for bus error)
skrawool 0:3954a906acc2 217 */
skrawool 0:3954a906acc2 218 void attach(Callback<void()> func, IrqType type=RxIrq);
skrawool 0:3954a906acc2 219
skrawool 0:3954a906acc2 220 /** Attach a member function to call whenever a CAN frame received interrupt
skrawool 0:3954a906acc2 221 * is generated.
skrawool 0:3954a906acc2 222 *
skrawool 0:3954a906acc2 223 * @param obj pointer to the object to call the member function on
skrawool 0:3954a906acc2 224 * @param method pointer to the member function to be called
skrawool 0:3954a906acc2 225 * @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, TxIrq for transmitted or aborted, EwIrq for error warning, DoIrq for data overrun, WuIrq for wake-up, EpIrq for error passive, AlIrq for arbitration lost, BeIrq for bus error)
skrawool 0:3954a906acc2 226 */
skrawool 0:3954a906acc2 227 template<typename T>
skrawool 0:3954a906acc2 228 void attach(T* obj, void (T::*method)(), IrqType type=RxIrq) {
skrawool 0:3954a906acc2 229 // Underlying call thread safe
skrawool 0:3954a906acc2 230 attach(Callback<void()>(obj, method), type);
skrawool 0:3954a906acc2 231 }
skrawool 0:3954a906acc2 232
skrawool 0:3954a906acc2 233 /** Attach a member function to call whenever a CAN frame received interrupt
skrawool 0:3954a906acc2 234 * is generated.
skrawool 0:3954a906acc2 235 *
skrawool 0:3954a906acc2 236 * @param obj pointer to the object to call the member function on
skrawool 0:3954a906acc2 237 * @param method pointer to the member function to be called
skrawool 0:3954a906acc2 238 * @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, TxIrq for transmitted or aborted, EwIrq for error warning, DoIrq for data overrun, WuIrq for wake-up, EpIrq for error passive, AlIrq for arbitration lost, BeIrq for bus error)
skrawool 0:3954a906acc2 239 */
skrawool 0:3954a906acc2 240 template<typename T>
skrawool 0:3954a906acc2 241 void attach(T* obj, void (*method)(T*), IrqType type=RxIrq) {
skrawool 0:3954a906acc2 242 // Underlying call thread safe
skrawool 0:3954a906acc2 243 attach(Callback<void()>(obj, method), type);
skrawool 0:3954a906acc2 244 }
skrawool 0:3954a906acc2 245
skrawool 0:3954a906acc2 246 static void _irq_handler(uint32_t id, CanIrqType type);
skrawool 0:3954a906acc2 247
skrawool 0:3954a906acc2 248 protected:
skrawool 0:3954a906acc2 249 virtual void lock();
skrawool 0:3954a906acc2 250 virtual void unlock();
skrawool 0:3954a906acc2 251 can_t _can;
skrawool 0:3954a906acc2 252 Callback<void()> _irq[IrqCnt];
skrawool 0:3954a906acc2 253 PlatformMutex _mutex;
skrawool 0:3954a906acc2 254 };
skrawool 0:3954a906acc2 255
skrawool 0:3954a906acc2 256 } // namespace mbed
skrawool 0:3954a906acc2 257
skrawool 0:3954a906acc2 258 #endif
skrawool 0:3954a906acc2 259
skrawool 0:3954a906acc2 260 #endif // MBED_CAN_H
skrawool 0:3954a906acc2 261
skrawool 0:3954a906acc2 262 /** @}*/