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 return $.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);
65 $.fn.exportPWM = function(menu) {
66 var number = menu.val();
69 return $.ajax({url : mctx.pintest.api, data : {type : "pwm", num : number, export : "1"}})
71 var form = $("<form/>", {"class" : "controls", action : "#", id : "pwm-" + number});
72 var title = $("<div/>", {"class" : "centre bold", text : "PWM " + number});
73 var table = $("<table/>", {"class" : "centre"});
74 var header = $("<tr/>");
75 var controls = $("<tr/>");
77 header.append($("<th/>", {text : "Frequency (Hz)"}))
78 .append($("<th/>", {text : "Duty cycle"}))
79 .append($("<th/>", {text : "Polarity"}))
80 .append($("<th/>", {text : "Set"}))
81 .append($("<th/>", {text : "Result"}))
82 .append($("<th/>", {text : "Unexport"}));
84 controls.append($("<td/>").append(
85 $("<input/>", {type : "text", name : "freq"})))
86 .append($("<td/>").append(
87 $("<input/>", {type : "text", name : "duty"})))
88 .append($("<td/>").append(
89 $("<input/>", {type : "checkbox", name : "pol"})))
90 .append($("<td/>").append(
91 $("<input/>", {type : "button", value: "Go", name : "set"})))
92 .append($("<td/>").append(
93 $("<input/>", {type : "text", readonly : "", name : "result"})))
94 .append($("<td/>").append(
95 $("<input/>", {type : "checkbox", name : "unexport", value :number})));
98 table.append(header).append(controls);
100 form.setPWMControl(number, menu);
101 container.append(form);
102 menu.find("option[value='" + number+"']").remove();
104 .fail(function (jqXHR) {
105 alert("Failed to export PWM " + number + ". Is the server running?\n" +
106 "Error code: " + jqXHR.status);
110 $.fn.setGPIOControl = function (number, menu) {
111 var container = this;
112 var dir = this.find("input[name='dir']");
113 var set = this.find("input[name='set']");
114 var result = this.find("input[name='result']");
115 var unexport = this.find("input[name='unexport']");
117 var updater = function() {
119 $.ajax({url : mctx.pintest.api, data : {type : "gpi", num : number}})
120 .done(function (data) {
123 .always(function () {
124 setTimeout(updater, mctx.pintest.refreshRate);
127 setTimeout(updater, mctx.pintest.idleRefreshRate);
131 dir.click(function () {
132 dir.attr('disabled', true);
133 var setOut = dir.val() === "In";
137 set.attr('disabled', false);
141 set.attr('disabled', true);
144 dir.attr('disabled', false);
147 set.click(function () {
148 dir.attr("disabled", true);
149 set.attr("disabled", true);
150 var val = (set.val() === "Off") ? 1 : 0;
151 $.ajax({url : mctx.pintest.api, data : {type : "gpo", num : number, set : val}})
152 .done(function (data) {
162 .always(function () {
163 dir.attr("disabled", false);
164 set.attr("disabled", false);
168 unexport.click(function () {
170 $.ajax({url : mctx.pintest.api, data : {type : "gpi", num : number, export : -1}})
172 menu.append($("<option />").val(number).text("GPIO " + number));
180 $.fn.setPWMControl = function (number, menu) {
181 var container = this;
182 var freq = this.find("input[name='freq']");
183 var duty = this.find("input[name='duty']");
184 var pol = this.find("input[name='pol']");
185 var set = this.find("input[name='set']");
186 var result = this.find("input[name='result']");
187 var unexport = this.find("input[name='unexport']");
189 set.click(function () {
190 var freqVal = parseFloat(freq.val());
191 var dutyVal = parseFloat(duty.val());
192 var polVal = pol.is(":checked") ? 1 : 0;
194 result.val("Processing...");
195 if (isNaN(freqVal) || isNaN(dutyVal) || freqVal <= 0 || dutyVal < 0 || dutyVal > 1) {
196 result.val("Invalid input");
198 $.ajax({url : mctx.pintest.api,
199 data : {type : "pwm", num : number, freq : freqVal,
200 duty : dutyVal, pol : polVal, set : 1}})
201 .done(function(data) {
207 unexport.click(function () {
208 $.ajax({url : mctx.pintest.api, data : {type : "pwm", num : number, export : -1}})
210 menu.append($("<option />").val(number).text("PWM " + number));
218 * Given the form containing the ADC control elements, it activates the controls.
221 $.fn.setADCControl = function() {
222 var container = this;
223 this.find("input[type='checkbox']").each(function () {
225 var number = $(this).attr("name");
226 var result = container.find("input[type='text'][name='" + number + "']");
228 var updater = function () {
230 $.ajax({url : mctx.pintest.api, data : {type : "adc", num : number}})
231 .done(function (data) {
237 result.val("fail - server not running?");
239 .always(function () {
240 setTimeout(updater, mctx.pintest.refreshRate);
243 setTimeout(updater, mctx.pintest.idleRefreshRate);
247 $(this).click(function () {
250 var exp = update ? 1 : -1;
251 $.ajax({url : mctx.pintest.api, data : {type : "adc", num : number, export : exp}});
260 <form class="controls" action="#">
261 <div class="centre bold">GPIO 20</div>
263 <table class="centre">
265 <th>Direction</th><th>Set</th><th>Result</th><th>Unexport</th>
268 <td><input type="button" value="Out"></td>
269 <td><input type="button" value="On"></td>
270 <td><input type="text" readonly></td>
271 <td><input type="checkbox"></td>
279 <form class="controls" action="#">
280 <table class="centre">
282 <th>Frequency (Hz)</th><th>Duty cycle</th>
283 <th>Polarity</th><th>Set</th>
284 <th>Result</th><th>Unexport</th>
287 <td><input type="text"></td>
288 <td><input type="text"></td>
289 <td><input type="checkbox"></td>
290 <td><input type="button" value="Go"></td>
291 <td><input type="text" readonly></td>
292 <td><input type="checkbox"></td>