Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Data-Management-Honka
Revision 3:14050370593a, committed 2015-02-06
- Comitter:
- mzling
- Date:
- Fri Feb 06 20:45:47 2015 +0000
- Parent:
- 2:be605799793f
- Child:
- 4:7e3bbf896e78
- Commit message:
- Revised code to match mbed SDK style guide
Changed in this revision
| BluetoothComm.cpp | Show annotated file Show diff for this revision Revisions of this file |
| BluetoothComm.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/BluetoothComm.cpp Wed Dec 17 21:46:45 2014 +0000
+++ b/BluetoothComm.cpp Fri Feb 06 20:45:47 2015 +0000
@@ -2,10 +2,9 @@
#include "MODSERIAL.h"
#include "BluetoothComm.h"
#include "init.h"
-//#include "SDFile.cpp"
#include <string>
#include <map>
-//Variable indices for setValues: setValues should take an array of length NUMVARS + NUMREADONLYPARAMS = 34.
+//Variable indices for set_values: set_values should take an array of length NUMVARS + NUMREADONLYPARAMS = 34.
//Map of variables to array indices is as follows:
//0 = Torso Angle, 1 = Left Knee Angle, 2 = Right Knee Angle, 3 = Left Hip angle, 4 = Right Hip angle, 5 = Left Hip Torque, 6 = Right Hip Torque
//8 = Exo State/Left Knee State/Right Knee State 9 = Torso Ref. Angle, 10 = Left Hip Ref. Angle, 11 = Right Hip Ref. Angle
@@ -13,15 +12,15 @@
//18 = KD Stance, 19 = KD Swing, 20 = KD Standing, 21 = KD Sitting, 22 = KD Standing up, 23 = KD sitting down
//24 = Standing angle, 25 = Sitting angle, 26 = Bent Forward Angle, 27 = Forward Angle, 28 = Rear Angle, 29 = IMU Angle
//30 = Knee Full Retract, 31 = Knee Full Extend, 32 = Lock Time, 33 = Rate
-BluetoothComm::BluetoothComm(PinName tx, PinName rx): rn42(tx, rx), len(0), counter(0), inMsg(false), numVars(22), numReadOnlyParams(12), escapesNeeded(8)
+BluetoothComm::BluetoothComm(PinName tx, PinName rx): _rn42(tx, rx), _len(0), _counter(0), _inMsg(false), _numVars(22), _numReadOnlyParams(12), _escapesNeeded(8)
{
- rn42.baud(9600);
+ _rn42.baud(9600);
printf("Started BTComm init \r\n");
- // rn42.baud(115200);
+ // _rn42.baud(115200);
int temp1[] = {0,1,2,3,4,8,9,10, -1};
- for (int i = 0; i < escapesNeeded+1; i++) {
- escapeNeeded[i] = temp1[i];
+ for (int i = 0; i < _escapesNeeded+1; i++) {
+ _escapeNeeded[i] = temp1[i];
}
@@ -31,37 +30,65 @@
"RHipTorque", "ExoAndKneeStates", "TorsoRefAngle",
"LHipRefAngle", "RHipRefAngle", "Charge"};
//Populate the map of indices to param names
- for (int j = 0; j < (numVars + numReadOnlyParams); j += 1) {
- indexMap[j] = temp2[j];
+ for (int j = 0; j < (_numVars + _numReadOnlyParams); j += 1) {
+ _indexMap[j] = temp2[j];
}
- //Fill the parameter map with data from SD card
- readDataFromSD();
-
- readParamsFromSD();
+
+ write_params_to_sd_card();
+ printf("Initialized PARAM \r\n");
+ //int temp4[] = {0x01fe, 0x02ac, 0x02ff, 0x0180, 0x0012, 0x0010, 0x0020, 0x00bf, 0x023f, 0x0123, 0x03a2, 0x10};
+ //readData.write(temp4, _numReadOnlyParams);
+ //for (int k = 0; k < _numReadOnlyParams; k += 1) {
+ // _paramMap[_indexMap[_numVars + k]] = temp4[k];
+ //}
+ //printf("Test: %x\r\n", _paramMap["TorsoAng"]);
+ write_data_to_sd_card();
+ //printf("Initialized data\r\n");
+ //Fill the parameter map with data from SD card
+ read_data_from_sd();
+ //printf("Test: %x\r\n", _paramMap["TorsoAng"]);
+ read_params_from_sd();
}
-//Reads read-only data from SD card, and fills in the parameter map
-void BluetoothComm::readDataFromSD() {
- /
- int *arr = readData.read(arr, numVars);
- for (int i = 0; i < numReadOnlyParams; i += 1) {
- paramMap[indexMap[i + numVars]] = arr[i];
+/**
+* Reads read-only data from SD card, and fills in the parameter map.
+* @author Michael Ling
+* @date 2/4/2015
+*/
+void BluetoothComm::read_data_from_sd()
+{
+
+ int *arr = readData.read(_numReadOnlyParams, arr);
+ for (int i = 0; i < _numReadOnlyParams; i += 1) {
+ _paramMap[_indexMap[i + _numVars]] = arr[i];
+ //printf("Read: %x\r\n", arr[i]);
}
+ //printf("Finished reading data\r\n");
}
-//Reads editable parameters from SD card, and fills in the parameter map
-void BluetoothComm::readParamsFromSD() {
- int *arr = param.read(arr, numVars);
- for (int i = 0; i < numVars; i += 1) {
- paramMap[indexMap[i]] = arr[i];
+/**
+* Reads editable parameters from SD card, and fills in the parameter map
+* @author Michael Ling
+* @date 2/4/2015
+*/
+void BluetoothComm::read_params_from_sd()
+{
+ int *arr = param.read(_numVars, arr);
+ for (int i = 0; i < _numVars; i += 1) {
+ _paramMap[_indexMap[i]] = arr[i];
}
+ //printf("Finished reading params\r\n");
}
-
-//Calculates parity--0 if c even, 1 if odd
+/**
+* Calculates parity--0 if c even, 1 if odd
+* @param c The short that we want to calculate parity of
+* @author Michael Ling
+* @date 2/4/2015
+*/
bool BluetoothComm::parity(short c)
{
bool p = false;
@@ -72,15 +99,20 @@
return p;
}
-//Calculates checksum of char array, sum of all chars in array
-char* BluetoothComm::checkSum(char* b, int len)
+/**
+* Calculates checksum of char array, sum of all chars in array
+* @param b The char array to be summed up
+* @param len Length of the array b
+* @author Michael Ling
+* @date 2/4/2015
+*/
+char* BluetoothComm::get_checksum(char* b, int len)
{
char* checksum = (char*)malloc(3*sizeof(char));
short sum = 0;
for (int i = 0; i < len; i += 1) {
sum += ((short) b[i] & 0xff);
}
- //sum = (short) (-1*sum);
checksum[1] = (char) ((sum >> 8) & 0xff);
checksum[2] = (char) (sum & 0xff);
char check = (char) (sum & 0xff);
@@ -94,74 +126,107 @@
return checksum;
}
-//Sends error message
-void BluetoothComm::sendError(char errCode)
+/**
+* Sends error message.
+* @param errCode character representation of the failure-causing error
+* @author Michael Ling
+* @data 2/4/2015
+*/
+void BluetoothComm::send_error(char errCode)
{
- rn42.putc(START);
- rn42.putc(errCode);
- rn42.putc(END);
- rn42.putc(0);
- rn42.putc(0);
- rn42.putc(0);
+ _rn42.putc(START);
+ _rn42.putc(errCode);
+ _rn42.putc(END);
+ _rn42.putc(0);
+ _rn42.putc(0);
+ _rn42.putc(0);
}
+/**
+//Sets charge level. DEPRECATED
+void BluetoothComm::setCharge(short level) {
+ // _readOnlyParams[11] = level;
+ _paramMap["Charge"] = level;
+}
+*/
-//Sets charge level
-void BluetoothComm::setCharge(short level) {
- // readOnlyParams[11] = level;
- paramMap["Charge"] = level;
-}
-
-//Sends the speified char array
+/**
+* Sends the specified char array through the _rn42 Bluetooth connection.
+* @param cmd The char array to be sent
+* @author Michael Ling
+* @date 2/4/2015
+*/
void BluetoothComm::send(char *cmd)
{
for (int i = 0; i < 50; i++) {
- rn42.putc(cmd[i]);
+ _rn42.putc(cmd[i]);
}
}
-//Sets the parameter map, based on the input map NEWVALUES
-void BluetoothComm::setValues(std::map<string, short> newValues) {
+/**
+* Sets the parameter map, based on the input map NEWVALUES
+* @param newValues A map of strings to shorts to be copied to _paramMap
+* @author Michael Ling
+* @date 2/4/2015
+*/
+void BluetoothComm::set_values(std::map<string, short> newValues)
+{
for (std::map<string, short>::iterator it = newValues.begin(); it != newValues.end(); ++it) {
- paramMap[it->first] = it->second;
+ _paramMap[it->first] = it->second;
}
}
-//Writes the editable params. stored in paramMap to the SD card
-void BluetoothComm::writeParamsToSDCard() {
+/**
+* Writes the editable params. stored in _paramMap to the SD card
+* @author Michael Ling
+* @date 2/4/2015
+*/
+void BluetoothComm::write_params_to_sd_card()
+{
- int paramValues[numVars];
- for (int i = 0; i < numVars; i += 1) {
- paramValues[i] = (int) paramMap[indexMap[i]];
+ int paramValues[_numVars];
+ for (int i = 0; i < _numVars; i += 1) {
+ paramValues[i] = (int) _paramMap[_indexMap[i]];
}
- param.write(paramValues, numVars);
+ param.write(_numVars, paramValues);
}
-//Write the read-only values stored in paramMap to the SD card
-void BluetoothComm::writeDataToSDCard() {
- int dataValues[numReadOnlyParams];
- for (int i = 0; i < numReadOnlyParams; i += 1) {
- dataValues[i] = (int) paramMap[indexMap[i + numVars]];
+/**
+* Write the read-only values stored in _paramMap to the SD card
+* @author Michael Ling
+* @date 2/4/2015
+*/
+void BluetoothComm::write_data_to_sd_card()
+{
+ int dataValues[_numReadOnlyParams];
+ for (int i = 0; i < _numReadOnlyParams; i += 1) {
+ dataValues[i] = (int) _paramMap[_indexMap[i + _numVars]];
+ // printf("Index %d of dataValues set to %x\r\n", i, dataValues[i]);
}
- param.write(dataValues, numReadOnlyParams);
+ readData.write(_numReadOnlyParams, dataValues);
}
-//Sends the paramList with START/END, parity bits, and a checksum
-void BluetoothComm::sendValues(char* paramList)
+/**
+* Sends the paramList with START/END, parity bits, and a checksum
+* @param paramList List of parameters to be sent over Bluetooth, represented as a char array
+* @author Michael Ling
+* @date 2/4/2015
+*/
+void BluetoothComm::send_values(char* paramList)
{
- char msg[2*numVars+6];
+ char msg[2*_numVars+6];
int len=2;
//printf("Sending values \r\n");
msg[0] = START;
msg[1] = 0;
- for (int i=0; i < numVars; i++) {
+ for (int i=0; i < _numVars; i++) {
if (i == 21) {
//printf("On final loop \r\n");
}
if (paramList[i] != 0xff) {
short s = (short)((i << 8) | paramList[i]);
- //printf("In sendValues, calculating parity of %x\r\n", s);
+ //printf("In send_values, calculating parity of %x\r\n", s);
if (parity(s)) {
//printf("%x requires TRUE parity bit\r\n", s);
msg[len] = (char)(i | 0x40);
@@ -177,40 +242,44 @@
}
msg[len] = END;
len += 1;
- char* checksum = checkSum(msg, len);
+ char* checksum = get_checksum(msg, len);
msg[len] = checksum[0];
msg[len+1] = checksum[1];
msg[len+2] = checksum[2];
len += 3;
for (int j = 0; j < len; j++) {
//printf("Sending char %x \r\n", msg[j]);
- rn42.putc(msg[j]);
+ _rn42.putc(msg[j]);
}
- memcpy(lastCmd, msg, 50);
+ memcpy(_lastCmd, msg, 50);
free(checksum);
return ;
}
-//Sends readONly Parameters, with START/END and checksum
-void BluetoothComm::sendReadOnlyValues()
+/**
+* Sends readOnly Parameters, with START/END and checksum
+* @author Michael Ling
+* @date 2/4/2015
+*/
+void BluetoothComm::send_read_only_values()
{
- int msgLen = 2*numReadOnlyParams+escapesNeeded+7;
+ int msgLen = 2*_numReadOnlyParams+_escapesNeeded+7;
char message[msgLen];
message[0] = START;
message[1] = 0;
message[2] = READONLY_IND;
int msgInd = 3;
int escapes = 0;
- //printf("%d readonly parameters", numReadOnlyParams);
- for (int i = 0; i < numReadOnlyParams; i++) {
- if (i == escapeNeeded[escapes]) {
+ //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);
- //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);
+ //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);
+ //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]);
if (conflict == (char) 0xfe) {
message[msgInd] = 1;
@@ -224,39 +293,45 @@
}
msgInd += 3;
} else {
- // message[msgInd] = (char) (readOnlyParams[i] >> 8);
- // message[msgInd+1] = (char) (readOnlyParams[i] & 0xff);
- message[msgInd] = (char) (paramMap[indexMap[i+numVars]] >> 8);
- message[msgInd+1] = (char) (paramMap[indexMap[i+numVars]] & 0xff);
+ // message[msgInd] = (char) (_readOnlyParams[i] >> 8);
+ // message[msgInd+1] = (char) (_readOnlyParams[i] & 0xff);
+ message[msgInd] = (char) (_paramMap[_indexMap[i+_numVars]] >> 8);
+ message[msgInd+1] = (char) (_paramMap[_indexMap[i+_numVars]] & 0xff);
msgInd += 2;
}
}
message[msgLen-4] = END;
- char* checksum = checkSum(message, msgLen-3);
+ char* checksum = get_checksum(message, msgLen-3);
message[msgLen-3] = checksum[0];
message[msgLen-2] = checksum[1];
message[msgLen-1] = checksum[2];
//printf("Sending the following readONly values: \r\n");
for (int j=0; j < msgLen; j++) {
- //printf("%x \r\n", message[j]);
- rn42.putc(message[j]);
+ printf("%x \r\n", message[j]);
+ _rn42.putc(message[j]);
}
- memcpy(lastCmd, message, 50);
+ memcpy(_lastCmd, message, 50);
free(checksum);
//printf("Finished sending readOnly values \r\n");
}
-//Checks the message with length len. Checks for START and END in correct spots, parity, all data and variable byte in proper range, and correct checksum
-bool BluetoothComm::msgCheck(char* message, int len)
+/**
+* Checks the message with length len. Checks for START and END in correct spots, parity, all data and variable byte in proper range, and correct checksum
+* @param message The received message to check
+* @param len Length of the message
+* @author Michael Ling
+* @date 2/4/2015
+*/
+bool BluetoothComm::msg_check(char* message, int len)
{
if (message[0] != START) {
//printf("Improper START or END \r\n");
- sendError(START_ERR);
+ send_error(START_ERR);
return false;
}
if (message[len-4] != END) {
- sendError(END_ERR);
+ send_error(END_ERR);
return false;
}
bool write = message[2] & 0x80;
@@ -269,12 +344,12 @@
}
if (((message[i] & 0x80) !=0) && !write) {
//printf("Does not match READ format \r\n");
- sendError((char)(((message[i] & 0x3f) << 3) | RW_ERR));
+ send_error((char)(((message[i] & 0x3f) << 3) | RW_ERR));
return false;
}
if (((message[i] & 0x80) != 0x80) && write) {
//printf("char %x Does not match WRITE format \r\n", message[i]);
- sendError((char)(((message[i] & 0x3f) << 3) | RW_ERR));
+ send_error((char)(((message[i] & 0x3f) << 3) | RW_ERR));
return false;
}
short s;
@@ -287,40 +362,40 @@
if (parity1 != parity(s & 0x4000)) {
//printf("Parity error in VAR char \r\n");
- sendError((char) (((message[i] & 0xbf) << 3) | PARITY_ERR));
+ send_error((char) (((message[i] & 0xbf) << 3) | PARITY_ERR));
return false;
}
char c = message[i] & 0x3f;
char c2 = message[i+1];
- if ((int) c < 0 || (int) c > numVars) {
+ if ((int) c < 0 || (int) c > _numVars) {
//printf("VAR char out of range \r\n");
- sendError((char) (((message[i] & 0xbf) << 3) | VAR_ERR));
+ send_error((char) (((message[i] & 0xbf) << 3) | VAR_ERR));
return false;
}
if ((int) c2 < 0 || (int) c2 > 100) {
//printf("DATA char out of range");
- sendError((char) (((message[i] & 0xbf) << 3) | DATA_ERR));
+ send_error((char) (((message[i] & 0xbf) << 3) | DATA_ERR));
return false;
}
}
- char* checksum = checkSum(message, len-3);
+ char* checksum = get_checksum(message, len-3);
if (checksum[0] != message[len-3]) {
- //printf("Checksum error in char 0, expected %x but got %x \r\n", checksum[0], message[len-3]);
+ //printf("checksum error in char 0, expected %x but got %x \r\n", checksum[0], message[len-3]);
free(checksum);
- sendError(CHECKSUM_ERR);
+ send_error(CHECKSUM_ERR);
return false;
}
if (checksum[1] != message[len-2]) {
//printf("Checksum error in char 1, expected %x but got %x \r\n", checksum[1], message[len-2]);
free(checksum);
- sendError(CHECKSUM_ERR);
+ send_error(CHECKSUM_ERR);
return false;
}
if (checksum[2] != message[len-1]) {
//printf("Checksum error in char 2, expected %x but got %x \r\n", checksum[2], message[len-1]);
free(checksum);
- sendError(CHECKSUM_ERR);
+ send_error(CHECKSUM_ERR);
return false;
}
free(checksum);
@@ -328,41 +403,54 @@
return true;
}
-//Checks a received readOnly message
-void BluetoothComm::processReadOnly(char* message, int len)
+/**
+* Checks a received readOnly message
+* @param message The readonly message received
+* @param len Length of the message
+* @author Michael Ling
+* @date 2/4/2015
+*/
+void BluetoothComm::process_read_only(char* message, int len)
{
//printf("Message is a ReadOnly \r\n");
- if (!msgCheck(message, len)) {
- //printf("MSGCHECK failed on read! \r\n");
+ if (!msg_check(message, len)) {
+ //printf("msg_check failed on read! \r\n");
return;
}
- failures = 0;
+ _failures = 0;
//printf("Sending readOnly values \r\n");
- sendReadOnlyValues();
+ send_read_only_values();
}
-//Checks received READ message, and places requested data into an array
-void BluetoothComm::processRead(char* message, int len)
+/**
+* Checks received READ message, and places requested data into an array
+* @param message The received READ request as a char array
+* @param len Length of the READ message
+* @author Michael Ling
+* @date 2/4/2015
+*/
+void BluetoothComm::process_read(char* message, int len)
{
//If the received message is an error message, resend the last command
- if (message[2] == START) {
- failures += 1;
- if (failures < 5) {
- send(lastCmd);
+ if (message[2] == END) {
+ _failures += 1;
+ //printf("_failures: %d\r\n", _failures);
+ if (_failures < 5) {
+ send(_lastCmd);
} else {
- failures = 0;
+ _failures = 0;
}
return;
}
- if (!msgCheck(message, len)) {
- //printf("MSGCHECK failed on read! \r\n");
+ if (!msg_check(message, len)) {
+ //printf("msg_check failed on read! \r\n");
return;
}
- failures = 0;
+ _failures = 0;
//printf("Message is a read \r\n");
- char paramList[numVars];
- memset(paramList, 0xff, numVars);
+ char paramList[_numVars];
+ memset(paramList, 0xff, _numVars);
for (int i=2; i < len-5; i++) {
char msg = message[i] & 0xbf;
@@ -373,33 +461,39 @@
int index = msg & 0xff;
//printf("Value at index %d requested \r\n", index);
- paramList[index] = paramMap[indexMap[index]];
+ paramList[index] = _paramMap[_indexMap[index]];
}
if (message[len-5] == READONLY_IND) {
- sendReadOnlyValues();
+ send_read_only_values();
}
- sendValues(paramList);
+ send_values(paramList);
}
-//Checks received WRITE message and writes to localValues
-void BluetoothComm::processWrite(char* message, int len)
+/**
+* Checks received WRITE message and writes to paramMap/SDCard
+* @param message The received WRITE message
+* @param len Length of the WRITE message
+* @author Michael Ling
+* @date 2/4/2015
+*/
+void BluetoothComm::process_write(char* message, int len)
{
- if (message[2] == START) {
- failures += 1;
- if (failures < 5) {
- send(lastCmd);
+ if (message[2] == END) {
+ _failures += 1;
+ if (_failures < 5) {
+ send(_lastCmd);
} else {
- failures = 0;
+ _failures = 0;
}
return;
}
- if (!msgCheck(message, len)) {
- //printf("MSGCHECK failed on write! \r\n");
+ if (!msg_check(message, len)) {
+ //printf("msg_check failed on write! \r\n");
return;
}
- char paramList[numVars];
- memset(paramList, 0xff, numVars);
+ char paramList[_numVars];
+ memset(paramList, 0xff, _numVars);
//printf("Message is a write \r\n");
for (int i=2; i < len-5; i+=2) {
char msg = message[i] & 0xbf;
@@ -407,93 +501,100 @@
return;
}
int index = msg & 0x7f;
- paramMap[indexMap[index]] = message[i+1];
+ _paramMap[_indexMap[index]] = message[i+1];
paramList[index] = message[i+1];
//printf("Wrote %x to index %d of localValues \r\n", localValues[index], index);
}
- sendValues(paramList);
- writeParamsToSDCard();
+ send_values(paramList);
+ write_params_to_sd_card();
//SD_Card.write(localValues)
//sendToControlBed(localValues+1, len-2)
}
-//Checks if received message is a read, write or readonly
+/**
+* Checks if received message is a read, write or readonly
+* @param message The received message
+* @param len Length of the message
+* @author Michael Ling
+* @date 2/4/2015
+*/
void BluetoothComm::process (char* message, int len)
{
char c = message[2];
- for (int i =0; i < len; i++) {
- printf("Message character: %x \r\n", message[i]);
- }
+ //for (int i =0; i < len; i++) {
+ // printf("Message character: %x \r\n", message[i]);
+ //}
if (c == READONLY_IND) {
- processReadOnly(message, len);
+ process_read_only(message, len);
return;
}
if ((c & 0x80) == 0) {
- processRead(message, len);
+ process_read(message, len);
return;
} else {
- processWrite(message, len);
+ process_write(message, len);
return;
}
}
//Warning: do not put print statements in the function attachment(); it will interfere with receiving messages
+/**
+* Scans for data received through Bluetooth, and passes it on if it detects a message-like chunk. Should be run via an interuupt.
+* @author Michael Ling
+* @date 2/4/2015
+*/
void BluetoothComm::attachment()
{
-if (rn42.readable()) {
- // //printf("rn42 is readable \r\n");
- data=rn42.getc();
+ if (_rn42.readable()) {
+ // //printf("_rn42 is readable \r\n");
+ _data=_rn42.getc();
- // if (data != NULL) {
- char b = data & 0xff;
- //printf("Got char: %x \r\n", b);
- if (b != NULL or inMsg) {
- // printf("Got char non null: %x \r\n", b);
+ // if (_data != NULL) {
+ char b = _data & 0xff;
+ //printf("Got char: %x \r\n", b);
+ if (b != NULL or _inMsg) {
+ // printf("Got char non null: %x \r\n", b);
+ }
+ //This marks the START of a message
+ if (_inMsg == false and b == START) {
+ // printf("Msg START received \r\n");
+ _inMsg = true;
+ _counter = 3;
+ _curMsg[_len] = b;
+ _len += 1;
+ } else if (_inMsg == true and b == START) {
+ // printf("Second start received, terminating\r\n");
+ _inMsg = false;
+ _counter = 0;
+ memset(_curMsg, 0, 50);
+ _rn42.rxBufferFlush();
+ process(_msg, _len);
+ _len = 0;
+ } else if (_inMsg || _counter > 0 ) {
+ // printf("_inMsg or _counter > 0 \r\n");
+ _curMsg[_len] = b;
+ _len += 1;
+ if (!_inMsg) {
+ _counter -= 1;
}
- //This marks the START of a message
- if (inMsg == false and b == START) {
- // printf("Msg START received \r\n");
- inMsg = true;
- counter = 3;
- curMsg[len] = b;
- len += 1;
- } else if (inMsg == true and b == START) {
- // printf("Second start received, terminating\r\n");
- inMsg = false;
- counter = 0;
- memset(curMsg, 0, 50);
- rn42.rxBufferFlush();
- process(msg, len);
- len = 0;
- } else if (inMsg || counter > 0 ) {
- // printf("inMsg or counter > 0 \r\n");
- curMsg[len] = b;
- len += 1;
- if (!inMsg) {
- counter -= 1;
- }
- //Marks end of message, and starts processing
- if (counter <= 0) {
- // printf("End of message \r\n");
- memset(msg, 0, 50);
- memcpy(msg, curMsg, 50);
- memset(curMsg, 0, 50);
- rn42.rxBufferFlush();
- process(msg, len);
- len = 0;
- }
+ //Marks end of message, and starts processing
+ if (_counter <= 0) {
+ // printf("End of message \r\n");
+ memset(_msg, 0, 50);
+ memcpy(_msg, _curMsg, 50);
+ memset(_curMsg, 0, 50);
+ _rn42.rxBufferFlush();
+ process(_msg, _len);
+ _len = 0;
}
- if (b == END) {
- inMsg = false;
+ }
+ if (b == END) {
+ _inMsg = false;
- // rn42.putc(msg);
- }
- // }
- //rn42.putc(data);
+ // _rn42.putc(msg);
+ }
+ //_rn42.putc(_data);
- }
+ }
}
-
-
-
--- a/BluetoothComm.h Wed Dec 17 21:46:45 2014 +0000
+++ b/BluetoothComm.h Fri Feb 06 20:45:47 2015 +0000
@@ -1,45 +1,68 @@
-#include "mbed.h"
-#include "MODSERIAL.h"
-//#include "init.h"
+#ifndef BLUETOOTHCOMM_H
+#define BLUETOOTHCOMM_H
#include <map>
#include <string>
+#include "mbed.h"
+#include "MODSERIAL.h"
+/**
+* @file BluetoothComm.h
+* @brief This header file describes BluetoothComm, an object that receives and processes messages through Bluetooth.
+* @author Michael Ling
+* @date 2/4/2015
+*/
class BluetoothComm
{
public:
BluetoothComm(PinName pwm, PinName dirpin);
- void readDataFromSD();
- void readParamsFromSD();
+ void read_data_from_sd();
+ void read_params_from_sd();
bool parity(short c);
- char* checkSum(char* b, int len);
- void sendError(char errCode);
- void setCharge(short level);
- void setValues(std::map<string, short> newValues);
- void writeParamsToSDCard();
- void writeDataToSDCard();
+ char* get_checksum(char* b, int len);
+ void send_error(char errCode);
+ //void setCharge(short level);
+ void set_values(std::map<string, short> newValues);
+ void write_params_to_sd_card();
+ void write_data_to_sd_card();
void send(char* cmd);
- void sendValues(char* paramList);
- void sendReadOnlyValues();
- bool msgCheck(char* msg, int len);
- void processReadOnly(char* message, int len);
- void processRead(char* message, int len);
- void processWrite(char* message, int len);
+ void send_values(char* paramList);
+ void send_read_only_values();
+ bool msg_check(char* msg, int len);
+ void process_read_only(char* message, int len);
+ void process_read(char* message, int len);
+ void process_write(char* message, int len);
void process(char* message, int len);
void attachment();
int main();
+
private:
- MODSERIAL rn42; // serial object to read data coming in
+ MODSERIAL _rn42; // serial object to read data coming in
//Map containing values of Exo parameters
- std::map<std::string, short> paramMap;
+ std::map<std::string, short> _paramMap;
//Maps parameter indices to param names
- std::string indexMap[50];
+ std::string _indexMap[50];
//Last received MSG
- char msg[50];
+ char _msg[50];
//Value of Exo parameters, stored locally
- char localValues[50];
- char curMsg[50];
+ // _localValues[50];
+ char _curMsg[50];
+ int _numVars;
+ //Last sent command
+ char _lastCmd[50];
+ int _failures;
+ //Readonly Parameters
+ int _numReadOnlyParams;
+ short _readOnlyParams[12];
+ //Indices where an escape char. is needed
+ int _escapeNeeded[9];
+ int _escapesNeeded;
+ int _len;
+ int _counter;
+ bool _inMsg;
+ int _data;
+
//START/END bytes, parameter indices
static const char START = 0xff;
static const char END = 0xfe;
@@ -65,10 +88,10 @@
static const char EXTEND_IND = 0x13;
static const char LOCK_IND = 0x14;
static const char RATE_IND = 0x15;
- int numVars;
- //Last sent command
- char lastCmd[50];
- int failures;
+
+ //Indicates a readonly request
+ static const char READONLY_IND = 0x1f;
+
//Error codes
static const char START_ERR = 1;
static const char END_ERR = 2;
@@ -77,16 +100,6 @@
static const char VAR_ERR = 5;
static const char DATA_ERR = 6;
static const char RW_ERR = 7;
- //Readonly Parameters
- int numReadOnlyParams;
- short readOnlyParams[12];
- //Indices where an escape char. is needed
- int escapeNeeded[9];
- int escapesNeeded;
- //Indicates a readonly request
- static const char READONLY_IND = 0x1f;
- int len;
- int counter;
- bool inMsg;
- int data;
-};
\ No newline at end of file
+
+};
+#endif