diff --git a/comms.ino b/comms.ino index 66fd035..b787252 100644 --- a/comms.ino +++ b/comms.ino @@ -27,13 +27,24 @@ void command() case 'C': // test communications. This is used by Tunerstudio to see whether there is an ECU on a given serial port testComm(); + break; + + case 'L': + sendPage(true); break; - + + case 'N': + Serial.println(); + break; + case 'P': // set the current page //A 2nd byte of data is required after the 'P' specifying the new page number. //This loop should never need to run as the byte should already be in the buffer, but is here just in case while (Serial.available() == 0) { } currentPage = Serial.read(); + if(currentPage >= '0') {currentPage -= '0';} + if(currentPage == veMapPage || currentPage == ignMapPage || currentPage == afrMapPage) {isMap = true;} + else{isMap = false;} break; case 'R': // send 39 bytes of realtime values @@ -50,14 +61,14 @@ void command() break; case 'V': // send VE table and constants - sendPage(); + sendPage(false); break; case 'W': // receive new VE or constant at 'W'++ int offset; while (Serial.available() == 0) { } - if(currentPage == veMapPage || currentPage == ignMapPage || currentPage == afrMapPage ) + if(isMap) { byte offset1, offset2; offset1 = Serial.read(); @@ -94,29 +105,28 @@ void command() digitalWrite(pinInjector1, LOW); digitalWrite(pinInjector2, LOW); return; - - Serial.println("Coolant"); + Serial.println(F("Coolant")); for(int x=0; x+++\n\n" + "===List of Commands===\n\n" + "A - Displays 31 bytes of currentStatus values in binary (live data)\n" + "B - Burn current map and configPage values to eeprom\n" + "C - Test COM port. Used by Tunerstudio to see whether an ECU is on a given serial \n" + " port. Returns a binary number.\n" + "L - Displays map page (aka table) or configPage values. Use P to change page (not \n" + " every page is a map)\n" + "N - Print new line.\n" + "P - Set current page. Syntax: P+\n" + "R - Same as A command\n" + "S - Display signature number\n" + "Q - Same as S command\n" + "V - Display map or configPage values in binary\n" + "W - Set one byte in map or configPage. Expects binary parameters. \n" + " Syntax: W++\n" + "t - Set calibration values. Expects binary parameters. Table index is either 0, \n" + " 1, or 2. Syntax: t++++\n" + "Z - Display calibration values\n" + "T - Displays 256 tooth log entries in binary\n" + "r - Displays 256 tooth log entries\n" + "? - Displays this help page" + )); + break; + + default: + break; } } @@ -150,8 +194,8 @@ void sendValues(int length) response[0] = currentStatus.secl; //secl is simply a counter that increments each second. Used to track unexpected resets (Which will reset this count to 0) response[1] = currentStatus.squirt; //Squirt Bitfield response[2] = currentStatus.engine; //Engine Status Bitfield - response[3] = (byte)(divu100(currentStatus.dwell)); //Dwell in ms * 10 - response[4] = (byte)(currentStatus.MAP >> 1); //map value is divided by 2 + response[3] = 0x00; //baro + response[4] = currentStatus.MAP; //map response[5] = (byte)(currentStatus.IAT + CALIBRATION_TEMPERATURE_OFFSET); //mat response[6] = (byte)(currentStatus.coolant + CALIBRATION_TEMPERATURE_OFFSET); //Coolant ADC response[7] = currentStatus.tpsADC; //TPS (Raw 0-255) @@ -192,7 +236,7 @@ void sendValues(int length) void receiveValue(int offset, byte newValue) { - byte* pnt_configPage; + void* pnt_configPage; switch (currentPage) { @@ -208,7 +252,7 @@ void receiveValue(int offset, byte newValue) if (offset < 272) { //X Axis - fuelTable.axisX[(offset-256)] = ((int)(newValue) * 100); //The RPM values sent by tunerstudio are divided by 100, need to multiple it back by 100 to make it correct + fuelTable.axisX[(offset-256)] = ((int)(newValue) * 100); //The RPM values sent by megasquirt are divided by 100, need to multiple it back by 100 to make it correct } else { @@ -221,11 +265,11 @@ void receiveValue(int offset, byte newValue) break; case veSetPage: - pnt_configPage = (byte *)&configPage1; //Setup a pointer to the relevant config page + pnt_configPage = &configPage1; //Setup a pointer to the relevant config page //For some reason, TunerStudio is sending offsets greater than the maximum page size. I'm not sure if it's their bug or mine, but the fix is to only update the config page if the offset is less than the maximum size if( offset < page_size) { - *(pnt_configPage + (byte)offset) = newValue; //Need to subtract 80 because the map and bins (Which make up 80 bytes) aren't part of the config pages + *((byte *)pnt_configPage + (byte)offset) = newValue; //Need to subtract 80 because the map and bins (Which make up 80 bytes) aren't part of the config pages } break; @@ -241,7 +285,7 @@ void receiveValue(int offset, byte newValue) if (offset < 272) { //X Axis - ignitionTable.axisX[(offset-256)] = (int)(newValue) * int(100); //The RPM values sent by TunerStudio are divided by 100, need to multiple it back by 100 to make it correct + ignitionTable.axisX[(offset-256)] = (int)(newValue) * int(100); //The RPM values sent by megasquirt are divided by 100, need to multiple it back by 100 to make it correct } else { @@ -253,11 +297,11 @@ void receiveValue(int offset, byte newValue) } case ignSetPage: - pnt_configPage = (byte *)&configPage2; + pnt_configPage = &configPage2; //For some reason, TunerStudio is sending offsets greater than the maximum page size. I'm not sure if it's their bug or mine, but the fix is to only update the config page if the offset is less than the maximum size if( offset < page_size) { - *(pnt_configPage + (byte)offset) = newValue; //Need to subtract 80 because the map and bins (Which make up 80 bytes) aren't part of the config pages + *((byte *)pnt_configPage + (byte)offset) = newValue; //Need to subtract 80 because the map and bins (Which make up 80 bytes) aren't part of the config pages } break; @@ -273,7 +317,7 @@ void receiveValue(int offset, byte newValue) if (offset < 272) { //X Axis - afrTable.axisX[(offset-256)] = int(newValue) * int(100); //The RPM values sent by TunerStudio are divided by 100, need to multiply it back by 100 to make it correct + afrTable.axisX[(offset-256)] = int(newValue) * int(100); //The RPM values sent by megasquirt are divided by 100, need to multiply it back by 100 to make it correct } else { @@ -286,23 +330,22 @@ void receiveValue(int offset, byte newValue) } case afrSetPage: - pnt_configPage = (byte *)&configPage3; + pnt_configPage = &configPage3; //For some reason, TunerStudio is sending offsets greater than the maximum page size. I'm not sure if it's their bug or mine, but the fix is to only update the config page if the offset is less than the maximum size if( offset < page_size) { - *(pnt_configPage + (byte)offset) = newValue; //Need to subtract 80 because the map and bins (Which make up 80 bytes) aren't part of the config pages + *((byte *)pnt_configPage + (byte)offset) = newValue; //Need to subtract 80 because the map and bins (Which make up 80 bytes) aren't part of the config pages } break; case iacPage: //Idle Air Control settings page (Page 4) - pnt_configPage = (byte *)&configPage4; + pnt_configPage = &configPage4; //For some reason, TunerStudio is sending offsets greater than the maximum page size. I'm not sure if it's their bug or mine, but the fix is to only update the config page if the offset is less than the maximum size if( offset < page_size) { - *(pnt_configPage + (byte)offset) = newValue; + *((byte *)pnt_configPage + (byte)offset) = newValue; } break; - case boostvvtPage: //Boost and VVT maps (8x8) if (offset < 64) //New value is part of the boost map { @@ -337,7 +380,6 @@ void receiveValue(int offset, byte newValue) vvtTable.axisY[(7-offset)] = int(newValue); return; } - default: break; } @@ -348,104 +390,186 @@ sendPage() packs the data within the current page (As set with the 'P' command) into a buffer and sends it. Note that some translation of the data is required to lay it out in the way Megasqurit / TunerStudio expect it */ -void sendPage() +void sendPage(bool useChar) { - byte* pnt_configPage; - + void* pnt_configPage; + struct table3D currentTable; + byte currentTitleIndex=0; + switch (currentPage) { case veMapPage: { - //Need to perform a translation of the values[MAP/TPS][RPM] into the MS expected format - //MS format has origin (0,0) in the bottom left corner, we use the top left for efficiency reasons - byte response[map_page_size]; - - for(int x=0;x<256;x++) { response[x] = fuelTable.values[15-x/16][x%16]; } //This is slightly non-intuitive, but essentially just flips the table vertically (IE top line becomes the bottom line etc). Columns are unchanged - for(int x=256;x<272;x++) { response[x] = byte(fuelTable.axisX[(x-256)] / 100); } //RPM Bins for VE table (Need to be dvidied by 100) - for(int y=272;y<288;y++) { response[y] = byte(fuelTable.axisY[15-(y-272)]); } //MAP or TPS bins for VE table - Serial.write((byte *)&response, sizeof(response)); - break; + currentTitleIndex = 0; + currentTable = fuelTable; + break; } case veSetPage: { - //All other bytes can simply be copied from the config table - byte response[page_size]; - - pnt_configPage = (byte *)&configPage1; //Create a pointer to Page 1 in memory - for(byte x=0; x