1
0

main.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. eel.expose(renderUI);
  2. eel.expose(error)
  3. eel.expose(warning)
  4. // Helper functions
  5. function range(n){
  6. return [...Array(n).keys()];
  7. }
  8. function onload(){ //Bind events
  9. // Changing tabs
  10. tabs = document.querySelector('#tabs').children
  11. for (var i = 0; i < tabs.length; i++){
  12. tabs[i].addEventListener("click", function(event){
  13. eel.set_selected_board(event.srcElement.getAttribute("i"))
  14. })
  15. }
  16. // Box information
  17. document.querySelector('#piFreq').addEventListener("focusout", function(event){
  18. eel.set_pi_freq(parseFloat(event.srcElement.value))
  19. })
  20. document.querySelector('#remoteTrigg').addEventListener("click", function(event){
  21. eel.set_remote_trigg(event.srcElement.checked ? 1 : 0)
  22. })
  23. // Learning options
  24. document.querySelector('#enabGnd').addEventListener("click", function(event){
  25. eel.set_enab_gnd(event.srcElement.checked ? 1 : 0)
  26. })
  27. document.querySelector('#Vlearn').addEventListener("focusout", function(event){
  28. eel.set_Vlearn(parseInt(event.srcElement.value))
  29. })
  30. document.querySelector('#start').addEventListener("focusout", function(event){
  31. eel.set_start(parseInt(event.srcElement.value))
  32. })
  33. document.querySelector('#stop').addEventListener("focusout", function(event){
  34. eel.set_stop(parseInt(event.srcElement.value))
  35. })
  36. document.querySelector('#startGnd').addEventListener("focusout", function(event){
  37. eel.set_start_gnd(parseInt(event.srcElement.value))
  38. })
  39. document.querySelector('#stopGnd').addEventListener("focusout", function(event){
  40. eel.set_stop_gnd(parseInt(event.srcElement.value))
  41. })
  42. document.querySelector('#N').addEventListener("focusout", function(event){
  43. eel.set_N(parseInt(event.srcElement.value))
  44. })
  45. document.querySelector('#wfLen').addEventListener("focusout", function(event){
  46. eel.set_wf_len(parseInt(event.srcElement.value))
  47. })
  48. // Locking options
  49. document.querySelector('#stepMax').addEventListener("focusout", function(event){
  50. eel.set_max_step(parseInt(event.srcElement.value))
  51. })
  52. document.querySelector('#Gain').addEventListener("focusout", function(event){
  53. eel.set_Gain(parseFloat(event.srcElement.value))
  54. })
  55. document.querySelector('#calibrateGain').addEventListener("click", function(event){
  56. eel.calibrate_gain()
  57. })
  58. // Input options
  59. document.querySelector('#autoSetPga').addEventListener("click", function(event){
  60. eel.set_auto_set_pga(event.srcElement.checked ? 1 : 0)
  61. })
  62. document.querySelector('#inputGain').addEventListener("focusout", function(event){
  63. eel.set_input_gain(parseInt(event.srcElement.value))
  64. })
  65. document.querySelector('#offset').addEventListener("focusout", function(event){
  66. eel.set_offset(parseInt(event.srcElement.value))
  67. })
  68. // Output options
  69. document.querySelector('#coarseFineRatio').addEventListener("focusout", function(event){
  70. eel.set_coarse_fine_ratio(parseFloat(event.srcElement.value))
  71. })
  72. document.querySelector('#calibrateCoarseFineRatio').addEventListener("click", function(event){
  73. eel.calibrate_coarse_fine_ratio()
  74. })
  75. // Analysis tools
  76. document.querySelector('#measureResponseFunction').addEventListener("click", function(event){
  77. eel.measure_response_function()
  78. })
  79. document.querySelector('#sdevTime').addEventListener("click", function(event){
  80. eel.sdev_time()
  81. })
  82. document.querySelector('#noisePowerSpectrum').addEventListener("click", function(event){
  83. eel.noise_power_spectrum()
  84. })
  85. // Graphs
  86. document.getElementById("waveformGraphSelect").addEventListener("change", function(){
  87. document.querySelector('#waveformGraphAutoupdate').checked = false
  88. loadGraph()
  89. })
  90. document.getElementById("longGraphX").addEventListener("change", function(){
  91. loadLongGraph()
  92. })
  93. document.getElementById("longGraphY").addEventListener("change", function(){
  94. loadLongGraph()
  95. })
  96. setTimeout(loop, 100)
  97. }
  98. j = 0
  99. function loop(){
  100. if(document.getElementById("waveformGraphSelect") != document.activeElement){
  101. loadGraphList()
  102. }
  103. if(document.querySelector('#longGraphAutoupdate').checked){
  104. // Don't update this too often
  105. if(j%10==0){loadLongGraph()}
  106. }
  107. j++
  108. setTimeout(loop, 1000)
  109. }
  110. function renderUI(state){
  111. //console.log('Updating UI')
  112. // BOX INFORMATION
  113. tabs = document.querySelector('#tabs').children
  114. for (var i = 0; i < tabs.length; i++){
  115. tabs[i].classList.remove('active')
  116. if(i>=state.n_boards){
  117. tabs[i].style.opacity = 0
  118. }
  119. else{
  120. tabs[i].style.opacity = 1
  121. }
  122. }
  123. tabs[state.selected_board].classList.add('active')
  124. document.querySelector('#boxAddress').value = "0x"+state.box_address.toString(16)
  125. document.querySelector('#isLearn').checked = false
  126. document.querySelector('#isLock').checked = false
  127. if(state.mode == 0) document.querySelector('#isLearn').checked = true
  128. if(state.mode == 1) document.querySelector('#isLock').checked = true
  129. document.querySelector('#outOfLock').checked = false
  130. if(state.out_of_lock == 1) document.querySelector('#outOfLock').checked = true
  131. document.querySelector('#piFreq').value = state.pi_freq
  132. document.querySelector('#boardFreq').value = state.board_freq
  133. document.querySelector('#remoteTrigg').checked = false
  134. if(state.remote_trigg == 1) document.querySelector('#remoteTrigg').checked = true
  135. // LEARNING OPTIONS
  136. document.querySelector('#enabGnd').checked = state.enab_gnd
  137. document.querySelector('#Vlearn').value = state.Vlearn
  138. document.querySelector('#start').value = state.start
  139. document.querySelector('#stop').value = state.stop
  140. document.querySelector('#startGnd').value = state.start_gnd
  141. document.querySelector('#stopGnd').value = state.stop_gnd
  142. document.querySelector('#N').value = state.N
  143. document.querySelector('#wfLen').value = state.wf_len
  144. // LOCKING OPTIONS
  145. document.querySelector('#stepMax').value = state.step_max
  146. document.querySelector('#Gain').value = state.Gain
  147. // INPUT OPTIONS
  148. document.querySelector('#autoSetPga').checked = state.auto_set_pga
  149. document.querySelector('#inputGain').disabled = state.auto_set_pga==1
  150. document.querySelector('#offset').disabled = state.auto_set_pga==1
  151. document.querySelector('#inputGain').value = state.input_gain
  152. document.querySelector('#offset').value = state.offset
  153. // OUTPUT OPTIONS
  154. document.querySelector('#coarseFineRatio').value = state.coarse_fine_ratio
  155. }
  156. async function loadGraphList(){
  157. files = await eel.listFiles('data/waveforms/')()
  158. timestamps = files.map(s => parseInt(s.replace('.csv', '')))
  159. timestamps.sort()
  160. dates = []
  161. for(timestamp of timestamps){
  162. var d = new Date(0);
  163. d.setUTCMilliseconds(timestamp)*1000;
  164. dates.push(d.toLocaleString() + "." + d.getMilliseconds())
  165. }
  166. var list = document.getElementById("waveformGraphSelect")
  167. index = list.selectedIndex
  168. list.innerHTML = ""
  169. for (var i = 0; i < dates.length; i++){
  170. var element = document.createElement("option")
  171. element.setAttribute('timestamp', timestamps[i])
  172. element.innerText = dates[i]
  173. list.append(element);
  174. }
  175. list.selectedIndex = index
  176. if(document.querySelector('#waveformGraphAutoupdate').checked){
  177. list.selectedIndex = list.children.length-1
  178. if(list.children.length-1 != index) loadGraph()
  179. }
  180. else if(index>-1){
  181. list.selectedIndex = index
  182. }
  183. else{
  184. loadGraph()
  185. }
  186. }
  187. async function loadGraph(){
  188. option = document.querySelector('#waveformGraphSelect')[document.querySelector('#waveformGraphSelect').selectedIndex]
  189. timestamp = option.getAttribute('timestamp')
  190. csv = await eel.loadFile('data/waveforms/'+timestamp+'.csv')()
  191. document.querySelector('#waveformGraphText').innerText = "Signal: "+csv[8]+" - Gnd: "+csv[9]+" - Signal_std: "+csv[10].toFixed(2)+" - Gnd_std: "+csv[11].toFixed(2)
  192. var data = [{
  193. x: range(csv[12]),
  194. y: csv.slice(13),
  195. type: 'scatter'
  196. },{
  197. x: [csv[2], csv[2]],
  198. y: [0, Math.max(...csv.slice(13))],
  199. line: {
  200. color: 'red'
  201. },
  202. type: 'scatter',
  203. mode: 'lines'
  204. },{
  205. x: [csv[3], csv[3]],
  206. y: [0, Math.max(...csv.slice(13))],
  207. line: {
  208. color: 'red'
  209. },
  210. type: 'scatter',
  211. mode: 'lines'
  212. },{
  213. x: [csv[4], csv[4]],
  214. y: [0, Math.max(...csv.slice(13))],
  215. line: {
  216. color: 'green'
  217. },
  218. type: 'scatter',
  219. mode: 'lines'
  220. },{
  221. x: [csv[5], csv[5]],
  222. y: [0, Math.max(...csv.slice(13))],
  223. line: {
  224. color: 'green'
  225. },
  226. type: 'scatter',
  227. mode: 'lines'
  228. }];
  229. var layout = {
  230. margin: {
  231. l: 50,
  232. r: 10,
  233. b: 50,
  234. t: 10,
  235. pad: 4
  236. },
  237. xaxis: {
  238. title: 'Time',
  239. showgrid: true,
  240. },
  241. yaxis: {
  242. title: 'Input voltage',
  243. showgrid: true
  244. },
  245. showlegend: false
  246. };
  247. Plotly.newPlot('waveformGraph', data, layout);
  248. }
  249. async function loadLongGraph(){
  250. csv = await eel.loadLongTerm()()
  251. xIndex = parseInt(document.querySelector('#longGraphX').children[document.querySelector('#longGraphX').selectedIndex].value)
  252. xTitle = document.querySelector('#longGraphX').children[document.querySelector('#longGraphX').selectedIndex].innerText
  253. yIndex = parseInt(document.querySelector('#longGraphY').children[document.querySelector('#longGraphY').selectedIndex].value)
  254. yTitle = document.querySelector('#longGraphY').children[document.querySelector('#longGraphY').selectedIndex].innerText
  255. var data = [{
  256. x: csv.map(l => l[xIndex]),
  257. y: csv.map(l => l[yIndex]),
  258. type: 'scatter'
  259. }]
  260. if(xIndex==1){
  261. max = Math.max(...data[0].x)
  262. data[0].x = data[0].x.map(v => (v-max)/60)
  263. }
  264. var layout = {
  265. margin: {
  266. l: 50,
  267. r: 10,
  268. b: 50,
  269. t: 10,
  270. pad: 4
  271. },
  272. xaxis: {
  273. title: xTitle,
  274. showgrid: true,
  275. },
  276. yaxis: {
  277. title: yTitle,
  278. showgrid: true
  279. },
  280. showlegend: false
  281. };
  282. Plotly.newPlot('longGraph', data, layout);
  283. }
  284. function error(txt){
  285. Toastify({
  286. text: "Error: "+txt,
  287. gravity: 'bottom',
  288. position: 'left',
  289. close: true,
  290. backgroundColor: "#ef4041",
  291. duration: 3000,}).showToast()
  292. }
  293. function warning(txt){
  294. Toastify({
  295. text: "Warning: "+txt,
  296. gravity: 'bottom',
  297. position: 'left',
  298. close: true,
  299. backgroundColor: "#e36d25",
  300. duration: 3000,}).showToast()
  301. }