BLE project for PILLAR
Dependencies: BLE_API mbed simpleBLEControl
Fork of BLE_API_DEMO by
Diff: main.cpp
- Revision:
- 51:0bf1116bca52
- Parent:
- 50:4c02add9abba
- Child:
- 52:8e483d1199f9
--- a/main.cpp Wed Oct 22 17:08:57 2014 +0000 +++ b/main.cpp Sun Nov 02 20:44:53 2014 +0000 @@ -1,86 +1,66 @@ +// This code is for the nRF51822 that is used to provide a BLE interface to PILLar +// There is 1 service and 1 characteristic +// The service provides a way to write a step value from the phone to a stepper motor +// controlled by an Arduino #include "mbed.h" #include "BLEDevice.h" -BLEDevice ble; -DigitalOut led1(LED1); +BLEDevice ble; // Declare the ble device +Serial arduino(USBTX, USBRX); // serial interface to PC or Arduino controlling the stepper -const uint8_t LED1_UUID[LENGTH_OF_LONG_UUID] = { +// Arbit 128-bit UIDs generated from +// http://www.guidgenerator.com/online-guid-generator.aspx +// We need one for each service and characteristic +const uint8_t ARDUINO_WRITE_CHARACTERISTIC_UUID[LENGTH_OF_LONG_UUID] = { 0xfb, 0x71, 0xbc, 0xc0, 0x5a, 0x0c, 0x11, 0xe4, 0x91, 0xae, 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b }; -const uint8_t BUTTON_UUID[LENGTH_OF_LONG_UUID] = { - 0x7a, 0x77, 0xbe, 0x20, 0x5a, 0x0d, 0x11, 0xe4, - 0xa9, 0x5e, 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b -}; + const uint8_t TEST_SERVICE_UUID[LENGTH_OF_LONG_UUID] = { 0xb0, 0xbb, 0x58, 0x20, 0x5a, 0x0d, 0x11, 0xe4, - 0x93, 0xee, 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b -}; + 0x93, 0xee, 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}; -const static char DEVICE_NAME[] = "Nordic"; -static volatile bool is_button_pressed = false; -static volatile uint16_t led1_handler; +const static char DEVICE_NAME[] = "mediCAL BLE"; // For Advertisement +static volatile uint16_t writeToArduino_handler; // handler to echo data -uint8_t led_state, button_state; - -void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) -{ +// What happens on disconnect event +void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason){ ble.startAdvertising(); // restart advertising } -void changeLED(const GattCharacteristicWriteCBParams *eventDataP) { +// This function is called whenever data is written to the BLE device through the phone +void onDatawritten(const GattCharacteristicWriteCBParams *eventDataP) { + arduino.printf("Enter onDataWritten\n\r"); // eventDataP->charHandle is just uint16_t // it's used to dispatch the callbacks - if (eventDataP->charHandle == led1_handler) { - led1 = eventDataP->data[0] % 2; + if(eventDataP->charHandle==writeToArduino_handler){ // The data is for the motor + arduino.printf("Got some Arduino-specific info!\n\r"); + uint16_t bytesRead = eventDataP->len; + int turnSteps = *((int16_t *)eventDataP->data); + arduino.printf("%i\n\r", turnSteps); } } -void button1Pressed() { - button_state = 1; - is_button_pressed = true; -} -void button2Pressed() { - button_state = 2; - is_button_pressed = true; -} - int main(void) { - // button initialization - InterruptIn button1(BUTTON1); - InterruptIn button2(BUTTON2); - button1.mode(PullUp); - button2.mode(PullUp); - button1.rise(&button1Pressed); - button2.rise(&button2Pressed); - led1 = 0; + arduino.printf("Entered main\n\r"); - // just a simple service example - // o led1 characteristics, you can write from the phone to control led1 - // o button characteristics, you can read and get notified - GattCharacteristic led1_characteristics( - LED1_UUID, &led_state, sizeof(led_state), sizeof(led_state), - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); - led1_handler = led1_characteristics.getValueAttribute().getHandle(); + // You can write from the phone to control what is written to Arduino + GattCharacteristic writeToArduino_characteristics( + ARDUINO_WRITE_CHARACTERISTIC_UUID, NULL, sizeof(int16_t), sizeof(int16_t), + GattCharacteristic::BLE_GATT_FORMAT_SINT16 | //16bit signed INT + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | // has read + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); // and write properties + writeToArduino_handler = writeToArduino_characteristics.getValueAttribute().getHandle();// save the handler - GattCharacteristic button_characteristics( - BUTTON_UUID, &button_state, sizeof(button_state), sizeof(button_state), - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); - - const uint8_t TEST_SERVICE_UUID[LENGTH_OF_LONG_UUID] = { - 0xb0, 0xbb, 0x58, 0x20, 0x5a, 0x0d, 0x11, 0xe4, - 0x93, 0xee, 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}; - GattCharacteristic *charTable[] = {&led1_characteristics, &button_characteristics}; - GattService testService(TEST_SERVICE_UUID, charTable, + GattCharacteristic *charTable[] = {&writeToArduino_characteristics}; // Characteristic table + GattService testService(TEST_SERVICE_UUID, charTable, // Add the char(s) to this service sizeof(charTable) / sizeof(GattCharacteristic *)); // BLE setup, mainly we add service and callbacks ble.init(); ble.addService(testService); - ble.onDataWritten(&changeLED); + ble.onDataWritten(&onDatawritten); ble.onDisconnection(disconnectionCallback); // setup advertising @@ -93,13 +73,7 @@ ble.startAdvertising(); while (true) { - if (is_button_pressed) { - // if button pressed, we update the characteristics - is_button_pressed = false; - ble.updateCharacteristicValue(button_characteristics.getValueAttribute().getHandle(), - &button_state, sizeof(button_state)); - } else { + arduino.printf("Waiting for ble Event\n\r"); ble.waitForEvent(); - } } } \ No newline at end of file