BLEUART

Fork of 0925BLEonly by LAXAN01

Committer:
Tanakacool
Date:
Fri Sep 29 08:51:40 2017 +0000
Revision:
4:d1480d298d7f
Parent:
3:837f3a27c2f7
Child:
5:c459bdd60522
V4?END

Who changed what in which revision?

UserRevisionLine numberNew 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 3:837f3a27c2f7 86 // uint8_t *a;
Tanakacool 3:837f3a27c2f7 87 // a=longble;
Tanakacool 3:837f3a27c2f7 88 // a+=3;
Tanakacool 4:d1480d298d7f 89 /* for(uint8_t a=0;a<252;a++){
Tanakacool 4:d1480d298d7f 90 longble[a]=longble[a+3];
Tanakacool 4:d1480d298d7f 91 }*/
Tanakacool 4:d1480d298d7f 92
Tanakacool 4:d1480d298d7f 93 uint8_t target[255];
Tanakacool 4:d1480d298d7f 94
Tanakacool 4:d1480d298d7f 95 memcpy( target, longble + 3, strlen((const char*)longble) );
Tanakacool 4:d1480d298d7f 96
Tanakacool 4:d1480d298d7f 97 //Serial.print(strlen(target));
Tanakacool 4:d1480d298d7f 98
Tanakacool 4:d1480d298d7f 99 pc.printf("%S\r\n",target);
Tanakacool 4:d1480d298d7f 100 // pc.printf("%S\r\n",longble) ;
Tanakacool 4:d1480d298d7f 101 // pc.printf("%d\r\n",(sizeof(longble))) ;
Tanakacool 3:837f3a27c2f7 102 }
Tanakacool 1:5d6887b45ff6 103 uint8_t blein[params->len];
Tanakacool 3:837f3a27c2f7 104 for(uint8_t count=0 ; count<((params->len)) ; ++count){ //取得藍芽資料
Tanakacool 0:f6f2880bef7f 105 blein[count]= *((params->data)+count);
Tanakacool 3:837f3a27c2f7 106 longble[count+lb]= *((params->data)+count);
Tanakacool 0:f6f2880bef7f 107 }
Tanakacool 2:0d84e066f294 108 //x=0;
Tanakacool 1:5d6887b45ff6 109 lb+=(params->len);
Tanakacool 1:5d6887b45ff6 110 if(lb>110)
Tanakacool 1:5d6887b45ff6 111 lb=0;
Tanakacool 3:837f3a27c2f7 112
Tanakacool 1:5d6887b45ff6 113 // uint8_t *a;
Tanakacool 1:5d6887b45ff6 114 // uint8_t test[] = "OPEN";
Tanakacool 1:5d6887b45ff6 115 // a = longble;
Tanakacool 1:5d6887b45ff6 116 //ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), a, 20);
Tanakacool 3:837f3a27c2f7 117
Tanakacool 1:5d6887b45ff6 118 //ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, params->len);// uint16_t bytesRead = params->len;
Tanakacool 1:5d6887b45ff6 119
Tanakacool 0:f6f2880bef7f 120 //uint8_t test[] = "123456789012345678901";
Tanakacool 0:f6f2880bef7f 121 // uint8_t *a;
Tanakacool 0:f6f2880bef7f 122 //a = test;
Tanakacool 1:5d6887b45ff6 123 // ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), a, params->len);
Tanakacool 0:f6f2880bef7f 124 }
Tanakacool 0:f6f2880bef7f 125 }
Tanakacool 0:f6f2880bef7f 126
Tanakacool 0:f6f2880bef7f 127 void button1_pressed()
Tanakacool 1:5d6887b45ff6 128 {
Tanakacool 1:5d6887b45ff6 129
Tanakacool 0:f6f2880bef7f 130 }
Tanakacool 0:f6f2880bef7f 131
Tanakacool 0:f6f2880bef7f 132 void periodicCallback(void)
Tanakacool 0:f6f2880bef7f 133 {
Tanakacool 0:f6f2880bef7f 134 led1 = !led1;
Tanakacool 0:f6f2880bef7f 135 led2= state;
Tanakacool 0:f6f2880bef7f 136 if (open==1){
Tanakacool 0:f6f2880bef7f 137 open=0;
Tanakacool 0:f6f2880bef7f 138 led3=0;
Tanakacool 0:f6f2880bef7f 139 led4=1;
Tanakacool 0:f6f2880bef7f 140 wait(1);
Tanakacool 0:f6f2880bef7f 141 led3=1;
Tanakacool 0:f6f2880bef7f 142 led4=0;
Tanakacool 0:f6f2880bef7f 143 wait(1);
Tanakacool 0:f6f2880bef7f 144 led3=0;
Tanakacool 0:f6f2880bef7f 145 led4=0;
Tanakacool 0:f6f2880bef7f 146 }
Tanakacool 0:f6f2880bef7f 147
Tanakacool 0:f6f2880bef7f 148 }
Tanakacool 0:f6f2880bef7f 149
Tanakacool 0:f6f2880bef7f 150 int main(void)
Tanakacool 0:f6f2880bef7f 151 {
Tanakacool 0:f6f2880bef7f 152 led1 = 1;
Tanakacool 0:f6f2880bef7f 153 led2 = 1;
Tanakacool 0:f6f2880bef7f 154 led3 = 1;
Tanakacool 0:f6f2880bef7f 155 led4 = 1;
Tanakacool 0:f6f2880bef7f 156 Ticker ticker;
Tanakacool 0:f6f2880bef7f 157 ticker.attach(periodicCallback, 1);
Tanakacool 0:f6f2880bef7f 158
Tanakacool 0:f6f2880bef7f 159 DEBUG("Initialising the nRF51822\n\r");
Tanakacool 0:f6f2880bef7f 160 ble.init();
Tanakacool 0:f6f2880bef7f 161 ble.onDisconnection(disconnectionCallback);
Tanakacool 0:f6f2880bef7f 162 ble.onDataWritten(onDataWritten);
Tanakacool 0:f6f2880bef7f 163
Tanakacool 0:f6f2880bef7f 164 /* setup advertising */
Tanakacool 0:f6f2880bef7f 165 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
Tanakacool 0:f6f2880bef7f 166 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
Tanakacool 0:f6f2880bef7f 167 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
Tanakacool 0:f6f2880bef7f 168 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
Tanakacool 0:f6f2880bef7f 169 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
Tanakacool 0:f6f2880bef7f 170 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
Tanakacool 0:f6f2880bef7f 171
Tanakacool 0:f6f2880bef7f 172 ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
Tanakacool 0:f6f2880bef7f 173 ble.startAdvertising();
Tanakacool 0:f6f2880bef7f 174
Tanakacool 0:f6f2880bef7f 175 UARTService uartService(ble);
Tanakacool 0:f6f2880bef7f 176 uartServicePtr = &uartService;
Tanakacool 0:f6f2880bef7f 177
Tanakacool 0:f6f2880bef7f 178 while (true) {
Tanakacool 0:f6f2880bef7f 179 ble.waitForEvent();
Tanakacool 0:f6f2880bef7f 180 }
Tanakacool 0:f6f2880bef7f 181 }