Abhishek Dwivedi / Mbed 2 deprecated BLE_PIR

Dependencies:   BLE_API mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "BLEDevice.h"
00019 #include "PIRService.h"
00020 
00021 BLEDevice ble;
00022 
00023 InterruptIn pir(p4);
00024 
00025 bool motion_detected = 0;
00026 int interval = 0;
00027 static const int PUBLISH_TIMESLISE = 10; //once detected, publish for 10 sec, important data so keep broadcasting for 10 sec.
00028 
00029 
00030 const static char     DEVICE_NAME[] = "PIR";
00031 static const uint16_t uuid16_list[] = {PIRService::PIR_SERVICE_UUID};
00032 
00033 //PIRService *pirServicePtr;
00034 
00035 void irq_handler(void)
00036 {
00037     motion_detected = 1;
00038     interval = PUBLISH_TIMESLISE;
00039 }
00040 
00041 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
00042 {
00043     ble.startAdvertising();
00044 }
00045 
00046 int main() {
00047 
00048     ble.init();
00049     ble.onDisconnection(disconnectionCallback);
00050 
00051     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00052     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
00053     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00054     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00055     ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000));
00056     ble.startAdvertising();
00057     
00058     PIRService pirService(ble, motion_detected);
00059     
00060     pir.rise(&irq_handler);
00061     
00062     while(1) {
00063         if(motion_detected) {
00064             pirService.UpdateMotionStatus(motion_detected);
00065             while(interval > 0) {
00066                 wait(1);
00067                 interval--;
00068             }
00069             motion_detected = 0;
00070             pirService.UpdateMotionStatus(motion_detected);
00071         }
00072     }
00073 }