Kris Scholte Lubberink
/
SSS_Ble
Ble for smart sOlutions
source/main.cpp@9:92d861703f96, 2019-06-11 (annotated)
- Committer:
- kris@kris-X682X
- Date:
- Tue Jun 11 11:16:55 2019 +0200
- Revision:
- 9:92d861703f96
- Parent:
- 8:369b80cef5ae
- Child:
- 10:d845189d146e
WS2812 working
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" |
krissl | 1:9fc54848a198 | 23 | #include "ColorService.h" |
kris@kris-X682X | 6:ee9c86f06eae | 24 | #include "BleDeviceCentral.h" |
kris@kris-X682X | 6:ee9c86f06eae | 25 | #include "BleDevicePeripheral.h" |
kris@kris-X682X | 6:ee9c86f06eae | 26 | #define MBED_CONF_PLATFORM_ERROR_FILENAME_CAPTURE_ENABLED true |
krissl | 0:fe5389573740 | 27 | DigitalOut led1(LED1, 1); |
krissl | 0:fe5389573740 | 28 | |
kris@kris-X682X | 6:ee9c86f06eae | 29 | //const static char DEVICE_NAME[] = "SSS-Wearable"; |
krissl | 1:9fc54848a198 | 30 | static const uint16_t uuid16_list[] = {0xF0C0FF}; |
kris@kris-X682X | 6:ee9c86f06eae | 31 | |
krissl | 3:f594022fe519 | 32 | int rainbow[] = {0xff00ff,0xff00cc,0xff0099,0xff0066,0xff0033,0xff0000,0xff3300,0xff6600, |
krissl | 3:f594022fe519 | 33 | 0xff9900,0xffcc00,0xffff00,0xccff00,0x99ff00,0x66ff00,0x33ff00,0x00ff00, |
krissl | 3:f594022fe519 | 34 | 0x00ff33,0x00ff66,0x00ff99,0x00ffcc}; |
kris@kris-X682X | 6:ee9c86f06eae | 35 | |
krissl | 0:fe5389573740 | 36 | |
kris@kris-X682X | 6:ee9c86f06eae | 37 | //static ColorService* colorServicePtr; |
krissl | 0:fe5389573740 | 38 | |
krissl | 0:fe5389573740 | 39 | static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE); |
kris@kris-X682X | 6:ee9c86f06eae | 40 | |
krissl | 0:fe5389573740 | 41 | void updateSensorValue() { |
kris@kris-X682X | 6:ee9c86f06eae | 42 | // printf("Right now, i should update sensor value. This runs only during connection \r\n"); |
krissl | 3:f594022fe519 | 43 | // 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 | 44 | } |
krissl | 0:fe5389573740 | 45 | |
krissl | 0:fe5389573740 | 46 | void blinkCallback(void) |
krissl | 0:fe5389573740 | 47 | { |
kris@kris-X682X | 8:369b80cef5ae | 48 | //TODO: Every |
krissl | 0:fe5389573740 | 49 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
krissl | 0:fe5389573740 | 50 | BLE &ble = BLE::Instance(); |
kris@kris-X682X | 6:ee9c86f06eae | 51 | |
kris@kris-X682X | 6:ee9c86f06eae | 52 | |
krissl | 0:fe5389573740 | 53 | if (ble.gap().getState().connected) { |
krissl | 0:fe5389573740 | 54 | eventQueue.call(updateSensorValue); |
krissl | 3:f594022fe519 | 55 | } else { |
kris@kris-X682X | 6:ee9c86f06eae | 56 | ble.gap().startAdvertising(); |
kris@kris-X682X | 6:ee9c86f06eae | 57 | printf("Stopped advertisement"); |
krissl | 0:fe5389573740 | 58 | } |
krissl | 0:fe5389573740 | 59 | } |
krissl | 0:fe5389573740 | 60 | |
krissl | 0:fe5389573740 | 61 | void printMacAddress() |
krissl | 0:fe5389573740 | 62 | { |
krissl | 0:fe5389573740 | 63 | /* Print out device MAC address to the console*/ |
krissl | 0:fe5389573740 | 64 | Gap::AddressType_t addr_type; |
krissl | 0:fe5389573740 | 65 | Gap::Address_t address; |
krissl | 0:fe5389573740 | 66 | BLE::Instance().gap().getAddress(&addr_type, address); |
kris@kris-X682X | 6:ee9c86f06eae | 67 | printf("DEVICE MAC ADDRESS: "); |
krissl | 0:fe5389573740 | 68 | for (int i = 5; i >= 1; i--){ |
kris@kris-X682X | 6:ee9c86f06eae | 69 | printf("%02x:", address[i]); |
krissl | 0:fe5389573740 | 70 | } |
kris@kris-X682X | 6:ee9c86f06eae | 71 | printf("%02x\r\n", address[0]); |
krissl | 0:fe5389573740 | 72 | } |
krissl | 0:fe5389573740 | 73 | |
krissl | 0:fe5389573740 | 74 | /** |
krissl | 0:fe5389573740 | 75 | * Callback triggered when the ble initialization process has finished |
krissl | 0:fe5389573740 | 76 | */ |
krissl | 0:fe5389573740 | 77 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
krissl | 0:fe5389573740 | 78 | { |
krissl | 0:fe5389573740 | 79 | |
krissl | 0:fe5389573740 | 80 | } |
krissl | 0:fe5389573740 | 81 | |
krissl | 3:f594022fe519 | 82 | |
krissl | 0:fe5389573740 | 83 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
krissl | 0:fe5389573740 | 84 | BLE &ble = BLE::Instance(); |
krissl | 3:f594022fe519 | 85 | //TODO: Event handling; wel benieuwd naar wat er allemaal gebeurt |
krissl | 0:fe5389573740 | 86 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
krissl | 0:fe5389573740 | 87 | } |
krissl | 0:fe5389573740 | 88 | |
krissl | 3:f594022fe519 | 89 | uint32_t colorMaker(uint8_t r, uint8_t g, uint8_t b){ |
kris@kris-X682X | 6:ee9c86f06eae | 90 | uint32_t c; |
kris@kris-X682X | 6:ee9c86f06eae | 91 | c = r; |
kris@kris-X682X | 6:ee9c86f06eae | 92 | c <<= 8; |
kris@kris-X682X | 6:ee9c86f06eae | 93 | c |= g; |
kris@kris-X682X | 6:ee9c86f06eae | 94 | c <<= 8; |
kris@kris-X682X | 6:ee9c86f06eae | 95 | c |= b; |
kris@kris-X682X | 6:ee9c86f06eae | 96 | return c; |
krissl | 3:f594022fe519 | 97 | } |
krissl | 3:f594022fe519 | 98 | |
kris@kris-X682X | 8:369b80cef5ae | 99 | |
kris@kris-X682X | 8:369b80cef5ae | 100 | |
krissl | 0:fe5389573740 | 101 | int main() |
krissl | 0:fe5389573740 | 102 | { |
kris@kris-X682X | 6:ee9c86f06eae | 103 | BLE& ble = BLE::Instance(); |
kris@kris-X682X | 6:ee9c86f06eae | 104 | events::EventQueue queue; |
kris@kris-X682X | 6:ee9c86f06eae | 105 | printf(" Is filename capture enabled? \t\t"); |
kris@kris-X682X | 9:92d861703f96 | 106 | // DigitalOut p = DigitalOut(p2); |
kris@kris-X682X | 9:92d861703f96 | 107 | // p = 1; |
kris@kris-X682X | 8:369b80cef5ae | 108 | #if MBED_CONF_PLATFORM_ERROR_FILENAME_CAPTURE_ENABLED |
kris@kris-X682X | 6:ee9c86f06eae | 109 | printf(" Enabled filename capture"); |
kris@kris-X682X | 8:369b80cef5ae | 110 | #endif |
kris@kris-X682X | 6:ee9c86f06eae | 111 | |
kris@kris-X682X | 7:9cda1b0f25ae | 112 | BleDevicePeripheral peripheral(ble, queue); |
kris@kris-X682X | 7:9cda1b0f25ae | 113 | BleDeviceCentral central(ble, queue); |
kris@kris-X682X | 7:9cda1b0f25ae | 114 | |
kris@kris-X682X | 6:ee9c86f06eae | 115 | |
kris@kris-X682X | 6:ee9c86f06eae | 116 | while(1) { |
kris@kris-X682X | 6:ee9c86f06eae | 117 | printf("\r\n PERIPHERAL \r\n\r\n"); |
kris@kris-X682X | 7:9cda1b0f25ae | 118 | peripheral.run(15000); |
kris@kris-X682X | 6:ee9c86f06eae | 119 | peripheral.stop(); |
kris@kris-X682X | 6:ee9c86f06eae | 120 | |
kris@kris-X682X | 6:ee9c86f06eae | 121 | printf("\r\n CENTRAL \r\n\r\n"); |
kris@kris-X682X | 8:369b80cef5ae | 122 | central.run(10000); |
kris@kris-X682X | 6:ee9c86f06eae | 123 | central.stop(); |
kris@kris-X682X | 6:ee9c86f06eae | 124 | |
kris@kris-X682X | 6:ee9c86f06eae | 125 | } |
krissl | 0:fe5389573740 | 126 | return 0; |
krissl | 0:fe5389573740 | 127 | } |
krissl | 3:f594022fe519 | 128 |