1'use strict';
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 = __native.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        __native.GPIO.write(this.gpioInstance, level);
31    }
32
33    toggle() {
34        if (!this.gpioInstance) {
35            throw new Error("gpio not init");
36        }
37        __native.GPIO.toggle(this.gpioInstance);
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        __native.GPIO.on(this.gpioInstance, options.cb);
45    }
46
47    readValue() {
48        if (!this.gpioInstance) {
49            throw new Error("gpio not init");
50        }
51        return __native.GPIO.read(this.gpioInstance);
52    };
53
54    close() {
55        if (!this.gpioInstance) {
56            throw new Error("gpio not init");
57        }
58        __native.GPIO.close(this.gpioInstance);
59    };
60}
61
62function open(options) {
63    return new HW_GPIO(options);
64}
65
66module.exports = {
67    open,
68}