motorino
Dependencies: BLE_API X_NUCLEO_IDB0XA1 mbed
Fork of Amaldi_5_BLE_LED_IDB0XA1_Relay by
BLE_LED_IDB0XA1_Relay.cpp@7:acc86cb1ff05, 2018-05-09 (annotated)
- Committer:
- pinofal
- Date:
- Wed May 09 15:21:51 2018 +0000
- Revision:
- 7:acc86cb1ff05
- Child:
- 8:0dc75af249df
Amaldi 5 BLE LED IDB0XA1 Relay
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pinofal | 7:acc86cb1ff05 | 1 | //Tested: NUCLEO-F401RE |
pinofal | 7:acc86cb1ff05 | 2 | #include "mbed.h" |
pinofal | 7:acc86cb1ff05 | 3 | |
pinofal | 7:acc86cb1ff05 | 4 | /****************** START BLE Declaration ****************/ |
pinofal | 7:acc86cb1ff05 | 5 | #include "ble/BLE.h" |
pinofal | 7:acc86cb1ff05 | 6 | #include "LEDService.h" |
pinofal | 7:acc86cb1ff05 | 7 | |
pinofal | 7:acc86cb1ff05 | 8 | DigitalOut Led1(LED1); |
pinofal | 7:acc86cb1ff05 | 9 | DigitalOut myLED(PB_2); |
pinofal | 7:acc86cb1ff05 | 10 | DigitalOut myRelay(PA_15); |
pinofal | 7:acc86cb1ff05 | 11 | DigitalIn myButton(USER_BUTTON); |
pinofal | 7:acc86cb1ff05 | 12 | Serial pc(USBTX, USBRX); |
pinofal | 7:acc86cb1ff05 | 13 | |
pinofal | 7:acc86cb1ff05 | 14 | |
pinofal | 7:acc86cb1ff05 | 15 | const static char DEVICE_NAME[] = "Amaldi_Ex_5"; |
pinofal | 7:acc86cb1ff05 | 16 | static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID}; |
pinofal | 7:acc86cb1ff05 | 17 | |
pinofal | 7:acc86cb1ff05 | 18 | LEDService *ledServicePtr; |
pinofal | 7:acc86cb1ff05 | 19 | /***************** END BLE Declaration ********************/ |
pinofal | 7:acc86cb1ff05 | 20 | |
pinofal | 7:acc86cb1ff05 | 21 | /**************** START BLE Functions **********************/ |
pinofal | 7:acc86cb1ff05 | 22 | |
pinofal | 7:acc86cb1ff05 | 23 | //**************************************************************** |
pinofal | 7:acc86cb1ff05 | 24 | //* Disconnection Callback |
pinofal | 7:acc86cb1ff05 | 25 | //**************************************************************** |
pinofal | 7:acc86cb1ff05 | 26 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
pinofal | 7:acc86cb1ff05 | 27 | { |
pinofal | 7:acc86cb1ff05 | 28 | (void)params; |
pinofal | 7:acc86cb1ff05 | 29 | BLE::Instance().gap().startAdvertising(); // restart advertising |
pinofal | 7:acc86cb1ff05 | 30 | |
pinofal | 7:acc86cb1ff05 | 31 | pc.printf("------ Sono qui 3 disconnetrionCallBack \r\n", error); |
pinofal | 7:acc86cb1ff05 | 32 | } |
pinofal | 7:acc86cb1ff05 | 33 | |
pinofal | 7:acc86cb1ff05 | 34 | //**************************************************************************************** |
pinofal | 7:acc86cb1ff05 | 35 | //* This callback allows the LEDService to receive updates to the ledState Characteristic. |
pinofal | 7:acc86cb1ff05 | 36 | //* |
pinofal | 7:acc86cb1ff05 | 37 | //* @param[in] params |
pinofal | 7:acc86cb1ff05 | 38 | //* Information about the characterisitc being updated. |
pinofal | 7:acc86cb1ff05 | 39 | //**************************************************************************************** |
pinofal | 7:acc86cb1ff05 | 40 | void onDataWrittenCallback(const GattWriteCallbackParams *params) |
pinofal | 7:acc86cb1ff05 | 41 | { |
pinofal | 7:acc86cb1ff05 | 42 | if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) |
pinofal | 7:acc86cb1ff05 | 43 | { |
pinofal | 7:acc86cb1ff05 | 44 | |
pinofal | 7:acc86cb1ff05 | 45 | //*switch(*(params->data)) |
pinofal | 7:acc86cb1ff05 | 46 | /*Led1 = *(params->data); // in params->data riceve il byte inviato dal cellulare |
pinofal | 7:acc86cb1ff05 | 47 | { |
pinofal | 7:acc86cb1ff05 | 48 | |
pinofal | 7:acc86cb1ff05 | 49 | case ?: |
pinofal | 7:acc86cb1ff05 | 50 | { |
pinofal | 7:acc86cb1ff05 | 51 | |
pinofal | 7:acc86cb1ff05 | 52 | } break; |
pinofal | 7:acc86cb1ff05 | 53 | |
pinofal | 7:acc86cb1ff05 | 54 | default: break; |
pinofal | 7:acc86cb1ff05 | 55 | }*/ |
pinofal | 7:acc86cb1ff05 | 56 | pc.printf("\n\r---- Ricevuto: %d \n\r",*(params->data)); |
pinofal | 7:acc86cb1ff05 | 57 | |
pinofal | 7:acc86cb1ff05 | 58 | //Led1 = *(params->data); // in params->data riceve il byte inviato dal cellulare |
pinofal | 7:acc86cb1ff05 | 59 | } |
pinofal | 7:acc86cb1ff05 | 60 | } |
pinofal | 7:acc86cb1ff05 | 61 | |
pinofal | 7:acc86cb1ff05 | 62 | //************************************************************************** |
pinofal | 7:acc86cb1ff05 | 63 | //* This function is called when the ble initialization process has failled |
pinofal | 7:acc86cb1ff05 | 64 | //************************************************************************** |
pinofal | 7:acc86cb1ff05 | 65 | void onBleInitError(BLE &ble, ble_error_t error) |
pinofal | 7:acc86cb1ff05 | 66 | { |
pinofal | 7:acc86cb1ff05 | 67 | /* Initialization error handling should go here */ |
pinofal | 7:acc86cb1ff05 | 68 | } |
pinofal | 7:acc86cb1ff05 | 69 | |
pinofal | 7:acc86cb1ff05 | 70 | //************************************************************************** |
pinofal | 7:acc86cb1ff05 | 71 | //* Callback triggered when the ble initialization process has finished |
pinofal | 7:acc86cb1ff05 | 72 | //************************************************************************** |
pinofal | 7:acc86cb1ff05 | 73 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
pinofal | 7:acc86cb1ff05 | 74 | { |
pinofal | 7:acc86cb1ff05 | 75 | BLE& ble = params->ble; |
pinofal | 7:acc86cb1ff05 | 76 | ble_error_t error = params->error; |
pinofal | 7:acc86cb1ff05 | 77 | |
pinofal | 7:acc86cb1ff05 | 78 | if (error != BLE_ERROR_NONE) |
pinofal | 7:acc86cb1ff05 | 79 | { |
pinofal | 7:acc86cb1ff05 | 80 | /* In case of error, forward the error handling to onBleInitError */ |
pinofal | 7:acc86cb1ff05 | 81 | onBleInitError(ble, error); |
pinofal | 7:acc86cb1ff05 | 82 | return; |
pinofal | 7:acc86cb1ff05 | 83 | } |
pinofal | 7:acc86cb1ff05 | 84 | |
pinofal | 7:acc86cb1ff05 | 85 | /* Ensure that it is the default instance of BLE */ |
pinofal | 7:acc86cb1ff05 | 86 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) |
pinofal | 7:acc86cb1ff05 | 87 | { |
pinofal | 7:acc86cb1ff05 | 88 | return; |
pinofal | 7:acc86cb1ff05 | 89 | } |
pinofal | 7:acc86cb1ff05 | 90 | |
pinofal | 7:acc86cb1ff05 | 91 | ble.gap().onDisconnection(disconnectionCallback); |
pinofal | 7:acc86cb1ff05 | 92 | ble.gattServer().onDataWritten(onDataWrittenCallback); |
pinofal | 7:acc86cb1ff05 | 93 | |
pinofal | 7:acc86cb1ff05 | 94 | bool initialValueForLEDCharacteristic = true; |
pinofal | 7:acc86cb1ff05 | 95 | ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic); |
pinofal | 7:acc86cb1ff05 | 96 | |
pinofal | 7:acc86cb1ff05 | 97 | /* setup advertising */ |
pinofal | 7:acc86cb1ff05 | 98 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
pinofal | 7:acc86cb1ff05 | 99 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
pinofal | 7:acc86cb1ff05 | 100 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
pinofal | 7:acc86cb1ff05 | 101 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
pinofal | 7:acc86cb1ff05 | 102 | ble.gap().setAdvertisingInterval(1000); // 1000ms. |
pinofal | 7:acc86cb1ff05 | 103 | ble.gap().startAdvertising(); |
pinofal | 7:acc86cb1ff05 | 104 | |
pinofal | 7:acc86cb1ff05 | 105 | while (true) |
pinofal | 7:acc86cb1ff05 | 106 | { |
pinofal | 7:acc86cb1ff05 | 107 | ble.waitForEvent(); |
pinofal | 7:acc86cb1ff05 | 108 | //pc.printf("-------- Sono qui 1 WaitForEvent() \n\r"); |
pinofal | 7:acc86cb1ff05 | 109 | } |
pinofal | 7:acc86cb1ff05 | 110 | } |
pinofal | 7:acc86cb1ff05 | 111 | /******************* END BLE Functions *************************/ |
pinofal | 7:acc86cb1ff05 | 112 | |
pinofal | 7:acc86cb1ff05 | 113 | |
pinofal | 7:acc86cb1ff05 | 114 | /********/ |
pinofal | 7:acc86cb1ff05 | 115 | /* MAIN */ |
pinofal | 7:acc86cb1ff05 | 116 | /********/ |
pinofal | 7:acc86cb1ff05 | 117 | int main(void) |
pinofal | 7:acc86cb1ff05 | 118 | { |
pinofal | 7:acc86cb1ff05 | 119 | // configura velocità della comunicazione seriale su USB-VirtualCom e invia messaggio di benvenuto |
pinofal | 7:acc86cb1ff05 | 120 | pc.baud(921600); //921600 bps |
pinofal | 7:acc86cb1ff05 | 121 | // messaggio di benvenuto |
pinofal | 7:acc86cb1ff05 | 122 | pc.printf("\r\nHallo Amaldi Students - Exercise 5 \r\n"); |
pinofal | 7:acc86cb1ff05 | 123 | pc.printf("\r\n*** Bluetooth Driving for LED and Relay ***\r\n"); |
pinofal | 7:acc86cb1ff05 | 124 | |
pinofal | 7:acc86cb1ff05 | 125 | //imposta il funzionamento del pulsante come "PullDown": Aperto = '0'. L'altra modalità di funzinamento è PullUp |
pinofal | 7:acc86cb1ff05 | 126 | myButton.mode(PullDown); |
pinofal | 7:acc86cb1ff05 | 127 | |
pinofal | 7:acc86cb1ff05 | 128 | /* ciclo di prova |
pinofal | 7:acc86cb1ff05 | 129 | while(true) |
pinofal | 7:acc86cb1ff05 | 130 | { |
pinofal | 7:acc86cb1ff05 | 131 | if(myButton==0x01) |
pinofal | 7:acc86cb1ff05 | 132 | { |
pinofal | 7:acc86cb1ff05 | 133 | myLED=0x01; |
pinofal | 7:acc86cb1ff05 | 134 | //while(myButton !=0x00); |
pinofal | 7:acc86cb1ff05 | 135 | Led1=0x01; |
pinofal | 7:acc86cb1ff05 | 136 | myRelay=0x01; // accendi relay |
pinofal | 7:acc86cb1ff05 | 137 | } |
pinofal | 7:acc86cb1ff05 | 138 | else |
pinofal | 7:acc86cb1ff05 | 139 | { |
pinofal | 7:acc86cb1ff05 | 140 | myLED=0x00; |
pinofal | 7:acc86cb1ff05 | 141 | Led1=0x00; |
pinofal | 7:acc86cb1ff05 | 142 | myRelay=0x00; // spegni relay |
pinofal | 7:acc86cb1ff05 | 143 | } |
pinofal | 7:acc86cb1ff05 | 144 | } |
pinofal | 7:acc86cb1ff05 | 145 | */ |
pinofal | 7:acc86cb1ff05 | 146 | /*************** START BLE Main ************/ |
pinofal | 7:acc86cb1ff05 | 147 | BLE &ble = BLE::Instance(); |
pinofal | 7:acc86cb1ff05 | 148 | ble.init(bleInitComplete); |
pinofal | 7:acc86cb1ff05 | 149 | /*************** END BLE Main ***********/ |
pinofal | 7:acc86cb1ff05 | 150 | |
pinofal | 7:acc86cb1ff05 | 151 | |
pinofal | 7:acc86cb1ff05 | 152 | |
pinofal | 7:acc86cb1ff05 | 153 | } |
pinofal | 7:acc86cb1ff05 | 154 |