Skip to content

mattfors/ng-scales

Repository files navigation

ng-scales

npm version workflow Coverage code ql Known Vulnerabilities code style: prettier

Angular library for connecting to USB scales

Demo

Check out the live demo

Get started

Installing

Add the npm package to your app

npm i ng-scales

Using in angular application

Add the providers in the app.config.ts

providers: [
  ...
  provideNgScales()
  ...
]

The easiest way to integrate is the provided button directive which handles connecting and disconnecting from the scale. Add this into your template

<button libNgScalesConnectionButton></button>

Import the component

@Component({
  ...
  imports: [NgScalesConnectionButtonDirective]
  ...
})

Hardware Support

Built in support

Vendor Vendor ID Product Product Id Name Verified
DYMO 2338 M25 32771 DYMO M25 25 Lb Digital Postal Scale

Adding support for additional scales

You can add a new scale by providing additional mappers. The mapper function coverts the array buffer into a usable data object

  provideNgScales({mappers: [
    {
      vendorId: 2338,
      productId: 32771,
      mapper: (arrayBuffer: ArrayBuffer): HardwareScaleReportEvent => {
        const d = new Uint8Array(arrayBuffer);
        let weight = (d[3] + 256 * d[4])/10;
        if (d[0] === 5) {
          weight *= -1;
        }
        return {
          units: d[1] === 2 ? 'grams' : 'ounces',
          weight
        };
      }
    }
  ]})