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