diff --git a/data/ui.js b/data/ui.js index 84033fb..aca1a62 100644 --- a/data/ui.js +++ b/data/ui.js @@ -29,9 +29,11 @@ var ui = { // temp variable to store updates from Parameter Database paramUpdates: "", - // Status of visibility of parameter categories. E.g. Motor, Inverter. true = visible, false = not visible. + // Status of visibility of parameter categories. E.g. Motor, Inverter. true = visible, false = not visible. categoryVisible: {}, + canMappingLoaded: false, + navbarIsBig: true, // When true, fetch all parameters including hidden ones (via 'json hidden') @@ -177,7 +179,6 @@ var ui = { ui.updateTables(); plot.generateChart(); ui.parameterDatabaseCheckForUpdates(); - inverter.canMapping(ui.populateExistingCanMappingTable); wifi.populateWiFiTab(); settings.populateSettingsTab(); ui.populateFileList(); @@ -366,6 +367,10 @@ var ui = { } ui.populateVersion(); ui.populateSpotValueDropDown(); + if (!ui.canMappingLoaded && Object.keys(values).length > 0) { + ui.canMappingLoaded = true; + inverter.canMapping(ui.populateExistingCanMappingTable); + } document.getElementById("paramDownload").href = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(params, null, 2)); document.getElementById("spinner-div").style.visibility = "hidden"; diff --git a/esp32-web-interface.ino b/esp32-web-interface.ino index 1e2eabf..91f9889 100644 --- a/esp32-web-interface.ino +++ b/esp32-web-interface.ino @@ -611,8 +611,10 @@ static void handleCanMap() { res = OICan::AddCanMapping(server.arg("edit")); } - if (res == OICan::Ok) - OICan::SendCanMapping(server.client()); + if (res == OICan::Ok) { + if (!OICan::SendCanMapping(server.client())) + server.send(500, "text/plain", "CAN communication error"); + } else if (res == OICan::CommError) server.send(500, "text/plain", "CAN communication error"); else if (res == OICan::UnknownIndex) diff --git a/src/oi_can.cpp b/src/oi_can.cpp index 822b8e1..22a09ee 100644 --- a/src/oi_can.cpp +++ b/src/oi_can.cpp @@ -58,9 +58,9 @@ #define SDO_CMD_LOAD 1 #define SDO_CMD_RESET 2 #define SDO_CMD_DEFAULTS 3 -#define SDO_CMD_CLEAR_CAN 4 #define SDO_CMD_START 4 #define SDO_CMD_STOP 5 +#define SDO_CMD_CLEAR_CAN 6 #define PARAM_FLAG_HIDDEN 1 #define MAX_ERROR_LOG_ENTRIES 100 @@ -404,7 +404,12 @@ bool SendJson(WiFiClient client, bool includeHidden) { return failed < 5; } -void SendCanMapping(WiFiClient client) { +bool SendCanMapping(WiFiClient client) { + if (state != IDLE || updstate != UPD_IDLE) { + DBG_OUTPUT_PORT.printf("SendCanMapping rejected: CAN state=%d update state=%d\r\n", state, updstate); + return false; + } + enum ReqMapStt { START, COBID, DATAPOSLEN, GAINOFS, DONE }; twai_message_t rxframe; @@ -509,6 +514,7 @@ void SendCanMapping(WiFiClient client) { WriteBufferingStream bufferedWifiClient{client, 1000}; serializeJson(doc, bufferedWifiClient); + return true; } SetResult AddCanMapping(String json) { @@ -916,9 +922,9 @@ void Loop() { retries--; - if (recvdResponse || retries < 0) + if (state == IDLE || state == OBTAIN_JSON || retries < 0) updstate = UPD_IDLE; //if request was successful - else + else if (!recvdResponse) requestSdoElement(SDO_INDEX_SERIAL, 0); delay(100);