Hsieh JenYun
/
test
BLEUART
Fork of 0925BLEonly by
main.cpp@0:f6f2880bef7f, 2017-09-26 (annotated)
- Committer:
- Tanakacool
- Date:
- Tue Sep 26 02:19:35 2017 +0000
- Revision:
- 0:f6f2880bef7f
- Child:
- 1:5d6887b45ff6
BLEUART
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Tanakacool | 0:f6f2880bef7f | 1 | /* mbed Microcontroller Library |
Tanakacool | 0:f6f2880bef7f | 2 | * Copyright (c) 2006-2013 ARM Limited |
Tanakacool | 0:f6f2880bef7f | 3 | * |
Tanakacool | 0:f6f2880bef7f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Tanakacool | 0:f6f2880bef7f | 5 | * you may not use this file except in compliance with the License. |
Tanakacool | 0:f6f2880bef7f | 6 | * You may obtain a copy of the License at |
Tanakacool | 0:f6f2880bef7f | 7 | * |
Tanakacool | 0:f6f2880bef7f | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Tanakacool | 0:f6f2880bef7f | 9 | * |
Tanakacool | 0:f6f2880bef7f | 10 | * Unless required by applicable law or agreed to in writing, software |
Tanakacool | 0:f6f2880bef7f | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Tanakacool | 0:f6f2880bef7f | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Tanakacool | 0:f6f2880bef7f | 13 | * See the License for the specific language governing permissions and |
Tanakacool | 0:f6f2880bef7f | 14 | * limitations under the License. |
Tanakacool | 0:f6f2880bef7f | 15 | */ |
Tanakacool | 0:f6f2880bef7f | 16 | |
Tanakacool | 0:f6f2880bef7f | 17 | #include "mbed.h" |
Tanakacool | 0:f6f2880bef7f | 18 | #include "ble/BLE.h" |
Tanakacool | 0:f6f2880bef7f | 19 | |
Tanakacool | 0:f6f2880bef7f | 20 | #include "ble/services/UARTService.h" |
Tanakacool | 0:f6f2880bef7f | 21 | |
Tanakacool | 0:f6f2880bef7f | 22 | #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console; |
Tanakacool | 0:f6f2880bef7f | 23 | * it will have an impact on code-size and power consumption. */ |
Tanakacool | 0:f6f2880bef7f | 24 | |
Tanakacool | 0:f6f2880bef7f | 25 | #if NEED_CONSOLE_OUTPUT |
Tanakacool | 0:f6f2880bef7f | 26 | #define DEBUG(...) { printf(__VA_ARGS__); } |
Tanakacool | 0:f6f2880bef7f | 27 | #else |
Tanakacool | 0:f6f2880bef7f | 28 | #define DEBUG(...) /* nothing */ |
Tanakacool | 0:f6f2880bef7f | 29 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
Tanakacool | 0:f6f2880bef7f | 30 | |
Tanakacool | 0:f6f2880bef7f | 31 | |
Tanakacool | 0:f6f2880bef7f | 32 | |
Tanakacool | 0:f6f2880bef7f | 33 | Serial pc(USBTX, USBRX); |
Tanakacool | 0:f6f2880bef7f | 34 | BLEDevice ble; |
Tanakacool | 0:f6f2880bef7f | 35 | DigitalOut led1(P0_13); |
Tanakacool | 0:f6f2880bef7f | 36 | DigitalOut led2(P0_14); |
Tanakacool | 0:f6f2880bef7f | 37 | DigitalOut led3(P0_15); |
Tanakacool | 0:f6f2880bef7f | 38 | DigitalOut led4(P0_16); |
Tanakacool | 0:f6f2880bef7f | 39 | InterruptIn button1(P0_11); |
Tanakacool | 0:f6f2880bef7f | 40 | uint8_t state; |
Tanakacool | 0:f6f2880bef7f | 41 | uint8_t open; |
Tanakacool | 0:f6f2880bef7f | 42 | |
Tanakacool | 0:f6f2880bef7f | 43 | UARTService *uartServicePtr; |
Tanakacool | 0:f6f2880bef7f | 44 | |
Tanakacool | 0:f6f2880bef7f | 45 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
Tanakacool | 0:f6f2880bef7f | 46 | { |
Tanakacool | 0:f6f2880bef7f | 47 | DEBUG("Disconnected!\n\r"); |
Tanakacool | 0:f6f2880bef7f | 48 | DEBUG("Restarting the advertising process\n\r"); |
Tanakacool | 0:f6f2880bef7f | 49 | ble.startAdvertising(); |
Tanakacool | 0:f6f2880bef7f | 50 | } |
Tanakacool | 0:f6f2880bef7f | 51 | /* |
Tanakacool | 0:f6f2880bef7f | 52 | void opendoor() |
Tanakacool | 0:f6f2880bef7f | 53 | { |
Tanakacool | 0:f6f2880bef7f | 54 | led4=0; |
Tanakacool | 0:f6f2880bef7f | 55 | led3=1; |
Tanakacool | 0:f6f2880bef7f | 56 | wait(3); |
Tanakacool | 0:f6f2880bef7f | 57 | led4=0; |
Tanakacool | 0:f6f2880bef7f | 58 | led3=0; |
Tanakacool | 0:f6f2880bef7f | 59 | wait(10); |
Tanakacool | 0:f6f2880bef7f | 60 | led4=1; |
Tanakacool | 0:f6f2880bef7f | 61 | led3=0; |
Tanakacool | 0:f6f2880bef7f | 62 | wait(3); |
Tanakacool | 0:f6f2880bef7f | 63 | led4=0; |
Tanakacool | 0:f6f2880bef7f | 64 | led3=0; |
Tanakacool | 0:f6f2880bef7f | 65 | |
Tanakacool | 0:f6f2880bef7f | 66 | }*/ |
Tanakacool | 0:f6f2880bef7f | 67 | |
Tanakacool | 0:f6f2880bef7f | 68 | void onDataWritten(const GattWriteCallbackParams *params) |
Tanakacool | 0:f6f2880bef7f | 69 | { |
Tanakacool | 0:f6f2880bef7f | 70 | if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) { |
Tanakacool | 0:f6f2880bef7f | 71 | // uint16_t bytesRead = params->len; |
Tanakacool | 0:f6f2880bef7f | 72 | DEBUG("received %u bytes\n\r", bytesRead); |
Tanakacool | 0:f6f2880bef7f | 73 | //ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, params->len);// uint16_t bytesRead = params->len; |
Tanakacool | 0:f6f2880bef7f | 74 | uint8_t blein[params->len]; |
Tanakacool | 0:f6f2880bef7f | 75 | for(uint8_t count=0 ; count<(params->len) ; ++count){ //取得藍芽資料 |
Tanakacool | 0:f6f2880bef7f | 76 | blein[count]= *((params->data)+count); |
Tanakacool | 0:f6f2880bef7f | 77 | } |
Tanakacool | 0:f6f2880bef7f | 78 | uint8_t *a; |
Tanakacool | 0:f6f2880bef7f | 79 | if((blein[0]=='A') && (state==0)){ |
Tanakacool | 0:f6f2880bef7f | 80 | uint8_t test[] = "OPEN"; |
Tanakacool | 0:f6f2880bef7f | 81 | a = test; |
Tanakacool | 0:f6f2880bef7f | 82 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), a, 4); |
Tanakacool | 0:f6f2880bef7f | 83 | state=1; |
Tanakacool | 0:f6f2880bef7f | 84 | open=1; |
Tanakacool | 0:f6f2880bef7f | 85 | // led2=0; |
Tanakacool | 0:f6f2880bef7f | 86 | //opendoor(); |
Tanakacool | 0:f6f2880bef7f | 87 | } |
Tanakacool | 0:f6f2880bef7f | 88 | else if((blein[0]=='B') && (state==1)){ |
Tanakacool | 0:f6f2880bef7f | 89 | uint8_t test[] = "TAKE"; |
Tanakacool | 0:f6f2880bef7f | 90 | a = test; |
Tanakacool | 0:f6f2880bef7f | 91 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), a, 4); |
Tanakacool | 0:f6f2880bef7f | 92 | state=0; |
Tanakacool | 0:f6f2880bef7f | 93 | open=1; |
Tanakacool | 0:f6f2880bef7f | 94 | // led2=1; |
Tanakacool | 0:f6f2880bef7f | 95 | //opendoor(); |
Tanakacool | 0:f6f2880bef7f | 96 | } |
Tanakacool | 0:f6f2880bef7f | 97 | else{ |
Tanakacool | 0:f6f2880bef7f | 98 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, params->len);// uint16_t bytesRead = params->len; |
Tanakacool | 0:f6f2880bef7f | 99 | } |
Tanakacool | 0:f6f2880bef7f | 100 | //uint8_t test[] = "123456789012345678901"; |
Tanakacool | 0:f6f2880bef7f | 101 | // uint8_t *a; |
Tanakacool | 0:f6f2880bef7f | 102 | //a = test; |
Tanakacool | 0:f6f2880bef7f | 103 | // ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), a, params->len); |
Tanakacool | 0:f6f2880bef7f | 104 | } |
Tanakacool | 0:f6f2880bef7f | 105 | } |
Tanakacool | 0:f6f2880bef7f | 106 | |
Tanakacool | 0:f6f2880bef7f | 107 | void button1_pressed() |
Tanakacool | 0:f6f2880bef7f | 108 | { |
Tanakacool | 0:f6f2880bef7f | 109 | } |
Tanakacool | 0:f6f2880bef7f | 110 | |
Tanakacool | 0:f6f2880bef7f | 111 | void periodicCallback(void) |
Tanakacool | 0:f6f2880bef7f | 112 | { |
Tanakacool | 0:f6f2880bef7f | 113 | led1 = !led1; |
Tanakacool | 0:f6f2880bef7f | 114 | led2= state; |
Tanakacool | 0:f6f2880bef7f | 115 | if (open==1){ |
Tanakacool | 0:f6f2880bef7f | 116 | open=0; |
Tanakacool | 0:f6f2880bef7f | 117 | led3=0; |
Tanakacool | 0:f6f2880bef7f | 118 | led4=1; |
Tanakacool | 0:f6f2880bef7f | 119 | wait(1); |
Tanakacool | 0:f6f2880bef7f | 120 | led3=1; |
Tanakacool | 0:f6f2880bef7f | 121 | led4=0; |
Tanakacool | 0:f6f2880bef7f | 122 | wait(1); |
Tanakacool | 0:f6f2880bef7f | 123 | led3=0; |
Tanakacool | 0:f6f2880bef7f | 124 | led4=0; |
Tanakacool | 0:f6f2880bef7f | 125 | } |
Tanakacool | 0:f6f2880bef7f | 126 | |
Tanakacool | 0:f6f2880bef7f | 127 | } |
Tanakacool | 0:f6f2880bef7f | 128 | |
Tanakacool | 0:f6f2880bef7f | 129 | int main(void) |
Tanakacool | 0:f6f2880bef7f | 130 | { |
Tanakacool | 0:f6f2880bef7f | 131 | led1 = 1; |
Tanakacool | 0:f6f2880bef7f | 132 | led2 = 1; |
Tanakacool | 0:f6f2880bef7f | 133 | led3 = 1; |
Tanakacool | 0:f6f2880bef7f | 134 | led4 = 1; |
Tanakacool | 0:f6f2880bef7f | 135 | Ticker ticker; |
Tanakacool | 0:f6f2880bef7f | 136 | ticker.attach(periodicCallback, 1); |
Tanakacool | 0:f6f2880bef7f | 137 | |
Tanakacool | 0:f6f2880bef7f | 138 | DEBUG("Initialising the nRF51822\n\r"); |
Tanakacool | 0:f6f2880bef7f | 139 | ble.init(); |
Tanakacool | 0:f6f2880bef7f | 140 | ble.onDisconnection(disconnectionCallback); |
Tanakacool | 0:f6f2880bef7f | 141 | ble.onDataWritten(onDataWritten); |
Tanakacool | 0:f6f2880bef7f | 142 | |
Tanakacool | 0:f6f2880bef7f | 143 | /* setup advertising */ |
Tanakacool | 0:f6f2880bef7f | 144 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
Tanakacool | 0:f6f2880bef7f | 145 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Tanakacool | 0:f6f2880bef7f | 146 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
Tanakacool | 0:f6f2880bef7f | 147 | (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); |
Tanakacool | 0:f6f2880bef7f | 148 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
Tanakacool | 0:f6f2880bef7f | 149 | (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
Tanakacool | 0:f6f2880bef7f | 150 | |
Tanakacool | 0:f6f2880bef7f | 151 | ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */ |
Tanakacool | 0:f6f2880bef7f | 152 | ble.startAdvertising(); |
Tanakacool | 0:f6f2880bef7f | 153 | |
Tanakacool | 0:f6f2880bef7f | 154 | UARTService uartService(ble); |
Tanakacool | 0:f6f2880bef7f | 155 | uartServicePtr = &uartService; |
Tanakacool | 0:f6f2880bef7f | 156 | |
Tanakacool | 0:f6f2880bef7f | 157 | while (true) { |
Tanakacool | 0:f6f2880bef7f | 158 | ble.waitForEvent(); |
Tanakacool | 0:f6f2880bef7f | 159 | } |
Tanakacool | 0:f6f2880bef7f | 160 | } |