From fe0cd8b9105bb1e19a48b5a37e3dc5bcfe5e001b Mon Sep 17 00:00:00 2001 From: Richard Cooper Date: Sun, 20 Oct 2019 22:26:52 +0100 Subject: [PATCH 1/3] Save vtx_table lua file. --- locales/en/messages.json | 4 ++ src/js/tabs/vtx.js | 87 ++++++++++++++++++++++++++++++++++++++++ src/tabs/vtx.html | 3 ++ 3 files changed, 94 insertions(+) diff --git a/locales/en/messages.json b/locales/en/messages.json index 911ae93b70..97417728e9 100644 --- a/locales/en/messages.json +++ b/locales/en/messages.json @@ -5225,6 +5225,10 @@ "message": "Save to file", "description": "Save to file button in the VTX tab" }, + "vtxButtonSaveLua": { + "message": "Save Lua Script", + "description": "Save Lua script button in the VTX tab" + }, "vtxButtonLoadFile": { "message": "Load from file", "description": "Load to file button in the VTX tab" diff --git a/src/js/tabs/vtx.js b/src/js/tabs/vtx.js index 929328e02b..3e215ce263 100644 --- a/src/js/tabs/vtx.js +++ b/src/js/tabs/vtx.js @@ -496,6 +496,10 @@ TABS.vtx.initialize = function (callback) { save_json(); }); + $('a.save_lua').click(function () { + save_lua(); + }); + $('a.load_file').click(function () { load_json(); }); @@ -512,6 +516,88 @@ TABS.vtx.initialize = function (callback) { } + function save_lua() { + let suggestedName = 'model01'; + let suffix = 'lua'; + + var filename; + if(CONFIG.name && CONFIG.name.trim() !== '') { + filename = CONFIG.name.trim().replace(' ', '_'); + }else{ + filename = suggestedName + } + filename += '.' + suffix; + + let accepts = [{ + description: suffix.toUpperCase() + ' files', extensions: [suffix], + }]; + + chrome.fileSystem.chooseEntry({type: 'saveFile', suggestedName: filename, accepts: accepts}, function(entry) { + + if (chrome.runtime.lastError) { + console.error(chrome.runtime.lastError.message); + return; + } + + if (!entry) { + console.log('No file selected'); + return; + } + + entry.createWriter(function (writer) { + + writer.onerror = function(){ + console.error('Failed to write VTX table lua file'); + GUI.log(i18n.getMessage('vtxSavedFileKo')); + }; + + writer.onwriteend = function() { + dump_html_to_msp(); + let vtxConfig = createVtxConfigInfo(); + let bands = "bandTable = { [0]=\"U\""; + let frequencies = "frequencyTable = {\n"; + let freqBands = "frequenciesPerBand = "; + let powers = "powerTable = { " + let a = vtxConfig.vtx_table.bands_list; + let b = vtxConfig.vtx_table.powerlevels_list; + var index, len, i , l; + for (index = 0, len = a.length; index < len; ++index) { + bands += ", \"" + a[index].letter + "\""; + frequencies += " { " + for (i = 0, l = a[index].frequencies.length; i < l; ++i) { + frequencies += a[index].frequencies[i] + ", "; + } + frequencies += "},\n" + } + bands += " }\n"; + frequencies += "}\n" + freqBands += a[1].frequencies.length + "\n"; + for (index = 0, len = b.length; index < len; ++index) { + powers += "[" + b[index].value +"]=" + b[index].label + ", "; + } + powers += "}\n" + let text = frequencies + freqBands + bands + powers; + let data = new Blob([text], { type: "application/text" }); + + // we get here at the end of the truncate method, change to the new end + writer.onwriteend = function() { + analytics.sendEvent(analytics.EVENT_CATEGORIES.FLIGHT_CONTROLLER, 'VtxTableLuaSave', text.length); + console.log(vtxConfig) + console.log('Write VTX table lua file end'); + GUI.log(i18n.getMessage('vtxSavedFileOk')); + } + + writer.write(data); + }; + + writer.truncate(0); + + }, function (){ + console.error('Failed to get VTX table lua file writer'); + GUI.log(i18n.getMessage('vtxSavedFileKo')); + }); + }); + } function save_json() { let suggestedName = 'vtxtable'; let suffix = 'json'; @@ -550,6 +636,7 @@ TABS.vtx.initialize = function (callback) { // we get here at the end of the truncate method, change to the new end writer.onwriteend = function() { analytics.sendEvent(analytics.EVENT_CATEGORIES.FLIGHT_CONTROLLER, 'VtxTableSave', text.length); + console.log(vtxConfig) console.log('Write VTX file end'); GUI.log(i18n.getMessage('vtxSavedFileOk')); } diff --git a/src/tabs/vtx.html b/src/tabs/vtx.html index c267a71f2c..9ce55abf73 100644 --- a/src/tabs/vtx.html +++ b/src/tabs/vtx.html @@ -252,6 +252,9 @@
+
+ +
From b05c3105ef6abe2b4cdb033e6d39f29f4604a6a3 Mon Sep 17 00:00:00 2001 From: Richard Cooper Date: Wed, 23 Oct 2019 09:46:29 +0100 Subject: [PATCH 2/3] Refactor table creation to function --- src/js/tabs/vtx.js | 54 +++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/src/js/tabs/vtx.js b/src/js/tabs/vtx.js index 3e215ce263..08501824f7 100644 --- a/src/js/tabs/vtx.js +++ b/src/js/tabs/vtx.js @@ -554,35 +554,12 @@ TABS.vtx.initialize = function (callback) { writer.onwriteend = function() { dump_html_to_msp(); let vtxConfig = createVtxConfigInfo(); - let bands = "bandTable = { [0]=\"U\""; - let frequencies = "frequencyTable = {\n"; - let freqBands = "frequenciesPerBand = "; - let powers = "powerTable = { " - let a = vtxConfig.vtx_table.bands_list; - let b = vtxConfig.vtx_table.powerlevels_list; - var index, len, i , l; - for (index = 0, len = a.length; index < len; ++index) { - bands += ", \"" + a[index].letter + "\""; - frequencies += " { " - for (i = 0, l = a[index].frequencies.length; i < l; ++i) { - frequencies += a[index].frequencies[i] + ", "; - } - frequencies += "},\n" - } - bands += " }\n"; - frequencies += "}\n" - freqBands += a[1].frequencies.length + "\n"; - for (index = 0, len = b.length; index < len; ++index) { - powers += "[" + b[index].value +"]=" + b[index].label + ", "; - } - powers += "}\n" - let text = frequencies + freqBands + bands + powers; + let text = creatLuaTables(vtxConfig); let data = new Blob([text], { type: "application/text" }); - + // we get here at the end of the truncate method, change to the new end writer.onwriteend = function() { analytics.sendEvent(analytics.EVENT_CATEGORIES.FLIGHT_CONTROLLER, 'VtxTableLuaSave', text.length); - console.log(vtxConfig) console.log('Write VTX table lua file end'); GUI.log(i18n.getMessage('vtxSavedFileOk')); } @@ -894,6 +871,33 @@ TABS.vtx.initialize = function (callback) { return vtxConfig; } + function creatLuaTables(vtxConfig) { + let bandsString = "bandTable = { [0]=\"U\""; + let frequencieString = "frequencyTable = {\n"; + let freqBandsString = "frequenciesPerBand = "; + let powersString = "powerTable = { "; + let bands_list = vtxConfig.vtx_table.bands_list; + let power_list = vtxConfig.vtx_table.powerlevels_list; + var index, len, i, l; + for (index = 0, len = bands_list.length; index < len; ++index) { + bandsString += ", \"" + bands_list[index].letter + "\""; + frequencieString += " { "; + for (i = 0, l = bands_list[index].frequencies.length; i < l; ++i) { + frequencieString += bands_list[index].frequencies[i] + ", "; + } + frequencieString += "},\n"; + } + bandsString += " }\n"; + frequencieString += "}\n"; + freqBandsString += bands_list[1].frequencies.length + "\n"; + for (index = 0, len = power_list.length; index < len; ++index) { + powersString += "[" + power_list[index].value + "]=" + power_list[index].label + ", "; + } + powersString += "}\n"; + let text = frequencieString + freqBandsString + bandsString + powersString; + return text; + } + }; TABS.vtx.cleanup = function (callback) { From 6f6f4738c41b95db18d0836d3e67598c96b6fbf0 Mon Sep 17 00:00:00 2001 From: Richard Cooper Date: Mon, 28 Oct 2019 21:11:00 +0000 Subject: [PATCH 3/3] Add tooltip and info for save lua button. --- locales/en/messages.json | 6 +++++- src/tabs/vtx.html | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/locales/en/messages.json b/locales/en/messages.json index 97417728e9..99b06a8521 100644 --- a/locales/en/messages.json +++ b/locales/en/messages.json @@ -4993,7 +4993,7 @@ }, "vtxHelp": { - "message": "Here you can configure the values for your Video Transmitter (VTX). You can view and change the transmission values, including the VTX Tables, if the flight controller and the VTX support it.
To set up your VTX use the following steps:
1. Go to this page;
2. Find the appropriate VTX configuration file for your country and your VTX model and download it;
3. Click '$t(vtxButtonLoadFile.message)' below, select the VTX configuration file, load it;
4. Verify that the settings are correct;
5. Click '$t(vtxButtonSave.message)' to store the VTX settings on the flight controller.", + "message": "Here you can configure the values for your Video Transmitter (VTX). You can view and change the transmission values, including the VTX Tables, if the flight controller and the VTX support it.
To set up your VTX use the following steps:
1. Go to this page;
2. Find the appropriate VTX configuration file for your country and your VTX model and download it;
3. Click '$t(vtxButtonLoadFile.message)' below, select the VTX configuration file, load it;
4. Verify that the settings are correct;
5. Click '$t(vtxButtonSave.message)' to store the VTX settings on the flight controller.
6. Optionally click '$t(vtxButtonSaveLua.message)' to save a lua configuration file you can use with the betaflight lua scripts (See more here.)", "description": "Introduction message in the VTX tab" }, "vtxMessageNotSupported": { @@ -5229,6 +5229,10 @@ "message": "Save Lua Script", "description": "Save Lua script button in the VTX tab" }, + "vtxLuaFileHelp" :{ + "message": "The '$t(vtxButtonSaveLua.message)' button will allow you to save a craftname.lua file containing the vtx table configuration that can be used with the betaflight lua scripts. (See more here.)", + "description": "Tooltip message for the Save Lua script button in the VTX tab" + }, "vtxButtonLoadFile": { "message": "Load from file", "description": "Load to file button in the VTX tab" diff --git a/src/tabs/vtx.html b/src/tabs/vtx.html index 9ce55abf73..0bb29e1f95 100644 --- a/src/tabs/vtx.html +++ b/src/tabs/vtx.html @@ -254,6 +254,7 @@
+