'use strict' const _ = require('@uci/utils') class Pin { constructor(id, address, opts = {}) { // at a minimum a byte address is required this.address = address // a pin id MUST be supplied in opts 1 to 8 this.id = id // a custom pin configuration can be passed or choosen from one of the sets, if neither than the default output config is chosen // TODO allow custom pin configurations based on passed opts so individual pins can be configured differently if (opts.pin_cfg_default) { if (_.keyExists(config_sets, opts.pin_cfg_default)) { this.config = _.clone(config_sets[opts.pin_cfg_default]) } else { console.log(`WARNING config set ${opts.pin_cfg_default} not found using default output set`) this.config = _.clone(config_sets.output) } } else { this.config = _.clone(config_sets.output) } // after setting a default overwrite part or all of pin config with custom if (opts.pinConfig) { this.config = Object.assign(this.config, opts.pinConfig) } // this.cfgSet = false // set to true when for sure the configuration has been written, changed to false when config is changed. } //getters and setters get adr() { return this.address } get pid() { return this.id } set pid(pid) { this.id = pid } // config getters and setters get cfg() { return this.config } set cfg(config) { this.config = _.merge(this.config, config) // this merges partial changes // write the config here. } show() { console.log(`\n=====Pin ${this.id} =====\n Decimal Address ${this.address}`) console.log('Conifguration: ', this.config) } } // End GPIO Pin //====================== // A port of 8 GPIO pins class Port { constructor(opts = {}) { this.id = opts.portID ? opts.portID : '' this.desc = opts.portDesc ? opts.portDesc : '' // Create group of 8 pins for port this.pins = new Array(8) let pid = '' for (let i = 0; i < 8; i++) { pid = opts.pids ? opts.pids[i] : i + 1 this.pins[i] = new Pin(pid, Math.pow(2, i), opts) } } // return handle to a port's pin from id pin(id) { for (let pins of this.pins) { if (pin.id === id) { return pin } } return false } // return pin's id on a port from address pid(address) { return Math.log(address) / Math.log(2) } } // end GPIO Port CLass //==================== // return list of all pin configs, handle to particular config or adding of custom pin configuration at runtime function configs(cfg) { if (_.isString(cfg)) { if (cfg === 'list') { return Object.keys(config_sets) } if (_.keyExists(config_sets, cfg)) { return config_sets[cfg] } return 'configuration no found' } _.merge(config_sets, cfg) // console.log(config_sets) return `pin configuration ${Object.keys(cfg)} added to configuration set` } module.exports = { Port, Pin, configs } let config_sets = { output: { dir: 0, // 0 output,1 input ivrt: 0, pullup: 0, intr: 0, // if intr = 0 usedef,deval not used usedef: 0, // if usedef = 0 defval not used defval: 0 }, toggle_switch: { dir: 1, // 0 output,1 input ivrt: 1, // for reading let 1 be zero and vice versa pullup: 1, intr: 1, // if intr = 0 usedef,deval not used usedef: 0, // if usedef = 0 defval not used defval: 0 }, momentary_switch: { dir: 1, // 0 output,1 input ivrt: 1, // for reading let 1 be zero and vice versa pullup: 1, intr: 1, // if intr = 0 usedef,deval not used usedef: 1, // if usedef = 0 defval not used defval: 1 } }