NQ62X BLE_DOORCTL_4CH

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-2014 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 <events/mbed_events.h>
00018 #include <mbed.h>
00019 #include "ble/BLE.h"
00020 #include "ble/Gap.h"
00021 #include "ble/services/BatteryService.h"
00022 #include "DOORService.h"
00023 
00024 //DigitalOut led1(LED1, 1);
00025 
00026 const static char     DEVICE_NAME[] = "DOORCTL";
00027 static const uint16_t uuid16_list[] = {DOORService::DOOR_SERVICE_UUID};
00028 
00029 
00030 uint8_t BLE_RX_CMD = 0xFF;
00031 uint8_t doorStatusPayload[2] = {0xFF,};
00032 
00033 DigitalOut relay1(D7); // Relay 1
00034 DigitalOut relay2(D6); // Relay 2
00035 DigitalOut relay3(D5); // Relay 3
00036 DigitalOut relay4(D4); // Relay 4
00037 
00038 BLEDevice  ble;
00039 
00040 
00041 static DOORService *doorServicePtr;
00042 static EventQueue eventQueue(
00043     /* event count */ 16 * /* event size */ 32
00044 );
00045 
00046 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00047 {
00048     BLE::Instance().gap().startAdvertising();
00049 }
00050 
00051 
00052 
00053 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
00054     
00055     
00056     if ((params->handle == doorServicePtr->getValueHandle()) && (params->len == 1)) {
00057         {
00058             BLE_RX_CMD = *(params->data);            
00059         }
00060     }
00061 }
00062 
00063 void cmd_thread(void const *args) {
00064     while (true) {
00065         switch (BLE_RX_CMD)
00066         {
00067             case 0x00:
00068             relay1 = 1;
00069             relay2 = 0;
00070             relay3 = 0;
00071             relay4 = 0;
00072            // Thread::wait(500);            
00073             doorStatusPayload[0]=0xF0;
00074             break;
00075             
00076             case 0x01:
00077             relay1 = 0;
00078             relay2 = 1;
00079             relay3 = 0;
00080             relay4 = 0;
00081             //Thread::wait(500);
00082             doorStatusPayload[0]=0xF1;
00083             break;
00084 
00085             case 0x02:
00086             relay1 = 0;
00087             relay2 = 0;
00088             relay3 = 1;
00089             relay4 = 0;
00090             //Thread::wait(500);
00091             doorStatusPayload[0]=0xF2;
00092             break;
00093 
00094             case 0x03:
00095             relay1 = 0;
00096             relay2 = 0;
00097             relay3 = 0;
00098             relay4 = 1;
00099             //Thread::wait(500);
00100             doorStatusPayload[0]=0xF3;
00101             break;
00102 
00103             case 0x04:
00104             relay1 = 0;
00105             relay2 = 0;
00106             relay3 = 0;
00107             relay4 = 0;            
00108             //Thread::wait(500);
00109             doorStatusPayload[0]=0xF4;            
00110             break;
00111 
00112             case 0x05: //Test Mode!!
00113             relay1 = 1;
00114             relay2 = 1;
00115             relay3 = 1;
00116             relay4 = 1;       
00117             //Thread::wait(500);                 
00118             doorStatusPayload[0]=0xF5;            
00119             break;
00120 
00121                         
00122             default:
00123             break;    
00124         }
00125        if (ble.getGapState().connected) {
00126             ble.updateCharacteristicValue((doorServicePtr->getValueHandle()),doorStatusPayload, 1);           
00127         }
00128     }
00129 }
00130 
00131 
00132 
00133 /**
00134  * This function is called when the ble initialization process has failled
00135  */
00136 void onBleInitError(BLE &ble, ble_error_t error)
00137 {
00138     /* Initialization error handling should go here */
00139 }
00140 
00141 /**
00142  * Callback triggered when the ble initialization process has finished
00143  */
00144 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00145 {
00146     BLE&        ble   = params->ble;
00147     ble_error_t error = params->error;
00148 
00149     if (error != BLE_ERROR_NONE) {
00150         /* In case of error, forward the error handling to onBleInitError */
00151         onBleInitError(ble, error);
00152         return;
00153     }
00154 
00155     /* Ensure that it is the default instance of BLE */
00156     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00157         return;
00158     }
00159 
00160     ble.gap().onDisconnection(disconnectionCallback);
00161     ble.gattServer().onDataWritten(onDataWrittenCallback);
00162     
00163     /* Setup primary services */  
00164     uint8_t initialValueForDOORCharacteristic = 0xFF;
00165     doorServicePtr = new DOORService(ble, initialValueForDOORCharacteristic);
00166 
00167     /* Setup advertising */
00168     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00169     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *) uuid16_list, sizeof(uuid16_list));
00170     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME));
00171     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00172     ble.gap().setAdvertisingInterval(1000); /* 1000ms */
00173     ble.gap().startAdvertising();
00174 }
00175 
00176 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
00177     BLE &ble = BLE::Instance();
00178     eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
00179 }
00180 
00181 int main()
00182 {
00183     relay1 = 0;
00184     relay2 = 0;
00185     relay3 = 0;
00186     relay4 = 0;     
00187     
00188     BLE &ble = BLE::Instance();
00189     ble.onEventsToProcess(scheduleBleEventsProcessing);
00190     ble.init(bleInitComplete);
00191 
00192     Thread thread0(cmd_thread,(void *)"DOORCTL");
00193     
00194     eventQueue.dispatch_forever();
00195     
00196     
00197     return 0;
00198 }