This is a demo for controlling your ShieldBot using your mobile phone

Dependencies:   BLE_API ShieldBot_v12 mbed nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

Revision:
12:900318ccf3ae
Parent:
10:633cec718bf4
--- a/main.cpp	Tue Dec 09 08:55:59 2014 +0000
+++ b/main.cpp	Tue Sep 15 13:42:59 2015 +0000
@@ -1,25 +1,40 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2013 ARM Limited
+/*    
+ * main.cpp
+ * A Demo for ShieldBot
+ *   
+ * Copyright (c) 2015 seeed technology inc.  
+ * Author      : Jiankai.li
+ * Create Time:  Sep  2015
+ * Change Log : 
  *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+ * The MIT License (MIT)
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
  */
 
+
 #include "mbed.h"
 #include "BLEDevice.h"
 
 #include "UARTService.h"
-
-#define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
+#include "ShieldBot_v12.h"
+#define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
                                * it will have an impact on code-size and power consumption. */
 
 #if NEED_CONSOLE_OUTPUT
@@ -32,6 +47,62 @@
 DigitalOut led1(LED1);
 
 UARTService *uartServicePtr;
+Shieldbot shieldbot = Shieldbot();
+
+void turnLeft()
+{
+    shieldbot.drive(-127,127);   //turn left on a dime
+    wait(0.4);
+    shieldbot.stop();
+}
+
+void turnRight()
+{
+    shieldbot.drive(127,-127);   //turn left on a dime
+    wait(0.4);
+    shieldbot.stop();
+}
+
+
+void processPacket(uint8_t *packet)
+{
+    static int16_t speed = 255;
+    uint8_t cmd = packet[0];
+    DEBUG("comand: %c\n\r", cmd);
+    switch(cmd) {
+    case 'a':
+        turnLeft();
+        break;
+    case 'd':
+        turnRight();
+        break;
+    case 's':
+        shieldbot.backward();
+        break;
+    case 'w':
+        shieldbot.forward();
+        break;
+    case ' ':
+        shieldbot.stop();
+        break;
+    case '-':
+        speed -= 50;
+        if(speed <= 5){
+            speed = 5;
+        }
+        shieldbot.setMaxSpeed(speed);
+        break;
+    case '+':
+        speed += 50;
+        if(speed >= 255) {
+            speed = 255;
+        }
+        shieldbot.setMaxSpeed(speed);
+        break;
+    default:
+        break;
+    }
+}
 
 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
 {
@@ -46,6 +117,7 @@
         uint16_t bytesRead = params->len;
         DEBUG("received %u bytes\n\r", bytesRead);
         ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
+        processPacket((uint8_t *)params->data);
     }
 }
 
@@ -69,7 +141,7 @@
     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
-                                     (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
+                                     (const uint8_t *)"ShieldBot", sizeof("ShieldBot") - 1);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
                                      (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));