Options
All
  • Public
  • Public/Protected
  • All
Menu

Class PeripheralI2C<EventTypes>

i2c can be used. Master/Slave mode. But slave mode only works with "written" events. You can't set data to be read.

Type parameters

  • EventTypes: string

Hierarchy

Index

Constructors

constructor

Properties

Obniz

Obniz: Obniz

obniz to be used

Optional onerror

onerror: undefined | ((error: any) => void)

from obniz.js 1.14.0

It sets a function to receive error when i2c bus error occurs. By setting a function, obniz.error will never be called.

// Javascript Example
obniz.i2c0.start({mode:"master", sda:2, scl:3, clock:400000});
obniz.i2c0.onerror = function(err) {
  console.log('Error', err);
}
var ret = await obniz.i2c0.readWait(0x50, 1);

Optional onwritten

onwritten: undefined | ((data: number[], address: number) => void)

Slave mode only.

It is a callback that is called when data is written. Received data is fragmented. When written data is 100byte, you possibly receive it in 56 byte and 44 byte. For data over 1024 bytes, few bytes may be dropped.

// Javascript Example
obniz.i2c0.start({mode: "slave", sda: 0, scl: 1, slave_address: 0x01});
obniz.i2c0.onwritten = function(data){
  console.log(data);
}

timeout

timeout: number = 30 * 1000

Rsponse waiting timeout in milliseconds

Static prefixed

prefixed: string | boolean

Methods

addListener

  • addListener(event: EventTypes, fn: ListenerFn, context?: any): this
  • Parameters

    • event: EventTypes
    • fn: ListenerFn
    • Optional context: any

    Returns this

emit

  • emit(event: EventTypes, ...args: Array<any>): boolean
  • Calls each of the listeners registered for a given event.

    Parameters

    • event: EventTypes
    • Rest ...args: Array<any>

    Returns boolean

end

  • end(): void
  • end i2c .

    // Javascript Example
    obniz.i2c0.start({mode:"master", sda:2, scl:3, clock:400000});
    obniz.i2c0.end();

    Returns void

eventNames

  • eventNames(): Array<EventTypes>
  • Return an array listing the events for which the emitter has registered listeners.

    Returns Array<EventTypes>

listenerCount

  • listenerCount(event: EventTypes): number
  • Return the number of listeners listening to a given event.

    Parameters

    • event: EventTypes

    Returns number

listeners

  • listeners(event: EventTypes): Array<ListenerFn>
  • Return the listeners registered for a given event.

    Parameters

    • event: EventTypes

    Returns Array<ListenerFn>

notifyFromObniz

  • notifyFromObniz(json: any): void

off

  • off(event: EventTypes, fn?: EventEmitter.ListenerFn, context?: any, once?: undefined | false | true): this
  • Parameters

    • event: EventTypes
    • Optional fn: EventEmitter.ListenerFn
    • Optional context: any
    • Optional once: undefined | false | true

    Returns this

on

  • on(event: EventTypes, fn: ListenerFn, context?: any): this
  • Add a listener for a given event.

    Parameters

    • event: EventTypes
    • fn: ListenerFn
    • Optional context: any

    Returns this

once

  • once(event: EventTypes, fn: ListenerFn, context?: any): this
  • Add a one-time listener for a given event.

    Parameters

    • event: EventTypes
    • fn: ListenerFn
    • Optional context: any

    Returns this

readWait

  • readWait(address: number, length: number): Promise<number[]>
  • It reads data from the device. length defines the length of bytes. The treatment of address is same as write() function. This function will wait until data is received.

    // Javascript Example
    obniz.i2c0.start({mode: "master",sda:2, scl:3, clock:400000, pull:null});
    var ret = await obniz.i2c0.readWait(0x50, 1);
    console.log("read "+ret);

    Parameters

    • address: number
    • length: number

      Max is 1024;

    Returns Promise<number[]>

removeAllListeners

  • Remove all listeners, or those of the specified event.

    Parameters

    Returns this

removeListener

  • removeListener(event: EventTypes, fn?: EventEmitter.ListenerFn, context?: any, once?: undefined | false | true): this
  • Remove the listeners of a given event.

    Parameters

    • event: EventTypes
    • Optional fn: EventEmitter.ListenerFn
    • Optional context: any
    • Optional once: undefined | false | true

    Returns this

start

  • It starts i2c on given io sda, scl.

    Internal pull up is optional for io output setting. By default it is pull:null. See more on obniz.ioX.pull().

    For using internal-pull-up, you should specify "3v" to connect to 3.3v targets, and "5v" for 5v targets. When you choose internal pull up, speed is limited to up to 100khz, because internal pull up is not so tough. Please add external pull-up resistor on scl/sda and choose pull:null when you need more speed.

    // Javascript Example
    obniz.i2c0.start({mode:"master", sda:2, scl:3, clock:400000});
    obniz.i2c0.write(0x50, [0x00, 0x00, 0x12]);
    var ret = await obniz.i2c0.readWait(0x50, 1);
    console.log("read "+ret);
    • use internal pull up
    obniz.i2c0.start({mode:"master", sda:2, scl:3, clock:400000, pull:"5v"});
    • save mode
    obniz.i2c0.start({mode: "slave", sda: 0, scl: 1, slave_address: 0x01});

    Parameters

    Returns void

validate

  • validate(commandUri: any, json: any): WSSchema.MultiResult
  • Parameters

    • commandUri: any
    • json: any

    Returns WSSchema.MultiResult

write

  • write(address: number, data: number[]): void
  • It sends data to device which has the address

    // Javascript Example
    obniz.i2c0.start({mode: "master",sda:2, scl:3, clock:400000, pull:null});
    obniz.i2c0.write(0x50, [0x00, 0x00, 0x12]);

    Parameters

    • address: number

      7bit address only.

    • data: number[]

      Max length is 1024;

    Returns void

Generated using TypeDoc