Dependencies:   USBDevice mbed

Files at this revision

API Documentation at this revision

Comitter:
rkk
Date:
Fri Mar 13 20:04:01 2015 +0000
Commit message:
slave functionality works

Changed in this revision

USBDevice.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
robotLibrary.cpp Show annotated file Show diff for this revision Revisions of this file
robotLibrary.h Show annotated file Show diff for this revision Revisions of this file
string_functions.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice.lib	Fri Mar 13 20:04:01 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/USBDevice/#c0605f23f916
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Mar 13 20:04:01 2015 +0000
@@ -0,0 +1,36 @@
+#include "mbed.h"
+#include "robotLibrary.h"
+
+int main()
+{
+    wait(4);
+    console1.printf("Started up\n");
+    console1.printf("USBTX: %d\n",USBTX);
+    console1.printf("USBRX: %d\n",USBRX);
+    console1.printf("p9: %d\n",p9);
+    console1.printf("p10: %d\n",p10);
+    console1.printf("console1 addr: %d\n",&console1);
+    console1.printf("bluetooth1 addr: %d\n",&bt);
+
+    robotSetup();
+    char data[50];
+
+    while(1) {
+        getBluetoothData();
+        if(isBluetoothDataValid()) {
+            // Enter your loop code below
+            //if(isBluetoothConnected()){
+            strcpy(returnBluetoothData(), data, 50);
+            int len = length(data);
+
+            // print it out
+            for (int i = 0; i < len; i++)
+                console1.putc(data[i]);
+        }
+        console1.printf("going...\n");
+        bt.printf("data");
+        bt.putc('\0');
+        wait(2);
+    }
+
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri Mar 13 20:04:01 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/7e07b6fb45cf
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/robotLibrary.cpp	Fri Mar 13 20:04:01 2015 +0000
@@ -0,0 +1,139 @@
+#include "robotLibrary.h"
+
+//USBSerial console1; // tx, rx
+Serial console1(USBTX, USBRX); // tx, rx
+//Serial btslave(p9,p10); // tx, rx
+Serial bt(p13,p14); // tx, rx
+
+char bluetoothData[50];
+bool bluetoothConnected;
+bool validBluetoothData;
+
+// Read data from the bluetooth
+// Stores data in the bluetoothData array
+// Messages must end in the null termination character ('\0')
+// Will return true if a complete bluetooth message has been received (and thus if the data in bluetoothData is valid)
+// This method DOES NOT BLOCK
+//   it will read as long as data is available and then stop, whether or not the message is complete
+//   at the next call, it will continue reading and adding to the array until '\0' is received
+//  Thus, this method is meant to be called once per loop
+bool getBluetoothData()
+{
+  if(validBluetoothData) // reset array
+  {
+    for(int i = 0; i < 50; i++)
+      bluetoothData[i] = '\0';
+    validBluetoothData = false;
+  }
+
+  if(!bluetoothAvailable())
+    return false;
+
+  int index = length(bluetoothData);
+  while(bluetoothAvailable() && !validBluetoothData)
+  {
+    bluetoothData[index++] = getBluetoothChar();
+    validBluetoothData = (bluetoothData[index-1] == '\0');
+  }
+  if(validBluetoothData)
+  {
+    // If it is a heartbeat, respond to it now
+    if(equals(bluetoothData, "?"))
+    {
+      sendBluetoothData("?");
+      bluetoothData[0] = '\0';
+      validBluetoothData = false;
+      return false;
+    }
+    robotPrintDebug("Got BT data <"); robotPrintDebug(bluetoothData); robotPrintlnDebug(">\0");
+  }
+  return validBluetoothData;
+}
+
+// This method is similar to the above method, except that this version DOES BLOCK
+// It will keep trying to read until a complete message has been receieved or a timeout is reached
+// Returns true if a valid message was received
+//bool getBluetoothData()
+//{
+//  bluetoothData[0] = '\0';
+//  if(!bluetoothAvailable())
+//    return false;
+//  int timeout = 50;
+//  int index = 0;
+//  bool terminated = false;
+//  unsigned long start = millis();
+//  while(!terminated && millis() - start < timeout)
+//  {
+//    while(!bluetoothAvailable() && millis() - start < timeout);
+//    bluetoothData[index++] = getBluetoothChar();
+//    start = millis();
+//    terminated = (bluetoothData[index-1] == '\0');
+//  }
+//  if(index > 0 && bluetoothData[index-1] != '\0')
+//    bluetoothData[index] = '\0';
+//  // If it is a heartbeat, respond to it now
+//  if(equals(bluetoothData, "?"))
+//  {
+//    sendBluetoothData("?");
+//    bluetoothData[0] = '\0';
+//    return false;
+//  }
+//  robotPrintDebug("Got BT data <"); robotPrintDebug(bluetoothData); robotPrintlnDebug(">");
+//  return true;
+//}
+
+// Returns the value of bluetoothConnected
+// This variable must be set somewhere, probably in processBluetoothData
+bool isBluetoothConnected()
+{
+  return bluetoothConnected;
+}
+
+// Read bluetooth data and then process it
+void processBluetoothData()
+{
+  if(!getBluetoothData())
+    return;
+  // DO SOMETHING WITH bluetoothData HERE
+  // If it is a valid message, set bluetoothConnected = true
+  bluetoothConnected = true;
+}
+
+char* returnBluetoothData(){
+  return bluetoothData;
+}
+
+bool isBluetoothDataValid()
+{
+  return validBluetoothData;
+}
+
+void robotLoop()
+{
+  //robotPrintlnDebug();
+  processBluetoothData();
+}
+
+void robotSetup()
+{
+  bt.baud(9600);
+  //sendBluetoothData("AT+NAME");
+  //sendBluetoothData("myRobotBT");
+  bluetoothConnected = false;
+  validBluetoothData = false;
+  bluetoothData[0] = '\0';
+}
+
+void sendBluetoothData(const char* data)
+{
+  robotPrintDebug("Sending BT data <"); robotPrintDebug(data); robotPrintlnDebug(">");
+  int index = 0;
+  for(; index < length(data); index++)
+    {
+      sendBluetoothChar(data[index]);
+      wait_ms(5);
+    }
+  if(data[index-1] != '\0')
+    sendBluetoothChar('\0');
+  robotPrintDebug("Sent BT data <"); robotPrintDebug(data); robotPrintlnDebug(">");
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/robotLibrary.h	Fri Mar 13 20:04:01 2015 +0000
@@ -0,0 +1,37 @@
+#ifndef INCL_ROBOTLIBRARY_H
+#define INCL_ROBOTLIBRARY_H
+
+#include "mbed.h"
+#include "USBSerial.h"
+
+#define bluetoothAvailable() (bt.readable())
+#define sendBluetoothChar(toSend) (bt.putc((char)toSend))
+#define getBluetoothChar() ((char) bt.getc())
+#include "string_functions.h"
+#define robotPrint(toPrint) console1.printf(toPrint)
+#define robotPrintln(toPrint) console1.printf(toPrint)
+
+#define PRINT_DEBUG 1
+#if PRINT_DEBUG == 1
+#define robotPrintDebug(toPrint) robotPrint(toPrint)
+#define robotPrintlnDebug(toPrint) robotPrintln(toPrint)
+#else
+#define robotPrintDebug(toPrint)
+#define robotPrintlnDebug(toPrint)
+#endif
+
+extern Serial bt; // tx, rx
+//extern USBSerial console1;
+extern Serial console1;
+
+bool getBluetoothData();
+bool isBluetoothConnected();
+void processBluetoothData();
+char* returnBluetoothData();
+bool isBluetoothDataValid();
+void robotLoop();
+void robotSetup();
+void sendBluetoothData(const char* data);
+
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/string_functions.h	Fri Mar 13 20:04:01 2015 +0000
@@ -0,0 +1,220 @@
+#ifndef INCL_STRING_FUNCTIONS
+#define INCL_STRING_FUNCTIONS
+
+#include <stdio.h>
+#include <stdlib.h>
+
+inline int length(const char* source);
+inline int indexOf(const char* source, const char* target);
+inline bool contains(const char* source, const char* target);
+inline void substring(const char* source, int startIndex, int endIndex, char* dest);
+inline void substring(const char* source, int startIndex, char* dest);
+inline void substring(const char* source, int startIndex, int endIndex, char* dest, int destLength);
+inline void trim(char* source, int sourceLength);
+inline bool equals(const char* first, const char* second);
+inline bool equalsIgnoreCase(const char* first, const char* second);
+inline void strcpy(const char* source, char* dest, int destLength);
+inline void concat(const char* first, const char* second, char* dest, int destLength);
+inline void concatInt(const char* first, int second, char* dest, int destLength);
+
+
+
+
+
+//============================================================================================
+// Gets the length of the character array
+//============================================================================================
+int length(const char* source)
+{
+  int length = 0;
+  for(; source[length] != '\0'; length++);
+  return length;
+}
+
+//============================================================================================
+// Gets the index of the given string, or -1 if not found
+//============================================================================================
+int indexOf(const char* source, const char* target)
+{
+  int targetLength = length(target);
+  int sourceLength = length(source);
+  int index = -1;
+  for(int i = 0; i <= sourceLength - targetLength && index == -1; i++)
+  {
+    bool foundTarget = true;
+    for(int n = 0; n < targetLength && i+n < sourceLength; n++)
+    {
+      if(source[i+n] != target[n])
+        foundTarget = false;
+    }
+    if(foundTarget)
+      index = i;
+  }
+  return index;
+}
+
+//============================================================================================
+// Checks if the given string is contained in the source string
+//============================================================================================
+bool contains(const char* source, const char* target)
+{
+  return indexOf(source, target) >= 0;
+}
+
+//============================================================================================
+// Returns a substring of the character array
+// First is inclusive, second is exclusive
+// Returns itself if bounds are bad
+// Assumes beginning / end if either bound is negative
+//============================================================================================
+void substring(const char* source, int startIndex, int endIndex, char* dest)
+{
+  substring(source, startIndex, endIndex, dest, 20);
+}
+
+//============================================================================================
+// Returns a substring of the character array
+// First is inclusive, second is exclusive
+// Returns itself if bounds are bad
+// Assumes beginning / end if either bound is negative
+//============================================================================================
+void substring(const char* source, int startIndex, char* dest)
+{
+  substring(source, startIndex, length(source), dest, 30);
+}
+
+//============================================================================================
+// Returns a substring of the character array
+// First is inclusive, second is exclusive
+// Returns itself if bounds are bad
+// Assumes beginning / end if either bound is negative
+//============================================================================================
+void substring(const char* source, int startIndex, int endIndex, char* dest, int destLength)
+{
+  char temp[destLength];
+  if(startIndex < 0)
+    startIndex = 0;
+  if(endIndex < 0)
+    endIndex = 0;
+  if(endIndex < startIndex)
+  {
+    dest[0] = '\0';
+    return;
+  }
+  if(endIndex >= length(source))
+    endIndex = length(source);
+  if(destLength < endIndex - startIndex + 1)
+  {
+    dest[0] = '\0';
+    return;
+  }
+  for(int i = 0; i < endIndex - startIndex; i++)
+  {
+    temp[i] = source[startIndex + i];
+  }
+  for(int i = 0; i < endIndex - startIndex; i++)
+    dest[i] = temp[i];
+  dest[endIndex - startIndex] = '\0';
+}
+
+//============================================================================================
+// Removing leading and trailing spaces or new lines
+//============================================================================================
+void trim(char* source, int sourceLength)
+{
+  char temp[sourceLength];
+  int startIndex = 0;
+  int endIndex = length(source)-1;
+  for(; startIndex < length(source) && (source[startIndex] == ' ' || source[startIndex] == '\n' || source[startIndex] == '\t'); startIndex++);
+  for(; endIndex >= 0 && (source[endIndex] == ' ' || source[endIndex] == '\n' || source[startIndex] == '\t'); endIndex--);
+  endIndex++;  
+  substring(source, startIndex, endIndex, temp, sizeof(temp)-1);
+  strcpy(temp, source, sourceLength-1);
+}
+
+//============================================================================================
+// Tests if two character arrays are equal
+//============================================================================================
+bool equals(const char* first, const char* second)
+{
+  if(length(first) != length(second))
+    return false;
+  for(int i = 0; i < length(first); i++)
+  {
+    if(first[i] != second[i])
+      return false;
+  }
+  return true;
+}
+
+//============================================================================================
+// Tests if two character arrays are equal, ignoring case
+//============================================================================================
+bool equalsIgnoreCase(const char* first, const char* second)
+{
+  if(length(first) != length(second))
+    return false;
+  for(int i = 0; i < length(first); i++)
+  {
+    int firstChar = first[i];
+    int secondChar = second[i];
+    // Make them lowercase
+    if(firstChar >= 'A' && firstChar <= 'Z')
+      firstChar += 'a' - 'A';
+    if(secondChar >= 'A' && secondChar <= 'Z')
+      secondChar += 'a' - 'A';
+    if(firstChar != secondChar)
+      return false;
+  }
+  return true;
+}
+
+//============================================================================================
+// Copies one array into the other
+//============================================================================================
+void strcpy(const char* source, char* dest, int destLength)
+{
+  if(destLength < length(source) + 1)
+  {
+    dest[0] = '\0';
+    return;
+  }
+  for(int i = 0; i < length(source); i++)
+  {
+    dest[i] = source[i];
+  }
+  dest[length(source)] = '\0';
+}
+
+//============================================================================================
+// Concatenates two character arrays
+//============================================================================================
+void concat(const char* first, const char* second, char* dest, int destLength)
+{
+  char temp[destLength];
+  if(destLength < length(first) + length(second) + 1)
+  {
+    dest[0] = '\0';
+    return;
+  }
+  for(int i = 0; i < length(first); i++)
+    temp[i] = first[i];
+  for(int i = 0; i < length(second); i++)
+    temp[i + length(first)] = second[i];
+  temp[length(second) + length(first)] = '\0';  
+  for(int i = 0; i < length(second) + length(first); i++)
+    dest[i] = temp[i];
+  dest[length(second) + length(first)] = '\0';  
+}
+
+//============================================================================================
+// Concatenates a character array with an integer
+//============================================================================================
+void concatInt(const char* first, int second, char* dest, int destLength)
+{
+  char secondChar[10];
+  sprintf(secondChar,"%d",second); //itoa(second, secondChar, 10);
+  concat(first, secondChar, dest, destLength);  
+}
+
+#endif
\ No newline at end of file