Dependencies:   mbed X_NUCLEO_IDB0XA1 BLE_API

Committer:
pinofal
Date:
Sat Dec 08 12:30:48 2018 +0000
Revision:
8:766bd3e610fe
Parent:
7:79fe72eb3771
StripLED Bluetooth per DEMO Open Day

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pinofal 7:79fe72eb3771 1 //Tested: NUCLEO-F401RE
pinofal 7:79fe72eb3771 2 #include "mbed.h"
pinofal 7:79fe72eb3771 3
pinofal 7:79fe72eb3771 4 /****************** START BLE Declaration ****************/
pinofal 7:79fe72eb3771 5 #include "ble/BLE.h"
pinofal 7:79fe72eb3771 6 #include "LEDService.h"
pinofal 7:79fe72eb3771 7
pinofal 7:79fe72eb3771 8 DigitalOut Led1(LED1);
pinofal 7:79fe72eb3771 9 DigitalOut myLED(PB_2);
pinofal 7:79fe72eb3771 10 DigitalOut myRelay(PA_15);
pinofal 7:79fe72eb3771 11 DigitalIn myButton(USER_BUTTON);
pinofal 7:79fe72eb3771 12 Serial pc(USBTX, USBRX);
pinofal 7:79fe72eb3771 13
pinofal 8:766bd3e610fe 14 DigitalOut OutRed(PA_4);
pinofal 8:766bd3e610fe 15 DigitalOut OutBlue(PA_1);
pinofal 8:766bd3e610fe 16 DigitalOut OutGreen(PB_0);
pinofal 7:79fe72eb3771 17
pinofal 8:766bd3e610fe 18
pinofal 8:766bd3e610fe 19 const static char DEVICE_NAME[] = "Amaldi_Exs_19";
pinofal 7:79fe72eb3771 20 static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID};
pinofal 7:79fe72eb3771 21
pinofal 7:79fe72eb3771 22 LEDService *ledServicePtr;
pinofal 7:79fe72eb3771 23 /***************** END BLE Declaration ********************/
pinofal 7:79fe72eb3771 24
pinofal 7:79fe72eb3771 25 /**************** START BLE Functions **********************/
pinofal 7:79fe72eb3771 26
pinofal 7:79fe72eb3771 27 //****************************************************************
pinofal 7:79fe72eb3771 28 //* Disconnection Callback
pinofal 7:79fe72eb3771 29 //****************************************************************
pinofal 7:79fe72eb3771 30 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
pinofal 7:79fe72eb3771 31 {
pinofal 7:79fe72eb3771 32 (void)params;
pinofal 7:79fe72eb3771 33 BLE::Instance().gap().startAdvertising(); // restart advertising
pinofal 7:79fe72eb3771 34
pinofal 7:79fe72eb3771 35 pc.printf("------ Sono qui 3 disconnetrionCallBack \r\n", error);
pinofal 7:79fe72eb3771 36 }
pinofal 7:79fe72eb3771 37
pinofal 7:79fe72eb3771 38 //****************************************************************************************
pinofal 7:79fe72eb3771 39 //* This callback allows the LEDService to receive updates to the ledState Characteristic.
pinofal 7:79fe72eb3771 40 //*
pinofal 7:79fe72eb3771 41 //* @param[in] params
pinofal 7:79fe72eb3771 42 //* Information about the characterisitc being updated.
pinofal 7:79fe72eb3771 43 //****************************************************************************************
pinofal 7:79fe72eb3771 44 void onDataWrittenCallback(const GattWriteCallbackParams *params)
pinofal 7:79fe72eb3771 45 {
pinofal 7:79fe72eb3771 46 if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1))
pinofal 7:79fe72eb3771 47 {
pinofal 7:79fe72eb3771 48 switch(*(params->data))
pinofal 7:79fe72eb3771 49 {
pinofal 7:79fe72eb3771 50 case 0:
pinofal 7:79fe72eb3771 51 {
pinofal 8:766bd3e610fe 52 Led1 = 0x00; // spegni LED su scheda
pinofal 8:766bd3e610fe 53 //++++++++++++++ INIZIO pilota accensione delle strip LED +++++++++++++++++++++++++
pinofal 8:766bd3e610fe 54 OutRed = 0;
pinofal 8:766bd3e610fe 55 OutGreen = 0;
pinofal 8:766bd3e610fe 56 OutBlue = 0;
pinofal 8:766bd3e610fe 57 //++++++++++++++ FINE pilota accensione delle strip LED +++++++++++++++++++++++++
pinofal 8:766bd3e610fe 58
pinofal 7:79fe72eb3771 59 } break;
pinofal 7:79fe72eb3771 60 case 1:
pinofal 7:79fe72eb3771 61 {
pinofal 8:766bd3e610fe 62 Led1 = 0x01; // accendi LED su scheda
pinofal 8:766bd3e610fe 63 //++++++++++++++ INIZIO pilota accensione delle strip LED +++++++++++++++++++++++++
pinofal 8:766bd3e610fe 64 OutRed = 1;
pinofal 8:766bd3e610fe 65 OutGreen = 0;
pinofal 8:766bd3e610fe 66 OutBlue = 0;
pinofal 8:766bd3e610fe 67 //++++++++++++++ FINE pilota accensione delle strip LED +++++++++++++++++++++++++
pinofal 7:79fe72eb3771 68 } break;
pinofal 7:79fe72eb3771 69 case 2:
pinofal 7:79fe72eb3771 70 {
pinofal 8:766bd3e610fe 71 Led1 = 0x00; // spegni LED su scheda
pinofal 8:766bd3e610fe 72 //++++++++++++++ INIZIO pilota accensione delle strip LED +++++++++++++++++++++++++
pinofal 8:766bd3e610fe 73 OutRed = 0;
pinofal 8:766bd3e610fe 74 OutGreen = 1;
pinofal 8:766bd3e610fe 75 OutBlue = 0;
pinofal 8:766bd3e610fe 76 //++++++++++++++ FINE pilota accensione delle strip LED +++++++++++++++++++++++++
pinofal 8:766bd3e610fe 77
pinofal 7:79fe72eb3771 78 } break;
pinofal 7:79fe72eb3771 79 case 3:
pinofal 7:79fe72eb3771 80 {
pinofal 8:766bd3e610fe 81 Led1 = 0x01; // accendi LED su scheda
pinofal 8:766bd3e610fe 82 //++++++++++++++ INIZIO pilota accensione delle strip LED +++++++++++++++++++++++++
pinofal 8:766bd3e610fe 83 OutRed = 0;
pinofal 8:766bd3e610fe 84 OutGreen = 0;
pinofal 8:766bd3e610fe 85 OutBlue = 1;
pinofal 8:766bd3e610fe 86 //++++++++++++++ FINE pilota accensione delle strip LED +++++++++++++++++++++++++
pinofal 8:766bd3e610fe 87
pinofal 7:79fe72eb3771 88 } break;
pinofal 7:79fe72eb3771 89 case 4:
pinofal 7:79fe72eb3771 90 {
pinofal 8:766bd3e610fe 91 Led1 = 0x01; // accendi LED su scheda
pinofal 8:766bd3e610fe 92 //++++++++++++++ INIZIO pilota accensione delle strip LED +++++++++++++++++++++++++
pinofal 8:766bd3e610fe 93 OutRed = 1;
pinofal 8:766bd3e610fe 94 OutGreen = 1;
pinofal 8:766bd3e610fe 95 OutBlue = 1;
pinofal 8:766bd3e610fe 96 //++++++++++++++ FINE pilota accensione delle strip LED +++++++++++++++++++++++++
pinofal 8:766bd3e610fe 97
pinofal 7:79fe72eb3771 98 } break;
pinofal 7:79fe72eb3771 99 case 5:
pinofal 7:79fe72eb3771 100 {
pinofal 8:766bd3e610fe 101 Led1 = 0x00; // spegni LED su scheda
pinofal 7:79fe72eb3771 102 myRelay = 0x00; // spegni Relay
pinofal 7:79fe72eb3771 103 } break;
pinofal 7:79fe72eb3771 104
pinofal 7:79fe72eb3771 105 default: break;
pinofal 7:79fe72eb3771 106 }
pinofal 7:79fe72eb3771 107 pc.printf("\n\r---- Ricevuto: %d \n\r",*(params->data));
pinofal 7:79fe72eb3771 108
pinofal 7:79fe72eb3771 109 //Led1 = *(params->data); // in params->data riceve il byte inviato dal cellulare
pinofal 7:79fe72eb3771 110 }
pinofal 7:79fe72eb3771 111 }
pinofal 7:79fe72eb3771 112
pinofal 7:79fe72eb3771 113 //**************************************************************************
pinofal 7:79fe72eb3771 114 //* This function is called when the ble initialization process has failled
pinofal 7:79fe72eb3771 115 //**************************************************************************
pinofal 7:79fe72eb3771 116 void onBleInitError(BLE &ble, ble_error_t error)
pinofal 7:79fe72eb3771 117 {
pinofal 7:79fe72eb3771 118 /* Initialization error handling should go here */
pinofal 7:79fe72eb3771 119 }
pinofal 7:79fe72eb3771 120
pinofal 7:79fe72eb3771 121 //**************************************************************************
pinofal 7:79fe72eb3771 122 //* Callback triggered when the ble initialization process has finished
pinofal 7:79fe72eb3771 123 //**************************************************************************
pinofal 7:79fe72eb3771 124 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
pinofal 7:79fe72eb3771 125 {
pinofal 7:79fe72eb3771 126 BLE& ble = params->ble;
pinofal 7:79fe72eb3771 127 ble_error_t error = params->error;
pinofal 7:79fe72eb3771 128
pinofal 7:79fe72eb3771 129 if (error != BLE_ERROR_NONE)
pinofal 7:79fe72eb3771 130 {
pinofal 7:79fe72eb3771 131 /* In case of error, forward the error handling to onBleInitError */
pinofal 7:79fe72eb3771 132 onBleInitError(ble, error);
pinofal 7:79fe72eb3771 133 return;
pinofal 7:79fe72eb3771 134 }
pinofal 7:79fe72eb3771 135
pinofal 7:79fe72eb3771 136 /* Ensure that it is the default instance of BLE */
pinofal 7:79fe72eb3771 137 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE)
pinofal 7:79fe72eb3771 138 {
pinofal 7:79fe72eb3771 139 return;
pinofal 7:79fe72eb3771 140 }
pinofal 7:79fe72eb3771 141
pinofal 7:79fe72eb3771 142 ble.gap().onDisconnection(disconnectionCallback);
pinofal 7:79fe72eb3771 143 ble.gattServer().onDataWritten(onDataWrittenCallback);
pinofal 7:79fe72eb3771 144
pinofal 7:79fe72eb3771 145 bool initialValueForLEDCharacteristic = true;
pinofal 7:79fe72eb3771 146 ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic);
pinofal 7:79fe72eb3771 147
pinofal 7:79fe72eb3771 148 /* setup advertising */
pinofal 7:79fe72eb3771 149 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
pinofal 7:79fe72eb3771 150 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
pinofal 7:79fe72eb3771 151 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
pinofal 7:79fe72eb3771 152 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
pinofal 7:79fe72eb3771 153 ble.gap().setAdvertisingInterval(1000); // 1000ms.
pinofal 7:79fe72eb3771 154 ble.gap().startAdvertising();
pinofal 7:79fe72eb3771 155
pinofal 7:79fe72eb3771 156 while (true)
pinofal 7:79fe72eb3771 157 {
pinofal 7:79fe72eb3771 158 ble.waitForEvent();
pinofal 7:79fe72eb3771 159 //pc.printf("-------- Sono qui 1 WaitForEvent() \n\r");
pinofal 7:79fe72eb3771 160 }
pinofal 7:79fe72eb3771 161 }
pinofal 7:79fe72eb3771 162 /******************* END BLE Functions *************************/
pinofal 7:79fe72eb3771 163
pinofal 7:79fe72eb3771 164
pinofal 7:79fe72eb3771 165 /********/
pinofal 7:79fe72eb3771 166 /* MAIN */
pinofal 7:79fe72eb3771 167 /********/
pinofal 7:79fe72eb3771 168 int main(void)
pinofal 7:79fe72eb3771 169 {
pinofal 7:79fe72eb3771 170 // configura velocità della comunicazione seriale su USB-VirtualCom e invia messaggio di benvenuto
pinofal 8:766bd3e610fe 171 pc.baud(9600); //921600 bps
pinofal 7:79fe72eb3771 172 // messaggio di benvenuto
pinofal 8:766bd3e610fe 173 pc.printf("\r\nHallo Amaldi Students - Exercise 19 \r\n");
pinofal 7:79fe72eb3771 174 pc.printf("\r\n*** Bluetooth Driving for LED and Relay ***\r\n");
pinofal 7:79fe72eb3771 175
pinofal 7:79fe72eb3771 176 //imposta il funzionamento del pulsante come "PullDown": Aperto = '0'. L'altra modalità di funzinamento è PullUp
pinofal 7:79fe72eb3771 177 myButton.mode(PullDown);
pinofal 7:79fe72eb3771 178
pinofal 8:766bd3e610fe 179 // inizializza variabili
pinofal 8:766bd3e610fe 180 OutRed=0;
pinofal 8:766bd3e610fe 181 OutBlue=0;
pinofal 8:766bd3e610fe 182 OutGreen=0;
pinofal 8:766bd3e610fe 183
pinofal 8:766bd3e610fe 184 //+++++++++++++ INIZIO ciclo di prova ++++++
pinofal 8:766bd3e610fe 185 /*
pinofal 7:79fe72eb3771 186 while(true)
pinofal 7:79fe72eb3771 187 {
pinofal 7:79fe72eb3771 188 if(myButton==0x01)
pinofal 7:79fe72eb3771 189 {
pinofal 7:79fe72eb3771 190 myLED=0x01;
pinofal 7:79fe72eb3771 191 //while(myButton !=0x00);
pinofal 7:79fe72eb3771 192 Led1=0x01;
pinofal 7:79fe72eb3771 193 myRelay=0x01; // accendi relay
pinofal 8:766bd3e610fe 194 OutRed = 1;
pinofal 8:766bd3e610fe 195 OutGreen = 1;
pinofal 8:766bd3e610fe 196 OutBlue = 1;
pinofal 8:766bd3e610fe 197
pinofal 7:79fe72eb3771 198 }
pinofal 7:79fe72eb3771 199 else
pinofal 7:79fe72eb3771 200 {
pinofal 7:79fe72eb3771 201 myLED=0x00;
pinofal 7:79fe72eb3771 202 Led1=0x00;
pinofal 7:79fe72eb3771 203 myRelay=0x00; // spegni relay
pinofal 8:766bd3e610fe 204 OutRed = 0;
pinofal 8:766bd3e610fe 205 OutGreen = 0;
pinofal 8:766bd3e610fe 206 OutBlue = 0;
pinofal 7:79fe72eb3771 207 }
pinofal 7:79fe72eb3771 208 }
pinofal 7:79fe72eb3771 209 */
pinofal 8:766bd3e610fe 210 //++++++ FINE Ciclo di Prove
pinofal 7:79fe72eb3771 211 /*************** START BLE Main ************/
pinofal 7:79fe72eb3771 212 BLE &ble = BLE::Instance();
pinofal 7:79fe72eb3771 213 ble.init(bleInitComplete);
pinofal 7:79fe72eb3771 214 /*************** END BLE Main ***********/
pinofal 7:79fe72eb3771 215
pinofal 7:79fe72eb3771 216
pinofal 7:79fe72eb3771 217
pinofal 7:79fe72eb3771 218 }
pinofal 7:79fe72eb3771 219