demo

Dependencies:   BLE_API mbed nRF51822 X_NUCLEO_IDB0XA1

Committer:
tridung141196
Date:
Wed Aug 02 01:36:20 2017 +0000
Revision:
2:cf9e91db9fb0
Parent:
1:cd80403c0bc1
Child:
3:aceb81e0eab5
app ho?n ch?nh ch?a c? uuid;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tridung141196 0:223260474c81 1 /* mbed Microcontroller Library
tridung141196 0:223260474c81 2 * Copyright (c) 2006-2013 ARM Limited
tridung141196 0:223260474c81 3 *
tridung141196 0:223260474c81 4 * Licensed under the Apache License, Version 2.0 (the "License");
tridung141196 0:223260474c81 5 * you may not use this file except in compliance with the License.
tridung141196 0:223260474c81 6 * You may obtain a copy of the License at
tridung141196 0:223260474c81 7 *
tridung141196 0:223260474c81 8 * http://www.apache.org/licenses/LICENSE-2.0
tridung141196 0:223260474c81 9 *
tridung141196 0:223260474c81 10 * Unless required by applicable law or agreed to in writing, software
tridung141196 0:223260474c81 11 * distributed under the License is distributed on an "AS IS" BASIS,
tridung141196 0:223260474c81 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tridung141196 0:223260474c81 13 * See the License for the specific language governing permissions and
tridung141196 0:223260474c81 14 * limitations under the License.
tridung141196 0:223260474c81 15 */
tridung141196 0:223260474c81 16
tridung141196 0:223260474c81 17 #include "mbed.h"
tridung141196 0:223260474c81 18 #include "ble/BLE.h"
tridung141196 0:223260474c81 19 #include "ble/services/UARTService.h"
tridung141196 0:223260474c81 20
tridung141196 0:223260474c81 21
tridung141196 2:cf9e91db9fb0 22 DigitalOut led1(p7);
tridung141196 0:223260474c81 23 DigitalOut Relay_1A(p28);
tridung141196 0:223260474c81 24 DigitalOut Relay_1B(p25);
tridung141196 0:223260474c81 25 DigitalOut Relay_2A(p24);
tridung141196 0:223260474c81 26 DigitalOut Relay_2B(p23);
tridung141196 0:223260474c81 27 DigitalOut Relay_3A(p22);
tridung141196 0:223260474c81 28 DigitalOut Relay_3B(p21);
tridung141196 0:223260474c81 29 DigitalOut Relay_4A(p9);
tridung141196 0:223260474c81 30 DigitalOut Relay_4B(p16);
tridung141196 0:223260474c81 31 DigitalOut Relay_1(p17);
tridung141196 0:223260474c81 32 DigitalOut Relay_2(p18);
tridung141196 0:223260474c81 33 DigitalOut Relay_3(p19);
tridung141196 0:223260474c81 34 DigitalOut Relay_4(p20);
tridung141196 0:223260474c81 35
tridung141196 0:223260474c81 36 Serial pc(p10, p11);
tridung141196 0:223260474c81 37 BLEDevice ble;
tridung141196 0:223260474c81 38 UARTService *uartServicePtr;
tridung141196 0:223260474c81 39
tridung141196 0:223260474c81 40 //// Other
tridung141196 0:223260474c81 41 #define ON 0
tridung141196 0:223260474c81 42 #define OFF 1
tridung141196 0:223260474c81 43 uint8_t g_cmd;
tridung141196 0:223260474c81 44 ////////////////////////////////////////////////////////////////////
tridung141196 0:223260474c81 45 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
tridung141196 0:223260474c81 46 {
tridung141196 0:223260474c81 47 ble.startAdvertising();
tridung141196 2:cf9e91db9fb0 48 pc.printf("\n\r# disconnected\n\r");
tridung141196 1:cd80403c0bc1 49 led1 = 0;
tridung141196 1:cd80403c0bc1 50 }
tridung141196 2:cf9e91db9fb0 51
tridung141196 1:cd80403c0bc1 52 void connectionCallback( const Gap::ConnectionCallbackParams_t *params ) {
tridung141196 2:cf9e91db9fb0 53 pc.printf("\n\r# connected\n\r");
tridung141196 1:cd80403c0bc1 54 led1 = 1;
tridung141196 0:223260474c81 55 }
tridung141196 0:223260474c81 56 void onDataWritten(const GattWriteCallbackParams *params)
tridung141196 0:223260474c81 57 {
tridung141196 0:223260474c81 58 if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
tridung141196 0:223260474c81 59 uint16_t bytesRead = params->len;
tridung141196 0:223260474c81 60 g_cmd = params->data[0];
tridung141196 0:223260474c81 61 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
tridung141196 0:223260474c81 62 }
tridung141196 0:223260474c81 63 }
tridung141196 0:223260474c81 64
tridung141196 0:223260474c81 65 /////////////////////////////////////////////////////////////////////
tridung141196 0:223260474c81 66 void periodicCallback(void)
tridung141196 0:223260474c81 67 {
tridung141196 2:cf9e91db9fb0 68 // led1 = !led1;
tridung141196 0:223260474c81 69 }
tridung141196 0:223260474c81 70 //////////////////////////////////////////////////////////////////////
tridung141196 0:223260474c81 71 void up(void) //su dung motor 1 & 2
tridung141196 0:223260474c81 72 {
tridung141196 0:223260474c81 73 Relay_1A = 0;
tridung141196 0:223260474c81 74 Relay_1B = 1;
tridung141196 0:223260474c81 75 Relay_2A = 0;
tridung141196 0:223260474c81 76 Relay_2B = 1;
tridung141196 0:223260474c81 77 pc.printf("\n\r# go up\n\r");
tridung141196 0:223260474c81 78 }
tridung141196 0:223260474c81 79 void down(void) //su dung motor 1 & 2
tridung141196 0:223260474c81 80 {
tridung141196 0:223260474c81 81 Relay_1A = 1;
tridung141196 0:223260474c81 82 Relay_1B = 0;
tridung141196 0:223260474c81 83 Relay_2A = 1;
tridung141196 0:223260474c81 84 Relay_2B = 0;
tridung141196 0:223260474c81 85 pc.printf("\n\r# go down\n\r");
tridung141196 0:223260474c81 86 }
tridung141196 0:223260474c81 87 void left(void) //su dung motor 1 & 2
tridung141196 0:223260474c81 88 {
tridung141196 0:223260474c81 89 Relay_1A = 0;
tridung141196 0:223260474c81 90 Relay_1B = 1;
tridung141196 0:223260474c81 91 Relay_2A = 1;
tridung141196 0:223260474c81 92 Relay_2B = 0;
tridung141196 0:223260474c81 93 pc.printf("\n\r# turn left\n\r");
tridung141196 0:223260474c81 94 }
tridung141196 0:223260474c81 95 void right(void) //su dung motor 1 & 2
tridung141196 0:223260474c81 96 {
tridung141196 0:223260474c81 97 Relay_1A = 1;
tridung141196 0:223260474c81 98 Relay_1B = 0;
tridung141196 0:223260474c81 99 Relay_2A = 0;
tridung141196 0:223260474c81 100 Relay_2B = 1;
tridung141196 0:223260474c81 101 pc.printf("\n\r# turn right\n\r");
tridung141196 0:223260474c81 102 }
tridung141196 0:223260474c81 103 void stop(void) //su dung motor 1 & 2
tridung141196 0:223260474c81 104 {
tridung141196 0:223260474c81 105 Relay_1A = 1;
tridung141196 0:223260474c81 106 Relay_1B = 1;
tridung141196 0:223260474c81 107 Relay_2A = 1;
tridung141196 0:223260474c81 108 Relay_2B = 1;
tridung141196 0:223260474c81 109 pc.printf("\n\r# stop\n\r");
tridung141196 0:223260474c81 110 }
tridung141196 0:223260474c81 111 /////////////////////////////////////////
tridung141196 0:223260474c81 112 void motor3_up(void)
tridung141196 0:223260474c81 113 {
tridung141196 0:223260474c81 114 Relay_3A = 0;
tridung141196 0:223260474c81 115 Relay_3B = 1;
tridung141196 0:223260474c81 116 }
tridung141196 0:223260474c81 117 void motor3_down(void)
tridung141196 0:223260474c81 118 {
tridung141196 0:223260474c81 119 Relay_3A = 1;
tridung141196 0:223260474c81 120 Relay_3B = 0;
tridung141196 0:223260474c81 121 pc.printf("\n\r# motor3 down\n\r");
tridung141196 0:223260474c81 122 }
tridung141196 0:223260474c81 123 void motor3_stop(void)
tridung141196 0:223260474c81 124 {
tridung141196 0:223260474c81 125 Relay_3A = 1;
tridung141196 0:223260474c81 126 Relay_3B = 1;
tridung141196 0:223260474c81 127 pc.printf("\n\r# motor 3 stop\n\r");
tridung141196 0:223260474c81 128 }
tridung141196 0:223260474c81 129 /////////////////////////////////////////
tridung141196 0:223260474c81 130 void motor4_up(void)
tridung141196 0:223260474c81 131 {
tridung141196 0:223260474c81 132 Relay_4A = 0;
tridung141196 0:223260474c81 133 Relay_4B = 1;
tridung141196 0:223260474c81 134 pc.printf("\n\r# motor 4 up\n\r");
tridung141196 0:223260474c81 135 }
tridung141196 0:223260474c81 136 void motor4_down(void)
tridung141196 0:223260474c81 137 {
tridung141196 0:223260474c81 138 Relay_4A = 1;
tridung141196 0:223260474c81 139 Relay_4B = 0;
tridung141196 0:223260474c81 140 pc.printf("\n\r# motor 4 down\n\r");
tridung141196 0:223260474c81 141 }
tridung141196 0:223260474c81 142 void motor4_stop(void)
tridung141196 0:223260474c81 143 {
tridung141196 0:223260474c81 144 Relay_4A = 1;
tridung141196 0:223260474c81 145 Relay_4B = 1;
tridung141196 0:223260474c81 146 pc.printf("\n\r# motor 4 stop\n\r");
tridung141196 0:223260474c81 147 }
tridung141196 0:223260474c81 148 //////////////////////////////////////////////////////////////////////
tridung141196 0:223260474c81 149 void test_relay(void)
tridung141196 0:223260474c81 150 {
tridung141196 0:223260474c81 151 Relay_1A = 0; wait(1); Relay_1A = 1; wait(2);
tridung141196 0:223260474c81 152 Relay_1B = 0; wait(1); Relay_1B = 1; wait(2);
tridung141196 0:223260474c81 153 Relay_2A = 0; wait(1); Relay_2A = 1; wait(2);
tridung141196 0:223260474c81 154 Relay_2B = 0; wait(1); Relay_2B = 1; wait(2);
tridung141196 0:223260474c81 155 Relay_3A = 0; wait(1); Relay_3A = 1; wait(2);
tridung141196 0:223260474c81 156 Relay_3B = 0; wait(1); Relay_3B = 1; wait(2);
tridung141196 0:223260474c81 157 Relay_4A = 0; wait(1); Relay_4A = 1; wait(2);
tridung141196 0:223260474c81 158 Relay_4B = 0; wait(1); Relay_4B = 1; wait(2);
tridung141196 0:223260474c81 159 Relay_1 = 0; wait(1); Relay_1 = 1; wait(2);
tridung141196 0:223260474c81 160 Relay_2 = 0; wait(1); Relay_2 = 1; wait(2);
tridung141196 0:223260474c81 161 Relay_3 = 0; wait(1); Relay_3 = 1; wait(2);
tridung141196 0:223260474c81 162 Relay_4 = 0; wait(1); Relay_4 = 1; wait(2);
tridung141196 0:223260474c81 163
tridung141196 0:223260474c81 164 Relay_1 = 0;
tridung141196 0:223260474c81 165 Relay_2 = 0;
tridung141196 0:223260474c81 166 Relay_3 = 0;
tridung141196 0:223260474c81 167 Relay_4 = 0;
tridung141196 0:223260474c81 168 Relay_1A = 1; Relay_1B = 0;
tridung141196 0:223260474c81 169 Relay_2A = 1; Relay_2B = 0;
tridung141196 0:223260474c81 170 Relay_3A = 1; Relay_3B = 0;
tridung141196 0:223260474c81 171 Relay_4A = 1; Relay_4B = 0;
tridung141196 0:223260474c81 172 wait(0.5);
tridung141196 0:223260474c81 173 Relay_1 = 1;
tridung141196 0:223260474c81 174 Relay_2 = 1;
tridung141196 0:223260474c81 175 Relay_3 = 1;
tridung141196 0:223260474c81 176 Relay_4 = 1;
tridung141196 0:223260474c81 177 Relay_1A = 0; Relay_1B = 1;
tridung141196 0:223260474c81 178 Relay_2A = 0; Relay_2B = 1;
tridung141196 0:223260474c81 179 Relay_3A = 0; Relay_3B = 1;
tridung141196 0:223260474c81 180 Relay_4A = 0; Relay_4B = 1;
tridung141196 0:223260474c81 181 wait(2);
tridung141196 0:223260474c81 182 }
tridung141196 0:223260474c81 183
tridung141196 0:223260474c81 184 //////////////////////////////////////////////////////////////////////
tridung141196 0:223260474c81 185 int main(void)
tridung141196 0:223260474c81 186 {
tridung141196 0:223260474c81 187 //Init hardware
tridung141196 1:cd80403c0bc1 188 led1 = 0;
tridung141196 0:223260474c81 189 Relay_1A = 1;
tridung141196 0:223260474c81 190 Relay_1B = 1;
tridung141196 0:223260474c81 191 Relay_2A = 1;
tridung141196 0:223260474c81 192 Relay_2B = 1;
tridung141196 0:223260474c81 193 Relay_3A = 1;
tridung141196 0:223260474c81 194 Relay_3B = 1;
tridung141196 0:223260474c81 195 Relay_4A = 1;
tridung141196 0:223260474c81 196 Relay_4B = 1;
tridung141196 0:223260474c81 197 Relay_1 = 1;
tridung141196 0:223260474c81 198 Relay_2 = 1;
tridung141196 0:223260474c81 199 Relay_3 = 1;
tridung141196 0:223260474c81 200 Relay_4 = 1;
tridung141196 0:223260474c81 201 //Init UART
tridung141196 0:223260474c81 202 pc.baud(115200);
tridung141196 0:223260474c81 203 pc.printf("\n\r# BOT_BATTLE\n\r");
tridung141196 0:223260474c81 204 //Init timer
tridung141196 0:223260474c81 205 Ticker ticker;
tridung141196 0:223260474c81 206 ticker.attach(periodicCallback, 0.1); //sec
tridung141196 0:223260474c81 207 //Init BLE
tridung141196 0:223260474c81 208 ble.init();
tridung141196 1:cd80403c0bc1 209 ble.onConnection(connectionCallback);
tridung141196 0:223260474c81 210 ble.onDisconnection(disconnectionCallback);
tridung141196 0:223260474c81 211 ble.onDataWritten(onDataWritten);
tridung141196 0:223260474c81 212 /* setup advertising */
tridung141196 0:223260474c81 213 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
tridung141196 0:223260474c81 214 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
tridung141196 0:223260474c81 215 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
tridung141196 0:223260474c81 216 (const uint8_t *)"BOT_BATTLE", sizeof("BOT_BATTLE") - 1);
tridung141196 0:223260474c81 217 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
tridung141196 0:223260474c81 218 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
tridung141196 0:223260474c81 219 ble.setAdvertisingInterval(200); /* 1000ms; in multiples of 0.625ms. */
tridung141196 0:223260474c81 220 ble.startAdvertising();
tridung141196 0:223260474c81 221 UARTService uartService(ble);
tridung141196 0:223260474c81 222 uartServicePtr = &uartService;
tridung141196 0:223260474c81 223
tridung141196 0:223260474c81 224 while (true) {
tridung141196 0:223260474c81 225 ble.waitForEvent();
tridung141196 0:223260474c81 226 //test_relay();
tridung141196 0:223260474c81 227
tridung141196 0:223260474c81 228 if (g_cmd == 1) {up(); }
tridung141196 0:223260474c81 229 else if(g_cmd == 2) {left(); }
tridung141196 0:223260474c81 230 else if(g_cmd == 3) {down(); }
tridung141196 0:223260474c81 231 else if(g_cmd == 4) {right(); }
tridung141196 0:223260474c81 232 else if(g_cmd == 5) {stop(); }
tridung141196 0:223260474c81 233
tridung141196 0:223260474c81 234 else if(g_cmd == 6) {motor3_up(); }
tridung141196 0:223260474c81 235 else if(g_cmd == 7) {motor3_down(); }
tridung141196 0:223260474c81 236 else if(g_cmd == 8) {motor3_stop(); }
tridung141196 0:223260474c81 237 else if(g_cmd == 9) {motor4_up(); }
tridung141196 0:223260474c81 238 else if(g_cmd == 10) {motor4_down(); }
tridung141196 0:223260474c81 239 else if(g_cmd == 11) {motor4_stop(); }
tridung141196 0:223260474c81 240
tridung141196 2:cf9e91db9fb0 241 else if(g_cmd == 12) {
tridung141196 2:cf9e91db9fb0 242 Relay_1 = ON;
tridung141196 2:cf9e91db9fb0 243 pc.printf("\n\r# Relay_1 ON\n\r"); }
tridung141196 2:cf9e91db9fb0 244 else if(g_cmd == 13) {
tridung141196 2:cf9e91db9fb0 245 Relay_1 = OFF;
tridung141196 2:cf9e91db9fb0 246 pc.printf("\n\r# Relay_1 OFF\n\r"); }
tridung141196 2:cf9e91db9fb0 247 else if(g_cmd == 14) {
tridung141196 2:cf9e91db9fb0 248 Relay_2 = ON;
tridung141196 2:cf9e91db9fb0 249 pc.printf("\n\r# Relay_2 ON\n\r"); }
tridung141196 2:cf9e91db9fb0 250 else if(g_cmd == 15) {
tridung141196 2:cf9e91db9fb0 251 Relay_2 = OFF;
tridung141196 2:cf9e91db9fb0 252 pc.printf("\n\r# Relay_2 OFF\n\r"); }
tridung141196 2:cf9e91db9fb0 253 else if(g_cmd == 16) {
tridung141196 2:cf9e91db9fb0 254 Relay_3 = ON;
tridung141196 2:cf9e91db9fb0 255 pc.printf("\n\r# Relay_3 ON\n\r"); }
tridung141196 2:cf9e91db9fb0 256 else if(g_cmd == 17) {
tridung141196 2:cf9e91db9fb0 257 Relay_3 = OFF;
tridung141196 2:cf9e91db9fb0 258 pc.printf("\n\r# Relay_3 OFF\n\r"); }
tridung141196 2:cf9e91db9fb0 259 else if(g_cmd == 18) {
tridung141196 2:cf9e91db9fb0 260 Relay_4 = ON;
tridung141196 2:cf9e91db9fb0 261 pc.printf("\n\r# Relay_4 ON\n\r"); }
tridung141196 2:cf9e91db9fb0 262 else if(g_cmd == 19) {
tridung141196 2:cf9e91db9fb0 263 Relay_4 = OFF;
tridung141196 2:cf9e91db9fb0 264 pc.printf("\n\r# Relay_4 OFF\n\r"); }
tridung141196 0:223260474c81 265
tridung141196 0:223260474c81 266 g_cmd = 0;
tridung141196 0:223260474c81 267 }
tridung141196 0:223260474c81 268 }