2 * mctx.pintest: Pin test stuff.
3 * Must be included after mctx.gui.js
8 mctx.pintest.api = mctx.api + "pin";
10 4, 5, 8, 9, 10, 11, 14, 15, 26, 27, 30, 31, 44, 45,
11 46, 47, 48, 49, 60, 61, 65, 66, 67, 68, 69, 70, 71, 72,
12 73, 74, 75, 76, 77, 78, 79, 80, 81, 86, 87, 88, 89, 112, 115
14 mctx.pintest.pwms = [0, 1, 2, 3, 4, 5, 6, 7];
15 mctx.pintest.refreshRate = 750;
16 mctx.pintest.idleRefreshRate = 1500;
18 $.fn.populateDropdown = function(items, pretext) {
20 $.each(items, function(index, value) {
21 options.append($("<option />").val(value).text(pretext + value));
26 $.fn.exportGPIO = function(menu) {
27 var number = menu.val();
30 $.ajax({url : mctx.pintest.api, data : {type : "gpi", num : number, export : 1}})
32 var form = $("<form/>", {"class" : "controls", action : "#", id : "gpio-" + number});
33 var title = $("<div/>", {"class" : "centre bold", text : "GPIO " + number});
34 var table = $("<table/>", {"class" : "centre"});
35 var header = $("<tr/>");
36 var controls = $("<tr/>");
38 header.append($("<th/>", {text : "Direction"}))
39 .append($("<th/>", {text : "Set"}))
40 .append($("<th/>", {text : "Result"}))
41 .append($("<th/>", {text : "Unexport"}));
43 controls.append($("<td/>").append(
44 $("<input/>", {type : "button", value : "In", name : "dir"})))
45 .append($("<td/>").append(
46 $("<input/>", {type : "button", value : "Off", name : "set", disabled : true})))
47 .append($("<td/>").append(
48 $("<input/>", {type : "text", readonly : "", name : "result"})))
49 .append($("<td/>").append(
50 $("<input/>", {type : "checkbox", name : "unexport", value : number})));
53 table.append(header).append(controls);
55 form.setGPIOControl(number, menu);
56 container.append(form);
57 menu.find("option[value='" + number+"']").remove();
59 .fail(function (jqXHR) {
60 alert("Failed to export GPIO " + number + ". Is the server running?\n" +
61 "Error code: " + jqXHR.status);
66 $.fn.exportPWM = function(menu) {
67 var number = menu.val();
70 $.ajax({url : mctx.pintest.api, data : {type : "pwm", num : number, export : "1"}})
72 var form = $("<form/>", {"class" : "controls", action : "#", id : "pwm-" + number});
73 var title = $("<div/>", {"class" : "centre bold", text : "PWM " + number});
74 var table = $("<table/>", {"class" : "centre"});
75 var header = $("<tr/>");
76 var controls = $("<tr/>");
78 header.append($("<th/>", {text : "Frequency (Hz)"}))
79 .append($("<th/>", {text : "Duty cycle"}))
80 .append($("<th/>", {text : "Polarity"}))
81 .append($("<th/>", {text : "Set"}))
82 .append($("<th/>", {text : "Result"}))
83 .append($("<th/>", {text : "Unexport"}));
85 controls.append($("<td/>").append(
86 $("<input/>", {type : "text", name : "freq"})))
87 .append($("<td/>").append(
88 $("<input/>", {type : "text", name : "duty"})))
89 .append($("<td/>").append(
90 $("<input/>", {type : "checkbox", name : "pol"})))
91 .append($("<td/>").append(
92 $("<input/>", {type : "button", value: "Go", name : "set"})))
93 .append($("<td/>").append(
94 $("<input/>", {type : "text", readonly : "", name : "result"})))
95 .append($("<td/>").append(
96 $("<input/>", {type : "checkbox", name : "unexport", value :number})));
99 table.append(header).append(controls);
101 form.setPWMControl(number, menu);
102 container.append(form);
103 menu.find("option[value='" + number+"']").remove();
105 .fail(function (jqXHR) {
106 alert("Failed to export PWM " + number + ". Is the server running?\n" +
107 "Error code: " + jqXHR.status);
112 $.fn.setGPIOControl = function (number, menu) {
113 var container = this;
114 var dir = this.find("input[name='dir']");
115 var set = this.find("input[name='set']");
116 var result = this.find("input[name='result']");
117 var unexport = this.find("input[name='unexport']");
119 var updater = function() {
121 $.ajax({url : mctx.pintest.api, data : {type : "gpi", num : number}})
122 .done(function (data) {
125 .always(function () {
126 setTimeout(updater, mctx.pintest.refreshRate);
129 setTimeout(updater, mctx.pintest.idleRefreshRate);
133 dir.click(function () {
134 dir.attr('disabled', true);
135 var setOut = dir.val() === "In";
139 set.attr('disabled', false);
143 set.attr('disabled', true);
146 dir.attr('disabled', false);
149 set.click(function () {
150 dir.attr("disabled", true);
151 set.attr("disabled", true);
152 var val = (set.val() === "Off") ? 1 : 0;
153 $.ajax({url : mctx.pintest.api, data : {type : "gpo", num : number, set : val}})
154 .done(function (data) {
164 .always(function () {
165 dir.attr("disabled", false);
166 set.attr("disabled", false);
170 unexport.click(function () {
172 $.ajax({url : mctx.pintest.api, data : {type : "gpi", num : number, export : -1}})
174 menu.append($("<option />").val(number).text("GPIO " + number));
182 $.fn.setPWMControl = function (number, menu) {
183 var container = this;
184 var freq = this.find("input[name='freq']");
185 var duty = this.find("input[name='duty']");
186 var pol = this.find("input[name='pol']");
187 var set = this.find("input[name='set']");
188 var result = this.find("input[name='result']");
189 var unexport = this.find("input[name='unexport']");
191 set.click(function () {
192 var freqVal = parseFloat(freq.val());
193 var dutyVal = parseFloat(duty.val());
194 var polVal = pol.is(":checked") ? 1 : 0;
196 result.val("Processing...");
197 if (isNaN(freqVal) || isNaN(dutyVal) || freqVal <= 0 || dutyVal < 0 || dutyVal > 1) {
198 result.val("Invalid input");
200 $.ajax({url : mctx.pintest.api,
201 data : {type : "pwm", num : number, freq : freqVal,
202 duty : dutyVal, pol : polVal, set : 1}})
203 .done(function(data) {
209 unexport.click(function () {
210 $.ajax({url : mctx.pintest.api, data : {type : "pwm", num : number, export : -1}})
212 menu.append($("<option />").val(number).text("PWM " + number));
220 * Given the form containing the ADC control elements, it activates the controls.
223 $.fn.setADCControl = function() {
224 var container = this;
225 this.find("input[type='checkbox']").each(function () {
227 var number = $(this).attr("name");
228 var result = container.find("input[type='text'][name='" + number + "']");
230 var updater = function () {
232 $.ajax({url : mctx.pintest.api, data : {type : "adc", num : number}})
233 .done(function (data) {
239 result.val("fail - server not running?");
241 .always(function () {
242 setTimeout(updater, mctx.pintest.refreshRate);
245 setTimeout(updater, mctx.pintest.idleRefreshRate);
249 $(this).click(function () {
252 var exp = update ? 1 : -1;
253 $.ajax({url : mctx.pintest.api, data : {type : "adc", num : number, export : exp}});
262 <form class="controls" action="#">
263 <div class="centre bold">GPIO 20</div>
265 <table class="centre">
267 <th>Direction</th><th>Set</th><th>Result</th><th>Unexport</th>
270 <td><input type="button" value="Out"></td>
271 <td><input type="button" value="On"></td>
272 <td><input type="text" readonly></td>
273 <td><input type="checkbox"></td>
281 <form class="controls" action="#">
282 <table class="centre">
284 <th>Frequency (Hz)</th><th>Duty cycle</th>
285 <th>Polarity</th><th>Set</th>
286 <th>Result</th><th>Unexport</th>
289 <td><input type="text"></td>
290 <td><input type="text"></td>
291 <td><input type="checkbox"></td>
292 <td><input type="button" value="Go"></td>
293 <td><input type="text" readonly></td>
294 <td><input type="checkbox"></td>