Gestione invio dati tramite seriale bluetooth
Dependencies: BLE_API X_NUCLEO_IDB0XA1 mbed
Fork of BLE_HeartRate_IDB0XA1 by
main.cpp@23:8315afc7bc95, 2017-02-19 (annotated)
- Committer:
- fdalforno
- Date:
- Sun Feb 19 15:54:06 2017 +0000
- Revision:
- 23:8315afc7bc95
- Parent:
- 22:3f0b2c0bf9f3
prova
Who changed what in which revision?
User | Revision | Line number | New 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 | 23:8315afc7bc95 | 38 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params){ |
fdalforno | 23:8315afc7bc95 | 39 | if(params->role == Gap::CENTRAL){ |
fdalforno | 23:8315afc7bc95 | 40 | pc.printf("Connesso dispositivo %u \r\n"); |
fdalforno | 23:8315afc7bc95 | 41 | } |
fdalforno | 23:8315afc7bc95 | 42 | } |
fdalforno | 23:8315afc7bc95 | 43 | |
fdalforno | 22:3f0b2c0bf9f3 | 44 | void periodicCallback(void){ |
fdalforno | 22:3f0b2c0bf9f3 | 45 | led1 = !led1; |
fdalforno | 22:3f0b2c0bf9f3 | 46 | |
fdalforno | 22:3f0b2c0bf9f3 | 47 | /*Per spedire qualcosa al client*/ |
fdalforno | 22:3f0b2c0bf9f3 | 48 | //char* str = "Ciao \r\n"; |
fdalforno | 22:3f0b2c0bf9f3 | 49 | //if (uart) uart->writeString(str); |
screamer | 0:eb7f02ad28a7 | 50 | } |
screamer | 0:eb7f02ad28a7 | 51 | |
fdalforno | 22:3f0b2c0bf9f3 | 52 | /** |
fdalforno | 22:3f0b2c0bf9f3 | 53 | * Chiamata in caso di errore, ottengo il codice sulla console |
fdalforno | 22:3f0b2c0bf9f3 | 54 | */ |
fdalforno | 22:3f0b2c0bf9f3 | 55 | void onBleInitError(BLE &ble, ble_error_t error){ |
fdalforno | 22:3f0b2c0bf9f3 | 56 | pc.printf("Errore interno %u \r\n",error); |
screamer | 0:eb7f02ad28a7 | 57 | } |
screamer | 0:eb7f02ad28a7 | 58 | |
fdalforno | 22:3f0b2c0bf9f3 | 59 | |
fdalforno | 22:3f0b2c0bf9f3 | 60 | void onDataWritten(const GattWriteCallbackParams *params){ |
fdalforno | 22:3f0b2c0bf9f3 | 61 | if ((uart != NULL) && (params->handle == uart->getTXCharacteristicHandle())) { |
fdalforno | 22:3f0b2c0bf9f3 | 62 | uint16_t bytesRead = params->len; |
fdalforno | 22:3f0b2c0bf9f3 | 63 | |
fdalforno | 22:3f0b2c0bf9f3 | 64 | pc.printf("ricevuti %u bytes\n\r ", bytesRead); |
fdalforno | 22:3f0b2c0bf9f3 | 65 | |
fdalforno | 22:3f0b2c0bf9f3 | 66 | if(bytesRead >= 255){ |
fdalforno | 22:3f0b2c0bf9f3 | 67 | pc.printf("Overflow comando %u n\r ", bytesRead); |
fdalforno | 22:3f0b2c0bf9f3 | 68 | bytesRead = 255; |
fdalforno | 22:3f0b2c0bf9f3 | 69 | } |
fdalforno | 22:3f0b2c0bf9f3 | 70 | |
fdalforno | 22:3f0b2c0bf9f3 | 71 | unsigned index = 0; |
fdalforno | 22:3f0b2c0bf9f3 | 72 | for (; index < bytesRead; index++) { |
fdalforno | 22:3f0b2c0bf9f3 | 73 | buffer[index] = params->data[index]; |
fdalforno | 22:3f0b2c0bf9f3 | 74 | } |
fdalforno | 22:3f0b2c0bf9f3 | 75 | |
fdalforno | 22:3f0b2c0bf9f3 | 76 | buffer[index++] = 0; |
fdalforno | 22:3f0b2c0bf9f3 | 77 | |
fdalforno | 22:3f0b2c0bf9f3 | 78 | pc.printf("Data : %s ",buffer); |
fdalforno | 22:3f0b2c0bf9f3 | 79 | pc.printf("\r\n"); |
fdalforno | 22:3f0b2c0bf9f3 | 80 | } |
apalmieri | 13:227a0149b677 | 81 | } |
apalmieri | 13:227a0149b677 | 82 | |
fdalforno | 22:3f0b2c0bf9f3 | 83 | |
fdalforno | 22:3f0b2c0bf9f3 | 84 | |
fdalforno | 22:3f0b2c0bf9f3 | 85 | /** |
fdalforno | 22:3f0b2c0bf9f3 | 86 | * Chiamata quando il bluetooth è inizializzato |
fdalforno | 22:3f0b2c0bf9f3 | 87 | */ |
fdalforno | 22:3f0b2c0bf9f3 | 88 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){ |
fdalforno | 22:3f0b2c0bf9f3 | 89 | |
apalmieri | 13:227a0149b677 | 90 | BLE& ble = params->ble; |
apalmieri | 13:227a0149b677 | 91 | ble_error_t error = params->error; |
fdalforno | 22:3f0b2c0bf9f3 | 92 | |
apalmieri | 13:227a0149b677 | 93 | if (error != BLE_ERROR_NONE) { |
apalmieri | 13:227a0149b677 | 94 | onBleInitError(ble, error); |
apalmieri | 13:227a0149b677 | 95 | return; |
apalmieri | 13:227a0149b677 | 96 | } |
fdalforno | 22:3f0b2c0bf9f3 | 97 | |
fdalforno | 22:3f0b2c0bf9f3 | 98 | /* Controllo di avere solo una istanza bluetooth attiva */ |
fdalforno | 22:3f0b2c0bf9f3 | 99 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
apalmieri | 13:227a0149b677 | 100 | return; |
apalmieri | 13:227a0149b677 | 101 | } |
fdalforno | 22:3f0b2c0bf9f3 | 102 | |
screamer | 0:eb7f02ad28a7 | 103 | ble.gap().onDisconnection(disconnectionCallback); |
fdalforno | 23:8315afc7bc95 | 104 | ble.gap().onConnection(connectionCallback); |
fdalforno | 22:3f0b2c0bf9f3 | 105 | |
fdalforno | 22:3f0b2c0bf9f3 | 106 | /*Quando mi arriva qualcosa dal client viene invocata questa funzione*/ |
fdalforno | 22:3f0b2c0bf9f3 | 107 | ble.onDataWritten(onDataWritten); |
fdalforno | 22:3f0b2c0bf9f3 | 108 | |
fdalforno | 22:3f0b2c0bf9f3 | 109 | /* Imposto il servizio uart (Ne posso avere più di uno attivo) */ |
fdalforno | 22:3f0b2c0bf9f3 | 110 | uart = new UARTService(ble); |
fdalforno | 22:3f0b2c0bf9f3 | 111 | |
fdalforno | 22:3f0b2c0bf9f3 | 112 | /* Imposto i pacchetti di avviso attenzione posso avere connesso solo un client per volta al server */ |
fdalforno | 22:3f0b2c0bf9f3 | 113 | |
fdalforno | 22:3f0b2c0bf9f3 | 114 | /* Imposto il disposiztivo come solo BLE e SCOPRIBILE*/ |
screamer | 0:eb7f02ad28a7 | 115 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
fdalforno | 22:3f0b2c0bf9f3 | 116 | |
fdalforno | 22:3f0b2c0bf9f3 | 117 | /* Imposto il nome della periferica*/ |
screamer | 0:eb7f02ad28a7 | 118 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
fdalforno | 22:3f0b2c0bf9f3 | 119 | /* Imposto i servizi scopribili*/ |
fdalforno | 22:3f0b2c0bf9f3 | 120 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
fdalforno | 22:3f0b2c0bf9f3 | 121 | |
fdalforno | 22:3f0b2c0bf9f3 | 122 | //Da approfondire |
screamer | 0:eb7f02ad28a7 | 123 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
fdalforno | 22:3f0b2c0bf9f3 | 124 | |
fdalforno | 22:3f0b2c0bf9f3 | 125 | /*Ogni quanto inviare un pacchetto di avviso*/ |
fdalforno | 23:8315afc7bc95 | 126 | ble.gap().setAdvertisingInterval(100); /* 1000ms. */ |
fdalforno | 22:3f0b2c0bf9f3 | 127 | |
fdalforno | 22:3f0b2c0bf9f3 | 128 | /*Inizio a spedire i pacchetti*/ |
screamer | 0:eb7f02ad28a7 | 129 | ble.gap().startAdvertising(); |
fdalforno | 22:3f0b2c0bf9f3 | 130 | |
fdalforno | 22:3f0b2c0bf9f3 | 131 | } |
screamer | 0:eb7f02ad28a7 | 132 | |
fdalforno | 22:3f0b2c0bf9f3 | 133 | |
fdalforno | 22:3f0b2c0bf9f3 | 134 | int main(void){ |
fdalforno | 22:3f0b2c0bf9f3 | 135 | BLE &ble = BLE::Instance(); |
fdalforno | 22:3f0b2c0bf9f3 | 136 | ble.init(bleInitComplete); |
fdalforno | 22:3f0b2c0bf9f3 | 137 | |
fdalforno | 22:3f0b2c0bf9f3 | 138 | /* Aspetto fino a che la periferica bluetooth non è inizializzata */ |
fdalforno | 22:3f0b2c0bf9f3 | 139 | while (ble.hasInitialized() == false) { /* spin loop */ } |
fdalforno | 22:3f0b2c0bf9f3 | 140 | |
fdalforno | 22:3f0b2c0bf9f3 | 141 | Ticker ticker; |
fdalforno | 22:3f0b2c0bf9f3 | 142 | ticker.attach(periodicCallback, 1); |
fdalforno | 22:3f0b2c0bf9f3 | 143 | |
apalmieri | 13:227a0149b677 | 144 | while (true) { |
fdalforno | 22:3f0b2c0bf9f3 | 145 | ble.waitForEvent(); |
screamer | 0:eb7f02ad28a7 | 146 | } |
screamer | 0:eb7f02ad28a7 | 147 | } |
apalmieri | 13:227a0149b677 | 148 |