Channels¶
This page describes everthing about the core/channel.js file, which provides classes to connect to a device with different protocols.
BIPES uses Web Serial API,
Web Bluetooth API and WebSocket API, in which we handle inside the webserial
, webbluetooth
and websocket
classes respectively.
All of them inside the core/channel.js along side the mux
class, a ‘multiplexer’ between these protocols.
Web Serial API and Web Bluetooth API require HTTPS for the server version, while the WebSocket API depends on the connection type of the device. For now, MicroPython firmware only does unsecure connection (see MicroPython WebREPL repository for more information), therefore BIPES connection with WebSocket API can only be done via HTTP.
The Multiplexer¶
To send data to a device, the ‘no ui’ way, just do:
Channel.mux.currentChannel = 'webserial'|'webbluetooth'|'websocket'
Channel.mux.connect();
mux.bufferPush (code_);
That’s it, BIPES will connect to the device, append the code to a buffer in the connected protocol’s class and send it. More info about the class:
- class mux()¶
Init the mux class as Channel.mux, will check if it has been loaded as HTTPS, HTTP or locally (“file:”) to handle browser policies for the protocols.
- mux.connect()¶
Connect using the target protocol, call as Channel.mux.connect()
- mux.switch(channel_)¶
Switch the target protocol if available, see mux.constructor
- Arguments
channel_ (
string()
) – the protocol to be switched to
- mux.bufferPush(code, callback)¶
Send data to the buffer of the target protocol, to be sent to the device with the target protocol. A callback function can be passed and will be called after the MicroPython REPL “>>> ” charset is detected. This means understanding how your code executes is crucial. Call as mux.bufferPush()
- Arguments
code (
string()
) – the code to be sent to the device with the target protocolcallback (
function()
) – the callback function to be called when the code has been executed.
- mux.bufferUnshift(code)¶
Send data to the first position of the buffer of the target protocol, to be sent to the device with the target protocol. This means will it’ll be executed as soon as possible, is useful for reset commands.
- Arguments
code (
string()
) – the code to be sent immediatally to the device with the target protocol
- mux.clearBuffer()¶
Clears the buffer of the connected protocol, code won’t be sent.
- mux.connected()¶
Return if a device is connected via any protocol, call as mux.connected()
- Returns
boolean – True if a device is connected via any protocol
- mux.disconnect()¶
Disconnect using the target protocol, call as mux.disconnect()
WebSerial¶
With the use of Web Serial API , data can be sent via a serial port, with the help of the webserial
class.
- class webserial()¶
Init the webserial, stored as Channel.websocket
- webserial.buffer_¶
Store the code to be sent to the device
- webserial.connect()¶
Connect using webserial protocol, will ask user permission for the serial port.
- webserial.connect_()¶
User interface styling for when connected via webserial protocol.
- webserial.disconnect()¶
Disconnect device connected with webserial protocol.
- webserial.resetBoard()¶
Reset board on connect with webserial protocol, action enabled by a checkbox on the user interface.
- webserial.serialWrite(data)¶
Directly send code via webserial, normally called by this.watch()
- Arguments
data (
Uint8Array|string|number()
) – code to be sent via webserial
- webserial.watch()¶
Runs every 50ms to check if there is code to be sent in the
webserial#buffer_
(appended withmux.bufferPush()
)
WebBluetooth¶
With the use of Web Bluetooth API , data can be sent via Bluetooth with the help of the webbluetooth
class.
- class webbluetooth()¶
Init the webbluetooth, stored as Channel.websocket
- webbluetooth.buffer_¶
Store the code to be sent to the device
- webbluetooth.connect()¶
Connect using webbluetooth protocol, will ask user permission for the bluetooth device.
- webbluetooth.disconnect()¶
Disconnect device connected with webbluetooth protocol.
- webbluetooth.handleNotifications(event)¶
Handles data received via webbluetooth
- Arguments
event (
string()
) – data received via webbluetooth
- webbluetooth.sendNextChunk(operation)¶
Directly send code via webbluetooth, normally called by this.watch() uses a promise to handshake sent chunks, will retry in 500ms if a chunk fails
- Arguments
operation (
string()
) – code to be sent via webbluetooth
- webbluetooth.watch()¶
Runs every 50ms to check if there is code to be sent in the
webbluetooth#buffer_
(appended withmux.bufferPush()
)
WebSockets¶
With the use of WebSocket API , data can be sent via TCP/IP, with the help of the websocket
class.
- class websocket()¶
Init the websocket, stored as Channel.websocket
- websocket.buffer_¶
Store the code to be sent to the device
- websocket.connect(url, pass)¶
Connect using websocket protocol.
- Arguments
url (
string()
) – url/IP of the devicepass (
string()
) – password to connect to the device
- websocket.watch()¶
Runs every 50ms to check if there is code to be sent in the
websocket#buffer_
(appended withmux.bufferPush()
)