source for demo Book And Plug

Dependencies:   APA102

Dependents:   BookAndPlug

Committer:
vrou44
Date:
Thu May 17 17:12:17 2018 +0000
Revision:
1:8c2e60bafc91
Parent:
0:5648c217e527
Initial release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vrou44 0:5648c217e527 1 /*
vrou44 0:5648c217e527 2 *
vrou44 0:5648c217e527 3 */
vrou44 0:5648c217e527 4 #include "mbed.h"
vrou44 0:5648c217e527 5 #include "string.h"
vrou44 0:5648c217e527 6 #include "mbed_events.h"
vrou44 0:5648c217e527 7 #include "ble/BLE.h"
vrou44 0:5648c217e527 8 #include "ble/services/UARTService.h"
vrou44 0:5648c217e527 9 #if 0
vrou44 0:5648c217e527 10 #include "X_NUCLEO_IDB0XA1/x-nucleo-idb0xa1/bluenrg-hci/ble_hci.h"
vrou44 0:5648c217e527 11 #endif
vrou44 0:5648c217e527 12
vrou44 0:5648c217e527 13 #include "BNP_Ctrl.h"
vrou44 0:5648c217e527 14 #include "BltLink.h"
vrou44 0:5648c217e527 15
vrou44 0:5648c217e527 16 #define NEED_CONSOLE_OUTPUT 1
vrou44 0:5648c217e527 17
vrou44 0:5648c217e527 18 #if NEED_CONSOLE_OUTPUT
vrou44 0:5648c217e527 19 #define PRINTF(...) { printf(__VA_ARGS__); }
vrou44 0:5648c217e527 20 #else
vrou44 0:5648c217e527 21 #define PRINTF(...)
vrou44 0:5648c217e527 22 #endif
vrou44 0:5648c217e527 23
vrou44 0:5648c217e527 24 // static
vrou44 0:5648c217e527 25 BltLink *BltLink::singleInstP = 0 ;
vrou44 0:5648c217e527 26
vrou44 0:5648c217e527 27 const SecurityManager::Passkey_t pinCode = {'6' , '5' , '4' , '3', '2', '1' } ;
vrou44 0:5648c217e527 28
vrou44 0:5648c217e527 29 void BltLink::connectionCallback(const Gap::ConnectionCallbackParams_t *params)
vrou44 0:5648c217e527 30 {
vrou44 0:5648c217e527 31 BltLink &bltLink = BltLink::getInst() ;
vrou44 0:5648c217e527 32 bltLink.connectionHandle = params->handle ;
vrou44 0:5648c217e527 33 bltLink.connectedB = true ;
vrou44 0:5648c217e527 34 printf ("Connected!\n\r") ;
vrou44 0:5648c217e527 35 }
vrou44 0:5648c217e527 36
vrou44 0:5648c217e527 37 //static
vrou44 0:5648c217e527 38 void BltLink::disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
vrou44 0:5648c217e527 39 {
vrou44 0:5648c217e527 40 BltLink &bltLink = BltLink::getInst() ;
vrou44 0:5648c217e527 41 bltLink.connectedB = false ;
vrou44 0:5648c217e527 42 printf ("Disconnected!\n\r");
vrou44 0:5648c217e527 43 BLE::Instance().startAdvertising();
vrou44 0:5648c217e527 44 }
vrou44 0:5648c217e527 45
vrou44 0:5648c217e527 46
vrou44 0:5648c217e527 47 // replace these supplied by mbed.
vrou44 0:5648c217e527 48 void UARTService::onDataWritten(const GattWriteCallbackParams *params)
vrou44 0:5648c217e527 49 {
vrou44 0:5648c217e527 50 if (params->handle == getTXCharacteristicHandle()) {
vrou44 0:5648c217e527 51 uint16_t bytesRead = params->len;
vrou44 0:5648c217e527 52 PRINTF ("UARTService::received %u bytes\n\r", bytesRead);
vrou44 0:5648c217e527 53 if (bytesRead <= BLE_UART_SERVICE_MAX_DATA_LEN) {
vrou44 0:5648c217e527 54 numBytesReceived = bytesRead;
vrou44 0:5648c217e527 55 receiveBufferIndex = 0;
vrou44 0:5648c217e527 56 memcpy(receiveBuffer, params->data, numBytesReceived);
vrou44 0:5648c217e527 57 if (!strncmp((char*)receiveBuffer, "$RESERVE", numBytesReceived)) {
vrou44 0:5648c217e527 58 printf("\r\nBltLink : msg RESERVE\n") ;
vrou44 0:5648c217e527 59 BNP_Ctrl::getInst().signalEvent(BNP_Ctrl::oprTerminalReserve) ;
vrou44 0:5648c217e527 60 }
vrou44 0:5648c217e527 61 if (!strncmp((char*)receiveBuffer, "$RELEASED", numBytesReceived)) {
vrou44 0:5648c217e527 62 printf("\r\nBltLink : msg RELEAS\n") ;
vrou44 0:5648c217e527 63 BNP_Ctrl::getInst().signalEvent(BNP_Ctrl::oprTerminalRelease) ;
vrou44 0:5648c217e527 64 }
vrou44 0:5648c217e527 65 if (!strncmp((char*)receiveBuffer, "$PLUG_E", numBytesReceived)) {
vrou44 0:5648c217e527 66 printf("\r\nBltLink : msg PLUG_E\n") ;
vrou44 0:5648c217e527 67 BltLink::getInst().selectedIrisPosition = BNP_Ctrl::irisAtPlugEPosition ;
vrou44 0:5648c217e527 68 BNP_Ctrl::getInst().signalEvent(BNP_Ctrl::oprPlugSelection) ;
vrou44 0:5648c217e527 69 }
vrou44 0:5648c217e527 70 if (!strncmp((char*)receiveBuffer, "$PLUG_C", numBytesReceived)) {
vrou44 0:5648c217e527 71 printf("\r\nBltLink : msg PLUG_C\n") ;
vrou44 0:5648c217e527 72 BltLink::getInst().selectedIrisPosition = BNP_Ctrl::irisAtPlugCPosition ; ;
vrou44 0:5648c217e527 73 BNP_Ctrl::getInst().signalEvent(BNP_Ctrl::oprPlugSelection) ;
vrou44 0:5648c217e527 74 }
vrou44 0:5648c217e527 75 if (!strncmp((char*)receiveBuffer, "$RESUME", numBytesReceived)) {
vrou44 0:5648c217e527 76 printf("\r\nBltLink : msg RESUME\n") ;
vrou44 0:5648c217e527 77 BNP_Ctrl::getInst().signalEvent(BNP_Ctrl::oprResumptionRequest) ;
vrou44 0:5648c217e527 78 }
vrou44 0:5648c217e527 79 }
vrou44 0:5648c217e527 80 }
vrou44 0:5648c217e527 81 }
vrou44 0:5648c217e527 82
vrou44 0:5648c217e527 83
vrou44 0:5648c217e527 84 void BltLink::printMacAddress()
vrou44 0:5648c217e527 85 {
vrou44 0:5648c217e527 86 /* Print out device MAC address to the console*/
vrou44 0:5648c217e527 87 Gap::AddressType_t addr_type;
vrou44 0:5648c217e527 88 Gap::Address_t address;
vrou44 0:5648c217e527 89 BLE::Instance().gap().getAddress(&addr_type, address);
vrou44 0:5648c217e527 90 printf("DEVICE MAC ADDRESS: ");
vrou44 0:5648c217e527 91 for (int i = 5; i >= 1; i--){
vrou44 0:5648c217e527 92 printf("%02x:", address[i]);
vrou44 0:5648c217e527 93 }
vrou44 0:5648c217e527 94 printf("%02x\r\n", address[0]);
vrou44 0:5648c217e527 95 }
vrou44 0:5648c217e527 96
vrou44 0:5648c217e527 97 #if 0
vrou44 0:5648c217e527 98 //static
vrou44 0:5648c217e527 99 void BltLink::updateRssi(void)
vrou44 0:5648c217e527 100 {
vrou44 0:5648c217e527 101 BltLink &bltLink = getInst() ;
vrou44 0:5648c217e527 102 if (bltLink.connectedB) {
vrou44 0:5648c217e527 103 *bltLink.led1P = !*bltLink.led1P ;
vrou44 0:5648c217e527 104 hci_read_rssi(&bltLink.connectionHandle, &bltLink.rssi);
vrou44 0:5648c217e527 105 }
vrou44 0:5648c217e527 106 }
vrou44 0:5648c217e527 107
vrou44 0:5648c217e527 108 #endif
vrou44 0:5648c217e527 109 //static
vrou44 0:5648c217e527 110 void BltLink::scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
vrou44 0:5648c217e527 111 BLE &ble = BLE::Instance();
vrou44 0:5648c217e527 112 mainQueueP->call(Callback<void()>(&ble, &BLE::processEvents));
vrou44 0:5648c217e527 113 }
vrou44 0:5648c217e527 114
vrou44 0:5648c217e527 115
vrou44 0:5648c217e527 116 void BltLink::init(void)
vrou44 0:5648c217e527 117 {
vrou44 0:5648c217e527 118 *led1P = 0;
vrou44 0:5648c217e527 119
vrou44 0:5648c217e527 120 BLE &ble = BLE::Instance();
vrou44 0:5648c217e527 121 ble.onEventsToProcess(scheduleBleEventsProcessing);
vrou44 0:5648c217e527 122 ble.init();
vrou44 0:5648c217e527 123 for(; !ble.hasInitialized() ; ) ;
vrou44 0:5648c217e527 124 printf ("!!!! ble init done\r\n") ;
vrou44 0:5648c217e527 125 printMacAddress() ;
vrou44 0:5648c217e527 126 ble.gap().onConnection (connectionCallback);
vrou44 0:5648c217e527 127 ble.gap().onDisconnection (disconnectionCallback);
vrou44 0:5648c217e527 128
vrou44 0:5648c217e527 129 uartServicePtr = new UARTService(ble) ;
vrou44 0:5648c217e527 130
vrou44 0:5648c217e527 131 /* setup advertising */
vrou44 0:5648c217e527 132 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
vrou44 0:5648c217e527 133 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, //GapAdvertisingData::SHORTENED_LOCAL_NAME,
vrou44 0:5648c217e527 134 (const uint8_t *)"Bk&PlgA", sizeof("Bk&PlgA") - 1);
vrou44 0:5648c217e527 135 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
vrou44 0:5648c217e527 136 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
vrou44 0:5648c217e527 137 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
vrou44 0:5648c217e527 138 ble.setAdvertisingInterval(1000); /* in multiples of 0.625ms. */
vrou44 0:5648c217e527 139 ble.gap().startAdvertising();
vrou44 0:5648c217e527 140
vrou44 0:5648c217e527 141 // queue.call_every(250, updateRssi);
vrou44 0:5648c217e527 142 printf ("BltLink::init() done\n\r");
vrou44 0:5648c217e527 143 }