1import * as DAC from 'DAC' 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 = 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 this.dacInstance.getVol(); 31 }; 32 33 writeValue(value) { 34 if (!this.dacInstance || !value) { 35 throw new Error('dac not init or params is invalid'); 36 } 37 this.dacInstance.setVol(value); 38 }; 39 40 close() { 41 if (!this.dacInstance) { 42 throw new Error('dac not init'); 43 } 44 this.dacInstance.close(); 45 }; 46} 47 48export function open(options) { 49 return new HW_DAC(options); 50} 51