Skip to content

Instantly share code, notes, and snippets.

@erjiang
Last active April 20, 2023 23:26
Show Gist options
  • Save erjiang/503896 to your computer and use it in GitHub Desktop.
Save erjiang/503896 to your computer and use it in GitHub Desktop.

Revisions

  1. erjiang revised this gist Nov 20, 2019. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions usbscale.pl
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,10 @@
    # http://notes.ericjiang.com/posts/54
    # This software is public domain.
    #
    # NOTE: This code is not maintained!
    # There is a newer version written in C:
    # https://github.com/erjiang/usbscale
    #
    use bytes;

    my $data;
  2. erjiang revised this gist Feb 14, 2011. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions usbscale.pl
    Original file line number Diff line number Diff line change
    @@ -15,12 +15,12 @@

    $data = `cat /dev/hidraw4 | head -c 7`;

    my $report = ord(substr($data, 1, 1));
    my $status = ord(substr($data, 2, 1));
    my $unit = ord(substr($data, 3, 1));
    my $exp = ord(substr($data, 4, 1));
    my $lsb = ord(substr($data, 5, 1));
    my $msb = ord(substr($data, 6, 1));
    my $report = ord(substr($data, 0, 1));
    my $status = ord(substr($data, 1, 1));
    my $unit = ord(substr($data, 2, 1));
    my $exp = ord(substr($data, 3, 1));
    my $lsb = ord(substr($data, 4, 1));
    my $msb = ord(substr($data, 5, 1));
    my $weight = ($msb * 256 + $lsb) / 10;
    if($exp != 255 && $exp != 0) {
    $weight ^= $exp;
  3. erjiang revised this gist Feb 14, 2011. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion usbscale.pl
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,9 @@
    #!/usr/bin/perl

    #
    # Eric Jiang
    # http://notes.ericjiang.com/posts/54
    # This software is public domain.
    #
    use bytes;

    my $data;
  4. erjiang revised this gist Feb 7, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion usbscale.pl
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@
    my $exp = ord(substr($data, 4, 1));
    my $lsb = ord(substr($data, 5, 1));
    my $msb = ord(substr($data, 6, 1));
    my $weight = ($msb * 255 + $lsb) / 10;
    my $weight = ($msb * 256 + $lsb) / 10;
    if($exp != 255 && $exp != 0) {
    $weight ^= $exp;
    }
  5. erjiang created this gist Aug 1, 2010.
    75 changes: 75 additions & 0 deletions usbscale.pl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    #!/usr/bin/perl

    use bytes;

    my $data;

    #prevents us from repeating messages
    my $waitingflag = 0;

    while (1) {

    $data = `cat /dev/hidraw4 | head -c 7`;

    my $report = ord(substr($data, 1, 1));
    my $status = ord(substr($data, 2, 1));
    my $unit = ord(substr($data, 3, 1));
    my $exp = ord(substr($data, 4, 1));
    my $lsb = ord(substr($data, 5, 1));
    my $msb = ord(substr($data, 6, 1));
    my $weight = ($msb * 255 + $lsb) / 10;
    if($exp != 255 && $exp != 0) {
    $weight ^= $exp;
    }
    #print "$report $status $unit $exp $weight\n";

    if($report != 0x03) {
    die "Error reading scale data!\n";
    }

    if($status == 0x01) {
    die "Scale reports FAULT!\n";
    } elsif ($status == 0x02 || $weight == 0) {
    if($waitingflag != 0x02) {
    print "Zero'd...\n";
    $waitingflag = 0x02;
    }
    } elsif ($status == 0x03) {
    if($waitingflag != 0x03) {
    print "Weighing...\n";
    $waitingflag = 0x03;
    }
    } elsif ($status == 0x04) {
    my $unitName = "units";
    if($unit == 11) {
    $unitName = "ounces";
    } elsif ($unit == 12) {
    $unitName = "pounds";
    }
    print "$weight $unitName\n";
    last;
    } elsif ($status == 0x05) {
    if($waitingflag != 0x05) {
    print "Scale reports Under Zero...\n";
    $waitingflag = 0x05;
    }
    } elsif ($status == 0x06) {
    if($waitingflag != 0x06) {
    print "Scale reports Over Weight!\n";
    $waitingflag = 0x06;
    }
    } elsif ($status == 0x07) {
    if($waitingflag != 0x07) {
    print "Scale reports Calibration Needed!\n";
    $waitingflag = 0x07;
    }
    } elsif ($status == 0x08) {
    if($waitingflag != 0x08) {
    print "Scale reports Re-zeroing Needed!\n";
    $waitingflag = 0x08;
    }
    } else {
    die "Unknown status code: $status\n";
    }

    }