1'use strict';
2
3class HW_SPI {
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.spiInstance = __native.SPI.open(this.options.id);
19        if (!this.spiInstance) {
20            this.fail();
21            return;
22        }
23        this.success();
24    }
25
26    write(data) {
27        if (!this.spiInstance || !data) {
28            throw new Error("spi not init or params is invalid");
29        }
30        __native.SPI.write(this.spiInstance, data);
31    }
32
33    read(bytes) {
34        if (!this.spiInstance || !bytes) {
35            throw new Error("spi not init or params is invalid");
36        }
37        return __native.SPI.read(this.spiInstance, bytes);
38    };
39
40    readWrite(sendData, bytes) {
41        if (!this.spiInstance || !sendData ||!bytes) {
42            throw new Error("spi not init or params is invalid");
43        }
44        return __native.SPI.sendRecv(this.spiInstance, sendData, bytes);
45    };
46
47    close() {
48        if (!this.spiInstance) {
49            throw new Error("spi not init");
50        }
51        __native.SPI.close(this.spiInstance);
52    };
53}
54
55function open(options) {
56    return new HW_SPI(options);
57}
58
59module.exports = {
60    open,
61}