![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
This is a basic program that provides the necessary BLE service to allow communications with the UPAS
Dependencies: BLE_API mbed nRF51822 CronoDot EEPROM NCP5623BMUTBG ADS1115 BME280 Calibration_one MCP40D17 SDFileSystem LSM303 SI1145 STC3100
Fork of BLE_Button by
Diff: main.cpp
- Revision:
- 10:66549fa08986
- Parent:
- 9:0f6951db24f1
- Child:
- 11:1058647c66e8
--- a/main.cpp Fri Oct 09 13:37:52 2015 +0000 +++ b/main.cpp Tue Oct 20 20:38:04 2015 +0000 @@ -1,54 +1,18 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2013 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - +//CODE BY JAKE LORD +//ALL RIGHTS RESERVED BY VOLCKENS GROUP, FORT COLLINS CO #include "mbed.h" #include "BLE.h" -#include "ButtonService.h" +#include "UPAS_Service.h" BLE ble; -DigitalOut led1(LED1); -InterruptIn button(BUTTON1); - -const static char DEVICE_NAME[] = "Button"; -static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID}; - -enum { - RELEASED = 0, - PRESSED, - IDLE -}; -static uint8_t buttonState = IDLE; - -ButtonService *buttonServicePtr; +DigitalOut led1(LED1); //Use of leds is important for debugging -void buttonPressedCallback(void) -{ - /* Note that the buttonPressedCallback() executes in interrupt context, so it is safer to access - * BLE device API from the main thread. */ - buttonState = PRESSED; -} +const static char DEVICE_NAME[] = "UPAS"; //Will hold the actual name of the whichever UPAS is being connected to +static const uint16_t uuid16_list[] = {UPAS_Service::UPAS_SERVICE_UUID}; //Currently a custom 16-bit representation of 128-bit UUID -void buttonReleasedCallback(void) -{ - /* Note that the buttonReleasedCallback() executes in interrupt context, so it is safer to access - * BLE device API from the main thread. */ - buttonState = RELEASED; -} +UPAS_Service *upasServicePtr; -void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) +void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)//Code called when mbed ble senses a disconnect { ble.gap().startAdvertising(); } @@ -58,34 +22,52 @@ led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */ } +void writeCharCallback(const GattWriteCallbackParams *params) +{ + led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */ + + // check to see what characteristic was written, by handle + if(params->handle == upasServicePtr->writeChar.getValueHandle()) { + // toggle LED if only 1 byte is written + if(params->len == 1) { + led1 = params->data[0]; + } + // update the readChar with the value of writeChar + ble.updateCharacteristicValue(upasServicePtr->readChar.getValueHandle(),params->data,params->len); + } +} + int main(void) { led1 = 1; Ticker ticker; ticker.attach(periodicCallback, 1); - button.fall(buttonPressedCallback); - button.rise(buttonReleasedCallback); + + uint8_t readValues[57] = {0x31,0x30,0x2F,0x31,0x38,0x2F,0x31,0x35,0x20,0x31,0x32,0x3A,0x30,0x30,0x3A,0x30,0x30}; //dummy array for testing ble.init(); ble.gap().onDisconnection(disconnectionCallback); + ble.gattServer().onDataWritten(writeCharCallback); //add writeCharCallback (custom function) to whenever data is being written to device + + UPAS_Service upasService(ble, false,readValues); //Create a GattService that is defined in UPAS_Service.h + upasServicePtr = &upasService; //Create a pointer to the service (Allows advertisement without specifically adding the service - ButtonService buttonService(ble, false /* initial value for button pressed */); - buttonServicePtr = &buttonService; - - /* setup advertising */ + /* setup advertising + Following lines do the follow: + 1:Declare the device as Bluetooth Smart(Low-Energy) + 2.Advertise the UPAS service that will send and receive the 57-bits of settable values in the UPAS EEPROM + 3.Advertise the name that will be associated with the UPAS + 4.Allow the UPAS to advertise unrestricted (this might change) */ + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); - ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ + ble.gap().setAdvertisingInterval(160); /* 160ms. */ ble.gap().startAdvertising(); - + + //Loop keeps device in infinite loop waiting for events to occur while (true) { - if (buttonState!=IDLE) { - buttonServicePtr->updateButtonState(buttonState); - buttonState = IDLE; - } - ble.waitForEvent(); } }