1'use strict'; 2 3class HW_DAC { 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.dacInstance = __native.DAC.open(this.options.id); 19 if (!this.dacInstance) { 20 this.fail(); 21 return; 22 } 23 this.success(); 24 } 25 26 readValue() { 27 if (!this.dacInstance) { 28 throw new Error('dac not init'); 29 } 30 return __native.DAC.getVol(this.dacInstance); 31 }; 32 33 writeValue(value) { 34 if (!this.dacInstance || !value) { 35 throw new Error('dac not init or params is invalid'); 36 } 37 __native.DAC.setVol(this.dacInstance, value); 38 }; 39 40 close() { 41 if (!this.dacInstance) { 42 throw new Error('dac not init'); 43 } 44 __native.DAC.close(this.dacInstance); 45 }; 46} 47 48function open(options) { 49 return new HW_DAC(options); 50} 51 52module.exports = { 53 open, 54}