Update pintest + fix css
[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   .fail(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   .done(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     if (setOut) {
137       update = false;
138       set.attr('disabled', false);
139       result.empty();
140       dir.val("Out");
141     } else {
142       update = true;
143       set.attr('disabled', true);
144       result.empty();
145       dir.val("In");
146     }
147     dir.attr('disabled', false);
148   });
149   
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) {
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     });
167   });
168   
169   unexport.click(function () {
170     update = false;
171     $.ajax({url : mctx.pintest.api, data : {type : "gpi", num : number, export : -1}})
172     container.remove();
173     menu.append($("<option />").val(number).text("GPIO " + number));
174     return false;
175   });
176   
177   updater();
178   return this;
179 };
180
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']");
189
190   set.click(function () {
191     var freqVal = parseFloat(freq.val());
192     var dutyVal = parseFloat(duty.val());
193     var polVal = pol.is(":checked") ? 1 : 0;
194     
195     result.val("Processing...");
196     if (isNaN(freqVal) || isNaN(dutyVal) || freqVal <= 0 || dutyVal < 0 || dutyVal > 1) {
197       result.val("Invalid input");
198     } else {
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) {
203         result.val(data);
204       })
205     }
206   });
207   
208   unexport.click(function () {
209     $.ajax({url : mctx.pintest.api, data : {type : "pwm", num : number, export : -1}})
210     container.remove();
211     menu.append($("<option />").val(number).text("PWM " + number));
212     return false;    
213   });
214   
215   return this;
216 };
217
218 /**
219  * Given the form containing the ADC control elements, it activates the controls.
220  * @returns {$.fn}
221  */
222 $.fn.setADCControl = function() {
223   var container = this;
224   this.find("input[type='checkbox']").each(function () {
225     var update = false;
226     var number = $(this).attr("name");
227     var result = container.find("input[type='text'][name='" + number + "']");
228     
229     var updater = function () {
230       if (update) {
231          $.ajax({url : mctx.pintest.api, data : {type : "adc", num : number}})
232          .done(function (data) {
233             result.val(data);
234          })
235          .always(function () {
236             setTimeout(updater, mctx.pintest.refreshRate);
237          });
238       } else {
239         setTimeout(updater, mctx.pintest.idleRefreshRate);
240       }
241     };
242     
243     $(this).click(function () {
244       update = !update;
245       result.val("");
246       var exp = update ? 1 : -1;
247        $.ajax({url : mctx.pintest.api, data : {type : "adc", num : number, export : exp}});
248     });
249     updater();
250   });
251   return this;
252 };
253
254 /* 
255  * GPIO template
256           <form class="controls" action="#">
257             <div class="centre bold">GPIO 20</div>
258             
259             <table class="centre">
260               <tr>
261                 <th>Direction</th><th>Set</th><th>Result</th><th>Unexport</th>
262               </tr>
263               <tr>
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>
268               </tr>
269             </table>
270           </form>
271  */
272
273 /*
274  * PWM template
275           <form class="controls" action="#">
276             <table class="centre">
277               <tr>
278                 <th>Frequency (Hz)</th><th>Duty cycle</th>
279                 <th>Polarity</th><th>Set</th>
280                 <th>Result</th><th>Unexport</th>
281               </tr>
282               <tr>
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>
289               </tr>
290             </table>
291           </form>
292  */

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