Dependencies: BLE_API mbed BLE_nRF8001 DebounceIn
Diff: main.cpp
- Revision:
- 2:f266f102ca80
- Parent:
- 0:adcc621e5713
- Child:
- 3:b3153fd49501
diff -r 92b5d947147e -r f266f102ca80 main.cpp --- a/main.cpp Sat Mar 14 19:29:32 2015 +0000 +++ b/main.cpp Sat Mar 21 17:48:29 2015 +0000 @@ -1,109 +1,92 @@ #include "mbed.h" -#include "BLEDevice.h" -#include "nRF8001.h" +//#include "DebounceIn.h" +#include "BLEPeripheral.h" + +Serial serial(USBTX, USBRX); + +// The SPI construct, REQN and RDYN IO construct should be modified manually +// It depend on the board you are using and the REQN&RDYN configuration on BLE Shield +SPI spi(PTD2, PTD3, PTD1); +DigitalInOut BLE_RDY(PTD5); +DigitalInOut BLE_REQ(PTD0); +DigitalInOut BLE_RESET(PTA13); +DigitalIn pb(D4); +int count = 0; +unsigned char txbuf[16] = {0}; +unsigned char txlen = 0; + +/*----- BLE Utility -------------------------------------------------------------------------*/ +// create peripheral instance, see pinouts above +BLEPeripheral blePeripheral = BLEPeripheral(&BLE_REQ, &BLE_RDY, NULL); + +// create service +BLEService uartService = BLEService("713d0000503e4c75ba943148f18d941e"); + +// create characteristic +BLECharacteristic txCharacteristic = BLECharacteristic("713d0002503e4c75ba943148f18d941e", BLENotify, 20); +BLECharacteristic rxCharacteristic = BLECharacteristic("713d0003503e4c75ba943148f18d941e", BLEWriteWithoutResponse, 20); +/*--------------------------------------------------------------------------------------------*/ + +unsigned int interval = 0; +unsigned char count_on = 0; + +int main() +{ + serial.printf("Hello SmartD!\n"); + //serial.baud(115200); + serial.printf("Serial begin!\r\n"); + + pb.mode(PullUp); + wait(.001); + + /*----- BLE Utility ---------------------------------------------*/ + // set advertised local name and service UUID + blePeripheral.setLocalName("Sita"); + + blePeripheral.setAdvertisedServiceUuid(uartService.uuid()); + + // add service and characteristic + blePeripheral.addAttribute(uartService); + blePeripheral.addAttribute(rxCharacteristic); + blePeripheral.addAttribute(txCharacteristic); + + // begin initialization + blePeripheral.begin(); + /*---------------------------------------------------------------*/ + + serial.printf("BLE UART Peripheral begin!\r\n"); + + while(1) + { + BLECentral central = blePeripheral.central(); + + if (central) + { + // central connected to peripheral + serial.printf("Connected to central\r\n"); + while (central.connected()) + { + // central still connected to peripheral + if (!pb) + { + count++; + wait(.5); + //serial.printf("%d",count); + } + if(count>0) + {serial.printf("button pressed\r\n"); + count = 0; + wait (.5);} + } + } + + // central disconnected + else + { + serial.printf("Disconnected from central\r\n"); + } + } + } -BLEDevice ble; -DigitalOut led1(LED1); -InterruptIn button1(D1); -InterruptIn button2(D2); - -const uint8_t LED1_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 -}; - -const static char DEVICE_NAME[] = "Sita"; -static volatile bool is_button_pressed = false; -static volatile uint16_t led1_handler; - -uint8_t led_state, button_state; - -void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) -{ - ble.startAdvertising(); // restart advertising -} - -void changeLED(const GattCharacteristicWriteCBParams *eventDataP) { - // eventDataP->charHandle is just uint16_t - // it's used to dispatch the callbacks - if (eventDataP->charHandle == led1_handler) { - led1 = eventDataP->data[0] % 2; - } -} - -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; - - // 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(); - - 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, - sizeof(charTable) / sizeof(GattCharacteristic *)); - - // BLE setup, mainly we add service and callbacks - ble.init(); - ble.addService(testService); - ble.onDataWritten(&changeLED); - ble.onDisconnection(disconnectionCallback); - - // setup advertising - ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | - GapAdvertisingData::LE_GENERAL_DISCOVERABLE); - ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, - (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); - ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); - ble.setAdvertisingInterval(1600); /* 1000ms; in multiples of 0.625ms. */ - 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 { - ble.waitForEvent(); - } - } -} \ No newline at end of file + \ No newline at end of file