mod-io

Package for interfacing with the Olimex MOD-IO board

Usage no npm install needed!

<script type="module">
  import modIo from 'https://cdn.skypack.dev/mod-io';
</script>

README

MOD IO

Package for interfacing with the Olimex MOD-IO development board. Since the board communicates over I2C, i2c-bus is used.

I2C setup

To give the node module i2c-bus access to I2C ports, i2c-dev needs to be added to system modules and given R/W permission. This can be done with

echo i2c-dev >> /etc/modules
touch /etc/udev/rules.d/99-i2c.rules
echo SUBSYSTEM==\"i2c-dev\", MODE=\"0666\" >> /etc/udev/rules.d/99-i2c.rules

Use

A connection is opened in the constructor of the ModIoConnection class. Both synchronous and Promisifed calls are supported.

Synchronous call example

for (let index = 1; index < 5; index++) {
    console.log(`Analog input ${index}: ${connection.readAnalogInputSync(index)}`);
}

Promisified call example

for (let index = 1; index < 5; index++) {
    promises.push(connection.readAnalogInputPromisified(index))
}
Promise.all(promises).then(result => console.log(result))

API

Synchronous and Promisified methods accept the same parameters, the only difference is their execution and return type

ModIoConnection(deviceId, address)

Both the device ID and address are optional and will default to 1 and 0x58 respectively. To check available list IDs, use

sudo i2cdetect -l

and then

sudo i2cdetect -y [deviceId]

to see if the set board address shows up (0x58 by default)

Previously read values can be accessed from the state attribute without the need to query the board

state = {
      relays: 0x00,
      analogInputs: Array(4),
      digitalInputs: Array(4),
    }

setRelaySync(id: number, value: boolean): number

setRelayPromisified(id: number, value: boolean): Promise

Set individual relay to provided value and stores the change in local state

@param id — Relay ID from 1 to 4

@param value - boolean to set the controll bit

@returns - current state of all relays

getRelay(id: number): boolean

Helper function that reads the individual bits that were set for each relay. The values are read from the internal object state, not from the board.

@param id — Relay ID from 1 to 4

@returns — Relay value

readAnalogInputSync(id: number): number

readAnalogInputPromisified(id: number): Promise

Read voltage from analog input in the range 0-3.3V

@param id — Analog input ID from 1 to 4

@returns — Voltage of selected input

readDigitalInputSync(id?: number | undefined): boolean | boolean[]

readDigitalInputPromisified(id?: number | undefined): Promise | Promise<boolean[]>

Reads all four digital inputs from the board. If a valid ID is provided, will return only that boolean, otherwise returns the entire Array. Values are stored in the local state so they can be checked without querying again.

@param id — Digital input ID from 1 to 4

@returns — Specific input value or all four values

sendCustomCommandSync(command: number, input?: number | Buffer | undefined, outputBuffer?: Buffer | undefined): void

sendCustomCommandPromisified(command: number, input?: number | Buffer | undefined, outputBuffer?: Buffer | undefined): Promise | void

Sending custom commands to MOD IO boards with modified firmware

@param command

@param input — Can be either a number as a command parameter or a Buffer with more parameters

@param outputBuffer — Userd for returning values and isn't mandatory

setNewAddressSync(newAddress: number): void

setNewAddressPromisified(newAddress: number): Promise

Special conditions must be met in order to change the MOD IO board address To protect the device from accidental address updates the user should hold the on-board button pressed (not the RESET button!) while issuing the command. Successful update is indicated with the onboard status LED being lit for 2-3 seconds. Address is immediately updated so the board will not respond to its old address any more.

@param newAddress