Kris Scholte Lubberink
/
SSS_Ble
Ble for smart sOlutions
source/main.cpp@3:f594022fe519, 2019-04-20 (annotated)
- Committer:
- krissl
- Date:
- Sat Apr 20 11:02:35 2019 +0000
- Revision:
- 3:f594022fe519
- Parent:
- 1:9fc54848a198
- Child:
- 6:ee9c86f06eae
test
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 | 3:f594022fe519 | 23 | #include "Adafruit_WS2801.h" |
krissl | 3:f594022fe519 | 24 | |
krissl | 3:f594022fe519 | 25 | #define STRIP_LENGTH 20 |
krissl | 3:f594022fe519 | 26 | static Adafruit_WS2801 mystrip(STRIP_LENGTH, p26,p27, WS2801_RGB); |
krissl | 1:9fc54848a198 | 27 | #include "ColorService.h" |
krissl | 0:fe5389573740 | 28 | |
krissl | 3:f594022fe519 | 29 | //Serial pc(USBTX, USBRX); |
krissl | 0:fe5389573740 | 30 | |
krissl | 0:fe5389573740 | 31 | DigitalOut led1(LED1, 1); |
krissl | 0:fe5389573740 | 32 | |
krissl | 1:9fc54848a198 | 33 | const static char DEVICE_NAME[] = "SSS-Wearable"; |
krissl | 1:9fc54848a198 | 34 | static const uint16_t uuid16_list[] = {0xF0C0FF}; |
krissl | 3:f594022fe519 | 35 | |
krissl | 3:f594022fe519 | 36 | int rainbow[] = {0xff00ff,0xff00cc,0xff0099,0xff0066,0xff0033,0xff0000,0xff3300,0xff6600, |
krissl | 3:f594022fe519 | 37 | 0xff9900,0xffcc00,0xffff00,0xccff00,0x99ff00,0x66ff00,0x33ff00,0x00ff00, |
krissl | 3:f594022fe519 | 38 | 0x00ff33,0x00ff66,0x00ff99,0x00ffcc}; |
krissl | 3:f594022fe519 | 39 | |
krissl | 0:fe5389573740 | 40 | |
krissl | 1:9fc54848a198 | 41 | static ColorService* colorServicePtr; |
krissl | 0:fe5389573740 | 42 | |
krissl | 0:fe5389573740 | 43 | static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE); |
krissl | 3:f594022fe519 | 44 | void discoveryCallback(const Gap::AdvertisementCallbackParams_t *params) |
krissl | 3:f594022fe519 | 45 | { |
krissl | 3:f594022fe519 | 46 | } |
krissl | 0:fe5389573740 | 47 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
krissl | 0:fe5389573740 | 48 | { |
krissl | 3:f594022fe519 | 49 | pc.printf("Found a device"); |
krissl | 0:fe5389573740 | 50 | } |
krissl | 0:fe5389573740 | 51 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) |
krissl | 0:fe5389573740 | 52 | { |
krissl | 0:fe5389573740 | 53 | pc.printf("\r\n *******Got connection****** \r\n"); |
krissl | 0:fe5389573740 | 54 | } |
krissl | 0:fe5389573740 | 55 | void updateSensorValue() { |
krissl | 3:f594022fe519 | 56 | // pc.printf("Right now, i should update sensor value. This runs only during connection \r\n"); |
krissl | 3:f594022fe519 | 57 | // 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 | 58 | } |
krissl | 0:fe5389573740 | 59 | |
krissl | 0:fe5389573740 | 60 | void blinkCallback(void) |
krissl | 0:fe5389573740 | 61 | { |
krissl | 3:f594022fe519 | 62 | //TODO: Every |
krissl | 0:fe5389573740 | 63 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
krissl | 0:fe5389573740 | 64 | BLE &ble = BLE::Instance(); |
krissl | 0:fe5389573740 | 65 | if (ble.gap().getState().connected) { |
krissl | 0:fe5389573740 | 66 | eventQueue.call(updateSensorValue); |
krissl | 3:f594022fe519 | 67 | } else { |
krissl | 3:f594022fe519 | 68 | ble.gap().startAdvertising(); |
krissl | 3:f594022fe519 | 69 | // wait_ms(20); |
krissl | 3:f594022fe519 | 70 | // ble.gap().stopAdvertising(); |
krissl | 3:f594022fe519 | 71 | pc.printf("Stopped advertisement"); |
krissl | 3:f594022fe519 | 72 | |
krissl | 3:f594022fe519 | 73 | // ble::ScanParameters scan_params; |
krissl | 3:f594022fe519 | 74 | // scan_params.setOwnAddressType(ble::own_address_type_t::RANDOM); |
krissl | 3:f594022fe519 | 75 | |
krissl | 3:f594022fe519 | 76 | // ble_error_t error = ble.gap().setScanParameters(scan_params); |
krissl | 3:f594022fe519 | 77 | // ble.gap().setScanParameters(scan_params); |
krissl | 3:f594022fe519 | 78 | |
krissl | 3:f594022fe519 | 79 | // if (error) { |
krissl | 3:f594022fe519 | 80 | // pc.printf("Error caused by Gap::setScanParameters\r\n"); |
krissl | 3:f594022fe519 | 81 | // pc.printf("%d", error); |
krissl | 3:f594022fe519 | 82 | // return; |
krissl | 3:f594022fe519 | 83 | // } |
krissl | 3:f594022fe519 | 84 | // ble_error_t error = ble.gap().startScan(ble::scan_duration_t(100)); |
krissl | 3:f594022fe519 | 85 | |
krissl | 3:f594022fe519 | 86 | // if (error) { |
krissl | 3:f594022fe519 | 87 | // pc.printf("Error caused by Gap::startScan\r\n"); |
krissl | 3:f594022fe519 | 88 | // return; |
krissl | 3:f594022fe519 | 89 | // } |
krissl | 3:f594022fe519 | 90 | |
krissl | 3:f594022fe519 | 91 | // printf("Scanning started\r\n"); |
krissl | 3:f594022fe519 | 92 | |
krissl | 3:f594022fe519 | 93 | |
krissl | 3:f594022fe519 | 94 | // iBeaconStartScan(); |
krissl | 3:f594022fe519 | 95 | // wait(130); |
krissl | 3:f594022fe519 | 96 | // ble.gap().stopScan(); |
krissl | 0:fe5389573740 | 97 | } |
krissl | 0:fe5389573740 | 98 | } |
krissl | 0:fe5389573740 | 99 | |
krissl | 0:fe5389573740 | 100 | /** |
krissl | 0:fe5389573740 | 101 | * This function is called when the ble initialization process has failled |
krissl | 0:fe5389573740 | 102 | */ |
krissl | 0:fe5389573740 | 103 | void onBleInitError(BLE &ble, ble_error_t error) |
krissl | 0:fe5389573740 | 104 | { |
krissl | 0:fe5389573740 | 105 | /* Initialization error handling should go here */ |
krissl | 0:fe5389573740 | 106 | } |
krissl | 0:fe5389573740 | 107 | |
krissl | 0:fe5389573740 | 108 | void printMacAddress() |
krissl | 0:fe5389573740 | 109 | { |
krissl | 0:fe5389573740 | 110 | /* Print out device MAC address to the console*/ |
krissl | 0:fe5389573740 | 111 | Gap::AddressType_t addr_type; |
krissl | 0:fe5389573740 | 112 | Gap::Address_t address; |
krissl | 0:fe5389573740 | 113 | BLE::Instance().gap().getAddress(&addr_type, address); |
krissl | 0:fe5389573740 | 114 | pc.printf("DEVICE MAC ADDRESS: "); |
krissl | 0:fe5389573740 | 115 | for (int i = 5; i >= 1; i--){ |
krissl | 0:fe5389573740 | 116 | pc.printf("%02x:", address[i]); |
krissl | 0:fe5389573740 | 117 | } |
krissl | 0:fe5389573740 | 118 | pc.printf("%02x\r\n", address[0]); |
krissl | 0:fe5389573740 | 119 | } |
krissl | 0:fe5389573740 | 120 | |
krissl | 0:fe5389573740 | 121 | /** |
krissl | 0:fe5389573740 | 122 | * Callback triggered when the ble initialization process has finished |
krissl | 0:fe5389573740 | 123 | */ |
krissl | 0:fe5389573740 | 124 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
krissl | 0:fe5389573740 | 125 | { |
krissl | 0:fe5389573740 | 126 | BLE& ble = params->ble; |
krissl | 0:fe5389573740 | 127 | ble_error_t error = params->error; |
krissl | 0:fe5389573740 | 128 | |
krissl | 0:fe5389573740 | 129 | if (error != BLE_ERROR_NONE) { |
krissl | 0:fe5389573740 | 130 | /* In case of error, forward the error handling to onBleInitError */ |
krissl | 0:fe5389573740 | 131 | onBleInitError(ble, error); |
krissl | 0:fe5389573740 | 132 | return; |
krissl | 0:fe5389573740 | 133 | } |
krissl | 0:fe5389573740 | 134 | |
krissl | 0:fe5389573740 | 135 | /* Ensure that it is the default instance of BLE */ |
krissl | 0:fe5389573740 | 136 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
krissl | 0:fe5389573740 | 137 | return; |
krissl | 0:fe5389573740 | 138 | } |
krissl | 0:fe5389573740 | 139 | |
krissl | 0:fe5389573740 | 140 | ble.gap().onDisconnection(disconnectionCallback); |
krissl | 0:fe5389573740 | 141 | ble.gap().onConnection(connectionCallback); |
krissl | 0:fe5389573740 | 142 | /* Setup primary service */ |
krissl | 1:9fc54848a198 | 143 | colorServicePtr = new ColorService(ble); |
krissl | 0:fe5389573740 | 144 | |
krissl | 0:fe5389573740 | 145 | /* Setup advertising */ |
krissl | 0:fe5389573740 | 146 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
krissl | 0:fe5389573740 | 147 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *) uuid16_list, sizeof(uuid16_list)); |
krissl | 0:fe5389573740 | 148 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME)); |
krissl | 0:fe5389573740 | 149 | // ble.gattServer().onDataRead(blinkCallback); |
krissl | 0:fe5389573740 | 150 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
krissl | 3:f594022fe519 | 151 | ble.gap().setAdvertisingInterval(150); /* 1000ms */ |
krissl | 3:f594022fe519 | 152 | pc.printf("Set up BLE \r\n"); |
krissl | 0:fe5389573740 | 153 | printMacAddress(); |
krissl | 0:fe5389573740 | 154 | } |
krissl | 0:fe5389573740 | 155 | |
krissl | 3:f594022fe519 | 156 | |
krissl | 0:fe5389573740 | 157 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
krissl | 0:fe5389573740 | 158 | BLE &ble = BLE::Instance(); |
krissl | 3:f594022fe519 | 159 | //TODO: Event handling; wel benieuwd naar wat er allemaal gebeurt |
krissl | 3:f594022fe519 | 160 | |
krissl | 0:fe5389573740 | 161 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
krissl | 0:fe5389573740 | 162 | } |
krissl | 0:fe5389573740 | 163 | |
krissl | 3:f594022fe519 | 164 | uint32_t colorMaker(uint8_t r, uint8_t g, uint8_t b){ |
krissl | 3:f594022fe519 | 165 | uint32_t c; |
krissl | 3:f594022fe519 | 166 | c = r; |
krissl | 3:f594022fe519 | 167 | c <<= 8; |
krissl | 3:f594022fe519 | 168 | c |= g; |
krissl | 3:f594022fe519 | 169 | c <<= 8; |
krissl | 3:f594022fe519 | 170 | c |= b; |
krissl | 3:f594022fe519 | 171 | return c; |
krissl | 3:f594022fe519 | 172 | } |
krissl | 3:f594022fe519 | 173 | |
krissl | 0:fe5389573740 | 174 | int main() |
krissl | 0:fe5389573740 | 175 | { |
krissl | 1:9fc54848a198 | 176 | pc.baud (115200); |
krissl | 0:fe5389573740 | 177 | |
krissl | 3:f594022fe519 | 178 | eventQueue.call_every(150, blinkCallback); |
krissl | 0:fe5389573740 | 179 | |
krissl | 0:fe5389573740 | 180 | BLE &ble = BLE::Instance(); |
krissl | 0:fe5389573740 | 181 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
krissl | 0:fe5389573740 | 182 | ble.init(bleInitComplete); |
krissl | 3:f594022fe519 | 183 | mystrip.begin(); |
krissl | 3:f594022fe519 | 184 | mystrip.show(); |
krissl | 3:f594022fe519 | 185 | |
krissl | 3:f594022fe519 | 186 | 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 | 187 | eventQueue.dispatch_forever(); |
krissl | 0:fe5389573740 | 188 | return 0; |
krissl | 0:fe5389573740 | 189 | } |
krissl | 3:f594022fe519 | 190 |