Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API mbed nRF51822
Diff: main.cpp
- Revision:
- 0:af868ad47854
- Child:
- 1:ebdf445c4bcc
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Sun Jan 18 15:17:53 2015 +0000
@@ -0,0 +1,201 @@
+/*
+* TODO:
+* - Make it work!
+*/
+
+#include "mbed.h"
+#include "BLEDevice.h"
+#include "CarCommsService.h"
+#include "TestGattService.h"
+#include <string>
+
+using namespace std;
+
+BLEDevice ble;
+DigitalOut led1(LED1);
+Serial pc(USBTX, USBRX);
+
+/*Variable Declarations*/
+const static char DEVICE_NAME[] = "BLE_CAR_NODE";
+
+///*
+//* Define a custom UUID, first as an array of uint8_t and then convert to
+//* a proper UUID later. The UUID must be 16 bytes (128-bits, 16 letters)
+//* long - here we have padded out to 16 bytes by adding an extra '0' at
+//* the end. Make sure you fill more than 4 bytes otherwise it will count
+//* as a 'short' code and you could end up using a predefined value from
+//* the BLE spec.
+//*/
+//uint8_t raw_characteristic_uuid[16] = {
+// 'M', 'Y', '_', 'T',
+// 'E', 'S', 'T', '_',
+// 'C', 'H', 'A', 'R',
+// 0, 0, 0, 0
+//};
+//
+//// Create a proper UUID
+//UUID characteristic_uuid = {0x4d,0x32,0x81,0xc0,0x86,0xd1,0x11,0xe4,0xb0,0x84,0x00,0x02,0xa5,0xd5,0xc5,0x10};
+
+uint8_t raw_service_uuid[16] = {
+ 'M', 'Y', '_', 'T',
+ 'E', 'S', 'T', '_',
+ 'S', 'E', 'R', 'V',
+ 'I', 'C', 'E', 0
+};
+
+static volatile bool connected;
+//Ticker ticker;
+//
+//
+//// Setup some dummy properties for our characteristic
+//static uint8_t my_char_values[2] = { 15, 10 };
+//
+///*
+//* Here we create our Characteristic adding in the dummy parameter values
+//* we just setup, we also make it readable and writeable meaning that a
+//* central can read and update the parameters we have stored.
+//*/
+//GattCharacteristic pattern(
+// characteristic_uuid,
+// my_char_values,
+// sizeof(my_char_values),
+// sizeof(my_char_values),
+// GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE
+//);
+//
+///*
+//* List the Characteristics of our custom Service, can have one or more
+//* of these each with a custom UUID and parameter values.
+//*/
+//GattCharacteristic *my_service_chars[] = {
+// &new_alert,
+//};
+//
+// Now setup a custom Service UUID, in the same fashion as for the Characteristic
+//uint8_t raw_service_uuid[16] = {
+// 'M', 'Y', '_', 'T',
+// 'E', 'S', 'T', '_',
+// 'S', 'E', 'R', 'V',
+// 'I', 'C', 'E', 0
+//};
+
+
+
+//
+//UUID service_uuid = UUID(raw_service_uuid);
+//// Setup the Service with the UUID and all of the Characteristics
+//GattService my_service(
+// service_uuid,
+// my_service_chars,
+// sizeof(my_service_chars) / sizeof(GattCharacteristic *)
+//);
+//
+///*
+//* Now list the long UUIDs of the services we offer, these will be bundled into the
+//* advertisement. It may look like repetition of 'raw_service_uuid' but here you can
+//* list multiple UUIDs one after another.
+//*/
+//static const uint8_t uuid128_list[] = {
+// 'M', 'Y', '_', 'T', 'E', 'S', 'T', '_', 'S', 'E', 'R', 'V', 'I', 'C', 'E', 0
+// // List more long UUIDs below...
+//};
+
+void blink(void)
+{
+ led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
+}
+
+void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
+{
+ pc.printf("Disconnected! - start advertising. Handle:%d, Reason:0x%02x\r\n", handle, reason);
+ connected=false;
+ ble.startAdvertising();
+}
+
+
+//Handle_t, addr_type_t peerAddrType, const address_t peerAddr, const ConnectionParams_t *
+void connectionCallback(Gap::Handle_t handle, Gap::addr_type_t peerAddrType, const Gap::address_t peerAddr, const Gap::ConnectionParams_t *parms)
+{
+ pc.printf("Connected! - stop advertising. Handle:%d, eType:%d, Add:%u.\r\n", handle, peerAddrType, peerAddr);
+
+ connected = true;
+// ble.stopAdvertising();
+}
+
+int main(void)
+{
+ uint8_t testLevel = 50;
+
+ //blinky
+ led1 = 1;
+ Ticker t;
+ t.attach(blink, 1.0f);
+
+ //Create BLE stuff
+ ble.init();
+ ble.onDisconnection(disconnectionCallback);
+ ble.onConnection(connectionCallback);
+
+ //CarCommsService commsService(ble, cmd);
+ TestGattService testService(ble, testLevel);
+
+
+ /*
+ **BREDR_NOT_SUPPORTED = BLE only
+ **LE_GENERAL_DISCOVERABLE = Device is discoverable at any moment (no time out)
+ **ADV_CONNECTABLE_UNDIRECTED = Any central device can connect
+ */
+ ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
+// ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
+ ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+ ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+
+ //Advertise
+ ble.setAdvertisingInterval(160); /* 1s; in multiples of 0.625ms. */ //was 1600
+ ble.startAdvertising();
+
+ pc.printf("Advertising node %s\n\r", DEVICE_NAME);
+
+//GATT service stuff - referring to CarCommsService.h
+//string testCommand = "Hello world";
+// CarCommsService carComm(ble, 150);
+//
+// uint8_t x = carComm.getCommand();
+//
+// pc.printf("Command: %u \n", x);
+
+
+ while(true) {
+
+ ble.waitForEvent(); // this will return upon any system event (such as an interrupt or a ticker wakeup)
+
+ //if(led1 == 1) {
+// cmd = 1;
+// } else {
+// cmd = 0;
+// }
+//
+// commsService.sendCommand(0);
+// pc.printf("Command: %u\r\n", commsService.getCommand());
+
+ testLevel++;
+
+ if (testLevel > 100) {
+ testLevel = 20;
+ }
+
+ pc.printf("Level = %u\r\n", testLevel);
+ testService.updateTestLevel(testLevel);
+
+ //if(connected) {
+// led1 = 1;
+// //read data
+// //pc.printf("Command: %u \n", carComm.getCommand());
+// } else {
+// led1 = 0;
+// pc.printf("Waiting...\n\r");
+// ble.waitForEvent();
+// connected = false;
+// }
+ }
+}
\ No newline at end of file