Add in change password
[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   return $.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 };
64
65 $.fn.exportPWM = function(menu) {
66   var number = menu.val();
67   var container = this;
68  
69   return $.ajax({url : mctx.pintest.api, data : {type : "pwm", num : number, export : "1"}})
70   .done(function () {
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/>");
76     
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"}));
83     
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})));
96     
97     form.append(title);
98     table.append(header).append(controls);
99     form.append(table);
100     form.setPWMControl(number, menu);
101     container.append(form);
102     menu.find("option[value='" + number+"']").remove();
103   })
104   .fail(function (jqXHR) {
105     alert("Failed to export PWM " + number + ". Is the server running?\n" +
106           "Error code: " + jqXHR.status);
107   });
108 };
109
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']");
116   var update = true;
117   var updater = function() {
118     if (update) {
119       $.ajax({url : mctx.pintest.api, data : {type : "gpi", num : number}})
120       .done(function (data) {
121         result.val(data);
122       })
123       .always(function () {
124         setTimeout(updater, mctx.pintest.refreshRate);
125       });
126     } else {
127       setTimeout(updater, mctx.pintest.idleRefreshRate);
128     }
129   };
130   
131   dir.click(function () {
132     dir.attr('disabled', true);
133     var setOut = dir.val() === "In";
134     result.val("");
135     if (setOut) {
136       update = false;
137       set.attr('disabled', false);
138       dir.val("Out");
139     } else {
140       update = true;
141       set.attr('disabled', true);
142       dir.val("In");
143     }
144     dir.attr('disabled', false);
145   });
146   
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) {
153       result.val(data);
154       if (val === 0)
155         set.val("Off");
156       else
157         set.val("On");
158     })
159     .fail(function () {
160       result.val("fail");
161     })
162     .always(function () {
163       dir.attr("disabled", false);
164       set.attr("disabled", false);
165     });
166   });
167   
168   unexport.click(function () {
169     update = false;
170     $.ajax({url : mctx.pintest.api, data : {type : "gpi", num : number, export : -1}})
171     container.remove();
172     menu.append($("<option />").val(number).text("GPIO " + number));
173     return false;
174   });
175   
176   updater();
177   return this;
178 };
179
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']");
188
189   set.click(function () {
190     var freqVal = parseFloat(freq.val());
191     var dutyVal = parseFloat(duty.val());
192     var polVal = pol.is(":checked") ? 1 : 0;
193     
194     result.val("Processing...");
195     if (isNaN(freqVal) || isNaN(dutyVal) || freqVal <= 0 || dutyVal < 0 || dutyVal > 1) {
196       result.val("Invalid input");
197     } else {
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) {
202         result.val(data);
203       })
204     }
205   });
206   
207   unexport.click(function () {
208     $.ajax({url : mctx.pintest.api, data : {type : "pwm", num : number, export : -1}})
209     container.remove();
210     menu.append($("<option />").val(number).text("PWM " + number));
211     return false;    
212   });
213   
214   return this;
215 };
216
217 /**
218  * Given the form containing the ADC control elements, it activates the controls.
219  * @returns {$.fn}
220  */
221 $.fn.setADCControl = function() {
222   var container = this;
223   this.find("input[type='checkbox']").each(function () {
224     var update = false;
225     var number = $(this).attr("name");
226     var result = container.find("input[type='text'][name='" + number + "']");
227     
228     var updater = function () {
229       if (update) {
230          $.ajax({url : mctx.pintest.api, data : {type : "adc", num : number}})
231          .done(function (data) {
232             if (update) {
233               result.val(data);
234             }
235          })
236          .fail(function () {
237             result.val("fail - server not running?");
238          })
239          .always(function () {
240             setTimeout(updater, mctx.pintest.refreshRate);
241          });
242       } else {
243         setTimeout(updater, mctx.pintest.idleRefreshRate);
244       }
245     };
246     
247     $(this).click(function () {
248       update = !update;
249       result.val("");
250       var exp = update ? 1 : -1;
251        $.ajax({url : mctx.pintest.api, data : {type : "adc", num : number, export : exp}});
252     });
253     updater();
254   });
255   return this;
256 };
257
258 /* 
259  * GPIO template
260           <form class="controls" action="#">
261             <div class="centre bold">GPIO 20</div>
262             
263             <table class="centre">
264               <tr>
265                 <th>Direction</th><th>Set</th><th>Result</th><th>Unexport</th>
266               </tr>
267               <tr>
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>
272               </tr>
273             </table>
274           </form>
275  */
276
277 /*
278  * PWM template
279           <form class="controls" action="#">
280             <table class="centre">
281               <tr>
282                 <th>Frequency (Hz)</th><th>Duty cycle</th>
283                 <th>Polarity</th><th>Set</th>
284                 <th>Result</th><th>Unexport</th>
285               </tr>
286               <tr>
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>
293               </tr>
294             </table>
295           </form>
296  */

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