Michael Ling / ExoController

Dependents:   Data-Management-Honka

Committer:
mzling
Date:
Sat Feb 14 00:37:05 2015 +0000
Revision:
5:92659a4c2f89
Parent:
4:7e3bbf896e78
Child:
6:721f5e8a794d
Minimal changes (print stmts. only)

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