Dissertation project, looking at BLE communication in cars. This project is BLE peripheral acting as car indicator stalk

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_GATT_test1 by Alexander Lea

Committer:
alexanderlea
Date:
Thu Mar 12 12:52:26 2015 +0000
Revision:
9:0ed64b14d46b
Parent:
6:1d7e5fcc8d00
Child:
10:2c5c202c69a5
Changed to deal with int arrays rather than int values :)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
alexanderlea 0:af868ad47854 1 /*
alexanderlea 0:af868ad47854 2 * TODO:
alexanderlea 0:af868ad47854 3 * - Make it work!
alexanderlea 0:af868ad47854 4 */
alexanderlea 0:af868ad47854 5
alexanderlea 0:af868ad47854 6 #include "mbed.h"
alexanderlea 0:af868ad47854 7 #include "BLEDevice.h"
alexanderlea 4:ac0ee88ea0ed 8 #include "BroadcasterService.h"
alexanderlea 0:af868ad47854 9 #include <string>
alexanderlea 0:af868ad47854 10
alexanderlea 0:af868ad47854 11 using namespace std;
alexanderlea 0:af868ad47854 12
alexanderlea 0:af868ad47854 13 BLEDevice ble;
alexanderlea 0:af868ad47854 14 DigitalOut led1(LED1);
alexanderlea 0:af868ad47854 15 Serial pc(USBTX, USBRX);
alexanderlea 0:af868ad47854 16
alexanderlea 0:af868ad47854 17 /*Variable Declarations*/
alexanderlea 3:f3d20b36b7ea 18 const static char DEVICE_NAME[] = "BLE_Broadcaster";
alexanderlea 5:b3f8e10b9602 19 static volatile bool go = false;
alexanderlea 0:af868ad47854 20
alexanderlea 0:af868ad47854 21 void blink(void)
alexanderlea 0:af868ad47854 22 {
alexanderlea 0:af868ad47854 23 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
alexanderlea 5:b3f8e10b9602 24 go = true;
alexanderlea 5:b3f8e10b9602 25
alexanderlea 0:af868ad47854 26 }
alexanderlea 0:af868ad47854 27
alexanderlea 0:af868ad47854 28 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
alexanderlea 0:af868ad47854 29 {
alexanderlea 0:af868ad47854 30 pc.printf("Disconnected! - start advertising. Handle:%d, Reason:0x%02x\r\n", handle, reason);
alexanderlea 0:af868ad47854 31 ble.startAdvertising();
alexanderlea 0:af868ad47854 32 }
alexanderlea 0:af868ad47854 33
alexanderlea 0:af868ad47854 34 void connectionCallback(Gap::Handle_t handle, Gap::addr_type_t peerAddrType, const Gap::address_t peerAddr, const Gap::ConnectionParams_t *parms)
alexanderlea 0:af868ad47854 35 {
alexanderlea 0:af868ad47854 36 pc.printf("Connected! - stop advertising. Handle:%d, eType:%d, Add:%u.\r\n", handle, peerAddrType, peerAddr);
alexanderlea 3:f3d20b36b7ea 37 ble.stopAdvertising();
alexanderlea 0:af868ad47854 38 }
alexanderlea 0:af868ad47854 39
alexanderlea 0:af868ad47854 40 int main(void)
alexanderlea 4:ac0ee88ea0ed 41 {
alexanderlea 3:f3d20b36b7ea 42 //TODO: I want to be able to send something like:
alexanderlea 9:0ed64b14d46b 43 uint8_t cmdOn[8] = { 0x4d,0x32,0x81,0xc0,0x01,0x01,0x01,0x01 };
alexanderlea 9:0ed64b14d46b 44 uint8_t cmdOff[8] = { 0x4d,0x32,0x81,0xc0,0x00,0x00,0x00,0x00 };
alexanderlea 3:f3d20b36b7ea 45 //with first 4 bits being type code, and last 4 being command, or something
alexanderlea 4:ac0ee88ea0ed 46
alexanderlea 9:0ed64b14d46b 47 //uint8_t cmdOn = 0x01;
alexanderlea 9:0ed64b14d46b 48 //uint8_t cmdOff = 0x02;
alexanderlea 4:ac0ee88ea0ed 49
alexanderlea 0:af868ad47854 50 //blinky
alexanderlea 4:ac0ee88ea0ed 51 led1 = 1;
alexanderlea 0:af868ad47854 52 Ticker t;
alexanderlea 6:1d7e5fcc8d00 53 t.attach(blink, 5.0f);
alexanderlea 0:af868ad47854 54
alexanderlea 0:af868ad47854 55 //Create BLE stuff
alexanderlea 0:af868ad47854 56 ble.init();
alexanderlea 0:af868ad47854 57 ble.onDisconnection(disconnectionCallback);
alexanderlea 0:af868ad47854 58 ble.onConnection(connectionCallback);
alexanderlea 0:af868ad47854 59
alexanderlea 0:af868ad47854 60 //CarCommsService commsService(ble, cmd);
alexanderlea 9:0ed64b14d46b 61 BroadcasterService broadcasterService(ble);
alexanderlea 4:ac0ee88ea0ed 62
alexanderlea 0:af868ad47854 63 /*
alexanderlea 0:af868ad47854 64 **BREDR_NOT_SUPPORTED = BLE only
alexanderlea 0:af868ad47854 65 **LE_GENERAL_DISCOVERABLE = Device is discoverable at any moment (no time out)
alexanderlea 0:af868ad47854 66 **ADV_CONNECTABLE_UNDIRECTED = Any central device can connect
alexanderlea 0:af868ad47854 67 */
alexanderlea 0:af868ad47854 68 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
alexanderlea 0:af868ad47854 69 // ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
alexanderlea 0:af868ad47854 70 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
alexanderlea 0:af868ad47854 71 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
alexanderlea 0:af868ad47854 72
alexanderlea 0:af868ad47854 73 //Advertise
alexanderlea 0:af868ad47854 74 ble.setAdvertisingInterval(160); /* 1s; in multiples of 0.625ms. */ //was 1600
alexanderlea 0:af868ad47854 75 ble.startAdvertising();
alexanderlea 0:af868ad47854 76
alexanderlea 0:af868ad47854 77 pc.printf("Advertising node %s\n\r", DEVICE_NAME);
alexanderlea 4:ac0ee88ea0ed 78 bool x = true;
alexanderlea 0:af868ad47854 79
alexanderlea 0:af868ad47854 80 while(true) {
alexanderlea 0:af868ad47854 81
alexanderlea 2:f538ff758828 82 ble.waitForEvent(); // this will return upon any system event (such as an interrupt or a ticker wakeup)
alexanderlea 5:b3f8e10b9602 83 if(go) {
alexanderlea 5:b3f8e10b9602 84 if(x) {
alexanderlea 5:b3f8e10b9602 85 pc.printf("Command = %u\r\n", cmdOn);
alexanderlea 5:b3f8e10b9602 86 broadcasterService.sendCommand(cmdOn);
alexanderlea 5:b3f8e10b9602 87 x = false;
alexanderlea 5:b3f8e10b9602 88 } else {
alexanderlea 5:b3f8e10b9602 89 pc.printf("Command = %u\r\n", cmdOff);
alexanderlea 5:b3f8e10b9602 90 broadcasterService.sendCommand(cmdOff);
alexanderlea 5:b3f8e10b9602 91 x = true;
alexanderlea 5:b3f8e10b9602 92 }
alexanderlea 5:b3f8e10b9602 93 go = false;
alexanderlea 0:af868ad47854 94 }
alexanderlea 0:af868ad47854 95 }
alexanderlea 0:af868ad47854 96 }