Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save vtx_table lua file. #1759

Merged
merged 3 commits into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -5225,6 +5225,10 @@
"message": "Save to file",
"description": "Save to file button in the VTX tab"
},
"vtxButtonSaveLua": {
"message": "Save Lua Script",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for this it would be helpful to add a tooltip to this button that provided a short explanation of how users can use this to get their custom VTX table supported in the lua scripts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea... I'll also look at updating the lua readme.

"description": "Save Lua script button in the VTX tab"
},
"vtxButtonLoadFile": {
"message": "Load from file",
"description": "Load to file button in the VTX tab"
Expand Down
91 changes: 91 additions & 0 deletions src/js/tabs/vtx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand All @@ -512,6 +516,65 @@ 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 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('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';
Expand Down Expand Up @@ -550,6 +613,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'));
}
Expand Down Expand Up @@ -807,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) {
Expand Down
3 changes: 3 additions & 0 deletions src/tabs/vtx.html
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@
</div>

<div class="content_toolbar">
<div class="btn save_lua_btn">
<a class="save_lua" id="save_lua_button" href="#" i18n="vtxButtonSaveLua"></a>
</div>
<div class="btn save_file_btn">
<a class="save_file" id="save_file_button" href="#" i18n="vtxButtonSaveFile"></a>
</div>
Expand Down