Skip to content

CANServer v2 Scripting

Chris Whiteford edited this page Mar 20, 2021 · 8 revisions

Firmware v2 Manual

Manual Root

Scripting Screen

The Scripting screen is where you can edit the processing script that the CANServer runs. You can analyse values, process data, check inputs, toggle outputs, and generate data for microDisplays using a single unified script. The scripting language used by the CANServer is LUA.

The scripting screen is made up of two sections. The status area and the script area.

Status Area

The status area shows details about the state of the scrips. If the state of the script if good and there are no errors have occurred the CANServer will display the some timing information related to how the script is performing. The performance statistics are helpful to see if you are writing a script that will cause the CANServer to run slower then you expect. The CANServer runs the script once every 40 milliseconds. If your script takes longer then that to run then the frequency it runs will be reduced. Its best to try and keep your scripts performance to under 10 milliseconds, even better if you can keep it under 5. Status Good

If an error has occurred (either a syntax error or a runtime error) then the CANServer will attempt to provide a meaningful error message and a location where the error occurred. Status Bad

Script Area

Overview The script area is where you edit all your code. When you are happy with the script and want to save it you can click the Save button or use the keyboard shortcut of ctrl+s (windows) or command+s (macos).

Available Script Functions

The CANServer provides a mostly complete LUA implementation. A number of methods have been disabled that don't apply (like file IO as an example), but most of the standard LUA methods are usable.

There are also a number of custom methods provided to interact with the various parts of the CANServer.

Variable methods

These methods are for manipulating variables that can be used and reused by the scripts. Their values persist between calls to the script.

  • CANServer_getVar(varname)

    Takes a variable name as a string, and returns the stored value. If no value is set nil will be returned.

  • CANServer_setVar(varname, value)

    Takes a variable name as a string, and the value you wish to set

  • CANServer_clearVar(varname)

    Takes a variable name as a string, and clears the variable

Analysis Item methods

  • CANServer_getAnalysisVar(itemname)

    Takes an itemname (that matches an item from the Analysis screen) as a string, and returns the last analyzed value and a timestamp for when that value was processed. The timestamp is in milliseconds since boot and can be used along with the value returned from CANServer_millis() to find the age of the value.

microDisplay methods

  • CANServer_setDisplayString(displayid, displaystring)

    Takes a displayid as an integer, and a displaystring as a string. When called this method will set the output string for the specified display id.

  • CANServer_getDisplayInput(displayid, inputside)

    Returns the current input state for the specified displayid and inputside (0 = Left side, 1 = Right side)

Logging methods

  • CANServer_getLoggingMode()

    Returns the current logging mode when called.

    -1 = Logging Disabled (if there isn't an SD card installed logging cannot be enabled)
    0 = No Logging Active
    1 = Raw Logging
    2 = Interval Logging
    3 = Filtered Logging

  • CANServer_getLoggingInterval()

    Returns the logging interval that will be used if interval logging is active

  • CANServer_setLoggingMode(mode[, interval])

    Takes the logging mode as a integer and will swtich to that logging mode when called. Optionally it also accepts an interval parameter as an integer in milliseconds to set the logging interval when interval logging is active.

  • CANServer_addLogMark([markname])

    Takes an optional markname as a string. If the optional parameter is provided then that string will be used as the log mark. If called without a parameter then an incrementing numeric mark will be added to the active log.

Onboard IO methods

  • CANServer_getPinState(pinnumber)

    For onboard IO pins configured as inputs, this method takes a pin number as an integer (from 1 to 5). Returns the current pin state for the passed in pin number. Pins are digital inputs and the return value will be a boolean value.

  • CANServer_setPinState(pinnumber, pinstate)

    For onboard IO pins configured as outputs, this method takes a pin number as an integer (from 1 to 5) and a pin state that is 0 ror 1 and will set the output value of the passed in pin number.

System methods

  • CANServer_millis()

    Returns the number of milliseconds since the CANServer booted.

  • CANServer_getSystemStat(statname)

    Documentation to come soon...

Clone this wiki locally