1'use strict'; 2 3class HW_ONEWIRE { 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.onewireinstance = __native.ONEWIRE.open(this.options.id); 19 if (!this.onewireinstance) { 20 this.fail(); 21 return; 22 } 23 this.success(); 24 } 25 setspeed(standard) { 26 if (!this.onewireinstance) { 27 throw new Error("onewire not init"); 28 } 29 __native.ONEWIRE.setspeed(this.onewireinstance, standard); 30 } 31 32 reset() { 33 if (!this.onewireinstance) { 34 throw new Error("onewire not init"); 35 } 36 __native.ONEWIRE.reset(this.onewireinstance); 37 } 38 39 readByte() { 40 if (!this.onewireinstance) { 41 throw new Error("onewire not init"); 42 } 43 return __native.ONEWIRE.readByte(this.onewireinstance); 44 }; 45 46 writeByte(data) { 47 if (!this.onewireinstance) { 48 throw new Error("onewire not init"); 49 } 50 return __native.ONEWIRE.writeByte(this.onewireinstance, data); 51 }; 52 53 close() { 54 if (!this.onewireinstance) { 55 throw new Error("onewire not init"); 56 } 57 __native.ONEWIRE.close(this.onewireinstance); 58 } 59} 60 61function open(options) { 62 return new HW_ONEWIRE(options); 63} 64 65module.exports = { 66 open, 67}