To get and handle event on getting motion event from PIR

Dependencies:   BLE_API mbed nRF51822

Committer:
abhishekkumardwivedi
Date:
Wed May 20 12:06:18 2015 +0000
Revision:
0:c165b9e652d8
To control device on getting motion event from PIR.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
abhishekkumardwivedi 0:c165b9e652d8 1 /* mbed Microcontroller Library
abhishekkumardwivedi 0:c165b9e652d8 2 * Copyright (c) 2006-2013 ARM Limited
abhishekkumardwivedi 0:c165b9e652d8 3 *
abhishekkumardwivedi 0:c165b9e652d8 4 * Licensed under the Apache License, Version 2.0 (the "License");
abhishekkumardwivedi 0:c165b9e652d8 5 * you may not use this file except in compliance with the License.
abhishekkumardwivedi 0:c165b9e652d8 6 * You may obtain a copy of the License at
abhishekkumardwivedi 0:c165b9e652d8 7 *
abhishekkumardwivedi 0:c165b9e652d8 8 * http://www.apache.org/licenses/LICENSE-2.0
abhishekkumardwivedi 0:c165b9e652d8 9 *
abhishekkumardwivedi 0:c165b9e652d8 10 * Unless required by applicable law or agreed to in writing, software
abhishekkumardwivedi 0:c165b9e652d8 11 * distributed under the License is distributed on an "AS IS" BASIS,
abhishekkumardwivedi 0:c165b9e652d8 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
abhishekkumardwivedi 0:c165b9e652d8 13 * See the License for the specific language governing permissions and
abhishekkumardwivedi 0:c165b9e652d8 14 * limitations under the License.
abhishekkumardwivedi 0:c165b9e652d8 15 */
abhishekkumardwivedi 0:c165b9e652d8 16
abhishekkumardwivedi 0:c165b9e652d8 17 #include "mbed.h"
abhishekkumardwivedi 0:c165b9e652d8 18 #include "BLEDevice.h"
abhishekkumardwivedi 0:c165b9e652d8 19 #include "PIRService.h"
abhishekkumardwivedi 0:c165b9e652d8 20
abhishekkumardwivedi 0:c165b9e652d8 21 BLEDevice ble;
abhishekkumardwivedi 0:c165b9e652d8 22
abhishekkumardwivedi 0:c165b9e652d8 23 InterruptIn pir(p4);
abhishekkumardwivedi 0:c165b9e652d8 24
abhishekkumardwivedi 0:c165b9e652d8 25 bool motion_detected = 0;
abhishekkumardwivedi 0:c165b9e652d8 26 int interval = 0;
abhishekkumardwivedi 0:c165b9e652d8 27 static const int PUBLISH_TIMESLISE = 10; //once detected, publish for 10 sec, important data so keep broadcasting for 10 sec.
abhishekkumardwivedi 0:c165b9e652d8 28
abhishekkumardwivedi 0:c165b9e652d8 29
abhishekkumardwivedi 0:c165b9e652d8 30 const static char DEVICE_NAME[] = "PIR";
abhishekkumardwivedi 0:c165b9e652d8 31 static const uint16_t uuid16_list[] = {PIRService::PIR_SERVICE_UUID};
abhishekkumardwivedi 0:c165b9e652d8 32
abhishekkumardwivedi 0:c165b9e652d8 33 //PIRService *pirServicePtr;
abhishekkumardwivedi 0:c165b9e652d8 34
abhishekkumardwivedi 0:c165b9e652d8 35 void irq_handler(void)
abhishekkumardwivedi 0:c165b9e652d8 36 {
abhishekkumardwivedi 0:c165b9e652d8 37 motion_detected = 1;
abhishekkumardwivedi 0:c165b9e652d8 38 interval = PUBLISH_TIMESLISE;
abhishekkumardwivedi 0:c165b9e652d8 39 }
abhishekkumardwivedi 0:c165b9e652d8 40
abhishekkumardwivedi 0:c165b9e652d8 41 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
abhishekkumardwivedi 0:c165b9e652d8 42 {
abhishekkumardwivedi 0:c165b9e652d8 43 ble.startAdvertising();
abhishekkumardwivedi 0:c165b9e652d8 44 }
abhishekkumardwivedi 0:c165b9e652d8 45
abhishekkumardwivedi 0:c165b9e652d8 46 int main() {
abhishekkumardwivedi 0:c165b9e652d8 47
abhishekkumardwivedi 0:c165b9e652d8 48 ble.init();
abhishekkumardwivedi 0:c165b9e652d8 49 ble.onDisconnection(disconnectionCallback);
abhishekkumardwivedi 0:c165b9e652d8 50
abhishekkumardwivedi 0:c165b9e652d8 51 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
abhishekkumardwivedi 0:c165b9e652d8 52 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
abhishekkumardwivedi 0:c165b9e652d8 53 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
abhishekkumardwivedi 0:c165b9e652d8 54 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
abhishekkumardwivedi 0:c165b9e652d8 55 ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000));
abhishekkumardwivedi 0:c165b9e652d8 56 ble.startAdvertising();
abhishekkumardwivedi 0:c165b9e652d8 57
abhishekkumardwivedi 0:c165b9e652d8 58 PIRService pirService(ble, motion_detected);
abhishekkumardwivedi 0:c165b9e652d8 59
abhishekkumardwivedi 0:c165b9e652d8 60 pir.rise(&irq_handler);
abhishekkumardwivedi 0:c165b9e652d8 61
abhishekkumardwivedi 0:c165b9e652d8 62 while(1) {
abhishekkumardwivedi 0:c165b9e652d8 63 if(motion_detected) {
abhishekkumardwivedi 0:c165b9e652d8 64 pirService.UpdateMotionStatus(motion_detected);
abhishekkumardwivedi 0:c165b9e652d8 65 while(interval > 0) {
abhishekkumardwivedi 0:c165b9e652d8 66 wait(1);
abhishekkumardwivedi 0:c165b9e652d8 67 interval--;
abhishekkumardwivedi 0:c165b9e652d8 68 }
abhishekkumardwivedi 0:c165b9e652d8 69 motion_detected = 0;
abhishekkumardwivedi 0:c165b9e652d8 70 pirService.UpdateMotionStatus(motion_detected);
abhishekkumardwivedi 0:c165b9e652d8 71 }
abhishekkumardwivedi 0:c165b9e652d8 72 }
abhishekkumardwivedi 0:c165b9e652d8 73 }