Skip to content

Commit

Permalink
Small fix
Browse files Browse the repository at this point in the history
  • Loading branch information
renat2985 committed Jun 10, 2024
1 parent 355e80a commit 46d6bf7
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 33 deletions.
17 changes: 14 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,21 @@
</head>
<body>
<!-- partial:index.partial.html -->
<div style="display:none">
<p>Vin: <span id="vin"></span> V</p>
<p>Vout: <span id="vout"></span> V</p>
<p>Iout: <span id="iout"></span> A</p>
<p>Vo_max: <span id="vo_max"></span> V</p>
<p>Temp1: <span id="temp1"></span> °C</p>
<p>Temp2: <span id="temp2"></span> °C</p>
<p>DC 5V: <span id="dc5v"></span> V</p>
<p>Out Mode: <span id="out_mode">N/A</span></p>
<p>Work State: <span id="work_state">N/A</span></p>
</div>
<div id="connect">
<h1>Alientek DP100</h1>
<p>Just plug in the DP100 using the USB-A to USB-A cable <i>(make sure it's in "USBD" mode; double-tap ◀ to switch)</i>, and click connect!</p>
<h2><a href="#" id="connectButton" onclick="toggleConnect()" class="j5-link">Connect...</a></h2>
<h2><a href="#" id="connectButton" class="j5-link">Connect...</a></h2>
</div>
<section class="dashboard dimmed" id="main-column">
<div id="about">
Expand Down Expand Up @@ -43,7 +54,7 @@ <h1> <span>Alientek</span><span> DP100</span> / High Performance Digital Power</
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.0/jquery.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/highcharts/9.0.0/highcharts.js'></script>
<script src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/4579/darkThemeHighCharts.js'></script>
<script src="./script.js?v=2"></script>
<script src="./script.js?v=9"></script>

</body>
</html>
</html>
133 changes: 103 additions & 30 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ $(document).ready(function() {
}

function updateVoltage(value) {
$voltageDisplay.html(value.toFixed(1));
$voltageDisplay.html(value);
}

function updateCurrent(value) {
$currentDisplay.html(value.toFixed(1));
$currentDisplay.html(value);
}

function updateMoisture(value) {
$moistureDisplay.html(value.toFixed(1) + '<span>%</span>');
$moistureDisplay.html(value.toFixed(2) + '<span>%</span>');
}

function updateSensorDisplayValues(d) {
Expand Down Expand Up @@ -63,17 +63,31 @@ $(document).ready(function() {
// faking sensor data
// data will be coming from sensors on the MKR1000
setInterval(function() {
x = (new Date()).getTime(),
volts = getRandomInt(vMin, vMax),
amps = getRandomInt(cMin, cMax),
mPercent = getRandomInt(mMin, mMax);


if (document.getElementById('vin').textContent != '') {
const thevolts = parseFloat(document.getElementById('vout').textContent);
const theamps = parseFloat(document.getElementById('iout').textContent);
x = (new Date()).getTime(),
volts = thevolts,
amps = theamps,
mPercent = (amps / 5) * 100;
} else {
x = (new Date()).getTime(),
volts = getRandomInt(vMin, vMax),
amps = getRandomInt(cMin, cMax),
mPercent = getRandomInt(mMin, mMax);
}



voltage.addPoint([x, volts], false, true);
current.addPoint([x, amps], false, true);
moisture.addPoint([x, mPercent], true, true);

updateSensorDisplayValues([volts, amps, mPercent]);
}, $delay);
},
$delay);
}
}
},
Expand Down Expand Up @@ -136,7 +150,8 @@ $(document).ready(function() {
formatter: function() {
var unitOfMeasurement = this.series.name === 'VOLTAGE' ? ' V': ' A';
return '<b>' + this.series.name + '</b><br/>' +
Highcharts.numberFormat(this.y, 1) + unitOfMeasurement;
Highcharts.numberFormat(this.y,
1) + unitOfMeasurement;
}
},
legend: {
Expand Down Expand Up @@ -212,49 +227,107 @@ function toggleConnect() {
}








async function connect() {
if (!('hid' in navigator)) {
alert('WebHID API is not supported in your browser.');
return;
}

const filters = [{
vendorId: 0x0483,
productId: 0x5750
vendorId: 0x2E3C,
productId: 0xAF01
}];

function crc16Modbus(buffer) {
let crc = 0xFFFF;
for (let pos = 0; pos < buffer.length; pos++) {
crc ^= buffer[pos];
for (let i = 8; i !== 0; i--) {
if ((crc & 0x0001) !== 0) {
crc >>= 1;
crc ^= 0xA001;
} else {
crc >>= 1;
}
}
}
return crc;
}

function interpretUint16(dataArray, index) {
return dataArray[index] | (dataArray[index + 1] << 8);
}

try {
const device = await navigator.hid.requestDevice({
const devices = await navigator.hid.requestDevice({
filters
});
if (devices.length === 0) {
alert("No device selected.");
return;
}
const device = devices[0];
await device.open();

console.log("Device connected");

console.log("Device connected:", device);
toggleConnect();
device.addEventListener('inputreport', event => {
const {
data
} = event;
const dataArray = new Uint8Array(data.buffer);
const voltage = dataArray[1] + (dataArray[2] / 100);
const current = dataArray[3] + (dataArray[4] / 1000);
// updateVoltage(value);
// updateCurrent(value);
// updateMoisture(value);
// document.getElementById('voltage').textContent = voltage.toFixed(2);
// document.getElementById('current').textContent = current.toFixed(3);
console.log("Voltage:", voltage, "V");
console.log("Current:", current, "A");
// console.log("Data received:", dataArray);
if (dataArray[0] === 0xFA) {
// Убедимся, что это ответ от устройства
const vin = interpretUint16(dataArray, 4) * 0.001;
const vout = interpretUint16(dataArray, 6) * 0.001;
const iout = interpretUint16(dataArray, 8) * 0.001;
const vo_max = interpretUint16(dataArray, 10) * 0.001;
const temp1 = interpretUint16(dataArray, 12);
const temp2 = interpretUint16(dataArray, 14);
const dc5v = interpretUint16(dataArray, 16) * 0.001;
const out_mode = dataArray[18];
const work_state = dataArray[19];
document.getElementById('vin').textContent = vin.toFixed(2);
document.getElementById('vout').textContent = vout.toFixed(2);
document.getElementById('iout').textContent = iout.toFixed(3);
document.getElementById('vo_max').textContent = vo_max.toFixed(2);
document.getElementById('temp1').textContent = temp1;
document.getElementById('temp2').textContent = temp2;
document.getElementById('dc5v').textContent = dc5v.toFixed(2);
document.getElementById('out_mode').textContent = out_mode;
document.getElementById('work_state').textContent = work_state;
}
});

// Example command to request voltage and current
const reportId = 0x01;
const data = new Uint8Array([0x01, 0x03, 0x04, 0x00]);
await device.sendReport(reportId, data);
async function requestData() {
let data = new Uint8Array(64);
data[0] = 0xFB; // Host to Device
data[1] = 0x30; // OpCode for BASIC_INFO
data[2] = 0x00; // Reserve
data[3] = 0x00; // Len (no additional data)

// Вычисляем CRC16
const crc = crc16Modbus(data.subarray(0, 4));
data[4] = crc & 0xFF; // CRC Low
data[5] = (crc >> 8) & 0xFF; // CRC High

await device.sendReport(0x00,
data);
//console.log("Data request sent");
}

console.log("Command sent");
setInterval(requestData,
1000);

} catch (error) {
console.error("There was an error:", error);
console.error("There was an error:",
error);
}
}

Expand Down

0 comments on commit 46d6bf7

Please sign in to comment.