demo
Dependencies: BLE_API mbed nRF51822 X_NUCLEO_IDB0XA1
main.cpp@3:aceb81e0eab5, 2017-08-05 (annotated)
- Committer:
- tridung141196
- Date:
- Sat Aug 05 09:24:38 2017 +0000
- Revision:
- 3:aceb81e0eab5
- Parent:
- 2:cf9e91db9fb0
add skill QWER
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tridung141196 | 3:aceb81e0eab5 | 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 | 3:aceb81e0eab5 | 23 | DigitalOut motorleft_A(p28); //Motor left |
tridung141196 | 3:aceb81e0eab5 | 24 | DigitalOut motorleft_B(p25); // |
tridung141196 | 3:aceb81e0eab5 | 25 | DigitalOut motorright_A(p24); //Motor right |
tridung141196 | 3:aceb81e0eab5 | 26 | DigitalOut motorright_B(p23); // |
tridung141196 | 3:aceb81e0eab5 | 27 | DigitalOut Relay_3A(p22); //Motor skill Q (may bao) |
tridung141196 | 3:aceb81e0eab5 | 28 | DigitalOut Relay_3B(p21); // |
tridung141196 | 3:aceb81e0eab5 | 29 | //DigitalOut Relay_4A(p9); // |
tridung141196 | 3:aceb81e0eab5 | 30 | //DigitalOut Relay_4B(p16); // |
tridung141196 | 3:aceb81e0eab5 | 31 | DigitalOut kichdien(p17); //relay ac inverter 12DC -220AC |
tridung141196 | 3:aceb81e0eab5 | 32 | //DigitalOut Relay_2(p18); // |
tridung141196 | 3:aceb81e0eab5 | 33 | //DigitalOut Relay_3(p19); // |
tridung141196 | 3:aceb81e0eab5 | 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 | 3:aceb81e0eab5 | 47 | ble.startAdvertising(); //gửi gói tin quảng bá |
tridung141196 | 2:cf9e91db9fb0 | 48 | pc.printf("\n\r# disconnected\n\r"); |
tridung141196 | 3:aceb81e0eab5 | 49 | led1 = 0; //led off when disconnected |
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 | 3:aceb81e0eab5 | 54 | led1 = 1; //led on when connected |
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 | 3:aceb81e0eab5 | 61 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); //update status to server |
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 | 3:aceb81e0eab5 | 71 | void down(void) //su dung motor 1 & 2 |
tridung141196 | 0:223260474c81 | 72 | { |
tridung141196 | 3:aceb81e0eab5 | 73 | motorleft_A = 0; //motor left go down |
tridung141196 | 3:aceb81e0eab5 | 74 | motorleft_B = 1; // |
tridung141196 | 3:aceb81e0eab5 | 75 | motorright_A = 0; //motor right do down |
tridung141196 | 3:aceb81e0eab5 | 76 | motorright_B = 1; // |
tridung141196 | 3:aceb81e0eab5 | 77 | pc.printf("\n\r# go down\n\r"); |
tridung141196 | 0:223260474c81 | 78 | } |
tridung141196 | 3:aceb81e0eab5 | 79 | void up(void) //su dung motor 1 & 2 |
tridung141196 | 0:223260474c81 | 80 | { |
tridung141196 | 3:aceb81e0eab5 | 81 | motorleft_A = 1; //motor left go up |
tridung141196 | 3:aceb81e0eab5 | 82 | motorleft_B = 0; // |
tridung141196 | 3:aceb81e0eab5 | 83 | motorright_A = 1; //motor right go up |
tridung141196 | 3:aceb81e0eab5 | 84 | motorright_B = 0; // |
tridung141196 | 3:aceb81e0eab5 | 85 | pc.printf("\n\r# go up\n\r"); |
tridung141196 | 0:223260474c81 | 86 | } |
tridung141196 | 0:223260474c81 | 87 | void left(void) //su dung motor 1 & 2 |
tridung141196 | 0:223260474c81 | 88 | { |
tridung141196 | 3:aceb81e0eab5 | 89 | motorleft_A = 0; //Motor left go down |
tridung141196 | 3:aceb81e0eab5 | 90 | motorleft_B = 1; // |
tridung141196 | 3:aceb81e0eab5 | 91 | motorright_A = 1; //Motor right go up |
tridung141196 | 3:aceb81e0eab5 | 92 | motorright_B = 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 | 3:aceb81e0eab5 | 97 | motorleft_A = 1; //Motor left go up |
tridung141196 | 3:aceb81e0eab5 | 98 | motorleft_B = 0; // |
tridung141196 | 3:aceb81e0eab5 | 99 | motorright_A = 0; //Motor right go down |
tridung141196 | 3:aceb81e0eab5 | 100 | motorright_B = 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 | 3:aceb81e0eab5 | 105 | motorleft_A = 1; |
tridung141196 | 3:aceb81e0eab5 | 106 | motorleft_B = 1; |
tridung141196 | 3:aceb81e0eab5 | 107 | motorright_A = 1; |
tridung141196 | 3:aceb81e0eab5 | 108 | motorright_B = 1; |
tridung141196 | 0:223260474c81 | 109 | pc.printf("\n\r# stop\n\r"); |
tridung141196 | 0:223260474c81 | 110 | } |
tridung141196 | 0:223260474c81 | 111 | ///////////////////////////////////////// |
tridung141196 | 3:aceb81e0eab5 | 112 | void maybao_down(void) |
tridung141196 | 0:223260474c81 | 113 | { |
tridung141196 | 0:223260474c81 | 114 | Relay_3A = 0; |
tridung141196 | 0:223260474c81 | 115 | Relay_3B = 1; |
tridung141196 | 0:223260474c81 | 116 | } |
tridung141196 | 3:aceb81e0eab5 | 117 | void maybao_up(void) |
tridung141196 | 0:223260474c81 | 118 | { |
tridung141196 | 0:223260474c81 | 119 | Relay_3A = 1; |
tridung141196 | 0:223260474c81 | 120 | Relay_3B = 0; |
tridung141196 | 3:aceb81e0eab5 | 121 | pc.printf("\n\r# may bao up\n\r"); |
tridung141196 | 0:223260474c81 | 122 | } |
tridung141196 | 3:aceb81e0eab5 | 123 | void maybao_stop(void) |
tridung141196 | 0:223260474c81 | 124 | { |
tridung141196 | 0:223260474c81 | 125 | Relay_3A = 1; |
tridung141196 | 0:223260474c81 | 126 | Relay_3B = 1; |
tridung141196 | 3:aceb81e0eab5 | 127 | pc.printf("\n\r# may bao down\n\r"); |
tridung141196 | 0:223260474c81 | 128 | } |
tridung141196 | 3:aceb81e0eab5 | 129 | void skillE(void) |
tridung141196 | 0:223260474c81 | 130 | { |
tridung141196 | 3:aceb81e0eab5 | 131 | |
tridung141196 | 3:aceb81e0eab5 | 132 | } |
tridung141196 | 3:aceb81e0eab5 | 133 | void skillR(void) |
tridung141196 | 3:aceb81e0eab5 | 134 | { |
tridung141196 | 3:aceb81e0eab5 | 135 | |
tridung141196 | 0:223260474c81 | 136 | } |
tridung141196 | 0:223260474c81 | 137 | |
tridung141196 | 0:223260474c81 | 138 | ////////////////////////////////////////////////////////////////////// |
tridung141196 | 0:223260474c81 | 139 | int main(void) |
tridung141196 | 0:223260474c81 | 140 | { |
tridung141196 | 0:223260474c81 | 141 | //Init hardware |
tridung141196 | 1:cd80403c0bc1 | 142 | led1 = 0; |
tridung141196 | 3:aceb81e0eab5 | 143 | motorleft_A = 1; |
tridung141196 | 3:aceb81e0eab5 | 144 | motorleft_B = 1; |
tridung141196 | 3:aceb81e0eab5 | 145 | motorright_A = 1; |
tridung141196 | 3:aceb81e0eab5 | 146 | motorright_B = 1; |
tridung141196 | 0:223260474c81 | 147 | Relay_3A = 1; |
tridung141196 | 0:223260474c81 | 148 | Relay_3B = 1; |
tridung141196 | 3:aceb81e0eab5 | 149 | kichdien = 1; |
tridung141196 | 0:223260474c81 | 150 | //Init UART |
tridung141196 | 0:223260474c81 | 151 | pc.baud(115200); |
tridung141196 | 0:223260474c81 | 152 | pc.printf("\n\r# BOT_BATTLE\n\r"); |
tridung141196 | 0:223260474c81 | 153 | //Init timer |
tridung141196 | 0:223260474c81 | 154 | Ticker ticker; |
tridung141196 | 0:223260474c81 | 155 | ticker.attach(periodicCallback, 0.1); //sec |
tridung141196 | 0:223260474c81 | 156 | //Init BLE |
tridung141196 | 3:aceb81e0eab5 | 157 | ble.init(); //start ble driver |
tridung141196 | 3:aceb81e0eab5 | 158 | ble.onConnection(connectionCallback); //callback when connected |
tridung141196 | 3:aceb81e0eab5 | 159 | ble.onDisconnection(disconnectionCallback); //callback when disconnected |
tridung141196 | 3:aceb81e0eab5 | 160 | ble.onDataWritten(onDataWritten); //callback when receive data |
tridung141196 | 0:223260474c81 | 161 | /* setup advertising */ |
tridung141196 | 3:aceb81e0eab5 | 162 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); //chế độ hoạt động BLE (only le peripheral) |
tridung141196 | 3:aceb81e0eab5 | 163 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // có khả năng kêt nối vô hướng |
tridung141196 | 3:aceb81e0eab5 | 164 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, //tên thiết bị ble peripheral |
tridung141196 | 3:aceb81e0eab5 | 165 | (const uint8_t *)"BOT_BATTLE", sizeof("BOT_BATTLE") - 1); // |
tridung141196 | 0:223260474c81 | 166 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
tridung141196 | 3:aceb81e0eab5 | 167 | (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); //UUID service |
tridung141196 | 0:223260474c81 | 168 | ble.setAdvertisingInterval(200); /* 1000ms; in multiples of 0.625ms. */ |
tridung141196 | 0:223260474c81 | 169 | ble.startAdvertising(); |
tridung141196 | 0:223260474c81 | 170 | UARTService uartService(ble); |
tridung141196 | 0:223260474c81 | 171 | uartServicePtr = &uartService; |
tridung141196 | 0:223260474c81 | 172 | |
tridung141196 | 0:223260474c81 | 173 | while (true) { |
tridung141196 | 0:223260474c81 | 174 | ble.waitForEvent(); |
tridung141196 | 0:223260474c81 | 175 | //test_relay(); |
tridung141196 | 0:223260474c81 | 176 | |
tridung141196 | 0:223260474c81 | 177 | if (g_cmd == 1) {up(); } |
tridung141196 | 0:223260474c81 | 178 | else if(g_cmd == 2) {left(); } |
tridung141196 | 0:223260474c81 | 179 | else if(g_cmd == 3) {down(); } |
tridung141196 | 0:223260474c81 | 180 | else if(g_cmd == 4) {right(); } |
tridung141196 | 0:223260474c81 | 181 | else if(g_cmd == 5) {stop(); } |
tridung141196 | 3:aceb81e0eab5 | 182 | else if(g_cmd == 6) {maybao_up(); } // |
tridung141196 | 3:aceb81e0eab5 | 183 | else if(g_cmd == 7) {maybao_down(); } // Skill Q |
tridung141196 | 3:aceb81e0eab5 | 184 | else if(g_cmd == 8) {maybao_stop(); } // |
tridung141196 | 2:cf9e91db9fb0 | 185 | else if(g_cmd == 12) { |
tridung141196 | 3:aceb81e0eab5 | 186 | skillE(); |
tridung141196 | 3:aceb81e0eab5 | 187 | pc.printf("\n\r# turn on skill E - turn round \n\r"); } //Skill E |
tridung141196 | 2:cf9e91db9fb0 | 188 | else if(g_cmd == 14) { |
tridung141196 | 3:aceb81e0eab5 | 189 | skillR(); |
tridung141196 | 3:aceb81e0eab5 | 190 | pc.printf("\n\r# turn on skill R - crazy dance \n\r");} //Skill R |
tridung141196 | 2:cf9e91db9fb0 | 191 | else if(g_cmd == 16) { |
tridung141196 | 3:aceb81e0eab5 | 192 | kichdien = ON; |
tridung141196 | 3:aceb81e0eab5 | 193 | pc.printf("\n\r# bat kich dien\n\r"); } // |
tridung141196 | 3:aceb81e0eab5 | 194 | else if(g_cmd == 17) { // |
tridung141196 | 3:aceb81e0eab5 | 195 | kichdien = OFF; //skill W |
tridung141196 | 3:aceb81e0eab5 | 196 | pc.printf("\n\r# tat kich dien\n\r"); } // |
tridung141196 | 0:223260474c81 | 197 | |
tridung141196 | 0:223260474c81 | 198 | g_cmd = 0; |
tridung141196 | 0:223260474c81 | 199 | } |
tridung141196 | 0:223260474c81 | 200 | } |