Gestione invio dati tramite seriale bluetooth

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Fork of BLE_HeartRate_IDB0XA1 by ST

Committer:
fdalforno
Date:
Tue Dec 27 09:48:25 2016 +0000
Revision:
22:3f0b2c0bf9f3
Parent:
21:0e7c08f5386f
Child:
23:8315afc7bc95
Gestione uart su bluetooth con invio e ricezione dati

Who changed what in which revision?

UserRevisionLine numberNew contents of line
screamer 0:eb7f02ad28a7 1 /* mbed Microcontroller Library
screamer 0:eb7f02ad28a7 2 * Copyright (c) 2006-2015 ARM Limited
screamer 0:eb7f02ad28a7 3 *
screamer 0:eb7f02ad28a7 4 * Licensed under the Apache License, Version 2.0 (the "License");
screamer 0:eb7f02ad28a7 5 * you may not use this file except in compliance with the License.
screamer 0:eb7f02ad28a7 6 * You may obtain a copy of the License at
screamer 0:eb7f02ad28a7 7 *
screamer 0:eb7f02ad28a7 8 * http://www.apache.org/licenses/LICENSE-2.0
screamer 0:eb7f02ad28a7 9 *
screamer 0:eb7f02ad28a7 10 * Unless required by applicable law or agreed to in writing, software
screamer 0:eb7f02ad28a7 11 * distributed under the License is distributed on an "AS IS" BASIS,
screamer 0:eb7f02ad28a7 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
screamer 0:eb7f02ad28a7 13 * See the License for the specific language governing permissions and
screamer 0:eb7f02ad28a7 14 * limitations under the License.
screamer 0:eb7f02ad28a7 15 */
fdalforno 22:3f0b2c0bf9f3 16 #include <string.h>
screamer 0:eb7f02ad28a7 17 #include "mbed.h"
screamer 0:eb7f02ad28a7 18 #include "ble/BLE.h"
fdalforno 22:3f0b2c0bf9f3 19 #include "UARTService.h"
fdalforno 22:3f0b2c0bf9f3 20
fdalforno 22:3f0b2c0bf9f3 21 #define BUFFER_LEN 256
screamer 0:eb7f02ad28a7 22
fdalforno 22:3f0b2c0bf9f3 23 DigitalOut led1(LED1);
fdalforno 22:3f0b2c0bf9f3 24 UARTService* uart;
fdalforno 22:3f0b2c0bf9f3 25 Serial pc(USBTX, USBRX);
screamer 0:eb7f02ad28a7 26
fdalforno 22:3f0b2c0bf9f3 27 const static char DEVICE_NAME[] = "Rob1";
fdalforno 22:3f0b2c0bf9f3 28
fdalforno 22:3f0b2c0bf9f3 29 char buffer[BUFFER_LEN];
apalmieri 13:227a0149b677 30
fdalforno 22:3f0b2c0bf9f3 31 /*Callback quando il dispositivo client si disconnette */
fdalforno 22:3f0b2c0bf9f3 32 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
fdalforno 22:3f0b2c0bf9f3 33 BLE::Instance().gap().startAdvertising();
fdalforno 22:3f0b2c0bf9f3 34
fdalforno 22:3f0b2c0bf9f3 35 pc.printf("Periferica disconnessa \r\n");
fdalforno 22:3f0b2c0bf9f3 36 }
screamer 0:eb7f02ad28a7 37
fdalforno 22:3f0b2c0bf9f3 38 void periodicCallback(void){
fdalforno 22:3f0b2c0bf9f3 39 led1 = !led1;
fdalforno 22:3f0b2c0bf9f3 40
fdalforno 22:3f0b2c0bf9f3 41 /*Per spedire qualcosa al client*/
fdalforno 22:3f0b2c0bf9f3 42 //char* str = "Ciao \r\n";
fdalforno 22:3f0b2c0bf9f3 43 //if (uart) uart->writeString(str);
screamer 0:eb7f02ad28a7 44 }
screamer 0:eb7f02ad28a7 45
fdalforno 22:3f0b2c0bf9f3 46 /**
fdalforno 22:3f0b2c0bf9f3 47 * Chiamata in caso di errore, ottengo il codice sulla console
fdalforno 22:3f0b2c0bf9f3 48 */
fdalforno 22:3f0b2c0bf9f3 49 void onBleInitError(BLE &ble, ble_error_t error){
fdalforno 22:3f0b2c0bf9f3 50 pc.printf("Errore interno %u \r\n",error);
screamer 0:eb7f02ad28a7 51 }
screamer 0:eb7f02ad28a7 52
fdalforno 22:3f0b2c0bf9f3 53
fdalforno 22:3f0b2c0bf9f3 54 void onDataWritten(const GattWriteCallbackParams *params){
fdalforno 22:3f0b2c0bf9f3 55 if ((uart != NULL) && (params->handle == uart->getTXCharacteristicHandle())) {
fdalforno 22:3f0b2c0bf9f3 56 uint16_t bytesRead = params->len;
fdalforno 22:3f0b2c0bf9f3 57
fdalforno 22:3f0b2c0bf9f3 58 pc.printf("ricevuti %u bytes\n\r ", bytesRead);
fdalforno 22:3f0b2c0bf9f3 59
fdalforno 22:3f0b2c0bf9f3 60 if(bytesRead >= 255){
fdalforno 22:3f0b2c0bf9f3 61 pc.printf("Overflow comando %u n\r ", bytesRead);
fdalforno 22:3f0b2c0bf9f3 62 bytesRead = 255;
fdalforno 22:3f0b2c0bf9f3 63 }
fdalforno 22:3f0b2c0bf9f3 64
fdalforno 22:3f0b2c0bf9f3 65 unsigned index = 0;
fdalforno 22:3f0b2c0bf9f3 66 for (; index < bytesRead; index++) {
fdalforno 22:3f0b2c0bf9f3 67 buffer[index] = params->data[index];
fdalforno 22:3f0b2c0bf9f3 68 }
fdalforno 22:3f0b2c0bf9f3 69
fdalforno 22:3f0b2c0bf9f3 70 buffer[index++] = 0;
fdalforno 22:3f0b2c0bf9f3 71
fdalforno 22:3f0b2c0bf9f3 72 pc.printf("Data : %s ",buffer);
fdalforno 22:3f0b2c0bf9f3 73 pc.printf("\r\n");
fdalforno 22:3f0b2c0bf9f3 74 }
apalmieri 13:227a0149b677 75 }
apalmieri 13:227a0149b677 76
fdalforno 22:3f0b2c0bf9f3 77
fdalforno 22:3f0b2c0bf9f3 78
fdalforno 22:3f0b2c0bf9f3 79 /**
fdalforno 22:3f0b2c0bf9f3 80 * Chiamata quando il bluetooth è inizializzato
fdalforno 22:3f0b2c0bf9f3 81 */
fdalforno 22:3f0b2c0bf9f3 82 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){
fdalforno 22:3f0b2c0bf9f3 83
apalmieri 13:227a0149b677 84 BLE& ble = params->ble;
apalmieri 13:227a0149b677 85 ble_error_t error = params->error;
fdalforno 22:3f0b2c0bf9f3 86
apalmieri 13:227a0149b677 87 if (error != BLE_ERROR_NONE) {
apalmieri 13:227a0149b677 88 onBleInitError(ble, error);
apalmieri 13:227a0149b677 89 return;
apalmieri 13:227a0149b677 90 }
fdalforno 22:3f0b2c0bf9f3 91
fdalforno 22:3f0b2c0bf9f3 92 /* Controllo di avere solo una istanza bluetooth attiva */
fdalforno 22:3f0b2c0bf9f3 93 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
apalmieri 13:227a0149b677 94 return;
apalmieri 13:227a0149b677 95 }
fdalforno 22:3f0b2c0bf9f3 96
screamer 0:eb7f02ad28a7 97 ble.gap().onDisconnection(disconnectionCallback);
fdalforno 22:3f0b2c0bf9f3 98
fdalforno 22:3f0b2c0bf9f3 99 /*Quando mi arriva qualcosa dal client viene invocata questa funzione*/
fdalforno 22:3f0b2c0bf9f3 100 ble.onDataWritten(onDataWritten);
fdalforno 22:3f0b2c0bf9f3 101
fdalforno 22:3f0b2c0bf9f3 102 /* Imposto il servizio uart (Ne posso avere più di uno attivo) */
fdalforno 22:3f0b2c0bf9f3 103 uart = new UARTService(ble);
fdalforno 22:3f0b2c0bf9f3 104
fdalforno 22:3f0b2c0bf9f3 105 /* Imposto i pacchetti di avviso attenzione posso avere connesso solo un client per volta al server */
fdalforno 22:3f0b2c0bf9f3 106
fdalforno 22:3f0b2c0bf9f3 107 /* Imposto il disposiztivo come solo BLE e SCOPRIBILE*/
screamer 0:eb7f02ad28a7 108 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
fdalforno 22:3f0b2c0bf9f3 109
fdalforno 22:3f0b2c0bf9f3 110 /* Imposto il nome della periferica*/
screamer 0:eb7f02ad28a7 111 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
fdalforno 22:3f0b2c0bf9f3 112 /* Imposto i servizi scopribili*/
fdalforno 22:3f0b2c0bf9f3 113 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
fdalforno 22:3f0b2c0bf9f3 114
fdalforno 22:3f0b2c0bf9f3 115 //Da approfondire
screamer 0:eb7f02ad28a7 116 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
fdalforno 22:3f0b2c0bf9f3 117
fdalforno 22:3f0b2c0bf9f3 118 /*Ogni quanto inviare un pacchetto di avviso*/
fdalforno 22:3f0b2c0bf9f3 119 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
fdalforno 22:3f0b2c0bf9f3 120
fdalforno 22:3f0b2c0bf9f3 121 /*Inizio a spedire i pacchetti*/
screamer 0:eb7f02ad28a7 122 ble.gap().startAdvertising();
fdalforno 22:3f0b2c0bf9f3 123
fdalforno 22:3f0b2c0bf9f3 124 }
screamer 0:eb7f02ad28a7 125
fdalforno 22:3f0b2c0bf9f3 126
fdalforno 22:3f0b2c0bf9f3 127 int main(void){
fdalforno 22:3f0b2c0bf9f3 128 BLE &ble = BLE::Instance();
fdalforno 22:3f0b2c0bf9f3 129 ble.init(bleInitComplete);
fdalforno 22:3f0b2c0bf9f3 130
fdalforno 22:3f0b2c0bf9f3 131 /* Aspetto fino a che la periferica bluetooth non è inizializzata */
fdalforno 22:3f0b2c0bf9f3 132 while (ble.hasInitialized() == false) { /* spin loop */ }
fdalforno 22:3f0b2c0bf9f3 133
fdalforno 22:3f0b2c0bf9f3 134 Ticker ticker;
fdalforno 22:3f0b2c0bf9f3 135 ticker.attach(periodicCallback, 1);
fdalforno 22:3f0b2c0bf9f3 136
apalmieri 13:227a0149b677 137 while (true) {
fdalforno 22:3f0b2c0bf9f3 138 ble.waitForEvent();
screamer 0:eb7f02ad28a7 139 }
screamer 0:eb7f02ad28a7 140 }
apalmieri 13:227a0149b677 141