Michael Ling / ExoController

Dependents:   Data-Management-Honka

Revision:
6:721f5e8a794d
Parent:
5:92659a4c2f89
Child:
7:d4a32c830e11
--- a/BluetoothComm.cpp	Sat Feb 14 00:37:05 2015 +0000
+++ b/BluetoothComm.cpp	Wed Feb 18 01:11:13 2015 +0000
@@ -15,10 +15,10 @@
 BluetoothComm::BluetoothComm(PinName tx, PinName rx): _rn42(tx, rx), _len(0), _counter(0), _inMsg(false), _numVars(22), _numReadOnlyParams(12), _escapesNeeded(8)
 {
     
-    
-    _rn42.baud(9600);
-    pc.printf("Started BTComm init \r\n");
-  //  _rn42.baud(115200);
+    /* Make sure baud rate is correct--rn42 cannot be read if code and MBED have different baud rates! */
+    //_rn42.baud(9600);
+    //pc.printf("Started BTComm init \r\n");
+    _rn42.baud(115200);
     int temp1[] = {0,1,2,3,4,8,9,10, -1};
   
     for (int i = 0; i < _escapesNeeded+1; i++) {
@@ -38,7 +38,7 @@
     
  
     write_params_to_sd_card();
-    pc.printf("Initialized PARAM \r\n");
+  //  pc.printf("Initialized PARAM \r\n");
     int temp4[] = {0x01fe, 0x02ac, 0x02ff, 0x0180, 0x0012, 0x0010, 0x0020, 0x00bf, 0x023f, 0x0123, 0x03a2, 0x10};
     readData.write(_numReadOnlyParams, temp4);
     for (int k = 0; k < _numReadOnlyParams; k += 1) {
@@ -49,7 +49,7 @@
     //printf("Initialized data\r\n");
     //Fill the parameter map with data from SD card
     read_data_from_sd();
-    pc.printf("Test: %x\r\n", _paramMap["TorsoAng"]);
+    //pc.printf("Test: %x\r\n", _paramMap["TorsoAng"]);
     read_params_from_sd();
  
 
@@ -82,7 +82,7 @@
     for (int i = 0; i < _numVars; i += 1) {
         _paramMap[_indexMap[i]] = arr[i];
     }
-    printf("Finished reading params\r\n");
+    //printf("Finished reading params\r\n");
 }   
 
 /**
@@ -265,24 +265,30 @@
 */
 void BluetoothComm::send_read_only_values()
 {
+    //printf("SENDING RO VALUES!\r\n");
     int msgLen = 2*_numReadOnlyParams+_escapesNeeded+7;
+ //   printf("msglen is %d\r\n", msgLen);
     char message[msgLen];
+   // printf("message of len 39 created\r\n");
     message[0] = START;
+    //printf("%x\r\n", message[0]);
     message[1] = 0;
+    //printf("%x\r\n", message[1]);
     message[2] = READONLY_IND;
+    //printf("3 bytes of message set\r\n");
     int msgInd = 3;
     int escapes = 0;
     //printf("%d readonly parameters", _numReadOnlyParams);
     for (int i = 0; i < _numReadOnlyParams; i++) {
         if (i == _escapeNeeded[escapes]) {
-            //printf("Escape char. needed at index %d \r\n", i);
+            printf("Escape char. needed at index %d \r\n", i);
             //char conflict = (char)(_readOnlyParams[i] & 0xff);
             char conflict = (char)(_paramMap[_indexMap[i+_numVars]] & 0xff);
             //printf("%x possibly has a conflict in %x \r\n", _readOnlyParams[i], conflict);
             escapes += 1;
             //message[msgInd+1] = (char) (_readOnlyParams[i] >> 8);
             message[msgInd+1] = (char) (_paramMap[_indexMap[i+_numVars]] >> 8);
-            //printf("Set msgInd+1 to %x \r\n", message[msgInd+1]);
+            printf("Set msgInd+1 to %x \r\n", message[msgInd+1]);
             if (conflict == (char) 0xfe) {
                 message[msgInd] = 1;
                 message[msgInd+2] = 0xfc;
@@ -416,9 +422,10 @@
 {
     //printf("Message is a ReadOnly \r\n");
     if (!msg_check(message, len)) {
-        //printf("msg_check failed on read! \r\n");
+        printf("msg_check failed on readonly! \r\n");
         return;
     }
+    //PASS TO CTRLBED
     _failures = 0;
 
     //printf("Sending readOnly values \r\n");
@@ -449,6 +456,7 @@
         //printf("msg_check failed on read! \r\n");
         return;
     }
+    //PASS MSG TO CTRLBED
     _failures = 0;
     //printf("Message is a read \r\n");
     char paramList[_numVars];
@@ -457,7 +465,7 @@
     for (int i=2; i < len-5; i++) {
         char msg = message[i] & 0xbf;
         if ((msg & 0x80) != 0) {
-            //printf("Got a non-read char %x...exiting \r\n", msg);
+       // printf("Got a non-read char %x...exiting \r\n", msg);
             return;
         }
 
@@ -467,8 +475,10 @@
 
     }
     if (message[len-5] == READONLY_IND) {
+        printf("Need to send RO vals\r\n");
         send_read_only_values();
     }
+    printf("About to send PARAMLIST\r\n");
     send_values(paramList);
 }
 
@@ -491,9 +501,10 @@
         return;
     }
     if (!msg_check(message, len)) {
-        //printf("msg_check failed on write! \r\n");
+        printf("msg_check failed on write! \r\n");
         return;
     }
+    //PASS msg. to ControlBed
     char paramList[_numVars];
     memset(paramList, 0xff, _numVars);
     //printf("Message is a write \r\n");
@@ -525,13 +536,14 @@
 {
     char c = message[2];
     for (int i =0; i < len; i++) {
-        printf("Message character: %x \r\n", message[i]);
+        //printf("Message character: %x \r\n", message[i]);
     }
     if (c == READONLY_IND) {
         process_read_only(message, len);
         return;
     }
     if ((c & 0x80) == 0) {
+        //printf("Is a read\r\n");
         process_read(message, len);
         return;
     } else {
@@ -549,7 +561,6 @@
 void BluetoothComm::attachment()
 {
     if (_rn42.readable()) {
-        //   //printf("_rn42 is readable \r\n");
         _data=_rn42.getc();
 
         //    if (_data != NULL) {