Pre-collect info about options on field init

- When initializing fields use the configuration to determine the format
- Cache the line for each option for fastest string compare / replace
- Add logging levels for debugging
master
Scott Lahteine 10 years ago
parent 32aaadc301
commit 94bebf31ac

@ -48,6 +48,15 @@
<label class="newline">Temp Sensor 2:</label><select name="TEMP_SENSOR_2"></select> <label class="newline">Temp Sensor 2:</label><select name="TEMP_SENSOR_2"></select>
<label class="newline">Bed Temp Sensor:</label><select name="TEMP_SENSOR_BED"></select> <label class="newline">Bed Temp Sensor:</label><select name="TEMP_SENSOR_BED"></select>
<label class="newline">Redundant Sensor:</label>
<input name="TEMP_SENSOR_1_AS_REDUNDANT" type="checkbox" value="1" checked />
<label>Max Diff:</label>
<input name="MAX_REDUNDANT_TEMP_SENSOR_DIFF" type="text" size="3" maxlength="2" />
<label class="newline">Temp Residency Time (s):</label>
<input name="TEMP_RESIDENCY_TIME" type="text" size="3" maxlength="2" />
<h2>Marlin/Configuration.h</h2> <h2>Marlin/Configuration.h</h2>
<pre id="config_text" class="prettyprint linenums"></pre> <pre id="config_text" class="prettyprint linenums"></pre>
<h2>Marlin/Configuration_adv.h</h2> <h2>Marlin/Configuration_adv.h</h2>

