X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=testing%2FMCTXWeb%2Fpublic_html%2Fstatic%2Fmctx.pintest.js;h=d2ea59e3542bfd31d7d6847a511ac00988dbc648;hb=989a1d2ff5f5afc20f78738f57cc829ae9d11e99;hp=7a792e82cee1a05bed02b7bcea2630e49002c6d3;hpb=9bdafbf8ef5a813c08161cb0a8b386f7c0e778e0;p=matches%2FMCTX3420.git
diff --git a/testing/MCTXWeb/public_html/static/mctx.pintest.js b/testing/MCTXWeb/public_html/static/mctx.pintest.js
index 7a792e8..d2ea59e 100644
--- a/testing/MCTXWeb/public_html/static/mctx.pintest.js
+++ b/testing/MCTXWeb/public_html/static/mctx.pintest.js
@@ -5,6 +5,7 @@
mctx.pintest = {};
+mctx.pintest.api = mctx.api + "pin";
mctx.pintest.gpios = [
4, 5, 8, 9, 10, 11, 14, 15, 26, 27, 30, 31, 44, 45,
46, 47, 48, 49, 60, 61, 65, 66, 67, 68, 69, 70, 71, 72,
@@ -12,49 +13,22 @@ mctx.pintest.gpios = [
];
mctx.pintest.pwms = [0, 1, 2, 3, 4, 5, 6, 7];
mctx.pintest.refreshRate = 750;
-
-
-function unexport (type, number, container, menu) {
- var url = mctx.api + "pin";
-
- $.ajax({url : url, data : {type : type, num : number, export : -1}})
- .fail(function () {
- switch(type) {
- case "adc" :
- var text = container.find("input[type='text'][name='" + number + "']");
- var check = container.find("input[type='checkbox'][name='unexport']");
- text.empty();
- check.attr("checked", true);
- break;
- case "gpi": case "gpo" :
- container.remove();
- menu.append($("").val(number).text("GPIO " + number));
- break;
- case "pwm":
- container.remove();
- menu.append($("").val(number).text("PWM " + number));
- break;
- }
- })
- .done(function () {
-
- });
-}
+mctx.pintest.idleRefreshRate = 1500;
$.fn.populateDropdown = function(items, pretext) {
var options = this;
$.each(items, function(index, value) {
options.append($("").val(value).text(pretext + value));
});
+ return this;
};
$.fn.exportGPIO = function(menu) {
var number = menu.val();
- var url = mctx.api + "pin";
var container = this;
- $.ajax({url : url, data : {type : "gpi", num : number, export : 1}})
- .fail(function () {
+ $.ajax({url : mctx.pintest.api, data : {type : "gpi", num : number, export : 1}})
+ .done(function () {
var form = $("
", {"class" : "controls", action : "#", id : "gpio-" + number});
var title = $("", {"class" : "centre bold", text : "GPIO " + number});
var table = $("", {"class" : "centre"});
@@ -82,19 +56,19 @@ $.fn.exportGPIO = function(menu) {
container.append(form);
menu.find("option[value='" + number+"']").remove();
})
- .done(function (jqXHR) {
+ .fail(function (jqXHR) {
alert("Failed to export GPIO " + number + ". Is the server running?\n" +
"Error code: " + jqXHR.status);
});
+ return this;
};
$.fn.exportPWM = function(menu) {
var number = menu.val();
- var url = mctx.api + "pin";
var container = this;
- $.ajax({url : url, data : {type : "pwm", num : number, export : "1"}})
- .fail(function () {
+ $.ajax({url : mctx.pintest.api, data : {type : "pwm", num : number, export : "1"}})
+ .done(function () {
var form = $("", {"class" : "controls", action : "#", id : "pwm-" + number});
var title = $("", {"class" : "centre bold", text : "PWM " + number});
var table = $("", {"class" : "centre"});
@@ -119,22 +93,20 @@ $.fn.exportPWM = function(menu) {
.append($(" | ").append(
$("", {type : "text", readonly : "", name : "result"})))
.append($(" | ").append(
- $("", {type : "checkbox", name : "unexport", value :umber})
- .click(function () {
- unexport("pwm", number, form, menu);
- return false;
- })));
+ $("", {type : "checkbox", name : "unexport", value :number})));
form.append(title);
table.append(header).append(controls);
form.append(table);
+ form.setPWMControl(number, menu);
container.append(form);
menu.find("option[value='" + number+"']").remove();
})
- .done(function (jqXHR) {
+ .fail(function (jqXHR) {
alert("Failed to export PWM " + number + ". Is the server running?\n" +
"Error code: " + jqXHR.status);
});
+ return this;
};
$.fn.setGPIOControl = function (number, menu) {
@@ -146,30 +118,29 @@ $.fn.setGPIOControl = function (number, menu) {
var update = true;
var updater = function() {
if (update) {
- $.ajax({url : mctx.api + "pin", data : {type : "gpi", num : number}})
+ $.ajax({url : mctx.pintest.api, data : {type : "gpi", num : number}})
.done(function (data) {
result.val(data);
})
- .fail(function () {
- update = false;
- result.val("GPIO retrieve failed.");
+ .always(function () {
+ setTimeout(updater, mctx.pintest.refreshRate);
});
+ } else {
+ setTimeout(updater, mctx.pintest.idleRefreshRate);
}
- setTimeout(updater, mctx.pintest.refreshRate);
};
dir.click(function () {
dir.attr('disabled', true);
var setOut = dir.val() === "In";
+ result.val("");
if (setOut) {
update = false;
set.attr('disabled', false);
- result.empty();
dir.val("Out");
} else {
update = true;
set.attr('disabled', true);
- result.empty();
dir.val("In");
}
dir.attr('disabled', false);
@@ -177,10 +148,11 @@ $.fn.setGPIOControl = function (number, menu) {
set.click(function () {
dir.attr("disabled", true);
+ set.attr("disabled", true);
var val = (set.val() === "Off") ? 1 : 0;
- $.ajax({url : mctx.api + "pin", data : {type : "gpo", num : number, set : val}})
+ $.ajax({url : mctx.pintest.api, data : {type : "gpo", num : number, set : val}})
.done(function (data) {
- result.text(data);
+ result.val(data);
if (val === 0)
set.val("Off");
else
@@ -191,18 +163,98 @@ $.fn.setGPIOControl = function (number, menu) {
})
.always(function () {
dir.attr("disabled", false);
+ set.attr("disabled", false);
});
});
unexport.click(function () {
update = false;
- $.ajax({url : mctx.api + "pin", data : {type : "gpi", num : number, export : -1}})
+ $.ajax({url : mctx.pintest.api, data : {type : "gpi", num : number, export : -1}})
container.remove();
menu.append($("").val(number).text("GPIO " + number));
return false;
});
updater();
+ return this;
+};
+
+$.fn.setPWMControl = function (number, menu) {
+ var container = this;
+ var freq = this.find("input[name='freq']");
+ var duty = this.find("input[name='duty']");
+ var pol = this.find("input[name='pol']");
+ var set = this.find("input[name='set']");
+ var result = this.find("input[name='result']");
+ var unexport = this.find("input[name='unexport']");
+
+ set.click(function () {
+ var freqVal = parseFloat(freq.val());
+ var dutyVal = parseFloat(duty.val());
+ var polVal = pol.is(":checked") ? 1 : 0;
+
+ result.val("Processing...");
+ if (isNaN(freqVal) || isNaN(dutyVal) || freqVal <= 0 || dutyVal < 0 || dutyVal > 1) {
+ result.val("Invalid input");
+ } else {
+ $.ajax({url : mctx.pintest.api,
+ data : {type : "pwm", num : number, freq : freqVal,
+ duty : dutyVal, pol : polVal, set : 1}})
+ .done(function(data) {
+ result.val(data);
+ })
+ }
+ });
+
+ unexport.click(function () {
+ $.ajax({url : mctx.pintest.api, data : {type : "pwm", num : number, export : -1}})
+ container.remove();
+ menu.append($("").val(number).text("PWM " + number));
+ return false;
+ });
+
+ return this;
+};
+
+/**
+ * Given the form containing the ADC control elements, it activates the controls.
+ * @returns {$.fn}
+ */
+$.fn.setADCControl = function() {
+ var container = this;
+ this.find("input[type='checkbox']").each(function () {
+ var update = false;
+ var number = $(this).attr("name");
+ var result = container.find("input[type='text'][name='" + number + "']");
+
+ var updater = function () {
+ if (update) {
+ $.ajax({url : mctx.pintest.api, data : {type : "adc", num : number}})
+ .done(function (data) {
+ if (update) {
+ result.val(data);
+ }
+ })
+ .fail(function () {
+ result.val("fail - server not running?");
+ })
+ .always(function () {
+ setTimeout(updater, mctx.pintest.refreshRate);
+ });
+ } else {
+ setTimeout(updater, mctx.pintest.idleRefreshRate);
+ }
+ };
+
+ $(this).click(function () {
+ update = !update;
+ result.val("");
+ var exp = update ? 1 : -1;
+ $.ajax({url : mctx.pintest.api, data : {type : "adc", num : number, export : exp}});
+ });
+ updater();
+ });
+ return this;
};
/*