@pinlab/mighty-gpio
Version:
Raspberry Pi GPIO module with emulation support
1 lines • 14.9 kB
JavaScript
(()=>{var __webpack_modules__={393:function(t,e,i){"use strict";var o=this&&this.__awaiter||function(t,e,i,o){function adopt(t){return t instanceof i?t:new i((function(e){e(t)}))}return new(i||(i=Promise))((function(i,s){function fulfilled(t){try{step(o.next(t))}catch(t){s(t)}}function rejected(t){try{step(o["throw"](t))}catch(t){s(t)}}function step(t){t.done?i(t.value):adopt(t.value).then(fulfilled,rejected)}step((o=o.apply(t,e||[])).next())}))};Object.defineProperty(e,"__esModule",{value:true});e.AckEventEmitter=void 0;const s=i(598);const n=i(997);const r=i(434);class AckEventEmitter extends r.EventEmitter{invoke(t,...e){const i=Buffer.from((0,s.randomBytes)(5)).toString("hex");return new Promise(((o,s)=>{const dispatcher=(t,...e)=>{if(i!==t)return;(0,n.clearTimeout)(r);o(...e)};const r=setTimeout((()=>{console.log(`AckEvent RES|${i}|${t} considered stale`);this.off(`RES|${i}|${t}`,dispatcher)}),AckEventEmitter.StaleTime);this.once(`RES|${i}|${t}`,dispatcher);this.emit(`REQ|${t}`,i,...e)}))}handle(t,e){const i=this;this.on(`REQ|${t}`,(function(s,...n){return o(this,void 0,void 0,(function*(){i.emit(`RES|${s}|${t}`,s,e(...n))}))}))}unhandle(t){this.removeAllListeners(`REQ|${t}`)}}e.AckEventEmitter=AckEventEmitter;AckEventEmitter.StaleTime=1e4},916:(t,e)=>{"use strict";Object.defineProperty(e,"__esModule",{value:true});e.PhysicalScheme=e.BroadcomScheme=void 0;e.BroadcomScheme={2:3,3:5,4:7,5:29,6:31,7:26,8:24,9:21,10:19,11:23,12:32,13:33,14:8,15:10,16:36,17:11,18:12,19:35,20:38,21:40,22:15,23:16,24:18,25:22,26:37,27:13};e.PhysicalScheme=Object.fromEntries(Object.entries(e.BroadcomScheme).map((([t,e])=>[e,t])))},730:function(t,e,i){"use strict";var o=this&&this.__createBinding||(Object.create?function(t,e,i,o){if(o===undefined)o=i;var s=Object.getOwnPropertyDescriptor(e,i);if(!s||("get"in s?!e.__esModule:s.writable||s.configurable)){s={enumerable:true,get:function(){return e[i]}}}Object.defineProperty(t,o,s)}:function(t,e,i,o){if(o===undefined)o=i;t[o]=e[i]});var s=this&&this.__setModuleDefault||(Object.create?function(t,e){Object.defineProperty(t,"default",{enumerable:true,value:e})}:function(t,e){t["default"]=e});var n=this&&this.__importStar||function(){var ownKeys=function(t){ownKeys=Object.getOwnPropertyNames||function(t){var e=[];for(var i in t)if(Object.prototype.hasOwnProperty.call(t,i))e[e.length]=i;return e};return ownKeys(t)};return function(t){if(t&&t.__esModule)return t;var e={};if(t!=null)for(var i=ownKeys(t),n=0;n<i.length;n++)if(i[n]!=="default")o(e,t,i[n]);s(e,t);return e}}();var r=this&&this.__awaiter||function(t,e,i,o){function adopt(t){return t instanceof i?t:new i((function(e){e(t)}))}return new(i||(i=Promise))((function(i,s){function fulfilled(t){try{step(o.next(t))}catch(t){s(t)}}function rejected(t){try{step(o["throw"](t))}catch(t){s(t)}}function step(t){t.done?i(t.value):adopt(t.value).then(fulfilled,rejected)}step((o=o.apply(t,e||[])).next())}))};Object.defineProperty(e,"__esModule",{value:true});e.startI2C=e.startSPI=e.startPWM=e.ready=e.setObservers=e.useBroadcomScheme=e.forceEmulation=e.setInverted=e.watchInput=e.out=e.setOutput=e["in"]=e.setInput=void 0;const a=i(393);const h=i(916);const u=i(439);const c=i(434);class MightyGpio{static setObservers(t){var e,i;MightyGpio.observers=t;MightyGpio._events.removeAllListeners("state-assigned");(i=(e=MightyGpio.observers).receive)===null||i===void 0?void 0:i.call(e,((t,e,i,o)=>r(this,void 0,void 0,(function*(){return yield MightyGpio._events.invoke(`state-received[${t}]`,+e,i,o)}))));MightyGpio._events.on("state-assigned",((t,e,i,o)=>{var s,n;(n=(s=MightyGpio.observers).send)===null||n===void 0?void 0:n.call(s,t,e,i,o)}));MightyGpio._gpioEvents.emit("inform-state")}static watchInput(...t){const{edge:e,callback:i,s:o}=MightyGpio.parseWatchInputArgs(...t);let s=0;MightyGpio._events.on("state-watch",(t=>{const n=Date.now();const r=s+o<=n;const a=e===t||e===u.Edge.Both;if(!r||!a)return;s=n;i()}))}static unwatchInput(){MightyGpio._events.removeAllListeners("state-watch")}static startPWM(t,e,i,o){return r(this,void 0,void 0,(function*(){MightyGpio._importArrayGpio();const s=yield MightyGpio.arrayGpio;const n=s===null||s===void 0?void 0:s.startPWM(t,e,i,o);if(!n){throw Error("PWM isn't supported in emulation mode")}return n}))}static startI2C(){return r(this,void 0,void 0,(function*(){MightyGpio._importArrayGpio();const t=yield MightyGpio.arrayGpio;const e=t===null||t===void 0?void 0:t.startI2C();if(!e){throw Error("I2C isn't supported in emulation mode")}return e}))}static startSPI(){return r(this,void 0,void 0,(function*(){MightyGpio._importArrayGpio();const t=yield MightyGpio.arrayGpio;const e=t===null||t===void 0?void 0:t.startSPI();if(!e){throw Error("SPI isn't supported in emulation mode")}return e}))}static ready(...t){return r(this,void 0,void 0,(function*(){const e=[t].flat().map((t=>t.ready()));return yield Promise.all(e).then((()=>true))}))}static initPin(t,e){const i=t.length===1&&typeof t[0]==="number";const o=Array.isArray(t)&&t.length>1;const s=t[0];if(i){return new e(s)}if(o){return t.map((t=>new e(t)))}if(typeof s==="object"){const t=s.pin.map((t=>new e(t)));if(!s.index){return t}const i=[];t.forEach((t=>{i[t.pin]=t}));return i}throw new Error("Invalid parameter")}static parseWatchInputArgs(...t){let e=u.Edge.Both;let i;let o=100;if(typeof t[0]==="function"){i=t[0];if(typeof t[1]==="number"){o=t[1]}}else{e=t[0];i=t[1];if(typeof t[2]==="number"){o=t[2]}}return{edge:e,callback:i,s:o}}static _importArrayGpio(){if(!MightyGpio.arrayGpio){MightyGpio.arrayGpio=Promise.resolve().then((()=>n(i(988)))).then((t=>t.default)).catch((t=>null))}}}MightyGpio._mode=u.Mode.Real;MightyGpio._inverted=false;MightyGpio._events=new a.AckEventEmitter;MightyGpio._gpioEvents=new c.EventEmitter;MightyGpio._gpioScheme=u.GpioScheme.Physical;MightyGpio.observers={};MightyGpio.forceEmulation=()=>MightyGpio._mode=u.Mode.Emulated;MightyGpio.setInverted=()=>{if(MightyGpio._mode===u.Mode.Real){MightyGpio._inverted=true}};MightyGpio.useBroadcomScheme=()=>{MightyGpio._gpioScheme=u.GpioScheme.Broadcom};MightyGpio.isBroadcomScheme=()=>MightyGpio._gpioScheme===u.GpioScheme.Broadcom;MightyGpio.supportsPeripherals=()=>MightyGpio._mode===u.Mode.Real;MightyGpio.supportsPWM=MightyGpio.supportsPeripherals;MightyGpio.supportsI2C=MightyGpio.supportsPeripherals;MightyGpio.supportsSPI=MightyGpio.supportsPeripherals;MightyGpio.setInput=(...t)=>MightyGpio.initPin(t,InputPin);MightyGpio.in=MightyGpio.setInput;MightyGpio.setOutput=(...t)=>MightyGpio.initPin(t,OutputPin);MightyGpio.out=MightyGpio.setOutput;e["default"]=MightyGpio;class Pin{get isOff(){return this.state===0}get isOn(){return this.state===1}constructor(t){this.resistor=u.Resistor.NoPull;this.state=0;this.id=Math.floor(Math.random()*10**5).toString(26);this.isMighty=true;this.gpioPromise=Promise.resolve(undefined);this.isHardware=false;this._informListener=()=>{this.setObserverState(this.state)};MightyGpio._importArrayGpio();this.listenInform();this.pin=t;this.state=0}ready(){return this.gpioPromise.then((()=>this))}close(){var t;this.unhandleStateReceived();this.unhandleStateConfirmed();this.unlistenInform();this.mode=undefined;this.resistor=u.Resistor.NoPull;this.setObserverState(0);if(this.gpio){(t=this.gpio)===null||t===void 0?void 0:t.close();return}(()=>r(this,void 0,void 0,(function*(){const t=yield this.gpioPromise;t===null||t===void 0?void 0:t.close()})))()}read(t){t===null||t===void 0?void 0:t(this.state);return this.state}handleStateReceived(t){MightyGpio._events.handle(`state-received[${this.pin}]`,t)}unhandleStateReceived(){MightyGpio._events.unhandle(`state-received[${this.pin}]`)}listenInform(){MightyGpio._gpioEvents.on("inform-state",this._informListener)}unlistenInform(){MightyGpio._gpioEvents.off("inform-state",this._informListener)}handleStateConfirmed(t){MightyGpio._events.handle(`state-confirmed[${this.pin}]`,t)}unhandleStateConfirmed(){MightyGpio._events.unhandle(`state-confirmed[${this.pin}]`)}invokeStateConfirmed(t,e){return MightyGpio._events.invoke(`state-confirmed[${this.pin}]`,t,e).catch((t=>{console.error(t)}))}setObserverState(t,e){MightyGpio._events.emit("state-assigned",this.pin,!!t,this.mode,e)}static getGpioPin(t,e){return r(this,void 0,void 0,(function*(){const i=yield MightyGpio.arrayGpio;let o;if(e===u.PinMode.In){o=i===null||i===void 0?void 0:i.setInput(t)}else{o=i===null||i===void 0?void 0:i.setOutput(t)}return o}))}}class InputPin extends Pin{constructor(t){super(t);this.mode=u.PinMode.In;this.resistor=u.Resistor.NoPull;this.gpioPromise=this.initGpio(this.pin);this.setObserverState(this.state);this.handleStateConfirmed(((t,e)=>{MightyGpio._events.emit("state-watch",this.pin,t,e);MightyGpio._events.emit(`state-watch[${this.pin}]`,t,e);return!!e}));this.handleStateReceived(((t,e,i)=>r(this,void 0,void 0,(function*(){if(e!==this.mode)return;const o=this.state;const s=u.Resistor[i]!==this.resistor;const n=!!i;if(n&&s){this.invokeStateConfirmed(u.Edge.Unknown,this.state);return}if(o===t){this.invokeStateConfirmed(u.Edge.Unknown,this.state);return}this.state=t;const r=!!this.state&&!o;const a=!this.state&&!!o;let h=u.Edge.Low;if(r){h=u.Edge.High}else if(a){h=u.Edge.Low}this.invokeStateConfirmed(h,t);MightyGpio._events.emit("state-watch",this.pin,h,t)}))))}watch(...t){const{edge:e,callback:i,s:o}=this.parseWatchArgs(...t);let s=0;MightyGpio._events.on(`state-watch[${this.pin}]`,((t,n)=>{const r=Date.now();const a=s+o<=r;const h=e===t||e===u.Edge.Both;if(!a||!h)return false;s=r;i(n)}))}unwatch(){var t;this.unhandleStateConfirmed();MightyGpio._events.removeAllListeners(`state-watch[${this.pin}]`);if(this.gpio){(t=this.gpio)===null||t===void 0?void 0:t.unwatch();return}(()=>r(this,void 0,void 0,(function*(){const t=yield this.gpioPromise;t===null||t===void 0?void 0:t.unwatch()})))()}close(){super.close();this.unwatch()}setPud(t){var e;if(t===undefined){this.resistor=u.Resistor.NoPull}else if(t==="pu"||t===1){this.resistor=u.Resistor.PullUp}else if(t==="pd"||t===0){this.resistor=u.Resistor.PullDown}if(this.gpio){(e=this.gpio)===null||e===void 0?void 0:e.setPud(t);return}(()=>r(this,void 0,void 0,(function*(){const e=yield this.gpioPromise;e===null||e===void 0?void 0:e.setPud(t)})))()}parseWatchArgs(...t){let e=u.Edge.Both;let i;let o=100;if(typeof t[0]==="function"){i=t[0];if(typeof t[1]==="number"){o=t[1]}}else{e=t[0];i=t[1];if(typeof t[2]==="number"){o=t[2]}}return{edge:e,callback:i,s:o}}initGpio(t){return r(this,void 0,void 0,(function*(){var e;if(MightyGpio._mode===u.Mode.Emulated)return;if(MightyGpio._gpioScheme===u.GpioScheme.Broadcom){t=h.BroadcomScheme[t]}const i=yield Pin.getGpioPin(t,u.PinMode.In);this.gpio=i;this.state=+!!((e=this.gpio)===null||e===void 0?void 0:e.state);this.isHardware=!!this.gpio;const o=1;let s=this.state;i===null||i===void 0?void 0:i.watch(u.Edge.Both,(t=>{if(MightyGpio._inverted){t=+!t}this.state=t;if(t===s)return;const e=t===1&&s===0;const i=t===0&&s===1;if(e){this.invokeStateConfirmed(u.Edge.High,t)}else if(i){this.invokeStateConfirmed(u.Edge.Low,t)}if(e||i){s=t;const e=u.Resistor[this.resistor];this.setObserverState(t,e)}}),o);return i}))}}class OutputPin extends Pin{constructor(t){super(t);this.mode=u.PinMode.Out;this.gpioPromise=this.initGpio(this.pin);this.setObserverState(this.state);this.handleStateReceived(((t,e)=>r(this,void 0,void 0,(function*(){if(e!==this.mode)return;const i=this.state;this.state=t;if(i===this.state){this.invokeStateConfirmed(u.Edge.Unknown,t);return}const o=this.state&&!i;const s=!this.state&&i;let n=u.Edge.Low;if(o){n=u.Edge.High}else if(s){n=u.Edge.Low}this.invokeStateConfirmed(n,t);const r=yield this.gpioPromise;r===null||r===void 0?void 0:r.write(t)}))))}on(...t){this.setStateWithDelay(1,...t)}off(...t){this.setStateWithDelay(0,...t)}write(t,e){this.setStateWithDelay(+!!t,e)}pulse(t,e){this.setStateWithDelay(1,(()=>{setTimeout((()=>{this.setStateWithDelay(0,e)}),t)}))}setStateWithDelay(t,...e){const{t:i,callback:o}=this.parseOnOffArgs(...e);const dispatcher=e=>{const i=this.state;this.state=t;if(!e)o===null||o===void 0?void 0:o(t);else e===null||e===void 0?void 0:e.write(t,(t=>o===null||o===void 0?void 0:o(t)));const s=t===1&&i===0;const n=t===0&&i===1;if(s||n){this.setObserverState(t,u.Resistor[this.resistor])}};if(!i){if(this.gpio){dispatcher(this.gpio);return}return this.gpioPromise.then((t=>{dispatcher(t)}))}return new Promise(((t,e)=>{setTimeout((()=>{this.gpioPromise.then((e=>{dispatcher(e);t()}))}),i)}))}parseOnOffArgs(...t){let e=0;let i;if(typeof t[0]==="function"){i=t[0]}else if(typeof t[0]==="number"){e=t[0];if(typeof t[1]==="function"){i=t[1]}}return{t:e,callback:i}}initGpio(t){return r(this,void 0,void 0,(function*(){var e;if(MightyGpio._mode===u.Mode.Emulated)return;if(MightyGpio._gpioScheme===u.GpioScheme.Broadcom){t=h.BroadcomScheme[t]}const i=yield Pin.getGpioPin(t,u.PinMode.Out);this.gpio=i;this.state=+!!((e=this.gpio)===null||e===void 0?void 0:e.state);this.isHardware=!!this.gpio;return i}))}}const p=Object.values(h.BroadcomScheme).length;MightyGpio._gpioEvents.setMaxListeners(p);e.setInput=MightyGpio.setInput;e["in"]=e.setInput;e.setOutput=MightyGpio.setOutput;e.out=MightyGpio.setOutput;e.watchInput=MightyGpio.watchInput;e.setInverted=MightyGpio.setInverted;e.forceEmulation=MightyGpio.forceEmulation;e.useBroadcomScheme=MightyGpio.useBroadcomScheme;e.setObservers=MightyGpio.setObservers;e.ready=MightyGpio.ready;e.startPWM=MightyGpio.startPWM;e.startSPI=MightyGpio.startSPI;e.startI2C=MightyGpio.startI2C},439:(t,e)=>{"use strict";Object.defineProperty(e,"__esModule",{value:true});e.Mode=e.GpioScheme=e.PinMode=e.Edge=e.Resistor=void 0;var i;(function(t){t[t["NoPull"]=-1]="NoPull";t[t["PullDown"]=0]="PullDown";t[t["PullUp"]=1]="PullUp"})(i||(e.Resistor=i={}));var o;(function(t){t[t["Low"]=0]="Low";t[t["High"]=1]="High";t["Both"]="both";t["Unknown"]="unknown"})(o||(e.Edge=o={}));var s;(function(t){t["Out"]="out";t["In"]="in"})(s||(e.PinMode=s={}));var n;(function(t){t["Physical"]="physical";t["Broadcom"]="broadcom"})(n||(e.GpioScheme=n={}));var r;(function(t){t["Real"]="real";t["Emulated"]="emulated"})(r||(e.Mode=r={}));e["default"]={Resistor:i,Edge:o,PinMode:s,GpioScheme:n,Mode:r}},988:module=>{module.exports=eval("require")("array-gpio")},434:t=>{"use strict";t.exports=require("events")},598:t=>{"use strict";t.exports=require("node:crypto")},997:t=>{"use strict";t.exports=require("node:timers")}};var __webpack_module_cache__={};function __nccwpck_require__(t){var e=__webpack_module_cache__[t];if(e!==undefined){return e.exports}var i=__webpack_module_cache__[t]={exports:{}};var o=true;try{__webpack_modules__[t].call(i.exports,i,i.exports,__nccwpck_require__);o=false}finally{if(o)delete __webpack_module_cache__[t]}return i.exports}if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var __webpack_exports__=__nccwpck_require__(730);module.exports=__webpack_exports__})();