Utilities

This page describes everything about the core/utils.js file, which provides functions and classes for specific features or modules.

Handling device files

All operations that involves devices files or the file editor happens inside the files class.

BIPES uses CodeMirror as its text editor.

class files(fileList)

Handle the Files tab.

files.editor

Object that contains a codemirror editor

files.delete(file)

Delete a file.

Arguments
  • file (string()) – File name of the file to be deleted.

files.editedXML2Workspace()

Push edited XML to the workspace.

files.files_download(file)

Request a file to download.

Arguments
  • file (string()) – File name of the file to be downloaded.

files.files_save_as()

Get file from ``codemirror``editor and calls Files.put_file() to upload.

files.files_view(file)

Request a file to show in the codemirror editor.

Arguments
  • file (string()) – File name of the file to be viewed.

files.get_file(src_fname)

Get file from device

Arguments
  • src_fname (string()) – File name of the file to be fetched.

files.get_file_webserial_()

Subfuction to get file from device with webserial or webbluetooth.

files.get_ver()

Get version.

files.handleCurrentProject()

Update the displayed name and automatic opened code when switching tabs or projects.

files.handle_put_file_select()

Get file from DOM #putFileButton and calls Files.put_file() to upload

files.internalPython()

“Open” MicroPython code generated from Blockly in the ``codemirror``editor.

files.internalXML()

“Open” XML code generated from Blockly and BIPES in the ``codemirror``editor.

files.listFiles()

List files from device, on success, calls files.updateTable() to display it.

files.put_file()

Upload file to device.

files.resize()

Resize the codemirror editor, triggered by window.onresize event.

files.run(file)

Execute a program.

Arguments
  • file (string()) – File name of the script to be executed.

files.updateTable()

Display fetched from device file list in DOM #fileList.

files.update_file_status(s)

Display text inside DOM #file-status as a operation progress

Arguments
  • s (string()) – The notification to be shown.

The terminal

BIPES uses xterm.js as its terminal. The term class makes working with the terminal easier.

class term()

Handle xterm.js terminal

term.off()

Disable the terminal.

term.on()

Enable the terminal.

term.resize()

Resize the xterm.js terminal, triggered by window.onresize event.

term.write(data)

Write data in the terminal.

DOM Node

To make creation of DOM elements easier, the class DOM provides some a fluent interface to create Node elements.

For example to create a div with two buttons (#trashIcon and #runIcon) with on click event, just do:

let deleteButton_ = new DOM ('span', {
                className:'icon',
                id:'trashIcon'
        })
        .onclick (this,delete,[file])
let runButton_ = new DOM ('span', {
                className:'icon',
                id:'runIcon'
        })
        .onclick (this,run,[file])

let wrapper_ = new DOM ('div');
        .append([runButton_, deleteButton_]);

Note that the function DOM.onclick binds the first argument as the this keyword of the function (second argument) to be called. The third argument is a list of the arguments that will be applied to the function.

class DOM(dom, tags)

Make DOM Node element

DOM.append(DOMS)

Appends others DOM().

Arguments
  • DOMS (Array.) – Array of DOM() or/and direct DOM Nodes.

DOM.flag(str)

Adds a label to the DOM().

Arguments
  • str (string()) – Message inside the label.

DOM.onclick(self, ev, args)

Append a onclick event.

Arguments
  • self (Array.) – Object to bind to the call.

  • ev (function()) – Function to call on click.

  • args (Array.) – Arguments to pass to the function.

Lots of tools

BIPES concentrates all generic function inside the Tool class, in which all functions are static and the class is not inited. A brief description of every function can be seen below, just Ctrl+F what you need.

class Tool()

All generic utilities are concetrated here. Should not be inited, since all functions are static.

Tool.EasyMQTTBridge(id_, value_)

Bridge incoming data to MQTT.

Arguments
  • id_ (number()) – ID for the MQTT message.

  • value_ (number()) – Value for the MQTT message.

Tool.HEX2RGB(hex)

Converts HEX to RGB

Arguments
  • hex (string()) – HEX code

Returns

Object|null – RGB code for the RGB color.

Tool.HUE2HEX(h, s, l)

Converts HUE to HEX

Arguments
  • h (number()) – Hue, from 0 to 360.

  • s (number()) – Saturation, from 0 to 100.

  • l (number()) – Lightness, from 0 to 100.

Returns

string – HEX code for the HUE color.

Tool.RGB2HEX(r, g, b)

Converts RGB to HEX

Arguments
  • r (number()) – Red color, from 0 to 255.

  • g (number()) – Green color, from 0 to 255.

  • b (number()) – Blue color, from 0 to 255.

Returns

string – HEX code for the RGB color.

Tool.asleep(milliseconds)

(DEPRECATED) New async sleep function, callend with async await(), which allows UI updates

Tool.bipesVerify()

Checks the income data for useful chuncks, like BIPES-DATA: for plotting

Tool.blocksToPython()

(DEPRECATED) Generate code from blocks, appends to the file editor.

Tool.clearQueue()

Clear ‘core/queue.js queue

Tool.decode_resp(data)

Decode data to fetch status code.

Arguments
  • data (char()) – Response data.

Returns

number – Status code

Tool.emptyXML()

Return a empty XML with only project description set, used for new projects

Tool.getText(pName)

Get code for a MicroPython library, must be available at ui/pylibs.

Arguments
  • pName (string()) – File name for a MicroPython library.

Tool.makeAName(code, ext)

Makes a name for a Blockly project.

Arguments
  • code (string()) – Blockly generated code.

  • ext (string()) – File extension.

Tool.runPython(code_)

(DEPRECATED) Alias for mux.bufferPush().

Arguments
  • code_ (string()) – Code to be sent.

Tool.sleep(milliseconds)

(DEPRECATED) Delay Javascript code execution.

Tool.stopPython()

Send \x03\x03 to stop running a program, see a ASCII table to know more.

Tool.uid()

Return a random UID

Tool.unix2date(timestamp)

Make a date with a unix time, if not passed, will make one.

Arguments
Returns

string – Formatted time, e.g. 08:02:01.

Tool.updateSourceCode(code, file_name)

Add a file to the file editor

Arguments
  • code (string()) – Code.

  • file_name (string()) – File name for the code.

Tool.warningIfTrue(self, criteria)

throw a notification is the criteria is met.

Arguments
  • self (object()) – A object that contains a warning_ array to keep track if the notification was already thrown.

  • criteria (array()) – A array composed by subarrays.

  • criteria.func (function()) – Function with the criteria.

  • criteria.str (string()) – Message to show as notification if the criteira is met.