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,
];
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($("<option />").val(number).text("GPIO " + number));
- break;
- case "pwm":
- container.remove();
- menu.append($("<option />").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($("<option />").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 = $("<form/>", {"class" : "controls", action : "#", id : "gpio-" + number});
var title = $("<div/>", {"class" : "centre bold", text : "GPIO " + number});
var table = $("<table/>", {"class" : "centre"});
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"}})
+ $.ajax({url : mctx.pintest.api, data : {type : "pwm", num : number, export : "1"}})
.fail(function () {
var form = $("<form/>", {"class" : "controls", action : "#", id : "pwm-" + number});
var title = $("<div/>", {"class" : "centre bold", text : "PWM " + number});
.append($("<td/>").append(
$("<input/>", {type : "text", readonly : "", name : "result"})))
.append($("<td/>").append(
- $("<input/>", {type : "checkbox", name : "unexport", value :umber})
- .click(function () {
- unexport("pwm", number, form, menu);
- return false;
- })));
+ $("<input/>", {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();
})
alert("Failed to export PWM " + number + ". Is the server running?\n" +
"Error code: " + jqXHR.status);
});
+ return this;
};
$.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 () {
set.click(function () {
dir.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
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($("<option />").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($("<option />").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) {
+ result.val(data);
+ })
+ .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;
};
/*