ixfx
    Preparing search index...

    Class EspruinoBleDevice

    An Espruino BLE-connection

    See online demos

    Use the puck function to initialise and connect to a Puck.js. It must be called in a UI event handler for browser security reasons.

    import { Espruino } from 'https://unpkg.com/ixfx/dist/io.js'
    const e = await Espruino.puck();

    To connect to a particular device:

    import { Espruino } from 'https://unpkg.com/ixfx/dist/io.js'
    const e = await Espruino.puck({name:`Puck.js a123`});

    Listen for events:

    // Received something
    e.addEventListener(`data`, d => console.log(d.data));
    // Monitor connection state
    e.addEventListener(`change`, c => console.log(`${d.priorState} -> ${d.newState}`));

    Write to the device (note the \n for a new line at the end of the string). This will execute the code on the Espruino.

    e.write(`digitalPulse(LED1,1,[10,500,10,500,10]);\n`);
    

    Run some code and return result:

    const result = await e.eval(`2+2\n`);
    

    Hierarchy (View Summary)

    Constructors

    Properties

    codec: Codec
    evalReplyBluetooth: boolean = true
    evalTimeoutMs: number
    gatt: undefined | BluetoothRemoteGATTServer
    rx: undefined | BluetoothRemoteGATTCharacteristic
    states: Flow.StateMachine.WithEvents<
        Readonly<
            Readonly<
                {
                    closed: "connecting";
                    connected: string[];
                    connecting: string[];
                    ready: "connecting";
                },
            >,
        >,
    >
    tx: undefined | BluetoothRemoteGATTCharacteristic
    verboseLogging: boolean = false

    Accessors

    • get isClosed(): boolean

      Returns boolean

    • get isConnected(): boolean

      Returns boolean

    • get isDisposed(): boolean

      Returns boolean

    Methods

    • Adds event listener.

      Type Parameters

      • Kextendskeyof IoEvents<
            Readonly<
                Readonly<
                    {
                        closed: "connecting";
                        connected: string[];
                        connecting: string[];
                        ready: "connecting";
                    },
                >,
            >,
        >

        Events

      Parameters

      • name: K

        Event name

      • listener: (
            event: IoEvents<
                Readonly<
                    Readonly<
                        {
                            closed: "connecting";
                            connected: string[];
                            connecting: string[];
                            ready: "connecting";
                        },
                    >,
                >,
            >[K],
            sender: SimpleEventEmitter<
                IoEvents<
                    Readonly<
                        Readonly<
                            {
                                closed: "connecting";
                                connected: string[];
                                connecting: string[];
                                ready: "connecting";
                            },
                        >,
                    >,
                >,
            >,
        ) => void

        Event handler

      Returns void

      Error if emitter is disposed

    • Sends some code to be executed on the Espruino. The result is packaged into JSON and sent back to your code. An exception is thrown if code can't be executed for some reason.

      const sum = await e.eval(`2+2`);
      

      It will wait for a period of time for a well-formed response from the Espruino. This might not happen if there is a connection problem or a syntax error in the code being evaled. In cases like the latter, it will take up to timeoutMs (default 5 seconds) before we give up waiting for a correct response and throw an error.

      Tweaking of the timeout may be required if eval() is giving up too quickly or too slowly. A default timeout can be given when creating the class.

      Options: timeoutMs: Timeout for execution. 5 seconds by default assumeExclusive If true, eval assumes all replies from controller are in response to eval. True by default debug: If true, execution is traced via warn callback

      Parameters

      • code: string

        Code to run on the Espruino.

      • opts: EvalOpts = {}

        Options

      • Optionalwarn: (message: string) => void

        Function to pass warning/trace messages to. If undefined, this.warn is used, printing to console.

      Returns Promise<string>

    • Fire event

      Type Parameters

      • Kextendskeyof IoEvents<
            Readonly<
                Readonly<
                    {
                        closed: "connecting";
                        connected: string[];
                        connecting: string[];
                        ready: "connecting";
                    },
                >,
            >,
        >

      Parameters

      • type: K

        Type of event

      • args: IoEvents<
            Readonly<
                Readonly<
                    {
                        closed: "connecting";
                        connected: string[];
                        connecting: string[];
                        ready: "connecting";
                    },
                >,
            >,
        >[K]

        Arguments for event

      Returns void

    • Remove event listener

      Type Parameters

      • Kextendskeyof IoEvents<
            Readonly<
                Readonly<
                    {
                        closed: "connecting";
                        connected: string[];
                        connecting: string[];
                        ready: "connecting";
                    },
                >,
            >,
        >

      Parameters

      • type: K
      • listener: (
            event: IoEvents<
                Readonly<
                    Readonly<
                        {
                            closed: "connecting";
                            connected: string[];
                            connecting: string[];
                            ready: "connecting";
                        },
                    >,
                >,
            >[K],
            sender: SimpleEventEmitter<
                IoEvents<
                    Readonly<
                        Readonly<
                            {
                                closed: "connecting";
                                connected: string[];
                                connecting: string[];
                                ready: "connecting";
                            },
                        >,
                    >,
                >,
            >,
        ) => void

      Returns void

    • Writes a script to Espruino.

      It will first send a CTRL+C to cancel any previous input, reset() to clear the board, and then the provided code followed by a new line.

      Use eval instead to execute remote code and get the result back.

      // Eg from https://www.espruino.com/Web+Bluetooth
      writeScript(`
      setInterval(() => Bluetooth.println(E.getTemperature()), 1000);
      NRF.on('disconnect',()=>reset());
      `);

      Parameters

      • code: string

        Code to send. A new line is added automatically.

      Returns Promise<void>