van

Dependencies:   BLE_API mbed VAN

Dependents:   VAN

Fork of BLE_iBeacon_POC by Fallen Fate

Committer:
peteratsl
Date:
Mon Nov 16 17:43:08 2015 +0000
Revision:
69:1bcf3f0174c9
Parent:
68:4a8fdfe70ab3
Child:
70:1281fe7166fb
v1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ktownsend 0:7613d21e5974 1 /* mbed Microcontroller Library
ktownsend 0:7613d21e5974 2 * Copyright (c) 2006-2013 ARM Limited
ktownsend 0:7613d21e5974 3 *
ktownsend 0:7613d21e5974 4 * Licensed under the Apache License, Version 2.0 (the "License");
ktownsend 0:7613d21e5974 5 * you may not use this file except in compliance with the License.
ktownsend 0:7613d21e5974 6 * You may obtain a copy of the License at
ktownsend 0:7613d21e5974 7 *
ktownsend 0:7613d21e5974 8 * http://www.apache.org/licenses/LICENSE-2.0
ktownsend 0:7613d21e5974 9 *
ktownsend 0:7613d21e5974 10 * Unless required by applicable law or agreed to in writing, software
ktownsend 0:7613d21e5974 11 * distributed under the License is distributed on an "AS IS" BASIS,
ktownsend 0:7613d21e5974 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ktownsend 0:7613d21e5974 13 * See the License for the specific language governing permissions and
ktownsend 0:7613d21e5974 14 * limitations under the License.
ktownsend 0:7613d21e5974 15 */
ktownsend 0:7613d21e5974 16
ktownsend 0:7613d21e5974 17 #include "mbed.h"
mbedAustin 53:f9ec2c7a47f5 18 #include "iBeaconService.h"
fallenfate 67:81b595625ce0 19 #include "UARTService.h"
ktownsend 0:7613d21e5974 20
mbedAustin 56:56bc0cab3916 21 /**
mbedAustin 56:56bc0cab3916 22 * For this demo application, populate the beacon advertisement payload
mbedAustin 56:56bc0cab3916 23 * with 2 AD structures: FLAG and MSD (manufacturer specific data).
mbedAustin 56:56bc0cab3916 24 *
mbedAustin 56:56bc0cab3916 25 * Reference:
mbedAustin 56:56bc0cab3916 26 * Bluetooth Core Specification 4.0 (Vol. 3), Part C, Section 11, 18
mbedAustin 56:56bc0cab3916 27 */
mbedAustin 56:56bc0cab3916 28
mbedAustin 50:7bc38f01d2d3 29 BLEDevice ble;
fallenfate 67:81b595625ce0 30 UARTService *uart;
mbedAustin 50:7bc38f01d2d3 31
mbedAustin 56:56bc0cab3916 32 /**
mbedAustin 56:56bc0cab3916 33 * The Beacon payload has the following composition:
mbedAustin 56:56bc0cab3916 34 * 128-Bit / 16byte UUID = E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61
mbedAustin 56:56bc0cab3916 35 * Major/Minor = 0x1122 / 0x3344
mbedAustin 56:56bc0cab3916 36 * Tx Power = 0xC8 = 200, 2's compliment is 256-200 = (-56dB)
mbedAustin 56:56bc0cab3916 37 *
mbedAustin 56:56bc0cab3916 38 * Note: please remember to calibrate your beacons
mbedAustin 56:56bc0cab3916 39 * TX Power for more accurate results.
mbedAustin 56:56bc0cab3916 40 */
rgrover1 57:9782cb35c494 41 const uint8_t uuid[] = {0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4,
fallenfate 68:4a8fdfe70ab3 42 0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x62
rgrover1 57:9782cb35c494 43 };
mbedAustin 56:56bc0cab3916 44 uint16_t majorNumber = 1122;
mbedAustin 56:56bc0cab3916 45 uint16_t minorNumber = 3344;
mbedAustin 56:56bc0cab3916 46 uint16_t txPower = 0xC8;
peteratsl 69:1bcf3f0174c9 47
peteratsl 69:1bcf3f0174c9 48
peteratsl 69:1bcf3f0174c9 49 const uint8_t b1_id = 0x01;
peteratsl 69:1bcf3f0174c9 50 const uint8_t b2_id = 0x02;
peteratsl 69:1bcf3f0174c9 51 const uint8_t b3_id = 0x03;
peteratsl 69:1bcf3f0174c9 52 const uint8_t b4_id = 0x04;
peteratsl 69:1bcf3f0174c9 53 const uint8_t b5_id = 0x05;
peteratsl 69:1bcf3f0174c9 54 const uint8_t b6_id = 0x06;
peteratsl 69:1bcf3f0174c9 55 const uint8_t b7_id = 0x07;
peteratsl 69:1bcf3f0174c9 56 const uint8_t b8_id = 0x08;
peteratsl 69:1bcf3f0174c9 57 const uint8_t b9_id = 0x09;
peteratsl 69:1bcf3f0174c9 58 const uint8_t b0_id = 0x00;
peteratsl 69:1bcf3f0174c9 59
peteratsl 69:1bcf3f0174c9 60 const uint8_t bA1_id = 0x31;
peteratsl 69:1bcf3f0174c9 61 const uint8_t bA2_id = 0x32;
peteratsl 69:1bcf3f0174c9 62 const uint8_t bA3_id = 0x33;
peteratsl 69:1bcf3f0174c9 63 const uint8_t bA4_id = 0x34;
peteratsl 69:1bcf3f0174c9 64 const uint8_t bA5_id = 0x35;
peteratsl 69:1bcf3f0174c9 65 const uint8_t bA6_id = 0x36;
peteratsl 69:1bcf3f0174c9 66 const uint8_t bA7_id = 0x37;
peteratsl 69:1bcf3f0174c9 67 const uint8_t bA8_id = 0x38;
peteratsl 69:1bcf3f0174c9 68 const uint8_t bA9_id = 0x39;
peteratsl 69:1bcf3f0174c9 69 const uint8_t bA0_id = 0x30;
peteratsl 69:1bcf3f0174c9 70
peteratsl 69:1bcf3f0174c9 71
peteratsl 69:1bcf3f0174c9 72 PinName b1_p = P0_28;
peteratsl 69:1bcf3f0174c9 73 PinName b2_p = P0_29;
peteratsl 69:1bcf3f0174c9 74 PinName b3_p = P0_15;
peteratsl 69:1bcf3f0174c9 75 PinName b4_p = P0_10; //CTX
peteratsl 69:1bcf3f0174c9 76 PinName b5_p = P0_9; //TXD
peteratsl 69:1bcf3f0174c9 77 PinName b6_p = P0_11; //RXD
peteratsl 69:1bcf3f0174c9 78 PinName b7_p = P0_8; //RTS
peteratsl 69:1bcf3f0174c9 79 PinName b8_p = P0_4;
peteratsl 69:1bcf3f0174c9 80 PinName b9_p = P0_28;
peteratsl 69:1bcf3f0174c9 81 PinName b0_p = P0_29;
peteratsl 69:1bcf3f0174c9 82
peteratsl 69:1bcf3f0174c9 83 //setup led
peteratsl 69:1bcf3f0174c9 84 DigitalOut led1(P0_19);
peteratsl 69:1bcf3f0174c9 85
peteratsl 69:1bcf3f0174c9 86 //setup digital outs
peteratsl 69:1bcf3f0174c9 87 DigitalOut b1(b1_p);
peteratsl 69:1bcf3f0174c9 88 DigitalOut b2(b2_p);
peteratsl 69:1bcf3f0174c9 89 DigitalOut b3(b3_p);
peteratsl 69:1bcf3f0174c9 90 DigitalOut b4(b4_p);
peteratsl 69:1bcf3f0174c9 91 DigitalOut b5(b5_p);
peteratsl 69:1bcf3f0174c9 92 DigitalOut b6(b6_p);
peteratsl 69:1bcf3f0174c9 93 DigitalOut b7(b7_p);
peteratsl 69:1bcf3f0174c9 94 DigitalOut b8(b8_p);
peteratsl 69:1bcf3f0174c9 95 DigitalOut b9(b9_p);
peteratsl 69:1bcf3f0174c9 96 DigitalOut b0(b0_p);
fallenfate 67:81b595625ce0 97
fallenfate 67:81b595625ce0 98 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason);
fallenfate 67:81b595625ce0 99 void onDataWritten(const GattCharacteristicWriteCBParams *params);
peteratsl 69:1bcf3f0174c9 100 int flip(char value);
fallenfate 67:81b595625ce0 101
fallenfate 67:81b595625ce0 102 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
fallenfate 67:81b595625ce0 103 {
fallenfate 67:81b595625ce0 104 ble.startAdvertising();
fallenfate 67:81b595625ce0 105 }
fallenfate 67:81b595625ce0 106
fallenfate 67:81b595625ce0 107 void onDataWritten(const GattCharacteristicWriteCBParams *params)
fallenfate 67:81b595625ce0 108 {
fallenfate 67:81b595625ce0 109 // TODO - this stuff will need to be retrofitted to deal with string commands
peteratsl 69:1bcf3f0174c9 110 /*
fallenfate 67:81b595625ce0 111 if(*params->data == 0x33){
fallenfate 67:81b595625ce0 112 led1 = 0;
fallenfate 67:81b595625ce0 113 }
fallenfate 67:81b595625ce0 114 else if(*params->data == 0x34){
fallenfate 67:81b595625ce0 115 led1 = 1;
peteratsl 69:1bcf3f0174c9 116 }*/
peteratsl 69:1bcf3f0174c9 117
peteratsl 69:1bcf3f0174c9 118 flip(params->data[0]);
fallenfate 67:81b595625ce0 119
fallenfate 67:81b595625ce0 120 }
mbedAustin 56:56bc0cab3916 121
ktownsend 0:7613d21e5974 122 int main(void)
ktownsend 0:7613d21e5974 123 {
peteratsl 69:1bcf3f0174c9 124 /* initialize stuff */
peteratsl 69:1bcf3f0174c9 125 led1 = 1;
fallenfate 67:81b595625ce0 126
peteratsl 69:1bcf3f0174c9 127 b1 = 1; // 0x01 flash hazards in5 d4 p28
peteratsl 69:1bcf3f0174c9 128 b2 = 1; // 0x02 horn in6 d5 p29
peteratsl 69:1bcf3f0174c9 129 b3 = 1; // 0x03 headlights in7 d6 p15
peteratsl 69:1bcf3f0174c9 130 b4 = 1; // 0x04 door locking 0.8 in2 in3 d2 cts
peteratsl 69:1bcf3f0174c9 131 b5 = 1; // 0x05 remote start 2x .25 in1 d1 txd
peteratsl 69:1bcf3f0174c9 132 b7 = 1; // 0x07 trip alarm 0.8 in4 d3 rts
peteratsl 69:1bcf3f0174c9 133
peteratsl 69:1bcf3f0174c9 134 b6 = 1; // P0_11 ???
peteratsl 69:1bcf3f0174c9 135 b8 = 1; // P0_9 ???
peteratsl 69:1bcf3f0174c9 136 b9 = 1; // P0_11 ???
peteratsl 69:1bcf3f0174c9 137 b0 = 1; // P0_8 ???
peteratsl 69:1bcf3f0174c9 138
peteratsl 69:1bcf3f0174c9 139
Rohit Grover 11:6774f4827024 140 ble.init();
fallenfate 67:81b595625ce0 141
fallenfate 67:81b595625ce0 142 ble.onDisconnection(disconnectionCallback);
fallenfate 67:81b595625ce0 143 ble.onDataWritten(onDataWritten);
fallenfate 67:81b595625ce0 144
fallenfate 67:81b595625ce0 145
fallenfate 67:81b595625ce0 146 uart = new UARTService(ble);
mbedAustin 56:56bc0cab3916 147 iBeaconService ibeacon(ble, uuid, majorNumber, minorNumber, txPower);
mbedAustin 53:f9ec2c7a47f5 148
rgrover1 60:3034dc913ea1 149 ble.setAdvertisingInterval(1000); /* 1000ms. */
Rohit Grover 19:869d8c7306b4 150 ble.startAdvertising();
ktownsend 0:7613d21e5974 151
mbedAustin 56:56bc0cab3916 152 while(1) {
mbedAustin 56:56bc0cab3916 153 ble.waitForEvent(); // allows or low power operation
ktownsend 0:7613d21e5974 154 }
Rohit Grover 10:391c1acf4b9d 155 }
peteratsl 69:1bcf3f0174c9 156
peteratsl 69:1bcf3f0174c9 157 void testAll(void)
peteratsl 69:1bcf3f0174c9 158 {
peteratsl 69:1bcf3f0174c9 159 for(int i=0; i<2; i++)
peteratsl 69:1bcf3f0174c9 160 {
peteratsl 69:1bcf3f0174c9 161 led1 = !led1;
peteratsl 69:1bcf3f0174c9 162 b1 = !b1;
peteratsl 69:1bcf3f0174c9 163 b2 = !b2;
peteratsl 69:1bcf3f0174c9 164 b3 = !b3;
peteratsl 69:1bcf3f0174c9 165 b4 = !b4;
peteratsl 69:1bcf3f0174c9 166 b5 = !b5;
peteratsl 69:1bcf3f0174c9 167 b6 = !b6;
peteratsl 69:1bcf3f0174c9 168 b7 = !b7;
peteratsl 69:1bcf3f0174c9 169 b8 = !b5;
peteratsl 69:1bcf3f0174c9 170 b9 = !b6;
peteratsl 69:1bcf3f0174c9 171 b0 = !b7;
peteratsl 69:1bcf3f0174c9 172 wait(1);
peteratsl 69:1bcf3f0174c9 173 }
peteratsl 69:1bcf3f0174c9 174 }
peteratsl 69:1bcf3f0174c9 175
peteratsl 69:1bcf3f0174c9 176 void blinky(void)
peteratsl 69:1bcf3f0174c9 177 {
peteratsl 69:1bcf3f0174c9 178 for(int i=0; i<15; i++)
peteratsl 69:1bcf3f0174c9 179 {
peteratsl 69:1bcf3f0174c9 180 led1 = !led1;
peteratsl 69:1bcf3f0174c9 181 wait(.3);
peteratsl 69:1bcf3f0174c9 182 }
peteratsl 69:1bcf3f0174c9 183 }
peteratsl 69:1bcf3f0174c9 184
peteratsl 69:1bcf3f0174c9 185 int flip(char value)
peteratsl 69:1bcf3f0174c9 186 {
peteratsl 69:1bcf3f0174c9 187 int ret = 0;
peteratsl 69:1bcf3f0174c9 188
peteratsl 69:1bcf3f0174c9 189 switch(value)
peteratsl 69:1bcf3f0174c9 190 {
peteratsl 69:1bcf3f0174c9 191 // 0x01 flash hazards in5 d4 p28
peteratsl 69:1bcf3f0174c9 192 case b1_id :
peteratsl 69:1bcf3f0174c9 193 case bA1_id : led1 = !led1;
peteratsl 69:1bcf3f0174c9 194 b1 = !b1;
peteratsl 69:1bcf3f0174c9 195 break;
peteratsl 69:1bcf3f0174c9 196 // 0x02 horn in6 d5 p29
peteratsl 69:1bcf3f0174c9 197 case b2_id :
peteratsl 69:1bcf3f0174c9 198 case bA2_id : led1 = !led1;
peteratsl 69:1bcf3f0174c9 199 b2 = !b2;
peteratsl 69:1bcf3f0174c9 200 break;
peteratsl 69:1bcf3f0174c9 201 // 0x03 headlights in7 d6 p15
peteratsl 69:1bcf3f0174c9 202 case b3_id :
peteratsl 69:1bcf3f0174c9 203 case bA3_id : led1 = !led1;
peteratsl 69:1bcf3f0174c9 204 b3 = !b3;
peteratsl 69:1bcf3f0174c9 205 break;
peteratsl 69:1bcf3f0174c9 206 // 0x04 door locking 0.8 in2 in3 d2 cts
peteratsl 69:1bcf3f0174c9 207 case b4_id :
peteratsl 69:1bcf3f0174c9 208 case bA4_id : led1 = !led1;
peteratsl 69:1bcf3f0174c9 209 b4 = !b4;
peteratsl 69:1bcf3f0174c9 210 wait(0.8);
peteratsl 69:1bcf3f0174c9 211 b4 = !b4;
peteratsl 69:1bcf3f0174c9 212 break;
peteratsl 69:1bcf3f0174c9 213 // 0x05 remote start 2x .25 in1 d1 txd
peteratsl 69:1bcf3f0174c9 214 case b5_id :
peteratsl 69:1bcf3f0174c9 215 case bA5_id : led1 = !led1;
peteratsl 69:1bcf3f0174c9 216 b5 = !b5;
peteratsl 69:1bcf3f0174c9 217 wait(0.25);
peteratsl 69:1bcf3f0174c9 218 b5 = !b5;
peteratsl 69:1bcf3f0174c9 219
peteratsl 69:1bcf3f0174c9 220 b5 = !b5;
peteratsl 69:1bcf3f0174c9 221 wait(0.25);
peteratsl 69:1bcf3f0174c9 222 b5 = !b5;
peteratsl 69:1bcf3f0174c9 223 break;
peteratsl 69:1bcf3f0174c9 224
peteratsl 69:1bcf3f0174c9 225 case b6_id :
peteratsl 69:1bcf3f0174c9 226 case bA6_id : led1 = !led1;
peteratsl 69:1bcf3f0174c9 227 b6 = !b6;
peteratsl 69:1bcf3f0174c9 228 break;
peteratsl 69:1bcf3f0174c9 229 // 0x07 trip alarm 0.8 in4 d3 rts
peteratsl 69:1bcf3f0174c9 230 case b7_id :
peteratsl 69:1bcf3f0174c9 231 case bA7_id : led1 = !led1;
peteratsl 69:1bcf3f0174c9 232 b7 = !b7;
peteratsl 69:1bcf3f0174c9 233 wait(0.8);
peteratsl 69:1bcf3f0174c9 234 b7 = !b7;
peteratsl 69:1bcf3f0174c9 235 break;
peteratsl 69:1bcf3f0174c9 236
peteratsl 69:1bcf3f0174c9 237 case b8_id :
peteratsl 69:1bcf3f0174c9 238 case bA8_id : led1 = !led1;
peteratsl 69:1bcf3f0174c9 239 b8 = !b8;
peteratsl 69:1bcf3f0174c9 240 break;
peteratsl 69:1bcf3f0174c9 241
peteratsl 69:1bcf3f0174c9 242 case b9_id :
peteratsl 69:1bcf3f0174c9 243 case bA9_id : led1 = !led1;
peteratsl 69:1bcf3f0174c9 244 b9 = !b9;
peteratsl 69:1bcf3f0174c9 245 break;
peteratsl 69:1bcf3f0174c9 246
peteratsl 69:1bcf3f0174c9 247 case b0_id :
peteratsl 69:1bcf3f0174c9 248 case bA0_id : led1 = !led1;
peteratsl 69:1bcf3f0174c9 249 b0 = !b0;
peteratsl 69:1bcf3f0174c9 250 break;
peteratsl 69:1bcf3f0174c9 251
peteratsl 69:1bcf3f0174c9 252 default : blinky();
peteratsl 69:1bcf3f0174c9 253 ret = 1;
peteratsl 69:1bcf3f0174c9 254 break;
peteratsl 69:1bcf3f0174c9 255 }
peteratsl 69:1bcf3f0174c9 256 return ret;
peteratsl 69:1bcf3f0174c9 257 }