Hsieh JenYun
/
test
BLEUART
Fork of 0925BLEonly by
main.cpp@5:c459bdd60522, 2017-09-30 (annotated)
- Committer:
- Tanakacool
- Date:
- Sat Sep 30 01:04:26 2017 +0000
- Revision:
- 5:c459bdd60522
- Parent:
- 4:d1480d298d7f
V5
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 | 3:837f3a27c2f7 | 19 | #include <stdio.h> |
Tanakacool | 3:837f3a27c2f7 | 20 | #include <string.h> |
Tanakacool | 3:837f3a27c2f7 | 21 | |
Tanakacool | 0:f6f2880bef7f | 22 | |
Tanakacool | 0:f6f2880bef7f | 23 | #include "ble/services/UARTService.h" |
Tanakacool | 0:f6f2880bef7f | 24 | |
Tanakacool | 0:f6f2880bef7f | 25 | #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console; |
Tanakacool | 0:f6f2880bef7f | 26 | * it will have an impact on code-size and power consumption. */ |
Tanakacool | 0:f6f2880bef7f | 27 | |
Tanakacool | 0:f6f2880bef7f | 28 | #if NEED_CONSOLE_OUTPUT |
Tanakacool | 0:f6f2880bef7f | 29 | #define DEBUG(...) { printf(__VA_ARGS__); } |
Tanakacool | 0:f6f2880bef7f | 30 | #else |
Tanakacool | 0:f6f2880bef7f | 31 | #define DEBUG(...) /* nothing */ |
Tanakacool | 0:f6f2880bef7f | 32 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
Tanakacool | 0:f6f2880bef7f | 33 | |
Tanakacool | 0:f6f2880bef7f | 34 | |
Tanakacool | 0:f6f2880bef7f | 35 | |
Tanakacool | 0:f6f2880bef7f | 36 | Serial pc(USBTX, USBRX); |
Tanakacool | 0:f6f2880bef7f | 37 | BLEDevice ble; |
Tanakacool | 0:f6f2880bef7f | 38 | DigitalOut led1(P0_13); |
Tanakacool | 0:f6f2880bef7f | 39 | DigitalOut led2(P0_14); |
Tanakacool | 0:f6f2880bef7f | 40 | DigitalOut led3(P0_15); |
Tanakacool | 0:f6f2880bef7f | 41 | DigitalOut led4(P0_16); |
Tanakacool | 0:f6f2880bef7f | 42 | InterruptIn button1(P0_11); |
Tanakacool | 0:f6f2880bef7f | 43 | uint8_t state; |
Tanakacool | 0:f6f2880bef7f | 44 | uint8_t open; |
Tanakacool | 1:5d6887b45ff6 | 45 | |
Tanakacool | 3:837f3a27c2f7 | 46 | uint8_t longble[255]; |
Tanakacool | 1:5d6887b45ff6 | 47 | uint8_t lb=0 ; |
Tanakacool | 0:f6f2880bef7f | 48 | |
Tanakacool | 0:f6f2880bef7f | 49 | UARTService *uartServicePtr; |
Tanakacool | 0:f6f2880bef7f | 50 | |
Tanakacool | 0:f6f2880bef7f | 51 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
Tanakacool | 0:f6f2880bef7f | 52 | { |
Tanakacool | 0:f6f2880bef7f | 53 | DEBUG("Disconnected!\n\r"); |
Tanakacool | 0:f6f2880bef7f | 54 | DEBUG("Restarting the advertising process\n\r"); |
Tanakacool | 0:f6f2880bef7f | 55 | ble.startAdvertising(); |
Tanakacool | 0:f6f2880bef7f | 56 | } |
Tanakacool | 0:f6f2880bef7f | 57 | /* |
Tanakacool | 0:f6f2880bef7f | 58 | void opendoor() |
Tanakacool | 0:f6f2880bef7f | 59 | { |
Tanakacool | 0:f6f2880bef7f | 60 | led4=0; |
Tanakacool | 0:f6f2880bef7f | 61 | led3=1; |
Tanakacool | 0:f6f2880bef7f | 62 | wait(3); |
Tanakacool | 0:f6f2880bef7f | 63 | led4=0; |
Tanakacool | 0:f6f2880bef7f | 64 | led3=0; |
Tanakacool | 0:f6f2880bef7f | 65 | wait(10); |
Tanakacool | 0:f6f2880bef7f | 66 | led4=1; |
Tanakacool | 0:f6f2880bef7f | 67 | led3=0; |
Tanakacool | 0:f6f2880bef7f | 68 | wait(3); |
Tanakacool | 0:f6f2880bef7f | 69 | led4=0; |
Tanakacool | 0:f6f2880bef7f | 70 | led3=0; |
Tanakacool | 0:f6f2880bef7f | 71 | |
Tanakacool | 0:f6f2880bef7f | 72 | }*/ |
Tanakacool | 0:f6f2880bef7f | 73 | |
Tanakacool | 0:f6f2880bef7f | 74 | void onDataWritten(const GattWriteCallbackParams *params) |
Tanakacool | 0:f6f2880bef7f | 75 | { |
Tanakacool | 0:f6f2880bef7f | 76 | if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) { |
Tanakacool | 0:f6f2880bef7f | 77 | // uint16_t bytesRead = params->len; |
Tanakacool | 0:f6f2880bef7f | 78 | DEBUG("received %u bytes\n\r", bytesRead); |
Tanakacool | 1:5d6887b45ff6 | 79 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, params->len);// uint16_t bytesRead = params->len; |
Tanakacool | 1:5d6887b45ff6 | 80 | if(((*(params->data))=='<')&&((*(params->data+1))=='S')&&((*(params->data+2))=='>')){ // <S> |
Tanakacool | 1:5d6887b45ff6 | 81 | lb=0; |
Tanakacool | 3:837f3a27c2f7 | 82 | memset(longble,'\0',sizeof(longble)); |
Tanakacool | 3:837f3a27c2f7 | 83 | |
Tanakacool | 3:837f3a27c2f7 | 84 | } |
Tanakacool | 3:837f3a27c2f7 | 85 | if(((*(params->data))=='<')&&((*(params->data+1))=='E')&&((*(params->data+2))=='>')){ // <E> |
Tanakacool | 5:c459bdd60522 | 86 | uint8_t target[255]; |
Tanakacool | 4:d1480d298d7f | 87 | memcpy( target, longble + 3, strlen((const char*)longble) ); |
Tanakacool | 4:d1480d298d7f | 88 | pc.printf("%S\r\n",target); |
Tanakacool | 5:c459bdd60522 | 89 | // pc.printf("%S\r\n",longble) ; |
Tanakacool | 5:c459bdd60522 | 90 | // pc.printf("%d\r\n",(sizeof(longble))) ; |
Tanakacool | 3:837f3a27c2f7 | 91 | } |
Tanakacool | 5:c459bdd60522 | 92 | |
Tanakacool | 1:5d6887b45ff6 | 93 | uint8_t blein[params->len]; |
Tanakacool | 3:837f3a27c2f7 | 94 | for(uint8_t count=0 ; count<((params->len)) ; ++count){ //取得藍芽資料 |
Tanakacool | 0:f6f2880bef7f | 95 | blein[count]= *((params->data)+count); |
Tanakacool | 3:837f3a27c2f7 | 96 | longble[count+lb]= *((params->data)+count); |
Tanakacool | 0:f6f2880bef7f | 97 | } |
Tanakacool | 2:0d84e066f294 | 98 | //x=0; |
Tanakacool | 1:5d6887b45ff6 | 99 | lb+=(params->len); |
Tanakacool | 1:5d6887b45ff6 | 100 | if(lb>110) |
Tanakacool | 1:5d6887b45ff6 | 101 | lb=0; |
Tanakacool | 3:837f3a27c2f7 | 102 | |
Tanakacool | 1:5d6887b45ff6 | 103 | // uint8_t *a; |
Tanakacool | 1:5d6887b45ff6 | 104 | // uint8_t test[] = "OPEN"; |
Tanakacool | 1:5d6887b45ff6 | 105 | // a = longble; |
Tanakacool | 1:5d6887b45ff6 | 106 | //ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), a, 20); |
Tanakacool | 3:837f3a27c2f7 | 107 | |
Tanakacool | 1:5d6887b45ff6 | 108 | //ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, params->len);// uint16_t bytesRead = params->len; |
Tanakacool | 1:5d6887b45ff6 | 109 | |
Tanakacool | 0:f6f2880bef7f | 110 | //uint8_t test[] = "123456789012345678901"; |
Tanakacool | 0:f6f2880bef7f | 111 | // uint8_t *a; |
Tanakacool | 0:f6f2880bef7f | 112 | //a = test; |
Tanakacool | 1:5d6887b45ff6 | 113 | // ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), a, params->len); |
Tanakacool | 0:f6f2880bef7f | 114 | } |
Tanakacool | 0:f6f2880bef7f | 115 | } |
Tanakacool | 0:f6f2880bef7f | 116 | |
Tanakacool | 0:f6f2880bef7f | 117 | void button1_pressed() |
Tanakacool | 1:5d6887b45ff6 | 118 | { |
Tanakacool | 1:5d6887b45ff6 | 119 | |
Tanakacool | 0:f6f2880bef7f | 120 | } |
Tanakacool | 0:f6f2880bef7f | 121 | |
Tanakacool | 0:f6f2880bef7f | 122 | void periodicCallback(void) |
Tanakacool | 0:f6f2880bef7f | 123 | { |
Tanakacool | 0:f6f2880bef7f | 124 | led1 = !led1; |
Tanakacool | 0:f6f2880bef7f | 125 | led2= state; |
Tanakacool | 0:f6f2880bef7f | 126 | if (open==1){ |
Tanakacool | 0:f6f2880bef7f | 127 | open=0; |
Tanakacool | 0:f6f2880bef7f | 128 | led3=0; |
Tanakacool | 0:f6f2880bef7f | 129 | led4=1; |
Tanakacool | 0:f6f2880bef7f | 130 | wait(1); |
Tanakacool | 0:f6f2880bef7f | 131 | led3=1; |
Tanakacool | 0:f6f2880bef7f | 132 | led4=0; |
Tanakacool | 0:f6f2880bef7f | 133 | wait(1); |
Tanakacool | 0:f6f2880bef7f | 134 | led3=0; |
Tanakacool | 0:f6f2880bef7f | 135 | led4=0; |
Tanakacool | 0:f6f2880bef7f | 136 | } |
Tanakacool | 0:f6f2880bef7f | 137 | |
Tanakacool | 0:f6f2880bef7f | 138 | } |
Tanakacool | 0:f6f2880bef7f | 139 | |
Tanakacool | 0:f6f2880bef7f | 140 | int main(void) |
Tanakacool | 0:f6f2880bef7f | 141 | { |
Tanakacool | 0:f6f2880bef7f | 142 | led1 = 1; |
Tanakacool | 0:f6f2880bef7f | 143 | led2 = 1; |
Tanakacool | 0:f6f2880bef7f | 144 | led3 = 1; |
Tanakacool | 0:f6f2880bef7f | 145 | led4 = 1; |
Tanakacool | 0:f6f2880bef7f | 146 | Ticker ticker; |
Tanakacool | 0:f6f2880bef7f | 147 | ticker.attach(periodicCallback, 1); |
Tanakacool | 0:f6f2880bef7f | 148 | |
Tanakacool | 0:f6f2880bef7f | 149 | DEBUG("Initialising the nRF51822\n\r"); |
Tanakacool | 0:f6f2880bef7f | 150 | ble.init(); |
Tanakacool | 0:f6f2880bef7f | 151 | ble.onDisconnection(disconnectionCallback); |
Tanakacool | 0:f6f2880bef7f | 152 | ble.onDataWritten(onDataWritten); |
Tanakacool | 0:f6f2880bef7f | 153 | |
Tanakacool | 0:f6f2880bef7f | 154 | /* setup advertising */ |
Tanakacool | 0:f6f2880bef7f | 155 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
Tanakacool | 0:f6f2880bef7f | 156 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Tanakacool | 0:f6f2880bef7f | 157 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
Tanakacool | 0:f6f2880bef7f | 158 | (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); |
Tanakacool | 0:f6f2880bef7f | 159 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
Tanakacool | 0:f6f2880bef7f | 160 | (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
Tanakacool | 0:f6f2880bef7f | 161 | |
Tanakacool | 0:f6f2880bef7f | 162 | ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */ |
Tanakacool | 0:f6f2880bef7f | 163 | ble.startAdvertising(); |
Tanakacool | 0:f6f2880bef7f | 164 | |
Tanakacool | 0:f6f2880bef7f | 165 | UARTService uartService(ble); |
Tanakacool | 0:f6f2880bef7f | 166 | uartServicePtr = &uartService; |
Tanakacool | 0:f6f2880bef7f | 167 | |
Tanakacool | 0:f6f2880bef7f | 168 | while (true) { |
Tanakacool | 0:f6f2880bef7f | 169 | ble.waitForEvent(); |
Tanakacool | 0:f6f2880bef7f | 170 | } |
Tanakacool | 0:f6f2880bef7f | 171 | } |