uci-mcp/lib/port-pin.js

177 lines
4.2 KiB
JavaScript

'use strict'
const _ = require('@uci/utils')
class Pin {
constructor(address, opts = {}) {
// at a minimum address is required
this.address = new _.Byte(address, opts.fmt)
// a unique pin id MUST be supplied in opts or it must be set afterwards!
this.id = opts.pid
// a custom pin configuration can be passed or choosen from one of the sets, if neither than the default output config is chosen
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
}
set adr(add) {
if (Number.isInteger(add)) {
this.address.reset(add)
} else {
this.address.reset(add.val, add.fmt)
}
}
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(`=====Pin ${this.id} =====\nAddress ${this.address.toFmt('HEX')}`)
console.log('Conifguration: ', this.config)
}
} // End GPIO Pin
//======================
// A port of 8 GPIO pins
class Port {
constructor(opts) {
if (opts) {
this.reverse = opts.reverse ? opts.reverse : false
this.id = opts.portID ? opts.portID : ''
this.desc = opts.portDesc ? opts.portDesc : ''
} else {
opts = {}
this.reverse = false
}
// Create group of 8 pins for port
this.pins = new Array(8)
let pid = ''
for (let i = 0; i < 8; i++) {
if (opts.pids) {
pid = opts.pids[i]
} else {
pid = this.reverse ? 8 - i : i + 1
}
// using default decimal for pin addresses
this.pins[i] = new Pin(Math.pow(2, i), _.merge(opts, { pid: pid }))
// console.log(`\n\n====PORT ${this.id} =====`)
// this.pins[i].show()
}
}
// return a handle to all pins on a port
get allPins() {
return this.pins
}
// return handle to a port's pin from id
pin(id) {
for (let i = 0; i < 8; i++) {
if (this.pins[i].id === id) {
return this.pins[i]
}
}
return false
}
// return pin's id on a port from address
pid(address) {
if (Number.isInteger(address)) { address = new _.Byte(address) }
let i = address.toFmt('PLC')[0] - 1
return this.pins[i].id
}
} // end GPIO Port CLass
//====================
// needs to take current pin and pick out details and make a call the contstructor.
// clone(adr, alias) {
// let newPin = _.clone(this)
// newPin.address = adr
// newPin.alias = alias
// return newPin
// }
// 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
}
}