Michael Ling / ExoController

Dependents:   Data-Management-Honka

Committer:
mzling
Date:
Fri Feb 06 21:16:31 2015 +0000
Revision:
4:7e3bbf896e78
Parent:
3:14050370593a
Child:
5:92659a4c2f89
put back in dummy code for SDCard initialization.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mzling 2:be605799793f 1 #include "mbed.h"
mzling 2:be605799793f 2 #include "MODSERIAL.h"
mzling 2:be605799793f 3 #include "BluetoothComm.h"
mzling 2:be605799793f 4 #include "init.h"
mzling 2:be605799793f 5 #include <string>
mzling 2:be605799793f 6 #include <map>
mzling 3:14050370593a 7 //Variable indices for set_values: set_values should take an array of length NUMVARS + NUMREADONLYPARAMS = 34.
mzling 2:be605799793f 8 //Map of variables to array indices is as follows:
mzling 2:be605799793f 9 //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
mzling 2:be605799793f 10 //8 = Exo State/Left Knee State/Right Knee State 9 = Torso Ref. Angle, 10 = Left Hip Ref. Angle, 11 = Right Hip Ref. Angle
mzling 2:be605799793f 11 //12 = KP Stance, 13 = KP Swing, 14 = KP Standing, 15 = KP Sitting, 16 = KP Standing up, 17 = KP Sitting down
mzling 2:be605799793f 12 //18 = KD Stance, 19 = KD Swing, 20 = KD Standing, 21 = KD Sitting, 22 = KD Standing up, 23 = KD sitting down
mzling 2:be605799793f 13 //24 = Standing angle, 25 = Sitting angle, 26 = Bent Forward Angle, 27 = Forward Angle, 28 = Rear Angle, 29 = IMU Angle
mzling 2:be605799793f 14 //30 = Knee Full Retract, 31 = Knee Full Extend, 32 = Lock Time, 33 = Rate
mzling 3:14050370593a 15 BluetoothComm::BluetoothComm(PinName tx, PinName rx): _rn42(tx, rx), _len(0), _counter(0), _inMsg(false), _numVars(22), _numReadOnlyParams(12), _escapesNeeded(8)
mzling 2:be605799793f 16 {
mzling 3:14050370593a 17 _rn42.baud(9600);
mzling 2:be605799793f 18 printf("Started BTComm init \r\n");
mzling 3:14050370593a 19 // _rn42.baud(115200);
mzling 2:be605799793f 20 int temp1[] = {0,1,2,3,4,8,9,10, -1};
mzling 2:be605799793f 21
mzling 3:14050370593a 22 for (int i = 0; i < _escapesNeeded+1; i++) {
mzling 3:14050370593a 23 _escapeNeeded[i] = temp1[i];
mzling 2:be605799793f 24 }
mzling 2:be605799793f 25
mzling 2:be605799793f 26
mzling 2:be605799793f 27 std::string temp2[] = {"KPStance", "KPSwing", "KPStanding", "KPSitting", "KPStandUp", "KPSitdown", "KDStance", "KDSwing",
mzling 2:be605799793f 28 "KDStanding", "KDSitting", "KDStandUp", "KDSitDown", "StandingAngle", "SittingAngle", "BentForwardAngle", "ForwardAngle", "RearAngle",
mzling 2:be605799793f 29 "IMUAngle", "KneeFullRetract", "KneeFullExtend", "LockTime", "Rate", "TorsoAng", "LKneeAng", "RKneeAng", "LHipAng", "RHipAng", "LHipTorque",
mzling 2:be605799793f 30 "RHipTorque", "ExoAndKneeStates", "TorsoRefAngle",
mzling 2:be605799793f 31 "LHipRefAngle", "RHipRefAngle", "Charge"};
mzling 2:be605799793f 32 //Populate the map of indices to param names
mzling 3:14050370593a 33 for (int j = 0; j < (_numVars + _numReadOnlyParams); j += 1) {
mzling 3:14050370593a 34 _indexMap[j] = temp2[j];
mzling 2:be605799793f 35 }
mzling 2:be605799793f 36
mzling 3:14050370593a 37
mzling 3:14050370593a 38 write_params_to_sd_card();
mzling 3:14050370593a 39 printf("Initialized PARAM \r\n");
mzling 4:7e3bbf896e78 40 int temp4[] = {0x01fe, 0x02ac, 0x02ff, 0x0180, 0x0012, 0x0010, 0x0020, 0x00bf, 0x023f, 0x0123, 0x03a2, 0x10};
mzling 4:7e3bbf896e78 41 readData.write(_numReadOnlyParams, temp4);
mzling 4:7e3bbf896e78 42 for (int k = 0; k < _numReadOnlyParams; k += 1) {
mzling 4:7e3bbf896e78 43 _paramMap[_indexMap[_numVars + k]] = temp4[k];
mzling 4:7e3bbf896e78 44 }
mzling 3:14050370593a 45 //printf("Test: %x\r\n", _paramMap["TorsoAng"]);
mzling 3:14050370593a 46 write_data_to_sd_card();
mzling 3:14050370593a 47 //printf("Initialized data\r\n");
mzling 3:14050370593a 48 //Fill the parameter map with data from SD card
mzling 3:14050370593a 49 read_data_from_sd();
mzling 4:7e3bbf896e78 50 printf("Test: %x\r\n", _paramMap["TorsoAng"]);
mzling 3:14050370593a 51 read_params_from_sd();
mzling 2:be605799793f 52
mzling 2:be605799793f 53
mzling 2:be605799793f 54 }
mzling 2:be605799793f 55
mzling 3:14050370593a 56 /**
mzling 3:14050370593a 57 * Reads read-only data from SD card, and fills in the parameter map.
mzling 3:14050370593a 58 * @author Michael Ling
mzling 3:14050370593a 59 * @date 2/4/2015
mzling 3:14050370593a 60 */
mzling 3:14050370593a 61 void BluetoothComm::read_data_from_sd()
mzling 3:14050370593a 62 {
mzling 3:14050370593a 63
mzling 3:14050370593a 64 int *arr = readData.read(_numReadOnlyParams, arr);
mzling 3:14050370593a 65 for (int i = 0; i < _numReadOnlyParams; i += 1) {
mzling 3:14050370593a 66 _paramMap[_indexMap[i + _numVars]] = arr[i];
mzling 4:7e3bbf896e78 67 printf("Read: %x\r\n", arr[i]);
mzling 2:be605799793f 68 }
mzling 4:7e3bbf896e78 69 printf("Finished reading data\r\n");
mzling 2:be605799793f 70 }
mzling 2:be605799793f 71
mzling 3:14050370593a 72 /**
mzling 3:14050370593a 73 * Reads editable parameters from SD card, and fills in the parameter map
mzling 3:14050370593a 74 * @author Michael Ling
mzling 3:14050370593a 75 * @date 2/4/2015
mzling 3:14050370593a 76 */
mzling 3:14050370593a 77 void BluetoothComm::read_params_from_sd()
mzling 3:14050370593a 78 {
mzling 3:14050370593a 79 int *arr = param.read(_numVars, arr);
mzling 3:14050370593a 80 for (int i = 0; i < _numVars; i += 1) {
mzling 3:14050370593a 81 _paramMap[_indexMap[i]] = arr[i];
mzling 2:be605799793f 82 }
mzling 3:14050370593a 83 //printf("Finished reading params\r\n");
mzling 2:be605799793f 84 }
mzling 2:be605799793f 85
mzling 3:14050370593a 86 /**
mzling 3:14050370593a 87 * Calculates parity--0 if c even, 1 if odd
mzling 3:14050370593a 88 * @param c The short that we want to calculate parity of
mzling 3:14050370593a 89 * @author Michael Ling
mzling 3:14050370593a 90 * @date 2/4/2015
mzling 3:14050370593a 91 */
mzling 2:be605799793f 92 bool BluetoothComm::parity(short c)
mzling 2:be605799793f 93 {
mzling 2:be605799793f 94 bool p = false;
mzling 2:be605799793f 95 while (c != 0) {
mzling 2:be605799793f 96 p = !p;
mzling 2:be605799793f 97 c = (short) (c & (c-1));
mzling 2:be605799793f 98 }
mzling 2:be605799793f 99 return p;
mzling 2:be605799793f 100 }
mzling 2:be605799793f 101
mzling 3:14050370593a 102 /**
mzling 3:14050370593a 103 * Calculates checksum of char array, sum of all chars in array
mzling 3:14050370593a 104 * @param b The char array to be summed up
mzling 3:14050370593a 105 * @param len Length of the array b
mzling 3:14050370593a 106 * @author Michael Ling
mzling 3:14050370593a 107 * @date 2/4/2015
mzling 3:14050370593a 108 */
mzling 3:14050370593a 109 char* BluetoothComm::get_checksum(char* b, int len)
mzling 2:be605799793f 110 {
mzling 2:be605799793f 111 char* checksum = (char*)malloc(3*sizeof(char));
mzling 2:be605799793f 112 short sum = 0;
mzling 2:be605799793f 113 for (int i = 0; i < len; i += 1) {
mzling 2:be605799793f 114 sum += ((short) b[i] & 0xff);
mzling 2:be605799793f 115 }
mzling 2:be605799793f 116 checksum[1] = (char) ((sum >> 8) & 0xff);
mzling 2:be605799793f 117 checksum[2] = (char) (sum & 0xff);
mzling 2:be605799793f 118 char check = (char) (sum & 0xff);
mzling 2:be605799793f 119 //Sets escape character if the checksum contains START or END
mzling 2:be605799793f 120 if (check == START || check == END) {
mzling 2:be605799793f 121 checksum[0] = 1;
mzling 2:be605799793f 122 checksum[2] = (char) (checksum[2] & 0x1);
mzling 2:be605799793f 123 } else {
mzling 2:be605799793f 124 checksum[0] = 0;
mzling 2:be605799793f 125 }
mzling 2:be605799793f 126 return checksum;
mzling 2:be605799793f 127 }
mzling 2:be605799793f 128
mzling 3:14050370593a 129 /**
mzling 3:14050370593a 130 * Sends error message.
mzling 3:14050370593a 131 * @param errCode character representation of the failure-causing error
mzling 3:14050370593a 132 * @author Michael Ling
mzling 3:14050370593a 133 * @data 2/4/2015
mzling 3:14050370593a 134 */
mzling 3:14050370593a 135 void BluetoothComm::send_error(char errCode)
mzling 2:be605799793f 136 {
mzling 3:14050370593a 137 _rn42.putc(START);
mzling 3:14050370593a 138 _rn42.putc(errCode);
mzling 3:14050370593a 139 _rn42.putc(END);
mzling 3:14050370593a 140 _rn42.putc(0);
mzling 3:14050370593a 141 _rn42.putc(0);
mzling 3:14050370593a 142 _rn42.putc(0);
mzling 2:be605799793f 143 }
mzling 3:14050370593a 144 /**
mzling 3:14050370593a 145 //Sets charge level. DEPRECATED
mzling 3:14050370593a 146 void BluetoothComm::setCharge(short level) {
mzling 3:14050370593a 147 // _readOnlyParams[11] = level;
mzling 3:14050370593a 148 _paramMap["Charge"] = level;
mzling 3:14050370593a 149 }
mzling 3:14050370593a 150 */
mzling 2:be605799793f 151
mzling 3:14050370593a 152 /**
mzling 3:14050370593a 153 * Sends the specified char array through the _rn42 Bluetooth connection.
mzling 3:14050370593a 154 * @param cmd The char array to be sent
mzling 3:14050370593a 155 * @author Michael Ling
mzling 3:14050370593a 156 * @date 2/4/2015
mzling 3:14050370593a 157 */
mzling 2:be605799793f 158 void BluetoothComm::send(char *cmd)
mzling 2:be605799793f 159 {
mzling 2:be605799793f 160 for (int i = 0; i < 50; i++) {
mzling 3:14050370593a 161 _rn42.putc(cmd[i]);
mzling 2:be605799793f 162 }
mzling 2:be605799793f 163 }
mzling 2:be605799793f 164
mzling 3:14050370593a 165 /**
mzling 3:14050370593a 166 * Sets the parameter map, based on the input map NEWVALUES
mzling 3:14050370593a 167 * @param newValues A map of strings to shorts to be copied to _paramMap
mzling 3:14050370593a 168 * @author Michael Ling
mzling 3:14050370593a 169 * @date 2/4/2015
mzling 3:14050370593a 170 */
mzling 3:14050370593a 171 void BluetoothComm::set_values(std::map<string, short> newValues)
mzling 3:14050370593a 172 {
mzling 2:be605799793f 173
mzling 2:be605799793f 174 for (std::map<string, short>::iterator it = newValues.begin(); it != newValues.end(); ++it) {
mzling 3:14050370593a 175 _paramMap[it->first] = it->second;
mzling 2:be605799793f 176 }
mzling 2:be605799793f 177 }
mzling 2:be605799793f 178
mzling 3:14050370593a 179 /**
mzling 3:14050370593a 180 * Writes the editable params. stored in _paramMap to the SD card
mzling 3:14050370593a 181 * @author Michael Ling
mzling 3:14050370593a 182 * @date 2/4/2015
mzling 3:14050370593a 183 */
mzling 3:14050370593a 184 void BluetoothComm::write_params_to_sd_card()
mzling 3:14050370593a 185 {
mzling 2:be605799793f 186
mzling 3:14050370593a 187 int paramValues[_numVars];
mzling 3:14050370593a 188 for (int i = 0; i < _numVars; i += 1) {
mzling 3:14050370593a 189 paramValues[i] = (int) _paramMap[_indexMap[i]];
mzling 2:be605799793f 190 }
mzling 3:14050370593a 191 param.write(_numVars, paramValues);
mzling 2:be605799793f 192
mzling 2:be605799793f 193 }
mzling 2:be605799793f 194
mzling 3:14050370593a 195 /**
mzling 3:14050370593a 196 * Write the read-only values stored in _paramMap to the SD card
mzling 3:14050370593a 197 * @author Michael Ling
mzling 3:14050370593a 198 * @date 2/4/2015
mzling 3:14050370593a 199 */
mzling 3:14050370593a 200 void BluetoothComm::write_data_to_sd_card()
mzling 3:14050370593a 201 {
mzling 3:14050370593a 202 int dataValues[_numReadOnlyParams];
mzling 3:14050370593a 203 for (int i = 0; i < _numReadOnlyParams; i += 1) {
mzling 3:14050370593a 204 dataValues[i] = (int) _paramMap[_indexMap[i + _numVars]];
mzling 3:14050370593a 205 // printf("Index %d of dataValues set to %x\r\n", i, dataValues[i]);
mzling 2:be605799793f 206 }
mzling 3:14050370593a 207 readData.write(_numReadOnlyParams, dataValues);
mzling 2:be605799793f 208 }
mzling 2:be605799793f 209
mzling 3:14050370593a 210 /**
mzling 3:14050370593a 211 * Sends the paramList with START/END, parity bits, and a checksum
mzling 3:14050370593a 212 * @param paramList List of parameters to be sent over Bluetooth, represented as a char array
mzling 3:14050370593a 213 * @author Michael Ling
mzling 3:14050370593a 214 * @date 2/4/2015
mzling 3:14050370593a 215 */
mzling 3:14050370593a 216 void BluetoothComm::send_values(char* paramList)
mzling 2:be605799793f 217 {
mzling 3:14050370593a 218 char msg[2*_numVars+6];
mzling 2:be605799793f 219 int len=2;
mzling 2:be605799793f 220 //printf("Sending values \r\n");
mzling 2:be605799793f 221 msg[0] = START;
mzling 2:be605799793f 222 msg[1] = 0;
mzling 3:14050370593a 223 for (int i=0; i < _numVars; i++) {
mzling 2:be605799793f 224 if (i == 21) {
mzling 2:be605799793f 225 //printf("On final loop \r\n");
mzling 2:be605799793f 226 }
mzling 2:be605799793f 227 if (paramList[i] != 0xff) {
mzling 2:be605799793f 228 short s = (short)((i << 8) | paramList[i]);
mzling 3:14050370593a 229 //printf("In send_values, calculating parity of %x\r\n", s);
mzling 2:be605799793f 230 if (parity(s)) {
mzling 2:be605799793f 231 //printf("%x requires TRUE parity bit\r\n", s);
mzling 2:be605799793f 232 msg[len] = (char)(i | 0x40);
mzling 2:be605799793f 233 len += 1;
mzling 2:be605799793f 234 } else {
mzling 2:be605799793f 235 //printf("%x requires FALSE parity bit\r\n", s);
mzling 2:be605799793f 236 msg[len] = (char)i;
mzling 2:be605799793f 237 len += 1;
mzling 2:be605799793f 238 }
mzling 2:be605799793f 239 msg[len] = paramList[i];
mzling 2:be605799793f 240 len += 1;
mzling 2:be605799793f 241 }
mzling 2:be605799793f 242 }
mzling 2:be605799793f 243 msg[len] = END;
mzling 2:be605799793f 244 len += 1;
mzling 3:14050370593a 245 char* checksum = get_checksum(msg, len);
mzling 2:be605799793f 246 msg[len] = checksum[0];
mzling 2:be605799793f 247 msg[len+1] = checksum[1];
mzling 2:be605799793f 248 msg[len+2] = checksum[2];
mzling 2:be605799793f 249 len += 3;
mzling 2:be605799793f 250 for (int j = 0; j < len; j++) {
mzling 2:be605799793f 251 //printf("Sending char %x \r\n", msg[j]);
mzling 3:14050370593a 252 _rn42.putc(msg[j]);
mzling 2:be605799793f 253 }
mzling 3:14050370593a 254 memcpy(_lastCmd, msg, 50);
mzling 2:be605799793f 255 free(checksum);
mzling 2:be605799793f 256 return ;
mzling 2:be605799793f 257 }
mzling 2:be605799793f 258
mzling 3:14050370593a 259 /**
mzling 3:14050370593a 260 * Sends readOnly Parameters, with START/END and checksum
mzling 3:14050370593a 261 * @author Michael Ling
mzling 3:14050370593a 262 * @date 2/4/2015
mzling 3:14050370593a 263 */
mzling 3:14050370593a 264 void BluetoothComm::send_read_only_values()
mzling 2:be605799793f 265 {
mzling 3:14050370593a 266 int msgLen = 2*_numReadOnlyParams+_escapesNeeded+7;
mzling 2:be605799793f 267 char message[msgLen];
mzling 2:be605799793f 268 message[0] = START;
mzling 2:be605799793f 269 message[1] = 0;
mzling 2:be605799793f 270 message[2] = READONLY_IND;
mzling 2:be605799793f 271 int msgInd = 3;
mzling 2:be605799793f 272 int escapes = 0;
mzling 3:14050370593a 273 //printf("%d readonly parameters", _numReadOnlyParams);
mzling 3:14050370593a 274 for (int i = 0; i < _numReadOnlyParams; i++) {
mzling 3:14050370593a 275 if (i == _escapeNeeded[escapes]) {
mzling 2:be605799793f 276 //printf("Escape char. needed at index %d \r\n", i);
mzling 3:14050370593a 277 //char conflict = (char)(_readOnlyParams[i] & 0xff);
mzling 3:14050370593a 278 char conflict = (char)(_paramMap[_indexMap[i+_numVars]] & 0xff);
mzling 3:14050370593a 279 //printf("%x possibly has a conflict in %x \r\n", _readOnlyParams[i], conflict);
mzling 2:be605799793f 280 escapes += 1;
mzling 3:14050370593a 281 //message[msgInd+1] = (char) (_readOnlyParams[i] >> 8);
mzling 3:14050370593a 282 message[msgInd+1] = (char) (_paramMap[_indexMap[i+_numVars]] >> 8);
mzling 2:be605799793f 283 //printf("Set msgInd+1 to %x \r\n", message[msgInd+1]);
mzling 2:be605799793f 284 if (conflict == (char) 0xfe) {
mzling 2:be605799793f 285 message[msgInd] = 1;
mzling 2:be605799793f 286 message[msgInd+2] = 0xfc;
mzling 2:be605799793f 287 } else if (conflict == (char) 0xff) {
mzling 2:be605799793f 288 message[msgInd] = 1;
mzling 2:be605799793f 289 message[msgInd+2] = 0xfd;
mzling 2:be605799793f 290 } else {
mzling 2:be605799793f 291 message[msgInd] = 0;
mzling 2:be605799793f 292 message[msgInd+2] = conflict;
mzling 2:be605799793f 293 }
mzling 2:be605799793f 294 msgInd += 3;
mzling 2:be605799793f 295 } else {
mzling 3:14050370593a 296 // message[msgInd] = (char) (_readOnlyParams[i] >> 8);
mzling 3:14050370593a 297 // message[msgInd+1] = (char) (_readOnlyParams[i] & 0xff);
mzling 3:14050370593a 298 message[msgInd] = (char) (_paramMap[_indexMap[i+_numVars]] >> 8);
mzling 3:14050370593a 299 message[msgInd+1] = (char) (_paramMap[_indexMap[i+_numVars]] & 0xff);
mzling 2:be605799793f 300 msgInd += 2;
mzling 2:be605799793f 301 }
mzling 2:be605799793f 302 }
mzling 2:be605799793f 303 message[msgLen-4] = END;
mzling 3:14050370593a 304 char* checksum = get_checksum(message, msgLen-3);
mzling 2:be605799793f 305 message[msgLen-3] = checksum[0];
mzling 2:be605799793f 306 message[msgLen-2] = checksum[1];
mzling 2:be605799793f 307 message[msgLen-1] = checksum[2];
mzling 2:be605799793f 308 //printf("Sending the following readONly values: \r\n");
mzling 2:be605799793f 309 for (int j=0; j < msgLen; j++) {
mzling 3:14050370593a 310 printf("%x \r\n", message[j]);
mzling 3:14050370593a 311 _rn42.putc(message[j]);
mzling 2:be605799793f 312 }
mzling 3:14050370593a 313 memcpy(_lastCmd, message, 50);
mzling 2:be605799793f 314 free(checksum);
mzling 2:be605799793f 315 //printf("Finished sending readOnly values \r\n");
mzling 2:be605799793f 316
mzling 2:be605799793f 317 }
mzling 2:be605799793f 318
mzling 3:14050370593a 319 /**
mzling 3:14050370593a 320 * 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
mzling 3:14050370593a 321 * @param message The received message to check
mzling 3:14050370593a 322 * @param len Length of the message
mzling 3:14050370593a 323 * @author Michael Ling
mzling 3:14050370593a 324 * @date 2/4/2015
mzling 3:14050370593a 325 */
mzling 3:14050370593a 326 bool BluetoothComm::msg_check(char* message, int len)
mzling 2:be605799793f 327 {
mzling 2:be605799793f 328 if (message[0] != START) {
mzling 2:be605799793f 329 //printf("Improper START or END \r\n");
mzling 3:14050370593a 330 send_error(START_ERR);
mzling 2:be605799793f 331 return false;
mzling 2:be605799793f 332 }
mzling 2:be605799793f 333 if (message[len-4] != END) {
mzling 3:14050370593a 334 send_error(END_ERR);
mzling 2:be605799793f 335 return false;
mzling 2:be605799793f 336 }
mzling 2:be605799793f 337 bool write = message[2] & 0x80;
mzling 2:be605799793f 338 for (int i=2; i < len-5; i+=2) {
mzling 2:be605799793f 339 if (i == 2 && message[i] == READONLY_IND) {
mzling 2:be605799793f 340 break;
mzling 2:be605799793f 341 }
mzling 2:be605799793f 342 if (i == (len-5) && message[i] == READONLY_IND) {
mzling 2:be605799793f 343 break;
mzling 2:be605799793f 344 }
mzling 2:be605799793f 345 if (((message[i] & 0x80) !=0) && !write) {
mzling 2:be605799793f 346 //printf("Does not match READ format \r\n");
mzling 3:14050370593a 347 send_error((char)(((message[i] & 0x3f) << 3) | RW_ERR));
mzling 2:be605799793f 348 return false;
mzling 2:be605799793f 349 }
mzling 2:be605799793f 350 if (((message[i] & 0x80) != 0x80) && write) {
mzling 2:be605799793f 351 //printf("char %x Does not match WRITE format \r\n", message[i]);
mzling 3:14050370593a 352 send_error((char)(((message[i] & 0x3f) << 3) | RW_ERR));
mzling 2:be605799793f 353 return false;
mzling 2:be605799793f 354 }
mzling 2:be605799793f 355 short s;
mzling 2:be605799793f 356 if (write) {
mzling 2:be605799793f 357 s = (short) ((message[i] << 8) | message[i+1]);
mzling 2:be605799793f 358 } else {
mzling 2:be605799793f 359 s = (short) (message[i] << 8);
mzling 2:be605799793f 360 }
mzling 2:be605799793f 361 bool parity1 = (s & 0x4000) != 0;
mzling 2:be605799793f 362
mzling 2:be605799793f 363 if (parity1 != parity(s & 0x4000)) {
mzling 2:be605799793f 364 //printf("Parity error in VAR char \r\n");
mzling 3:14050370593a 365 send_error((char) (((message[i] & 0xbf) << 3) | PARITY_ERR));
mzling 2:be605799793f 366 return false;
mzling 2:be605799793f 367 }
mzling 2:be605799793f 368
mzling 2:be605799793f 369 char c = message[i] & 0x3f;
mzling 2:be605799793f 370 char c2 = message[i+1];
mzling 3:14050370593a 371 if ((int) c < 0 || (int) c > _numVars) {
mzling 2:be605799793f 372 //printf("VAR char out of range \r\n");
mzling 3:14050370593a 373 send_error((char) (((message[i] & 0xbf) << 3) | VAR_ERR));
mzling 2:be605799793f 374 return false;
mzling 2:be605799793f 375 }
mzling 2:be605799793f 376 if ((int) c2 < 0 || (int) c2 > 100) {
mzling 2:be605799793f 377 //printf("DATA char out of range");
mzling 3:14050370593a 378 send_error((char) (((message[i] & 0xbf) << 3) | DATA_ERR));
mzling 2:be605799793f 379 return false;
mzling 2:be605799793f 380 }
mzling 2:be605799793f 381 }
mzling 3:14050370593a 382 char* checksum = get_checksum(message, len-3);
mzling 2:be605799793f 383 if (checksum[0] != message[len-3]) {
mzling 3:14050370593a 384 //printf("checksum error in char 0, expected %x but got %x \r\n", checksum[0], message[len-3]);
mzling 2:be605799793f 385 free(checksum);
mzling 3:14050370593a 386 send_error(CHECKSUM_ERR);
mzling 2:be605799793f 387 return false;
mzling 2:be605799793f 388 }
mzling 2:be605799793f 389 if (checksum[1] != message[len-2]) {
mzling 2:be605799793f 390 //printf("Checksum error in char 1, expected %x but got %x \r\n", checksum[1], message[len-2]);
mzling 2:be605799793f 391 free(checksum);
mzling 3:14050370593a 392 send_error(CHECKSUM_ERR);
mzling 2:be605799793f 393 return false;
mzling 2:be605799793f 394 }
mzling 2:be605799793f 395 if (checksum[2] != message[len-1]) {
mzling 2:be605799793f 396 //printf("Checksum error in char 2, expected %x but got %x \r\n", checksum[2], message[len-1]);
mzling 2:be605799793f 397 free(checksum);
mzling 3:14050370593a 398 send_error(CHECKSUM_ERR);
mzling 2:be605799793f 399 return false;
mzling 2:be605799793f 400 }
mzling 2:be605799793f 401 free(checksum);
mzling 2:be605799793f 402
mzling 2:be605799793f 403 return true;
mzling 2:be605799793f 404 }
mzling 2:be605799793f 405
mzling 3:14050370593a 406 /**
mzling 3:14050370593a 407 * Checks a received readOnly message
mzling 3:14050370593a 408 * @param message The readonly message received
mzling 3:14050370593a 409 * @param len Length of the message
mzling 3:14050370593a 410 * @author Michael Ling
mzling 3:14050370593a 411 * @date 2/4/2015
mzling 3:14050370593a 412 */
mzling 3:14050370593a 413 void BluetoothComm::process_read_only(char* message, int len)
mzling 2:be605799793f 414 {
mzling 2:be605799793f 415 //printf("Message is a ReadOnly \r\n");
mzling 3:14050370593a 416 if (!msg_check(message, len)) {
mzling 3:14050370593a 417 //printf("msg_check failed on read! \r\n");
mzling 2:be605799793f 418 return;
mzling 2:be605799793f 419 }
mzling 3:14050370593a 420 _failures = 0;
mzling 2:be605799793f 421
mzling 2:be605799793f 422 //printf("Sending readOnly values \r\n");
mzling 3:14050370593a 423 send_read_only_values();
mzling 2:be605799793f 424 }
mzling 2:be605799793f 425
mzling 3:14050370593a 426 /**
mzling 3:14050370593a 427 * Checks received READ message, and places requested data into an array
mzling 3:14050370593a 428 * @param message The received READ request as a char array
mzling 3:14050370593a 429 * @param len Length of the READ message
mzling 3:14050370593a 430 * @author Michael Ling
mzling 3:14050370593a 431 * @date 2/4/2015
mzling 3:14050370593a 432 */
mzling 3:14050370593a 433 void BluetoothComm::process_read(char* message, int len)
mzling 2:be605799793f 434 {
mzling 2:be605799793f 435 //If the received message is an error message, resend the last command
mzling 3:14050370593a 436 if (message[2] == END) {
mzling 3:14050370593a 437 _failures += 1;
mzling 3:14050370593a 438 //printf("_failures: %d\r\n", _failures);
mzling 3:14050370593a 439 if (_failures < 5) {
mzling 3:14050370593a 440 send(_lastCmd);
mzling 2:be605799793f 441 } else {
mzling 3:14050370593a 442 _failures = 0;
mzling 2:be605799793f 443 }
mzling 2:be605799793f 444 return;
mzling 2:be605799793f 445 }
mzling 3:14050370593a 446 if (!msg_check(message, len)) {
mzling 3:14050370593a 447 //printf("msg_check failed on read! \r\n");
mzling 2:be605799793f 448 return;
mzling 2:be605799793f 449 }
mzling 3:14050370593a 450 _failures = 0;
mzling 2:be605799793f 451 //printf("Message is a read \r\n");
mzling 3:14050370593a 452 char paramList[_numVars];
mzling 3:14050370593a 453 memset(paramList, 0xff, _numVars);
mzling 2:be605799793f 454
mzling 2:be605799793f 455 for (int i=2; i < len-5; i++) {
mzling 2:be605799793f 456 char msg = message[i] & 0xbf;
mzling 2:be605799793f 457 if ((msg & 0x80) != 0) {
mzling 2:be605799793f 458 //printf("Got a non-read char %x...exiting \r\n", msg);
mzling 2:be605799793f 459 return;
mzling 2:be605799793f 460 }
mzling 2:be605799793f 461
mzling 2:be605799793f 462 int index = msg & 0xff;
mzling 2:be605799793f 463 //printf("Value at index %d requested \r\n", index);
mzling 3:14050370593a 464 paramList[index] = _paramMap[_indexMap[index]];
mzling 2:be605799793f 465
mzling 2:be605799793f 466 }
mzling 2:be605799793f 467 if (message[len-5] == READONLY_IND) {
mzling 3:14050370593a 468 send_read_only_values();
mzling 2:be605799793f 469 }
mzling 3:14050370593a 470 send_values(paramList);
mzling 2:be605799793f 471 }
mzling 2:be605799793f 472
mzling 3:14050370593a 473 /**
mzling 3:14050370593a 474 * Checks received WRITE message and writes to paramMap/SDCard
mzling 3:14050370593a 475 * @param message The received WRITE message
mzling 3:14050370593a 476 * @param len Length of the WRITE message
mzling 3:14050370593a 477 * @author Michael Ling
mzling 3:14050370593a 478 * @date 2/4/2015
mzling 3:14050370593a 479 */
mzling 3:14050370593a 480 void BluetoothComm::process_write(char* message, int len)
mzling 2:be605799793f 481 {
mzling 3:14050370593a 482 if (message[2] == END) {
mzling 3:14050370593a 483 _failures += 1;
mzling 3:14050370593a 484 if (_failures < 5) {
mzling 3:14050370593a 485 send(_lastCmd);
mzling 2:be605799793f 486 } else {
mzling 3:14050370593a 487 _failures = 0;
mzling 2:be605799793f 488 }
mzling 2:be605799793f 489 return;
mzling 2:be605799793f 490 }
mzling 3:14050370593a 491 if (!msg_check(message, len)) {
mzling 3:14050370593a 492 //printf("msg_check failed on write! \r\n");
mzling 2:be605799793f 493 return;
mzling 2:be605799793f 494 }
mzling 3:14050370593a 495 char paramList[_numVars];
mzling 3:14050370593a 496 memset(paramList, 0xff, _numVars);
mzling 2:be605799793f 497 //printf("Message is a write \r\n");
mzling 2:be605799793f 498 for (int i=2; i < len-5; i+=2) {
mzling 2:be605799793f 499 char msg = message[i] & 0xbf;
mzling 2:be605799793f 500 if ((msg & 0x80) != 0x80) {
mzling 2:be605799793f 501 return;
mzling 2:be605799793f 502 }
mzling 2:be605799793f 503 int index = msg & 0x7f;
mzling 3:14050370593a 504 _paramMap[_indexMap[index]] = message[i+1];
mzling 2:be605799793f 505 paramList[index] = message[i+1];
mzling 2:be605799793f 506 //printf("Wrote %x to index %d of localValues \r\n", localValues[index], index);
mzling 2:be605799793f 507
mzling 2:be605799793f 508 }
mzling 3:14050370593a 509 send_values(paramList);
mzling 3:14050370593a 510 write_params_to_sd_card();
mzling 2:be605799793f 511 //SD_Card.write(localValues)
mzling 2:be605799793f 512 //sendToControlBed(localValues+1, len-2)
mzling 2:be605799793f 513 }
mzling 2:be605799793f 514
mzling 3:14050370593a 515 /**
mzling 3:14050370593a 516 * Checks if received message is a read, write or readonly
mzling 3:14050370593a 517 * @param message The received message
mzling 3:14050370593a 518 * @param len Length of the message
mzling 3:14050370593a 519 * @author Michael Ling
mzling 3:14050370593a 520 * @date 2/4/2015
mzling 3:14050370593a 521 */
mzling 2:be605799793f 522 void BluetoothComm::process (char* message, int len)
mzling 2:be605799793f 523 {
mzling 2:be605799793f 524 char c = message[2];
mzling 4:7e3bbf896e78 525 for (int i =0; i < len; i++) {
mzling 4:7e3bbf896e78 526 printf("Message character: %x \r\n", message[i]);
mzling 4:7e3bbf896e78 527 }
mzling 2:be605799793f 528 if (c == READONLY_IND) {
mzling 3:14050370593a 529 process_read_only(message, len);
mzling 2:be605799793f 530 return;
mzling 2:be605799793f 531 }
mzling 2:be605799793f 532 if ((c & 0x80) == 0) {
mzling 3:14050370593a 533 process_read(message, len);
mzling 2:be605799793f 534 return;
mzling 2:be605799793f 535 } else {
mzling 3:14050370593a 536 process_write(message, len);
mzling 2:be605799793f 537 return;
mzling 2:be605799793f 538 }
mzling 2:be605799793f 539 }
mzling 2:be605799793f 540
mzling 2:be605799793f 541 //Warning: do not put print statements in the function attachment(); it will interfere with receiving messages
mzling 3:14050370593a 542 /**
mzling 3:14050370593a 543 * Scans for data received through Bluetooth, and passes it on if it detects a message-like chunk. Should be run via an interuupt.
mzling 3:14050370593a 544 * @author Michael Ling
mzling 3:14050370593a 545 * @date 2/4/2015
mzling 3:14050370593a 546 */
mzling 2:be605799793f 547 void BluetoothComm::attachment()
mzling 2:be605799793f 548 {
mzling 3:14050370593a 549 if (_rn42.readable()) {
mzling 3:14050370593a 550 // //printf("_rn42 is readable \r\n");
mzling 3:14050370593a 551 _data=_rn42.getc();
mzling 2:be605799793f 552
mzling 3:14050370593a 553 // if (_data != NULL) {
mzling 3:14050370593a 554 char b = _data & 0xff;
mzling 3:14050370593a 555 //printf("Got char: %x \r\n", b);
mzling 3:14050370593a 556 if (b != NULL or _inMsg) {
mzling 3:14050370593a 557 // printf("Got char non null: %x \r\n", b);
mzling 3:14050370593a 558 }
mzling 3:14050370593a 559 //This marks the START of a message
mzling 3:14050370593a 560 if (_inMsg == false and b == START) {
mzling 3:14050370593a 561 // printf("Msg START received \r\n");
mzling 3:14050370593a 562 _inMsg = true;
mzling 3:14050370593a 563 _counter = 3;
mzling 3:14050370593a 564 _curMsg[_len] = b;
mzling 3:14050370593a 565 _len += 1;
mzling 3:14050370593a 566 } else if (_inMsg == true and b == START) {
mzling 3:14050370593a 567 // printf("Second start received, terminating\r\n");
mzling 3:14050370593a 568 _inMsg = false;
mzling 3:14050370593a 569 _counter = 0;
mzling 3:14050370593a 570 memset(_curMsg, 0, 50);
mzling 3:14050370593a 571 _rn42.rxBufferFlush();
mzling 3:14050370593a 572 process(_msg, _len);
mzling 3:14050370593a 573 _len = 0;
mzling 3:14050370593a 574 } else if (_inMsg || _counter > 0 ) {
mzling 3:14050370593a 575 // printf("_inMsg or _counter > 0 \r\n");
mzling 3:14050370593a 576 _curMsg[_len] = b;
mzling 3:14050370593a 577 _len += 1;
mzling 3:14050370593a 578 if (!_inMsg) {
mzling 3:14050370593a 579 _counter -= 1;
mzling 2:be605799793f 580 }
mzling 3:14050370593a 581 //Marks end of message, and starts processing
mzling 3:14050370593a 582 if (_counter <= 0) {
mzling 3:14050370593a 583 // printf("End of message \r\n");
mzling 3:14050370593a 584 memset(_msg, 0, 50);
mzling 3:14050370593a 585 memcpy(_msg, _curMsg, 50);
mzling 3:14050370593a 586 memset(_curMsg, 0, 50);
mzling 3:14050370593a 587 _rn42.rxBufferFlush();
mzling 3:14050370593a 588 process(_msg, _len);
mzling 3:14050370593a 589 _len = 0;
mzling 2:be605799793f 590 }
mzling 3:14050370593a 591 }
mzling 3:14050370593a 592 if (b == END) {
mzling 3:14050370593a 593 _inMsg = false;
mzling 2:be605799793f 594
mzling 3:14050370593a 595 // _rn42.putc(msg);
mzling 3:14050370593a 596 }
mzling 3:14050370593a 597 //_rn42.putc(_data);
mzling 2:be605799793f 598
mzling 3:14050370593a 599 }
mzling 2:be605799793f 600 }