Kris Scholte Lubberink
/
SSS_Ble
Ble for smart sOlutions
source/main.cpp@7:9cda1b0f25ae, 2019-05-20 (annotated)
- Committer:
- kris@kris-X682X
- Date:
- Mon May 20 13:20:35 2019 +0200
- Revision:
- 7:9cda1b0f25ae
- Parent:
- 6:ee9c86f06eae
- Child:
- 8:369b80cef5ae
Working broadcast & advertise
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
krissl | 0:fe5389573740 | 1 | /* mbed Microcontroller Library |
krissl | 0:fe5389573740 | 2 | * Copyright (c) 2006-2014 ARM Limited |
krissl | 0:fe5389573740 | 3 | * |
krissl | 0:fe5389573740 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
krissl | 0:fe5389573740 | 5 | * you may not use this file except in compliance with the License. |
krissl | 0:fe5389573740 | 6 | * You may obtain a copy of the License at |
krissl | 0:fe5389573740 | 7 | * |
krissl | 0:fe5389573740 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
krissl | 0:fe5389573740 | 9 | * |
krissl | 0:fe5389573740 | 10 | * Unless required by applicable law or agreed to in writing, software |
krissl | 0:fe5389573740 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
krissl | 0:fe5389573740 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
krissl | 0:fe5389573740 | 13 | * See the License for the specific language governing permissions and |
krissl | 0:fe5389573740 | 14 | * limitations under the License. |
krissl | 0:fe5389573740 | 15 | */ |
krissl | 0:fe5389573740 | 16 | |
krissl | 0:fe5389573740 | 17 | #include <events/mbed_events.h> |
krissl | 0:fe5389573740 | 18 | #include <mbed.h> |
krissl | 0:fe5389573740 | 19 | #include "ble/BLE.h" |
krissl | 0:fe5389573740 | 20 | #include "ble/Gap.h" |
krissl | 0:fe5389573740 | 21 | #include "GattCallbackParamTypes.h" |
krissl | 1:9fc54848a198 | 22 | #include "mbed.h" |
kris@kris-X682X | 6:ee9c86f06eae | 23 | #include "Adafruit_WS2801/Adafruit_WS2801.h" |
krissl | 3:f594022fe519 | 24 | |
krissl | 1:9fc54848a198 | 25 | #include "ColorService.h" |
kris@kris-X682X | 6:ee9c86f06eae | 26 | #include "BleDeviceCentral.h" |
kris@kris-X682X | 6:ee9c86f06eae | 27 | #include "BleDevicePeripheral.h" |
kris@kris-X682X | 6:ee9c86f06eae | 28 | #define MBED_CONF_PLATFORM_ERROR_FILENAME_CAPTURE_ENABLED true |
krissl | 0:fe5389573740 | 29 | DigitalOut led1(LED1, 1); |
krissl | 0:fe5389573740 | 30 | |
kris@kris-X682X | 6:ee9c86f06eae | 31 | //const static char DEVICE_NAME[] = "SSS-Wearable"; |
krissl | 1:9fc54848a198 | 32 | static const uint16_t uuid16_list[] = {0xF0C0FF}; |
kris@kris-X682X | 6:ee9c86f06eae | 33 | |
krissl | 3:f594022fe519 | 34 | int rainbow[] = {0xff00ff,0xff00cc,0xff0099,0xff0066,0xff0033,0xff0000,0xff3300,0xff6600, |
krissl | 3:f594022fe519 | 35 | 0xff9900,0xffcc00,0xffff00,0xccff00,0x99ff00,0x66ff00,0x33ff00,0x00ff00, |
krissl | 3:f594022fe519 | 36 | 0x00ff33,0x00ff66,0x00ff99,0x00ffcc}; |
kris@kris-X682X | 6:ee9c86f06eae | 37 | |
krissl | 0:fe5389573740 | 38 | |
kris@kris-X682X | 6:ee9c86f06eae | 39 | //static ColorService* colorServicePtr; |
krissl | 0:fe5389573740 | 40 | |
krissl | 0:fe5389573740 | 41 | static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE); |
kris@kris-X682X | 6:ee9c86f06eae | 42 | |
krissl | 0:fe5389573740 | 43 | void updateSensorValue() { |
kris@kris-X682X | 6:ee9c86f06eae | 44 | // printf("Right now, i should update sensor value. This runs only during connection \r\n"); |
krissl | 3:f594022fe519 | 45 | // colorServicePtr->updateColor((int[]){16711680, 16711682, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680}); |
krissl | 0:fe5389573740 | 46 | } |
krissl | 0:fe5389573740 | 47 | |
krissl | 0:fe5389573740 | 48 | void blinkCallback(void) |
krissl | 0:fe5389573740 | 49 | { |
krissl | 3:f594022fe519 | 50 | //TODO: Every |
krissl | 0:fe5389573740 | 51 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
krissl | 0:fe5389573740 | 52 | BLE &ble = BLE::Instance(); |
kris@kris-X682X | 6:ee9c86f06eae | 53 | |
kris@kris-X682X | 6:ee9c86f06eae | 54 | |
krissl | 0:fe5389573740 | 55 | if (ble.gap().getState().connected) { |
krissl | 0:fe5389573740 | 56 | eventQueue.call(updateSensorValue); |
krissl | 3:f594022fe519 | 57 | } else { |
kris@kris-X682X | 6:ee9c86f06eae | 58 | ble.gap().startAdvertising(); |
kris@kris-X682X | 6:ee9c86f06eae | 59 | printf("Stopped advertisement"); |
krissl | 0:fe5389573740 | 60 | } |
krissl | 0:fe5389573740 | 61 | } |
krissl | 0:fe5389573740 | 62 | |
krissl | 0:fe5389573740 | 63 | void printMacAddress() |
krissl | 0:fe5389573740 | 64 | { |
krissl | 0:fe5389573740 | 65 | /* Print out device MAC address to the console*/ |
krissl | 0:fe5389573740 | 66 | Gap::AddressType_t addr_type; |
krissl | 0:fe5389573740 | 67 | Gap::Address_t address; |
krissl | 0:fe5389573740 | 68 | BLE::Instance().gap().getAddress(&addr_type, address); |
kris@kris-X682X | 6:ee9c86f06eae | 69 | printf("DEVICE MAC ADDRESS: "); |
krissl | 0:fe5389573740 | 70 | for (int i = 5; i >= 1; i--){ |
kris@kris-X682X | 6:ee9c86f06eae | 71 | printf("%02x:", address[i]); |
krissl | 0:fe5389573740 | 72 | } |
kris@kris-X682X | 6:ee9c86f06eae | 73 | printf("%02x\r\n", address[0]); |
krissl | 0:fe5389573740 | 74 | } |
krissl | 0:fe5389573740 | 75 | |
krissl | 0:fe5389573740 | 76 | /** |
krissl | 0:fe5389573740 | 77 | * Callback triggered when the ble initialization process has finished |
krissl | 0:fe5389573740 | 78 | */ |
krissl | 0:fe5389573740 | 79 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
krissl | 0:fe5389573740 | 80 | { |
krissl | 0:fe5389573740 | 81 | |
krissl | 0:fe5389573740 | 82 | } |
krissl | 0:fe5389573740 | 83 | |
krissl | 3:f594022fe519 | 84 | |
krissl | 0:fe5389573740 | 85 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
krissl | 0:fe5389573740 | 86 | BLE &ble = BLE::Instance(); |
krissl | 3:f594022fe519 | 87 | //TODO: Event handling; wel benieuwd naar wat er allemaal gebeurt |
krissl | 0:fe5389573740 | 88 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
krissl | 0:fe5389573740 | 89 | } |
krissl | 0:fe5389573740 | 90 | |
krissl | 3:f594022fe519 | 91 | uint32_t colorMaker(uint8_t r, uint8_t g, uint8_t b){ |
kris@kris-X682X | 6:ee9c86f06eae | 92 | uint32_t c; |
kris@kris-X682X | 6:ee9c86f06eae | 93 | c = r; |
kris@kris-X682X | 6:ee9c86f06eae | 94 | c <<= 8; |
kris@kris-X682X | 6:ee9c86f06eae | 95 | c |= g; |
kris@kris-X682X | 6:ee9c86f06eae | 96 | c <<= 8; |
kris@kris-X682X | 6:ee9c86f06eae | 97 | c |= b; |
kris@kris-X682X | 6:ee9c86f06eae | 98 | return c; |
krissl | 3:f594022fe519 | 99 | } |
krissl | 3:f594022fe519 | 100 | |
krissl | 0:fe5389573740 | 101 | int main() |
krissl | 0:fe5389573740 | 102 | { |
kris@kris-X682X | 6:ee9c86f06eae | 103 | printf("\r\n CENTRAL \r\n\r\n"); |
krissl | 0:fe5389573740 | 104 | |
kris@kris-X682X | 6:ee9c86f06eae | 105 | // mystrip.begin(); |
kris@kris-X682X | 6:ee9c86f06eae | 106 | // mystrip.show(); |
kris@kris-X682X | 6:ee9c86f06eae | 107 | // colorServicePtr->updateColor(rainbow);//(int[]){16711680, 16711682, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680, 16711680}); |
kris@kris-X682X | 6:ee9c86f06eae | 108 | |
kris@kris-X682X | 6:ee9c86f06eae | 109 | BLE& ble = BLE::Instance(); |
kris@kris-X682X | 6:ee9c86f06eae | 110 | events::EventQueue queue; |
kris@kris-X682X | 6:ee9c86f06eae | 111 | printf(" Is filename capture enabled? \t\t"); |
krissl | 0:fe5389573740 | 112 | |
kris@kris-X682X | 6:ee9c86f06eae | 113 | #if MBED_CONF_PLATFORM_ERROR_FILENAME_CAPTURE_ENABLED |
kris@kris-X682X | 6:ee9c86f06eae | 114 | printf(" Enabled filename capture"); |
kris@kris-X682X | 6:ee9c86f06eae | 115 | #endif |
kris@kris-X682X | 6:ee9c86f06eae | 116 | |
kris@kris-X682X | 7:9cda1b0f25ae | 117 | BleDevicePeripheral peripheral(ble, queue); |
kris@kris-X682X | 7:9cda1b0f25ae | 118 | BleDeviceCentral central(ble, queue); |
kris@kris-X682X | 7:9cda1b0f25ae | 119 | |
kris@kris-X682X | 6:ee9c86f06eae | 120 | |
kris@kris-X682X | 6:ee9c86f06eae | 121 | while(1) { |
kris@kris-X682X | 6:ee9c86f06eae | 122 | printf("\r\n PERIPHERAL \r\n\r\n"); |
kris@kris-X682X | 7:9cda1b0f25ae | 123 | peripheral.run(15000); |
kris@kris-X682X | 6:ee9c86f06eae | 124 | peripheral.stop(); |
kris@kris-X682X | 6:ee9c86f06eae | 125 | |
kris@kris-X682X | 6:ee9c86f06eae | 126 | printf("\r\n CENTRAL \r\n\r\n"); |
kris@kris-X682X | 6:ee9c86f06eae | 127 | central.run(5000); |
kris@kris-X682X | 6:ee9c86f06eae | 128 | central.stop(); |
kris@kris-X682X | 6:ee9c86f06eae | 129 | |
kris@kris-X682X | 6:ee9c86f06eae | 130 | } |
krissl | 0:fe5389573740 | 131 | return 0; |
krissl | 0:fe5389573740 | 132 | } |
krissl | 3:f594022fe519 | 133 |