1import * as GPIO from 'GPIO'
2
3class HW_GPIO {
4    constructor(options) {
5        if (!options || !options.id) {
6            throw new Error("options is invalid");
7        }
8        this.options = {
9            id: options.id
10        };
11
12        this.success = options.success || function(){};
13        this.fail = options.fail || function(){};
14        this._open();
15    }
16
17    _open() {
18        this.gpioInstance = GPIO.open(this.options.id);
19        if (!this.gpioInstance) {
20            this.fail();
21            return;
22        }
23        this.success();
24    }
25
26    writeValue(level) {
27        if (!this.gpioInstance) {
28            throw new Error("gpio not init");
29        }
30        this.gpioInstance.write(level);
31    }
32
33    toggle() {
34        if (!this.gpioInstance) {
35            throw new Error("gpio not init");
36        }
37        this.gpioInstance.toggle();
38    }
39
40    onIRQ(options) {
41        if (!this.gpioInstance || !options || !options.cb) {
42            throw new Error("gpio not init or params is invalid");
43        }
44        this.gpioInstance.on(options.cb);
45    }
46
47    readValue() {
48        if (!this.gpioInstance) {
49            throw new Error("gpio not init");
50        }
51        return this.gpioInstance.read();
52    };
53
54    close() {
55        if (!this.gpioInstance) {
56            throw new Error("gpio not init");
57        }
58        this.gpioInstance.close();
59    };
60}
61
62export function open(options) {
63    return new HW_GPIO(options);
64}
65
66
67