RTL00_WEB/ExampleHTM/dygraph/ws_test_ina219d.html

161 lines
4.9 KiB
HTML
Raw Permalink Normal View History

2017-07-01 08:17:15 +00:00
<!DOCTYPE html>
<html>
<head>
2017-12-12 20:34:32 +00:00
<link rel="stylesheet" href="ina2.css">
2017-07-01 08:17:15 +00:00
<title>Get data INA219</title>
2018-04-20 07:53:32 +00:00
<script type="text/javascript" src="dygraph.min.js"></script>
2017-07-01 08:17:15 +00:00
</head>
<body>
2018-04-20 07:53:32 +00:00
<div style="height: auto; width: auto; text-align: center;">
<div id="div_v" style="height:400px;"></div>
2017-12-12 20:34:32 +00:00
<input type="button" id="butOnOff" value="Stop">
2018-04-20 07:53:32 +00:00
<input type="radio" checked name='rm1' id="FixEnd"/>
2017-12-12 20:34:32 +00:00
<label for="FixEnd">Fixed</label>
2018-04-20 07:53:32 +00:00
<input type="radio" name='rm1' id="FixNone"/>
<label for="FixNone">Float</label>
<input type="button" id="butSave" value="Save data.csv"><br>
Window: <span id='wdsize'>?</span> sec, Sample Rate: <span id='smprate'>?</span> smps
<div id='labdiv'></div>
2017-12-12 20:34:32 +00:00
</div>
<script type="text/javascript">
2018-04-20 07:53:32 +00:00
var $ = function(id) { return document.getElementById(id);}
2017-12-12 20:34:32 +00:00
var stg = 0;
2018-03-04 11:05:52 +00:00
var smprate = 963.4; // 1819;
2017-12-12 20:34:32 +00:00
var smps = smprate;
var samples = 10*smprate;
var rend = 1;
2017-07-01 08:17:15 +00:00
var oldblkid = 0;
var rdnextflg = false;
var cur_idx = 0;
2017-12-12 20:34:32 +00:00
var sttim = 0;
$("butOnOff").onclick = function() {
if(rend) {
rend = 0;
$("butOnOff").value = "Run";
} else {
rend = 1;
$("butOnOff").value = "Stop";
}
}
var datau = [];
var gu;
function wsping() {
clearTimeout(wstt);
ws.send('ina219');
2018-04-20 07:53:32 +00:00
wstt = setTimeout(wsping, 150);
2017-12-12 20:34:32 +00:00
}
2017-07-01 08:17:15 +00:00
ws = new WebSocket('ws://rtl871x0/web.cgi');
2018-04-20 07:53:32 +00:00
//ws = new WebSocket("ws://"+window.location.host.toString()+"/web.cgi");
2017-07-01 08:17:15 +00:00
ws.binaryType = 'arraybuffer';
2017-10-17 14:42:32 +00:00
ws.onopen = function(){ ws.send('user=rtl871x:supervisor'); ws.send('sys_debug=0'); wstt = setTimeout(wsping, 50);};
2017-07-01 08:17:15 +00:00
ws.onmessage = function (event) {
if(event.data instanceof ArrayBuffer) {
2017-10-17 14:42:32 +00:00
clearTimeout(wstt);
2017-07-01 08:17:15 +00:00
var wordarray = new Int16Array(event.data);
if(wordarray.length > 2) {
var blksz = wordarray[0];
if(wordarray.length == blksz*2 + 2) {
2017-12-12 20:34:32 +00:00
if(!sttim) {
sttim = new Date().getTime();
setInterval(function(){wsSmpRate()}, 500);
} else {
var blkid = wordarray[1] & 0xFFFF;
if(rdnextflg) {
cur_idx += (blkid - oldblkid) & 0xFFFF;
} else rdnextflg = true;
oldblkid = blkid + blksz;
for (var i=2; i<wordarray.length; i+=2) {
if(rend) {
if(cur_idx >= samples ) datau.shift();
if(wordarray[i] & 2) datau.push([cur_idx/smprate, wordarray[i]*0.0005, wordarray[i+1]*0.1]);
else datau.push([cur_idx/smprate]);
2018-03-04 11:05:52 +00:00
}
2017-12-12 20:34:32 +00:00
cur_idx++;
}
if(!stg) {
gu = new Dygraph(
$("div_v"),
datau,
{
title: 'U & I (INA219)',
showRangeSelector: true,
showRoller: true,
xlabel: 'T(sec)',
ylabel: 'U(V)',
y2label: 'I(mA)',
colors: ['rgb(51,204,204)','rgb(255,100,100)'],
series : { 'I': { axis: 'y2' } },
axes: {
2018-03-04 11:05:52 +00:00
x: {valueFormatter: function(x){return this.getLabels()[0] + ': '+ x.toPrecision(5);}},
2017-12-12 20:34:32 +00:00
y: {valueRange: [0,]},
y2: {valueRange: [0,]}},
labels: ['T', 'U', 'I'],
labelsDiv: $('labdiv'),
2018-04-20 07:53:32 +00:00
legend: 'always',
2018-03-04 11:05:52 +00:00
digitsAfterDecimal: 3,
2017-12-12 20:34:32 +00:00
});
setInterval(function(){renderChart()}, 50);
stg = 1;
}
} }
wstt = setTimeout(wsping, 40);
} } }
function wsSmpRate() {
smps = cur_idx * 1000/ (new Date().getTime() - sttim);
$('smprate').innerHTML = smps.toFixed(1);
}
var renderChart = function() {
var dl;
if (gu.dateWindow_) {
dl = gu.dateWindow_[1] - gu.dateWindow_[0];
if ($("FixEnd").checked) {
var ls = datau.length - 1;
gu.dateWindow_[1] = datau[ls][0];
gu.dateWindow_[0] = datau[ls][0] - dl;
} else if (gu.dateWindow_[0] < datau[0][0]) {
gu.dateWindow_[0] = datau[0][0];
gu.dateWindow_[1] = datau[0][0] + dl;
}
} else dl = datau.length/smprate;
$("wdsize").innerHTML = dl.toFixed(3);
if(rend) gu.updateOptions({'file': datau});
2017-07-01 08:17:15 +00:00
}
2018-04-20 07:53:32 +00:00
function convertArrayOfObjectsToCSV(value){
var result, ctr, keys, columnDelimiter, lineDelimiter, data;
data = value.data || null;
if (data == null || !data.length) {return null;}
columnDelimiter = value.columnDelimiter || ';';
lineDelimiter = value.lineDelimiter || '\n';
keys = Object.keys(data[1]);
result = '';
result += keys.join(columnDelimiter);
result += lineDelimiter;
data.forEach(function(item){
ctr = 0;
keys.forEach(function(key){
if (ctr > 0)
result += columnDelimiter;
result += item[key];
ctr++;
});
result += lineDelimiter;
});
return result;
}
$("butSave").onclick = function() {
var csv = convertArrayOfObjectsToCSV({data: datau});
if (!csv.match(/^data:text\/csv/i)) {csv = 'data:text/csv;charset=utf-8,' + csv;}
var encodedUri = encodeURI(csv);
var link = document.createElement('a');
link.setAttribute('href', encodedUri);
link.setAttribute('download',"data.csv");
link.click();
}
if(window.innerHeight > 320) $('div_v').style.height = (window.innerHeight-100) + 'px';
window.onresize = function(){
if(window.innerHeight > 320) $('div_v').style.height = (window.innerHeight-100) + 'px';
// $('div_v').style.width = (window.innerWidth-50) + 'px';
}
</script></body>
2017-07-01 08:17:15 +00:00
</html>