@ -34,6 +34,10 @@ String.prototype.zeroPad = function(len) {
return len ? this.prePad(len, '0') : this; return len ? this.prePad(len, '0') : this;
}; };
String.prototype.regEsc = function() {
return this.replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&");
}
/** /**
* selectField.addOptions takes an array or keyed object * selectField.addOptions takes an array or keyed object
*/ */
@ -66,6 +70,7 @@ var configuratorApp = (function(){
// Return this anonymous object as configuratorApp // Return this anonymous object as configuratorApp
return { return {
my_public_var: 4, my_public_var: 4,
logging: 1,
init: function() { init: function() {
self = this; // a 'this' for use when 'this' is something else self = this; // a 'this' for use when 'this' is something else
@ -74,7 +79,7 @@ var configuratorApp = (function(){
var $uploader = $('#file-upload'); var $uploader = $('#file-upload');
var fileUploader = new BinaryFileUploader({ var fileUploader = new BinaryFileUploader({
element: $uploader[0], element: $uploader[0],
onFileLoad: function(file) { console.log(this); self.handleFileLoad(file, $uploader); } onFileLoad: function(file) { self.handleFileLoad(file, $uploader); }
}); });
if (!fileUploader.hasFileUploaderSupport()) alert('Your browser doesn\'t support the file reading API'); if (!fileUploader.hasFileUploaderSupport()) alert('Your browser doesn\'t support the file reading API');
@ -107,6 +112,7 @@ var configuratorApp = (function(){
success: function(txt) { success: function(txt) {
// File contents into the textarea // File contents into the textarea
$config.text(txt); $config.text(txt);
// Get thermistor info too
self.initThermistorsFromText(txt); self.initThermistorsFromText(txt);
}, },
error: errFunc error: errFunc
@ -130,7 +136,7 @@ var configuratorApp = (function(){
initBoardsFromText: function(txt) { initBoardsFromText: function(txt) {
boards_list = {}; boards_list = {};
var r, findDef = new RegExp('[ \\t]*#define[ \\t]+(BOARD_[^ \\t]+)[ \\t]+(\\d+)[ \\t]*(//[ \\t]*)?(.+)?', 'gm'); var r, findDef = new RegExp('[ \\t]*#define[ \\t]+(BOARD_[\\w_]+)[ \\t]+(\\d+)[ \\t]*(//[ \\t]*)?(.+)?', 'gm');
while((r = findDef.exec(txt)) !== null) { while((r = findDef.exec(txt)) !== null) {
boards_list[r[1]] = r[2].prePad(3, '  ') + " — " + r[4].replace(/\).*/, ')'); boards_list[r[1]] = r[2].prePad(3, '  ') + " — " + r[4].replace(/\).*/, ')');
} }
@ -165,7 +171,7 @@ var configuratorApp = (function(){
this.initField('MOTHERBOARD'); this.initField('MOTHERBOARD');
break; break;
default: default:
console.log("Can't parse "+filename); this.log("Can't parse "+filename, 1);
break; break;
} }
}, },
@ -247,6 +253,10 @@ var configuratorApp = (function(){
this.initField('TEMP_SENSOR_2'); this.initField('TEMP_SENSOR_2');
this.initField('TEMP_SENSOR_BED'); this.initField('TEMP_SENSOR_BED');
this.initField('TEMP_SENSOR_1_AS_REDUNDANT');
this.initField('MAX_REDUNDANT_TEMP_SENSOR_DIFF');
this.initField('TEMP_RESIDENCY_TIME');
$('#serial_stepper').jstepper({ $('#serial_stepper').jstepper({
min: 0, min: 0,
@ -266,58 +276,108 @@ var configuratorApp = (function(){
// prettyPrint(); // prettyPrint();
}, },
/**
* initField - make a field responsive and get info
* about its configuration file define
*/
initField: function(name) { initField: function(name) {
var $elm = $('#'+name), isText = $elm.attr('type') == 'text'; var $elm = $('#'+name), elm = $elm[0];
if (elm.configInfo === undefined) {
elm.configInfo = this.getDefineInfo(name);
$elm.on($elm.attr('type') == 'text' ? 'input' : 'change', this.handleChange);
}
this.setFieldFromDefine(name); this.setFieldFromDefine(name);
isText ? $elm.bind('input', this.handleChange) : $elm.change(this.handleChange)
}, },
handleChange: function(e) { handleChange: function(e) {
self.updateDefineForField(e.target); self.updateDefineForField(e.target.id);
}, },
handleSwitch: function(e) { handleSwitch: function(e) {
var $elm = $(e.target), $prev = $elm.prev(); var $elm = $(e.target), $prev = $elm.prev();
var on = $elm.prop('checked') || false; var on = $elm.prop('checked') || false;
$prev.attr('disabled', !on); $prev.attr('disabled', !on);
self.setDefineEnabled($prev[0], on); self.setDefineEnabled($prev[0].id, on);
}, },
setDefineEnabled: function(elm, val) { setDefineEnabled: function(name, val) {
var $elm = $(elm); this.log('setDefineEnabled:'+name,3);
// console.log("Enable: " + elm.id + " = " + val); var $elm = $('#'+name), elm = $elm[0], inf = elm.configInfo;
var $c = $config; // for now
var txt = $c.text();
var txt = $config.text(); var slash = val ? '' : '// ';
var newline = inf.line
.replace(/^([ \t]*)(\/\/)([ \t]*)/, '$1$3') // remove slashes
.replace('#define', slash + '#define'); // add them back
var findDef = new RegExp('^[ \\t]*(//[ \\t]*)?(#define[ \\t]+' + elm.id + '([ \\t].*)?)$', 'm'); txt = txt.replace(inf.line, newline);
txt = txt.replace(findDef, val ? '$2': '//$2'); inf.line = newline;
this.log(newline, 2);
// Now set the text in the field $c.text(txt);
$config.text(txt);
}, },
updateDefineForField: function(elm) { defineIsEnabled: function(name, adv) {
var $elm = $(elm), this.log('defineIsEnabled:'+name,4);
isCheck = $elm.attr('type') == 'checkbox', var $elm = $('#'+name), elm = $elm[0];
val = isCheck ? $elm.prop('checked') : $elm.val(); var $c = adv ? $config_adv : $config;
// console.log("Set: " + elm.id + " = " + val); var result = elm.configInfo.regex.exec($c.text());
this.log(result,2);
var txt = $config.text(); var on = result !== null ? result[1].trim() != '//' : true;
this.log(name + ' = ' + on, 4);
if (isCheck) { return on;
var findDef = new RegExp('^([ \\t]*)(//[ \\t]*)?(#define[ \\t]+' + elm.id + '([ \\t].*)?)$', 'm'); },
txt = txt.replace(findDef, val ? '$1$3': '$1//$3');
} updateDefineForField: function(name, adv) {
else { this.log('updateDefineForField:'+name,4);
// Match the define name, 1 = "#define name ", 3=value, 4=comment var $elm = $('#'+name), elm = $elm[0], inf = elm.configInfo;
var findDef = new RegExp('^([ \\t]*(//)?[ \\t]*#define[ \\t]+' + elm.id + '[ \\t]+)(.*)([ \\t]*(//)?.*)$', 'm'); var $c = adv ? $config_adv : $config;
if ($elm.hasClass('quote')) val = '"' + val + '"' var txt = $c.text();
txt = txt.replace(findDef, '$1!!REGEXP!!$4').replace('!!REGEXP!!', val);
// var result = inf.repl.exec(txt);
// this.log(result, 2);
var isCheck = $elm.attr('type') == 'checkbox',
val = isCheck ? $elm.prop('checked') : $elm.val();
var newline;
switch(inf.type) {
case 'switch':
var slash = val ? '' : '//';
newline = (inf.pre + slash + inf.define + inf.post);
break;
case 'quoted':
if (isCheck) {
this.log(name + ' should not be a checkbox', 1);
var slash = val ? '' : '//';
newline = (inf.pre + slash + inf.define + '"'+val+'"' + inf.post);
}
else {
newline = inf.pre + inf.define + '"'+val+'"' + inf.post;
}
break;
case 'plain':
if (isCheck) {
this.log(name + ' should not be a checkbox', 1);
var slash = val ? '' : '//';
newline = (inf.pre + slash + inf.define + val + inf.post);
}
else {
newline = inf.pre + inf.define + val + inf.post;
}
break;
} }
// Now set the text in the field
$config.text(txt); txt = txt.replace(inf.line, newline);
inf.line = newline;
this.log(newline, 2);
$c.text(txt);
}, },
setFieldFromDefine: function(name, adv) { setFieldFromDefine: function(name, adv) {
@ -325,6 +385,8 @@ var configuratorApp = (function(){
var isCheck = $elm.attr('type') == 'checkbox'; var isCheck = $elm.attr('type') == 'checkbox';
var val = this.defineValue(name, adv); var val = this.defineValue(name, adv);
this.log('setFieldFromDefine:' + name + ' to ' + val, 4);
isCheck ? $elm.prop('checked', val) : $elm.val("" + val); isCheck ? $elm.prop('checked', val) : $elm.val("" + val);
// If the item has a checkbox then set enabled state too // If the item has a checkbox then set enabled state too
@ -336,40 +398,88 @@ var configuratorApp = (function(){
} }
}, },
defineValue: function(name, adv) { getDefineInfo: function(name, adv) {
this.log('getDefineInfo:'+name,4);
var $elm = $('#'+name), elm = $elm[0]; var $elm = $('#'+name), elm = $elm[0];
var $c = adv ? $config_adv : $config; var $c = adv ? $config_adv : $config;
if ($elm.attr('type') == 'checkbox') {
// maybe spaces, maybe comment, #define, spaces, name, maybe a comment // a switch line with no value
var findDef = new RegExp('^[ \\t]*(//[ \\t]*)?(#define[ \\t]+' + elm.id + '([ \\t]*(//)?.*)?)$', 'm'); var findDef = new RegExp('^(.*//)?(.*#define[ \\t]+' + elm.id + ')([ \\t]*/[*/].*)?$', 'm');
var result = findDef.exec($c.text()); var result = findDef.exec($c.text());
return (result ? result[1] == undefined || result[1].trim() != '//' : false); if (result !== null) {
var info = {
type: 'switch',
line: result[0], // whole line
pre: result[1] === undefined ? '' : result[1].replace('//',''),
define: result[2],
post: result[3] === undefined ? '' : result[3]
};
info.regex = new RegExp('(.*//)?(.*' + info.define.regEsc() + info.post.regEsc() + ')', 'm');
info.repl = info.regex;
this.log(info,2);
return info;
} }
else {
// maybe spaces, maybe comment, #define, spaces, name, one or more spaces, value, maybe a comment // a define with quotes
var findDef = new RegExp('^([ \\t]*(//[ \\t]*)?#define[ \\t]+' + elm.id + '[ \\t]+)(.*)([ \\t]*(//)?.*)$', 'm'); findDef = new RegExp('^(.*//)?(.*#define[ \\t]+' + elm.id + '[ \\t]+)("[^"]*")([ \\t]*/[*/].*)?$', 'm');
var result = findDef.exec($c.text()); result = findDef.exec($c.text());
if (result !== null) { if (result !== null) {
var val = result[3]; var info = {
if (val.search(/^".*"$/) >= 0) $elm.addClass('quote'); type: 'quoted',
if ($elm.hasClass('quote')) val = val.replace(/^"(.*)"$/, '$1'); line: result[0],
return val; pre: result[1] === undefined ? '' : result[1].replace('//',''),
} define: result[2],
return 'fail'; post: result[4] === undefined ? '' : result[4]
};
info.regex = new RegExp('(.*//)?.*' + info.define.regEsc() + '"([^"]*)"' + info.post.regEsc(), 'm');
info.repl = new RegExp('((.*//)?.*' + info.define.regEsc() + '")[^"]*("' + info.post.regEsc() + ')', 'm');
this.log(info,2);
return info;
}
// a define with no quotes
findDef = new RegExp('^(.*//)?(.*#define[ \\t]+' + elm.id + '[ \\t]+)(\\S*)([ \\t]*/[*/].*)?$', 'm');
result = findDef.exec($c.text());
if (result !== null) {
var info = {
type: 'plain',
line: result[0],
pre: result[1] === undefined ? '' : result[1].replace('//',''),
define: result[2],
post: result[4] === undefined ? '' : result[4]
};
info.regex = new RegExp('(.*//)?.*' + info.define.regEsc() + '(\\S*)' + info.post.regEsc(), 'm');
info.repl = new RegExp('((.*//)?.*' + info.define.regEsc() + ')\\S*(' + info.post.regEsc() + ')', 'm');
this.log(info,2);
return info;
} }
return null;
}, },
defineIsEnabled: function(name, adv) { defineValue: function(name, adv) {
var $elm = $('#'+name); this.log('defineValue:'+name,4);
var $elm = $('#'+name), elm = $elm[0];
var $c = adv ? $config_adv : $config; var $c = adv ? $config_adv : $config;
var findDef = new RegExp('^[ \\t]*(//[ \\t]*)?(#define[ \\t]+' + name + '([ \\t]*(//)?.*)?)$', 'm'); var inf = elm.configInfo;
var result = findDef.exec($c.text());
return (result ? result[1].trim() != '//' : false); var result = inf.regex.exec($c.text());
this.log(result,2);
switch(inf.type) {
case 'switch': return result[1] != '//';
case 'quoted': return result[2];
case 'plain': return result[2];
}
},
log: function(o,l) {
if (l === undefined) l = 0;
if (this.logging>=l*1) console.log(o);
}, },
logOnce: function(o) { logOnce: function(o) {
if (typeof o.didLogThisObject === 'undefined') { if (typeof o.didLogThisObject === 'undefined') {
console.log(o); this.log(o);
o.didLogThisObject = true; o.didLogThisObject = true;
} }
}, },

Loading…
Cancel
Save