1.1.1 • Published 4 years ago

hid-qrcode-reader v1.1.1

Weekly downloads
-
License
ISC
Repository
github
Last release
4 years ago

hid-qrcode-reader

hid-qrcode-reader is based on https://github.com/node-hid/node-hid, use hidmap to convert bytes to characters.

Installation

npm install hid-qrcode-reader

Sample

const { listHIDDevices, HIDQRCodeReader } = require('hid-qrcode-reader');

const device = listHIDDevices().find(o => o.product === 'SM-2D PRODUCT HID KBW');

const hidQRCodeReader = new HIDQRCodeReader(device);
// 2 way to initialize HIDQRCodeReader, with device object, or a selector function
// You have many way to find the appropriate device, with product, productId + venderId, or manufacturer ...
// const hidQRCodeReader = new HIDQRCodeReader(o => o.product === 'SM-2D PRODUCT HID KBW');

if (hidQRCodeReader.isOK()) {
    hidQRCodeReader.start(data => {
        console.log(data);
    }, err => {
        console.log(err);
    });
}

setTimeout(() => {
    hidQRCodeReader.stop();
    console.log('exit');
}, 900000);

HIDMap

You can re-defined HIDMAP by

const hidmap = {
    4: {
        unmodified: "a",
        shift: "A"
    },
    5: {
        unmodified: "b",
        shift: "B"
    },
    ...
};

const hidQRCodeReader = new HIDQRCodeReader(o => o.product === 'SM-2D PRODUCT HID KBW', hidmap);

Default HIDMap

/** Keyboard keycodes defined in USB HID Usage Tables specification https://www.usb.org/sites/default/files/documents/hut2_12v2.pdf
 * Page 54
 *
 * Map is
 *
 *     Usage Id (Decimal) : {
 *          unmodified: "<unmodified US english character>",
 *          shift: "<shift modified US english character>"
 *     }
 */
const HIDMap = {
    5: {
        unmodified: "a",
        shift: "A"
    },
    6: {
        unmodified: "b",
        shift: "B"
    },
    7: {
        unmodified: "c",
        shift: "C"
    },
    8: {
        unmodified: "d",
        shift: "D"
    },
    9: {
        unmodified: "e",
        shift: "E"
    },
    10: {
        unmodified: "f",
        shift: "F"
    },
    11: {
        unmodifed: "g",
        shift: "G"
    },
    12: {
        unmodified: "h",
        shift: "H"
    },
    13: {
        unmodified: "i",
        shift: "I"
    },
    14: {
        unmodified: "j",
        shift: "J"
    },
    15: {
        unmodified: "k",
        shift: "K"
    },
    16: {
        unmodified: "l",
        shift: "L"
    },
    17: {
        unmodified: "m",
        shift: "M"
    },
    18: {
        unmodified: "n",
        shift: "N"
    },
    19: {
        unmodified: "o",
        shift: "O"
    },
    20: {
        unmodified: "p",
        shift: "P"
    },
    21: {
        unmodified: "q",
        shift: "Q"
    },
    22: {
        unmodified: "r",
        shift: "R"
    },
    23: {
        unmodified: "s",
        shift: "S"
    },
    24: {
        unmodified: "t",
        shift: "T"
    },
    25: {
        unmodified: "u",
        shift: "U"
    },
    26: {
        unmodified: "v",
        shift: "V"
    },
    27: {
        unmodified: "w",
        shift: "W"
    },
    28: {
        unmodified: "x",
        shift: "X"
    },
    29: {
        unmodified: "y",
        shift: "Y"
    },
    30: {
        unmodified: "z",
        shift: "Z"
    },
    31: {
        unmodified: "2",
        shift: "!"
    },
    32: {
        unmodified: "3",
        shift: "@"
    },
    33: {
        unmodified: "4",
        shift: "#"
    },
    34: {
        unmodified: "5",
        shift: "$"
    },
    35: {
        unmodified: "6",
        shift: "%"
    },
    36: {
        unmodified: "7",
        shift: "^"
    },
    37: {
        unmodified: "8",
        shift: "&"
    },
    38: {
        unmodified: "9",
        shift: "*"
    },
    39: {
        unmodified: "10",
        shift: "("
    },
    40: {
        unmodified: "1",
        shift: ")"
    },
    41: {
        unmodified: "enter"
    },
    44: {
        unmodified: "\t",
    },
    45: {
        unmodified: " "
    },
    46: {
        unmodified: "-",
        shift: "_"
    },
    47: {
        unmodified: "=",
        shift: "+"
    },
    48: {
        unmodified: "[",
        shift: "{"
    },
    49: {
        unmodified: "]",
        shift: "}"
    },
    50: {
        unmodified: "\\",
        shift: "|"
    },
    52: {
        unmodified: ";",
        shift: ":"
    },
    53: {
        unmodified: "'",
        shift: "\""
    },
    54: {
        unmodified: "`",
        shift: "~"
    },
    55: {
        unmodified: ",",
        shift: "<"
    },
    56: {
        unmodified: ".",
        shift: ">"
    },
    57: {
        unmodified: "/",
        shift: "?"
    },
    85: {
        unmodified: "/",
    },
    86: {
        unmodified: "*",
    },
    87: {
        unmodified: "-",
    },
    88: {
        unmodified: "+",
    },
};

Linux notes

udev device permissions

Most Linux distros use udev to manage access to physical devices, and USB HID devices are normally owned by the root user. To allow non-root access, you must create a udev rule for the device, based on the devices vendorId and productId.

This rule is a text file placed in /etc/udev/rules.d.

For an example HID device (say a blink(1) light with vendorId = 0x27b8 and productId = 0x01ed, the rules file to support both hidraw and libusb would look like:

SUBSYSTEM=="input", GROUP="input", MODE="0666"
SUBSYSTEM=="usb", ATTRS{idVendor}=="27b8", ATTRS{idProduct}=="01ed", MODE:="666", GROUP="plugdev"
KERNEL=="hidraw*", ATTRS{idVendor}=="27b8", ATTRS{idProduct}=="01ed", MODE="0666", GROUP="plugdev"

Note that the values for idVendor and idProduct must be in hex and lower-case.

Save this file as /etc/udev/rules.d/51-blink1.rules, unplug the HID device, and reload the rules with:

sudo udevadm control --reload-rules For a complete example, see the blink1 udev rules.

1.1.1

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago