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";
138 set.attr('disabled', false);
143 set.attr('disabled', true);
147 dir.attr('disabled', false);
150 set.click(function () {
151 dir.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);
169 unexport.click(function () {
171 $.ajax({url : mctx.pintest.api, data : {type : "gpi", num : number, export : -1}})
173 menu.append($("<option />").val(number).text("GPIO " + number));
181 $.fn.setPWMControl = function (number, menu) {
182 var container = this;
183 var freq = this.find("input[name='freq']");
184 var duty = this.find("input[name='duty']");
185 var pol = this.find("input[name='pol']");
186 var set = this.find("input[name='set']");
187 var result = this.find("input[name='result']");
188 var unexport = this.find("input[name='unexport']");
190 set.click(function () {
191 var freqVal = parseFloat(freq.val());
192 var dutyVal = parseFloat(duty.val());
193 var polVal = pol.is(":checked") ? 1 : 0;
195 result.val("Processing...");
196 if (isNaN(freqVal) || isNaN(dutyVal) || freqVal <= 0 || dutyVal < 0 || dutyVal > 1) {
197 result.val("Invalid input");
199 $.ajax({url : mctx.pintest.api,
200 data : {type : "pwm", num : number, freq : freqVal,
201 duty : dutyVal, pol : polVal, set : 1}})
202 .done(function(data) {
208 unexport.click(function () {
209 $.ajax({url : mctx.pintest.api, data : {type : "pwm", num : number, export : -1}})
211 menu.append($("<option />").val(number).text("PWM " + number));
219 * Given the form containing the ADC control elements, it activates the controls.
222 $.fn.setADCControl = function() {
223 var container = this;
224 this.find("input[type='checkbox']").each(function () {
226 var number = $(this).attr("name");
227 var result = container.find("input[type='text'][name='" + number + "']");
229 var updater = function () {
231 $.ajax({url : mctx.pintest.api, data : {type : "adc", num : number}})
232 .done(function (data) {
235 .always(function () {
236 setTimeout(updater, mctx.pintest.refreshRate);
239 setTimeout(updater, mctx.pintest.idleRefreshRate);
243 $(this).click(function () {
246 var exp = update ? 1 : -1;
247 $.ajax({url : mctx.pintest.api, data : {type : "adc", num : number, export : exp}});
256 <form class="controls" action="#">
257 <div class="centre bold">GPIO 20</div>
259 <table class="centre">
261 <th>Direction</th><th>Set</th><th>Result</th><th>Unexport</th>
264 <td><input type="button" value="Out"></td>
265 <td><input type="button" value="On"></td>
266 <td><input type="text" readonly></td>
267 <td><input type="checkbox"></td>
275 <form class="controls" action="#">
276 <table class="centre">
278 <th>Frequency (Hz)</th><th>Duty cycle</th>
279 <th>Polarity</th><th>Set</th>
280 <th>Result</th><th>Unexport</th>
283 <td><input type="text"></td>
284 <td><input type="text"></td>
285 <td><input type="checkbox"></td>
286 <td><input type="button" value="Go"></td>
287 <td><input type="text" readonly></td>
288 <td><input type="checkbox"></td>