Pin test page cleanup
[matches/MCTX3420.git] / testing / MCTXWeb / public_html / static / mctx.pintest.js
1 /**
2  * mctx.pintest: Pin test stuff.
3  * Must be included after mctx.gui.js
4  */
5
6
7 mctx.pintest = {};
8 mctx.pintest.api = mctx.api + "pin";
9 mctx.pintest.gpios = [    
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
13  ];
14 mctx.pintest.pwms = [0, 1, 2, 3, 4, 5, 6, 7];
15 mctx.pintest.refreshRate = 750;
16 mctx.pintest.idleRefreshRate = 1500;
17
18 $.fn.populateDropdown = function(items, pretext) {
19   var options = this;
20   $.each(items, function(index, value) {
21     options.append($("<option />").val(value).text(pretext + value));
22   });
23   return this;
24 };
25
26 $.fn.exportGPIO = function(menu) {
27   var number = menu.val();
28   var container = this;
29   
30   $.ajax({url : mctx.pintest.api, data : {type : "gpi", num : number, export : 1}})
31   .done(function () {
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/>");
37     
38     header.append($("<th/>", {text : "Direction"}))
39       .append($("<th/>", {text : "Set"}))
40       .append($("<th/>", {text : "Result"}))
41       .append($("<th/>", {text : "Unexport"}));
42     
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})));
51     
52     form.append(title);
53     table.append(header).append(controls);
54     form.append(table);
55     form.setGPIOControl(number, menu);
56     container.append(form);
57     menu.find("option[value='" + number+"']").remove();
58   })
59   .fail(function (jqXHR) {
60     alert("Failed to export GPIO " + number + ". Is the server running?\n" +
61           "Error code: " + jqXHR.status);
62   });
63   return this;
64 };
65
66 $.fn.exportPWM = function(menu) {
67   var number = menu.val();
68   var container = this;
69  
70   $.ajax({url : mctx.pintest.api, data : {type : "pwm", num : number, export : "1"}})
71   .done(function () {
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/>");
77     
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"}));
84     
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})));
97     
98     form.append(title);
99     table.append(header).append(controls);
100     form.append(table);
101     form.setPWMControl(number, menu);
102     container.append(form);
103     menu.find("option[value='" + number+"']").remove();
104   })
105   .fail(function (jqXHR) {
106     alert("Failed to export PWM " + number + ". Is the server running?\n" +
107           "Error code: " + jqXHR.status);
108   });
109   return this;
110 };
111
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']");
118   var update = true;
119   var updater = function() {
120     if (update) {
121       $.ajax({url : mctx.pintest.api, data : {type : "gpi", num : number}})
122       .done(function (data) {
123         result.val(data);
124       })
125       .always(function () {
126         setTimeout(updater, mctx.pintest.refreshRate);
127       });
128     } else {
129       setTimeout(updater, mctx.pintest.idleRefreshRate);
130     }
131   };
132   
133   dir.click(function () {
134     dir.attr('disabled', true);
135     var setOut = dir.val() === "In";
136     result.val("");
137     if (setOut) {
138       update = false;
139       set.attr('disabled', false);
140       dir.val("Out");
141     } else {
142       update = true;
143       set.attr('disabled', true);
144       dir.val("In");
145     }
146     dir.attr('disabled', false);
147   });
148   
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) {
155       result.val(data);
156       if (val === 0)
157         set.val("Off");
158       else
159         set.val("On");
160     })
161     .fail(function () {
162       result.val("fail");
163     })
164     .always(function () {
165       dir.attr("disabled", false);
166       set.attr("disabled", false);
167     });
168   });
169   
170   unexport.click(function () {
171     update = false;
172     $.ajax({url : mctx.pintest.api, data : {type : "gpi", num : number, export : -1}})
173     container.remove();
174     menu.append($("<option />").val(number).text("GPIO " + number));
175     return false;
176   });
177   
178   updater();
179   return this;
180 };
181
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']");
190
191   set.click(function () {
192     var freqVal = parseFloat(freq.val());
193     var dutyVal = parseFloat(duty.val());
194     var polVal = pol.is(":checked") ? 1 : 0;
195     
196     result.val("Processing...");
197     if (isNaN(freqVal) || isNaN(dutyVal) || freqVal <= 0 || dutyVal < 0 || dutyVal > 1) {
198       result.val("Invalid input");
199     } else {
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) {
204         result.val(data);
205       })
206     }
207   });
208   
209   unexport.click(function () {
210     $.ajax({url : mctx.pintest.api, data : {type : "pwm", num : number, export : -1}})
211     container.remove();
212     menu.append($("<option />").val(number).text("PWM " + number));
213     return false;    
214   });
215   
216   return this;
217 };
218
219 /**
220  * Given the form containing the ADC control elements, it activates the controls.
221  * @returns {$.fn}
222  */
223 $.fn.setADCControl = function() {
224   var container = this;
225   this.find("input[type='checkbox']").each(function () {
226     var update = false;
227     var number = $(this).attr("name");
228     var result = container.find("input[type='text'][name='" + number + "']");
229     
230     var updater = function () {
231       if (update) {
232          $.ajax({url : mctx.pintest.api, data : {type : "adc", num : number}})
233          .done(function (data) {
234             if (update) {
235               result.val(data);
236             }
237          })
238          .fail(function () {
239             result.val("fail - server not running?");
240          })
241          .always(function () {
242             setTimeout(updater, mctx.pintest.refreshRate);
243          });
244       } else {
245         setTimeout(updater, mctx.pintest.idleRefreshRate);
246       }
247     };
248     
249     $(this).click(function () {
250       update = !update;
251       result.val("");
252       var exp = update ? 1 : -1;
253        $.ajax({url : mctx.pintest.api, data : {type : "adc", num : number, export : exp}});
254     });
255     updater();
256   });
257   return this;
258 };
259
260 /* 
261  * GPIO template
262           <form class="controls" action="#">
263             <div class="centre bold">GPIO 20</div>
264             
265             <table class="centre">
266               <tr>
267                 <th>Direction</th><th>Set</th><th>Result</th><th>Unexport</th>
268               </tr>
269               <tr>
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>
274               </tr>
275             </table>
276           </form>
277  */
278
279 /*
280  * PWM template
281           <form class="controls" action="#">
282             <table class="centre">
283               <tr>
284                 <th>Frequency (Hz)</th><th>Duty cycle</th>
285                 <th>Polarity</th><th>Set</th>
286                 <th>Result</th><th>Unexport</th>
287               </tr>
288               <tr>
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>
295               </tr>
296             </table>
297           </form>
298  */

UCC git Repository :: git.ucc.asn.au