A simple extension to exististing sample

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_Button by Bluetooth Low Energy

Committer:
blinky
Date:
Fri Jun 12 16:33:21 2015 +0000
Revision:
6:c18ec2f18b33
Parent:
5:bf55ff59a71f
Blinky sample modifed by blinky.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 0:28f095301cb2 1 /* mbed Microcontroller Library
rgrover1 0:28f095301cb2 2 * Copyright (c) 2006-2013 ARM Limited
rgrover1 0:28f095301cb2 3 *
rgrover1 0:28f095301cb2 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 0:28f095301cb2 5 * you may not use this file except in compliance with the License.
rgrover1 0:28f095301cb2 6 * You may obtain a copy of the License at
rgrover1 0:28f095301cb2 7 *
rgrover1 0:28f095301cb2 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 0:28f095301cb2 9 *
rgrover1 0:28f095301cb2 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 0:28f095301cb2 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 0:28f095301cb2 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 0:28f095301cb2 13 * See the License for the specific language governing permissions and
rgrover1 0:28f095301cb2 14 * limitations under the License.
rgrover1 0:28f095301cb2 15 */
rgrover1 0:28f095301cb2 16
rgrover1 0:28f095301cb2 17 #include "mbed.h"
rgrover1 0:28f095301cb2 18 #include "BLEDevice.h"
rgrover1 0:28f095301cb2 19 #include "ButtonService.h"
rgrover1 0:28f095301cb2 20
blinky 5:bf55ff59a71f 21 #define DEF_BUTTON_1 0x01
blinky 5:bf55ff59a71f 22 #define DEF_BUTTON_2 0x02
blinky 5:bf55ff59a71f 23 #define DEF_BUTTON_3 0x04
blinky 5:bf55ff59a71f 24 #define DEF_BUTTON_4 0x08
blinky 5:bf55ff59a71f 25
blinky 5:bf55ff59a71f 26 #define DEF_BUTTON_NUMBER DEF_BUTTON_1
blinky 5:bf55ff59a71f 27 #define DEF_BUTTON_DOWN 0x10
blinky 5:bf55ff59a71f 28
rgrover1 0:28f095301cb2 29 BLEDevice ble;
blinky 5:bf55ff59a71f 30
blinky 5:bf55ff59a71f 31 // "BBC MICROBIT P [1-4]
blinky 5:bf55ff59a71f 32 const static char MSG[] = "-... -... -.-. -- .. -.-. -.- --- -... .. - .--. .---- ";
blinky 5:bf55ff59a71f 33 //const static char MSG[] = "-... -... -.-. -- .. -.-. -.- --- -... .. - .--. ..--- ";
blinky 5:bf55ff59a71f 34 //const static char MSG[] = "-... -... -.-. -- .. -.-. -.- --- -... .. - .--. ...-- ";
blinky 5:bf55ff59a71f 35 //const static char MSG[] = "-... -... -.-. -- .. -.-. -.- --- -... .. - .--. ....- ";
rgrover1 0:28f095301cb2 36
blinky 5:bf55ff59a71f 37 int size = sizeof(MSG)-1;
blinky 5:bf55ff59a71f 38 int i = 0;
blinky 5:bf55ff59a71f 39 int j = 0;
blinky 5:bf55ff59a71f 40
blinky 5:bf55ff59a71f 41 DigitalOut led1(LED1);
blinky 5:bf55ff59a71f 42 DigitalOut led2(LED2);
blinky 5:bf55ff59a71f 43
blinky 5:bf55ff59a71f 44 InterruptIn buttonA(BUTTON1);
blinky 5:bf55ff59a71f 45 InterruptIn buttonB(BUTTON2);
rgrover1 0:28f095301cb2 46
blinky 5:bf55ff59a71f 47 const static char DEVICE_NAME[] = "BBC MicroBit vigov";
blinky 5:bf55ff59a71f 48 static const uint16_t uuid16_list[] = {ButtonServiceA::BUTTON_SERVICE_UUID, ButtonServiceB::BUTTON_SERVICE_UUID};
rgrover1 0:28f095301cb2 49
blinky 5:bf55ff59a71f 50 ButtonServiceA *buttonServicePtrA;
blinky 5:bf55ff59a71f 51 ButtonServiceB *buttonServicePtrB;
blinky 5:bf55ff59a71f 52
blinky 5:bf55ff59a71f 53 void buttonPressedCallbackA(void)
rgrover1 0:28f095301cb2 54 {
blinky 5:bf55ff59a71f 55 buttonServicePtrA->updateButtonState(DEF_BUTTON_NUMBER | DEF_BUTTON_DOWN);
rgrover1 0:28f095301cb2 56 }
rgrover1 0:28f095301cb2 57
blinky 5:bf55ff59a71f 58 void buttonReleasedCallbackA(void)
blinky 5:bf55ff59a71f 59 {
blinky 5:bf55ff59a71f 60 buttonServicePtrA->updateButtonState(DEF_BUTTON_NUMBER);
blinky 5:bf55ff59a71f 61 }
blinky 5:bf55ff59a71f 62
blinky 5:bf55ff59a71f 63 void buttonPressedCallbackB(void)
rgrover1 0:28f095301cb2 64 {
blinky 5:bf55ff59a71f 65 buttonServicePtrB->updateButtonState(DEF_BUTTON_NUMBER | DEF_BUTTON_DOWN);
blinky 5:bf55ff59a71f 66 }
blinky 5:bf55ff59a71f 67
blinky 5:bf55ff59a71f 68 void buttonReleasedCallbackB(void)
blinky 5:bf55ff59a71f 69 {
blinky 5:bf55ff59a71f 70 buttonServicePtrB->updateButtonState(DEF_BUTTON_NUMBER);
rgrover1 0:28f095301cb2 71 }
rgrover1 0:28f095301cb2 72
rgrover1 0:28f095301cb2 73 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
rgrover1 0:28f095301cb2 74 {
rgrover1 0:28f095301cb2 75 ble.startAdvertising();
rgrover1 0:28f095301cb2 76 }
rgrover1 0:28f095301cb2 77
blinky 5:bf55ff59a71f 78 void dotDash(int dash)
blinky 5:bf55ff59a71f 79 {
blinky 5:bf55ff59a71f 80 led1 = 1;
blinky 5:bf55ff59a71f 81 if(dash)
blinky 5:bf55ff59a71f 82 led2 = 1;
blinky 5:bf55ff59a71f 83 else
blinky 5:bf55ff59a71f 84 led2 = 0;
blinky 5:bf55ff59a71f 85 }
blinky 5:bf55ff59a71f 86
blinky 5:bf55ff59a71f 87 void ledOff()
blinky 5:bf55ff59a71f 88 {
blinky 5:bf55ff59a71f 89 led1 = 0;
blinky 5:bf55ff59a71f 90 led2 = 0;
blinky 5:bf55ff59a71f 91 }
blinky 5:bf55ff59a71f 92
rgrover1 0:28f095301cb2 93 void periodicCallback(void)
rgrover1 0:28f095301cb2 94 {
blinky 5:bf55ff59a71f 95 if(j>size) {
blinky 5:bf55ff59a71f 96 j = 0;
blinky 5:bf55ff59a71f 97 }
blinky 5:bf55ff59a71f 98
blinky 5:bf55ff59a71f 99 if (MSG[j] != ' ') {
blinky 5:bf55ff59a71f 100 if(i == 0) {
blinky 5:bf55ff59a71f 101 if(MSG[j] == '-') {
blinky 5:bf55ff59a71f 102 dotDash(1);
blinky 5:bf55ff59a71f 103 } else {
blinky 5:bf55ff59a71f 104 dotDash(0);
blinky 5:bf55ff59a71f 105 }
blinky 5:bf55ff59a71f 106 } else {
blinky 5:bf55ff59a71f 107 ledOff();
blinky 5:bf55ff59a71f 108 j++;
blinky 5:bf55ff59a71f 109 i = 0;
blinky 5:bf55ff59a71f 110 return;
blinky 5:bf55ff59a71f 111 }
blinky 5:bf55ff59a71f 112 } else {
blinky 5:bf55ff59a71f 113 if(i == 4) {
blinky 5:bf55ff59a71f 114 j++;
blinky 5:bf55ff59a71f 115 i = 0;
blinky 5:bf55ff59a71f 116 return;
blinky 5:bf55ff59a71f 117 }
blinky 5:bf55ff59a71f 118 }
blinky 5:bf55ff59a71f 119
blinky 5:bf55ff59a71f 120 i++;
rgrover1 0:28f095301cb2 121 }
rgrover1 0:28f095301cb2 122
rgrover1 0:28f095301cb2 123 int main(void)
rgrover1 0:28f095301cb2 124 {
blinky 5:bf55ff59a71f 125 led1 = 0;
blinky 5:bf55ff59a71f 126 led2 = 0;
rgrover1 0:28f095301cb2 127 Ticker ticker;
blinky 5:bf55ff59a71f 128 ticker.attach(periodicCallback, 0.2);
blinky 5:bf55ff59a71f 129
blinky 5:bf55ff59a71f 130 buttonA.fall(buttonPressedCallbackA);
blinky 5:bf55ff59a71f 131 buttonA.rise(buttonReleasedCallbackA);
blinky 5:bf55ff59a71f 132
blinky 5:bf55ff59a71f 133 buttonB.fall(buttonPressedCallbackB);
blinky 5:bf55ff59a71f 134 buttonB.rise(buttonReleasedCallbackB);
rgrover1 0:28f095301cb2 135
rgrover1 0:28f095301cb2 136 ble.init();
rgrover1 0:28f095301cb2 137 ble.onDisconnection(disconnectionCallback);
rgrover1 0:28f095301cb2 138
blinky 5:bf55ff59a71f 139 ButtonServiceA buttonServiceA(ble, DEF_BUTTON_NUMBER); /* initial value for button pressed */
blinky 5:bf55ff59a71f 140 buttonServicePtrA = &buttonServiceA;
blinky 5:bf55ff59a71f 141
blinky 5:bf55ff59a71f 142 ButtonServiceB buttonServiceB(ble, DEF_BUTTON_NUMBER); /* initial value for button pressed */
blinky 5:bf55ff59a71f 143 buttonServicePtrB = &buttonServiceB;
rgrover1 0:28f095301cb2 144
rgrover1 0:28f095301cb2 145 /* setup advertising */
rgrover1 0:28f095301cb2 146 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
rgrover1 0:28f095301cb2 147 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
blinky 5:bf55ff59a71f 148
rgrover1 0:28f095301cb2 149 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
rgrover1 0:28f095301cb2 150 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
blinky 5:bf55ff59a71f 151
rgrover1 2:84dd6d29bc59 152 ble.setAdvertisingInterval(1000); /* 1000ms. */
rgrover1 0:28f095301cb2 153 ble.startAdvertising();
rgrover1 0:28f095301cb2 154
rgrover1 0:28f095301cb2 155 while (true) {
rgrover1 0:28f095301cb2 156 ble.waitForEvent();
rgrover1 0:28f095301cb2 157 }
rgrover1 0:28f095301cb2 158 }