van

Dependencies:   BLE_API mbed VAN

Dependents:   VAN

Fork of BLE_iBeacon_POC by Fallen Fate

Committer:
peteratsl
Date:
Mon Nov 16 18:46:46 2015 +0000
Revision:
70:1281fe7166fb
Parent:
69:1bcf3f0174c9
VAN

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 70:1281fe7166fb 55
peteratsl 69:1bcf3f0174c9 56
peteratsl 69:1bcf3f0174c9 57 const uint8_t bA1_id = 0x31;
peteratsl 69:1bcf3f0174c9 58 const uint8_t bA2_id = 0x32;
peteratsl 69:1bcf3f0174c9 59 const uint8_t bA3_id = 0x33;
peteratsl 69:1bcf3f0174c9 60 const uint8_t bA4_id = 0x34;
peteratsl 69:1bcf3f0174c9 61 const uint8_t bA5_id = 0x35;
peteratsl 69:1bcf3f0174c9 62 const uint8_t bA6_id = 0x36;
peteratsl 70:1281fe7166fb 63
peteratsl 69:1bcf3f0174c9 64
peteratsl 69:1bcf3f0174c9 65
peteratsl 70:1281fe7166fb 66 PinName b1_p = P0_4;
peteratsl 70:1281fe7166fb 67 PinName b2_p = P0_5;
peteratsl 70:1281fe7166fb 68 PinName b3_p = P0_11; //RXD
peteratsl 69:1bcf3f0174c9 69 PinName b4_p = P0_10; //CTX
peteratsl 69:1bcf3f0174c9 70 PinName b5_p = P0_9; //TXD
peteratsl 70:1281fe7166fb 71 PinName b6_p = P0_8; //RTS
peteratsl 70:1281fe7166fb 72
peteratsl 70:1281fe7166fb 73
peteratsl 69:1bcf3f0174c9 74
peteratsl 69:1bcf3f0174c9 75 //setup led
peteratsl 69:1bcf3f0174c9 76 DigitalOut led1(P0_19);
peteratsl 69:1bcf3f0174c9 77
peteratsl 69:1bcf3f0174c9 78 //setup digital outs
peteratsl 69:1bcf3f0174c9 79 DigitalOut b1(b1_p);
peteratsl 69:1bcf3f0174c9 80 DigitalOut b2(b2_p);
peteratsl 69:1bcf3f0174c9 81 DigitalOut b3(b3_p);
peteratsl 69:1bcf3f0174c9 82 DigitalOut b4(b4_p);
peteratsl 69:1bcf3f0174c9 83 DigitalOut b5(b5_p);
peteratsl 69:1bcf3f0174c9 84 DigitalOut b6(b6_p);
peteratsl 70:1281fe7166fb 85
fallenfate 67:81b595625ce0 86
fallenfate 67:81b595625ce0 87 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason);
fallenfate 67:81b595625ce0 88 void onDataWritten(const GattCharacteristicWriteCBParams *params);
peteratsl 69:1bcf3f0174c9 89 int flip(char value);
fallenfate 67:81b595625ce0 90
fallenfate 67:81b595625ce0 91 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
fallenfate 67:81b595625ce0 92 {
fallenfate 67:81b595625ce0 93 ble.startAdvertising();
fallenfate 67:81b595625ce0 94 }
fallenfate 67:81b595625ce0 95
fallenfate 67:81b595625ce0 96 void onDataWritten(const GattCharacteristicWriteCBParams *params)
fallenfate 67:81b595625ce0 97 {
peteratsl 69:1bcf3f0174c9 98 flip(params->data[0]);
fallenfate 67:81b595625ce0 99 }
mbedAustin 56:56bc0cab3916 100
ktownsend 0:7613d21e5974 101 int main(void)
ktownsend 0:7613d21e5974 102 {
peteratsl 69:1bcf3f0174c9 103 /* initialize stuff */
peteratsl 69:1bcf3f0174c9 104 led1 = 1;
fallenfate 67:81b595625ce0 105
peteratsl 70:1281fe7166fb 106 b1 = 1; // 0x01 flash hazards in5 d4 p04
peteratsl 70:1281fe7166fb 107 b2 = 1; // 0x02 horn in6 d5 p05
peteratsl 69:1bcf3f0174c9 108 b3 = 1; // 0x03 headlights in7 d6 p15
peteratsl 69:1bcf3f0174c9 109 b4 = 1; // 0x04 door locking 0.8 in2 in3 d2 cts
peteratsl 69:1bcf3f0174c9 110 b5 = 1; // 0x05 remote start 2x .25 in1 d1 txd
peteratsl 70:1281fe7166fb 111 b6 = 1; // 0x06 trip alarm 0.8 in4 d3 rts
peteratsl 69:1bcf3f0174c9 112
peteratsl 70:1281fe7166fb 113
peteratsl 69:1bcf3f0174c9 114
peteratsl 69:1bcf3f0174c9 115
Rohit Grover 11:6774f4827024 116 ble.init();
fallenfate 67:81b595625ce0 117
fallenfate 67:81b595625ce0 118 ble.onDisconnection(disconnectionCallback);
fallenfate 67:81b595625ce0 119 ble.onDataWritten(onDataWritten);
fallenfate 67:81b595625ce0 120
fallenfate 67:81b595625ce0 121
fallenfate 67:81b595625ce0 122 uart = new UARTService(ble);
mbedAustin 56:56bc0cab3916 123 iBeaconService ibeacon(ble, uuid, majorNumber, minorNumber, txPower);
mbedAustin 53:f9ec2c7a47f5 124
rgrover1 60:3034dc913ea1 125 ble.setAdvertisingInterval(1000); /* 1000ms. */
Rohit Grover 19:869d8c7306b4 126 ble.startAdvertising();
ktownsend 0:7613d21e5974 127
mbedAustin 56:56bc0cab3916 128 while(1) {
mbedAustin 56:56bc0cab3916 129 ble.waitForEvent(); // allows or low power operation
ktownsend 0:7613d21e5974 130 }
Rohit Grover 10:391c1acf4b9d 131 }
peteratsl 69:1bcf3f0174c9 132
peteratsl 69:1bcf3f0174c9 133 void testAll(void)
peteratsl 69:1bcf3f0174c9 134 {
peteratsl 69:1bcf3f0174c9 135 for(int i=0; i<2; i++)
peteratsl 69:1bcf3f0174c9 136 {
peteratsl 69:1bcf3f0174c9 137 led1 = !led1;
peteratsl 69:1bcf3f0174c9 138 b1 = !b1;
peteratsl 69:1bcf3f0174c9 139 b2 = !b2;
peteratsl 69:1bcf3f0174c9 140 b3 = !b3;
peteratsl 69:1bcf3f0174c9 141 b4 = !b4;
peteratsl 69:1bcf3f0174c9 142 b5 = !b5;
peteratsl 69:1bcf3f0174c9 143 b6 = !b6;
peteratsl 69:1bcf3f0174c9 144 wait(1);
peteratsl 69:1bcf3f0174c9 145 }
peteratsl 69:1bcf3f0174c9 146 }
peteratsl 69:1bcf3f0174c9 147
peteratsl 69:1bcf3f0174c9 148 void blinky(void)
peteratsl 69:1bcf3f0174c9 149 {
peteratsl 69:1bcf3f0174c9 150 for(int i=0; i<15; i++)
peteratsl 69:1bcf3f0174c9 151 {
peteratsl 69:1bcf3f0174c9 152 led1 = !led1;
peteratsl 69:1bcf3f0174c9 153 wait(.3);
peteratsl 69:1bcf3f0174c9 154 }
peteratsl 69:1bcf3f0174c9 155 }
peteratsl 69:1bcf3f0174c9 156
peteratsl 69:1bcf3f0174c9 157 int flip(char value)
peteratsl 69:1bcf3f0174c9 158 {
peteratsl 69:1bcf3f0174c9 159 int ret = 0;
peteratsl 69:1bcf3f0174c9 160
peteratsl 69:1bcf3f0174c9 161 switch(value)
peteratsl 69:1bcf3f0174c9 162 {
peteratsl 70:1281fe7166fb 163 // 0x01 flash hazards in5 d4 p4
peteratsl 69:1bcf3f0174c9 164 case b1_id :
peteratsl 69:1bcf3f0174c9 165 case bA1_id : led1 = !led1;
peteratsl 69:1bcf3f0174c9 166 b1 = !b1;
peteratsl 69:1bcf3f0174c9 167 break;
peteratsl 70:1281fe7166fb 168 // 0x02 horn in6 d5 p5
peteratsl 69:1bcf3f0174c9 169 case b2_id :
peteratsl 69:1bcf3f0174c9 170 case bA2_id : led1 = !led1;
peteratsl 69:1bcf3f0174c9 171 b2 = !b2;
peteratsl 69:1bcf3f0174c9 172 break;
peteratsl 70:1281fe7166fb 173 // 0x03 headlights in7 d6 rxd
peteratsl 69:1bcf3f0174c9 174 case b3_id :
peteratsl 69:1bcf3f0174c9 175 case bA3_id : led1 = !led1;
peteratsl 69:1bcf3f0174c9 176 b3 = !b3;
peteratsl 69:1bcf3f0174c9 177 break;
peteratsl 69:1bcf3f0174c9 178 // 0x04 door locking 0.8 in2 in3 d2 cts
peteratsl 69:1bcf3f0174c9 179 case b4_id :
peteratsl 69:1bcf3f0174c9 180 case bA4_id : led1 = !led1;
peteratsl 69:1bcf3f0174c9 181 b4 = !b4;
peteratsl 69:1bcf3f0174c9 182 wait(0.8);
peteratsl 69:1bcf3f0174c9 183 b4 = !b4;
peteratsl 69:1bcf3f0174c9 184 break;
peteratsl 69:1bcf3f0174c9 185 // 0x05 remote start 2x .25 in1 d1 txd
peteratsl 69:1bcf3f0174c9 186 case b5_id :
peteratsl 69:1bcf3f0174c9 187 case bA5_id : led1 = !led1;
peteratsl 69:1bcf3f0174c9 188 b5 = !b5;
peteratsl 69:1bcf3f0174c9 189 wait(0.25);
peteratsl 69:1bcf3f0174c9 190 b5 = !b5;
peteratsl 69:1bcf3f0174c9 191
peteratsl 69:1bcf3f0174c9 192 b5 = !b5;
peteratsl 69:1bcf3f0174c9 193 wait(0.25);
peteratsl 69:1bcf3f0174c9 194 b5 = !b5;
peteratsl 69:1bcf3f0174c9 195 break;
peteratsl 69:1bcf3f0174c9 196
peteratsl 70:1281fe7166fb 197 // 0x06 trip alarm 0.8 in4 d3 rts
peteratsl 69:1bcf3f0174c9 198 case b6_id :
peteratsl 69:1bcf3f0174c9 199 case bA6_id : led1 = !led1;
peteratsl 69:1bcf3f0174c9 200 b6 = !b6;
peteratsl 69:1bcf3f0174c9 201 wait(0.8);
peteratsl 70:1281fe7166fb 202 b6 = !b6;
peteratsl 69:1bcf3f0174c9 203 break;
peteratsl 69:1bcf3f0174c9 204
peteratsl 69:1bcf3f0174c9 205 default : blinky();
peteratsl 69:1bcf3f0174c9 206 ret = 1;
peteratsl 69:1bcf3f0174c9 207 break;
peteratsl 69:1bcf3f0174c9 208 }
peteratsl 69:1bcf3f0174c9 209 return ret;
peteratsl 69:1bcf3f0174c9 210 }