forked from sanmai/usb-scale-reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_stupid.php
36 lines (30 loc) · 925 Bytes
/
read_stupid.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
/***
* Copyright (c) 2014 Alexey Kopytko
* Released under the MIT license.
*/
if (empty($argv[1])) {
echo "Usage: DEBUG=1 php $argv[0] /dev/hidrawX\n";
return;
}
$binary = fread(fopen($argv[1], 'r'), 6);
if (isset($_SERVER['DEBUG'])) {
$dataHex = bin2hex($binary);
echo "Data: $dataHex\n";
file_put_contents("$dataHex.bin", $binary);
}
$data = (object) unpack('Creport/Cstatus/Cunit/cexponent/vweight', $binary);
if ($data->report == 0x03 && $data->status == 0x04) {
$data->weight = $data->weight * pow(10, $data->exponent);
if ($data->unit == 0x0B) {
// convert ounces to grams
$data->weight = round($data->weight * 28.349523125, 2);
// and unit to grams
$data->unit = 0x02;
}
if ($data->unit == 0x02) {
fprintf(STDOUT, "%.2f g\n", $data->weight);
} else {
fprintf(STDOUT, "%.2f in other unit\n", $data->weight);
}
}