Merge branch 'master' of https://github.com/firefields/MCTX3420
[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.gpios = [    
9   4,   5,   8,   9,  10,  11,  14,  15,  26,  27,  30,  31,  44,  45,
10   46,  47,  48,  49,  60,  61,  65,  66,  67,  68,  69,  70,  71,  72,
11   73,  74,  75,  76,  77,  78,  79,  80,  81,  86,  87,  88,  89, 112, 115
12  ];
13 mctx.pintest.pwms = [0, 1, 2, 3, 4, 5, 6, 7];
14 mctx.pintest.refreshRate = 750;
15
16
17 function unexport (type, number, container, menu) {
18   var url = mctx.api + "pin";
19
20   $.ajax({url : url, data : {type : type, num : number, export : -1}})
21   .fail(function () {
22     switch(type) {
23       case "adc" :
24         var text = container.find("input[type='text'][name='" + number + "']");
25         var check = container.find("input[type='checkbox'][name='unexport']");
26         text.empty();
27         check.attr("checked", true);
28         break;
29       case "gpi": case "gpo" :
30           container.remove();
31           menu.append($("<option />").val(number).text("GPIO " + number));
32         break;
33       case "pwm":
34           container.remove();
35           menu.append($("<option />").val(number).text("PWM " + number));
36         break;
37     }
38   })
39   .done(function () {
40     
41   });
42 }
43
44 $.fn.populateDropdown = function(items, pretext) {
45   var options = this;
46   $.each(items, function(index, value) {
47     options.append($("<option />").val(value).text(pretext + value));
48   });
49 };
50
51 $.fn.exportGPIO = function(menu) {
52   var number = menu.val();
53   var url = mctx.api + "pin";
54   var container = this;
55   
56   $.ajax({url : url, data : {type : "gpi", num : number, export : 1}})
57   .fail(function () {
58     var form = $("<form/>", {"class" : "controls", action : "#", id : "gpio-" + number});
59     var title = $("<div/>", {"class" : "centre bold", text : "GPIO " + number});
60     var table = $("<table/>", {"class" : "centre"});
61     var header = $("<tr/>");
62     var controls = $("<tr/>");
63     
64     header.append($("<th/>", {text : "Direction"}))
65       .append($("<th/>", {text : "Set"}))
66       .append($("<th/>", {text : "Result"}))
67       .append($("<th/>", {text : "Unexport"}));
68     
69     controls.append($("<td/>").append(
70               $("<input/>", {type : "button", value : "In", name : "dir"})))
71       .append($("<td/>").append(
72         $("<input/>", {type : "button", value : "Off", name : "set", disabled : true})))
73       .append($("<td/>").append(
74         $("<input/>", {type : "text", readonly : "", name : "result"})))
75       .append($("<td/>").append(
76         $("<input/>", {type : "checkbox", name : "unexport", value : number})));
77     
78     form.append(title);
79     table.append(header).append(controls);
80     form.append(table);
81     form.setGPIOControl(number, menu);
82     container.append(form);
83     menu.find("option[value='" + number+"']").remove();
84   })
85   .done(function (jqXHR) {
86     alert("Failed to export GPIO " + number + ". Is the server running?\n" +
87           "Error code: " + jqXHR.status);
88   });
89 };
90
91 $.fn.exportPWM = function(menu) {
92   var number = menu.val();
93   var url = mctx.api + "pin";
94   var container = this;
95  
96   $.ajax({url : url, data : {type : "pwm", num : number, export : "1"}})
97   .fail(function () {
98     var form = $("<form/>", {"class" : "controls", action : "#", id : "pwm-" + number});
99     var title = $("<div/>", {"class" : "centre bold", text : "PWM " + number});
100     var table = $("<table/>", {"class" : "centre"});
101     var header = $("<tr/>");
102     var controls = $("<tr/>");
103     
104     header.append($("<th/>", {text : "Frequency (Hz)"}))
105       .append($("<th/>", {text : "Duty cycle"}))
106       .append($("<th/>", {text : "Polarity"}))
107       .append($("<th/>", {text : "Set"}))
108       .append($("<th/>", {text : "Result"}))
109       .append($("<th/>", {text : "Unexport"}));
110     
111     controls.append($("<td/>").append(
112               $("<input/>", {type : "text", name : "freq"})))
113       .append($("<td/>").append(
114         $("<input/>", {type : "text", name : "duty"})))
115       .append($("<td/>").append(
116         $("<input/>", {type : "checkbox", name : "pol"})))
117       .append($("<td/>").append(
118         $("<input/>", {type : "button", value: "Go", name : "set"})))
119       .append($("<td/>").append(
120         $("<input/>", {type : "text", readonly : "", name : "result"})))
121       .append($("<td/>").append(
122         $("<input/>", {type : "checkbox", name : "unexport", value :umber})
123         .click(function () {
124           unexport("pwm", number, form, menu);
125           return false;
126         })));
127     
128     form.append(title);
129     table.append(header).append(controls);
130     form.append(table);
131     container.append(form);
132     menu.find("option[value='" + number+"']").remove();
133   })
134   .done(function (jqXHR) {
135     alert("Failed to export PWM " + number + ". Is the server running?\n" +
136           "Error code: " + jqXHR.status);
137   });
138 };
139
140 $.fn.setGPIOControl = function (number, menu) {
141   var container = this;
142   var dir = this.find("input[name='dir']");
143   var set = this.find("input[name='set']");
144   var result = this.find("input[name='result']");
145   var unexport = this.find("input[name='unexport']");
146   var update = true;
147   var updater = function() {
148     if (update) {
149       $.ajax({url : mctx.api + "pin", data : {type : "gpi", num : number}})
150       .done(function (data) {
151         result.val(data);
152       })
153       .fail(function () {
154         update = false;
155         result.val("GPIO retrieve failed.");
156       });
157     }
158     setTimeout(updater, mctx.pintest.refreshRate);
159   };
160   
161   dir.click(function () {
162     dir.attr('disabled', true);
163     var setOut = dir.val() === "In";
164     if (setOut) {
165       update = false;
166       set.attr('disabled', false);
167       result.empty();
168       dir.val("Out");
169     } else {
170       update = true;
171       set.attr('disabled', true);
172       result.empty();
173       dir.val("In");
174     }
175     dir.attr('disabled', false);
176   });
177   
178   set.click(function () {
179     dir.attr("disabled", true);
180     var val = (set.val() === "Off") ? 1 : 0;
181     $.ajax({url : mctx.api + "pin", data : {type : "gpo", num : number, set : val}})
182     .done(function (data) {
183       result.text(data);
184       if (val === 0)
185         set.val("Off");
186       else
187         set.val("On");
188     })
189     .fail(function () {
190       result.val("fail");
191     })
192     .always(function () {
193       dir.attr("disabled", false);
194     });
195   });
196   
197   unexport.click(function () {
198     update = false;
199     $.ajax({url : mctx.api + "pin", data : {type : "gpi", num : number, export : -1}})
200     container.remove();
201     menu.append($("<option />").val(number).text("GPIO " + number));
202     return false;
203   });
204   
205   updater();
206 };
207
208 /* 
209  * GPIO template
210           <form class="controls" action="#">
211             <div class="centre bold">GPIO 20</div>
212             
213             <table class="centre">
214               <tr>
215                 <th>Direction</th><th>Set</th><th>Result</th><th>Unexport</th>
216               </tr>
217               <tr>
218                 <td><input type="button" value="Out"></td>
219                 <td><input type="button" value="On"></td>
220                 <td><input type="text" readonly></td>
221                 <td><input type="checkbox"></td>
222               </tr>
223             </table>
224           </form>
225  */
226
227 /*
228  * PWM template
229           <form class="controls" action="#">
230             <table class="centre">
231               <tr>
232                 <th>Frequency (Hz)</th><th>Duty cycle</th>
233                 <th>Polarity</th><th>Set</th>
234                 <th>Result</th><th>Unexport</th>
235               </tr>
236               <tr>
237                 <td><input type="text"></td>
238                 <td><input type="text"></td>
239                 <td><input type="checkbox"></td>
240                 <td><input type="button" value="Go"></td>
241                 <td><input type="text" readonly></td>
242                 <td><input type="checkbox"></td>
243               </tr>
244             </table>
245           </form>
246  */

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