1import * as ADC from 'ADC' 2 3class HW_ADC { 4 constructor(options) { 5 if (!options || !options.id) { 6 throw new Error('options is invalid'); 7 } 8 9 this.options = { 10 id: options.id 11 }; 12 13 this.success = options.success || function(){}; 14 this.fail = options.fail || function(){}; 15 this._open(); 16 } 17 18 _open() { 19 this.adcInstance = ADC.open(this.options.id); 20 if (!this.adcInstance) { 21 this.fail(); 22 return; 23 } 24 this.success(); 25 } 26 27 readValue() { 28 if (!this.adcInstance) { 29 throw new Error('adc not init'); 30 } 31 32 return this.adcInstance.read(); 33 }; 34 35 close() { 36 if (!this.adcInstance) { 37 throw new Error('adc not init'); 38 } 39 40 this.adcInstance.close(); 41 }; 42} 43 44export function open(options) { 45 return new HW_ADC(options); 46} 47