Service_SPIKE()

ServiceDock library for interfacing with LEGO® SPIKE™ Prime

new Service_SPIKE()

Example
// assuming you declared <service-spike> with the id, "service_spike"
var serviceSPIKE = document.getElemenyById("service_spike").getService();
serviceSPIKE.executeAfterInit(async function() {
    // write code here
})

serviceSPIKE.init();

Namespaces

PrimeHub

Motor

ColorSensor

DistanceSensor

ForceSensor

MotorPair

Methods

async, inner init() → {boolean}

initialize SPIKE_service

Makes prompt in Google Chrome ( Google Chrome Browser needs "Experimental Web Interface" enabled)

Starts streaming UJSONRPC

this function needs to be executed after executeAfterInit but before all other public functions

Returns:
boolean -

True if service was successsfully initialized, false otherwise

inner executeAfterInit(callback)

Get the callback function to execute after service is initialized.

This function needs to be executed before calling init()

Parameters:
Name Type Description
callback function

Function to execute after initialization ( during init() )

Example
serviceSPIKE.executeAfterInit( function () {
    var motor = serviceSPIKE.Motor("A");
    var speed = motor.get_speed();
    // do something with speed
})

inner executeWithStream(callback)

Execute a stack of functions continuously with SPIKE sensor feed

Parameters:
Name Type Description
callback any
Example
var motor = new serviceSPIKE.Motor('A')
serviceSPIKE.executeWithStream( async function() {
     var speed = await motor.get_speed();
     // do something with motor speed
})

inner rebootHub()

Send character sequences to reboot SPIKE Prime

Run this function to exit micropython interpreter

Example
serviceSPIKE.rebootHub();

inner getHubName()

Get the name of the hub

Returns:
-

name of hub

inner isActive() → {boolean}

Get whether the Service was initialized or not

Returns:
boolean -

True if service initialized, false otherwise

Example
if (serviceSPIKE.isActive()) {
     // do something
}

inner getHubEvent() → {string}

Get the most recently detected event on the display of the hub

Returns:
string -

['tapped','doubletapped'] var event = await serviceSPIKE.getHubEvent(); if (event == "tapped" ) { console.log("SPIKE is tapped"); }

inner getHubGesture() → {string}

Get the most recently detected gesture of the hub ( Gesture names differ from SPIKE app )

Returns:
string -

['shaken', 'freefall', 'tapped', 'doubletapped']

Example
var gesture = await serviceSPIKE.getHubGesture();
if (gesture == "shaken") {
     console.log("SPIKE is being shaked");
}

inner getHubOrientation() → {string}

Get the most recently detected orientation of the hub

Returns:
string -

['up','down','front','back','leftside','rightside']

Example
var orientation = await serviceSPIKE.getHubOrientation();
if (orientation == "front") {
     console.log("SPIKE is facing up");
}

inner getMotorPorts() → {string|Array}

Get the letters of ports connected to any kind of Motors

Returns:
string | Array -

Ports that are connected to Motors

Example
var motorPorts = serviceSPIKE.getMotorPorts();

// get the alphabetically earliest port connected to a motor
var randomPort = motorPorts[0];

// get Motor object connected to the port
var mySensor = new Motor(randomPort);

inner getSmallMotorPorts() → {string|Array}

Get the letters of ports connected to Small Motors

Returns:
string | Array -

Ports that are connected to Small Motors

Example
var smallMotorPorts = serviceSPIKE.getSmallMotorPorts();

// get the alphabetically earliest port connected to a small motor
var randomPort = smallMotorPorts[0];

// get Motor object connected to the port
var mySensor = new Motor(randomPort);

inner getBigMotorPorts() → {string|Array}

Get the letters of ports connected to Big Motors

Returns:
string | Array -

Ports that are connected to Big Motors

Example
var bigMotorPorts = serviceSPIKE.getBigMotorPorts();

// get the alphabetically earliest port connected to a big motor
var randomPort = bigMotorPorts[0];

// get Motor object connected to the port
var mySensor = new Motor(randomPort);

inner getUltrasonicPorts() → {string|Array}

Get the letters of ports connected to Distance Sensors

Returns:
string | Array -

Ports that are connected to Distance Sensors

Example
var distanceSensorPorts = serviceSPIKE.getDistancePorts();

// get the alphabetically earliest port connected to a DistanceSensor
var randomPort = distanceSensorPorts[0];

// get DistanceSensor object connected to the port
var mySensor = new DistanceSensor(randomPort);

inner getColorPorts() → {string|Array}

Get the letters of ports connected to Color Sensors

Returns:
string | Array -

Ports that are connected to Color Sensors

Example
var colorSensorPorts = serviceSPIKE.getColorPorts();

// get the alphabetically earliest port connected to a ColorSensor
var randomPort = colorSensorPorts[0];

// get ColorSensor object connected to the port
var mySensor = new ColorSensor(randomPort);

inner getForcePorts() → {string|Array}

Get the letters of ports connected to Force Sensors

Returns:
string | Array -

Ports that are connected to Force Sensors

Example
var forceSensorPorts = serviceSPIKE.getForcePorts();

// get the alphabetically earliest port connected to a ForceSensor
var randomPort = forceSensorPorts[0];

// get ForceSensor object connected to the port
var mySensor = new ForceSensor(randomPort);

inner getMotors() → {array}

Get all motor objects currently connected to SPIKE

Returns:
array -

All connected Motor objects

Example
var motors = serviceSPIKE.getMotors();

if (motors.length > 0) {

     var myMotor = motors[0]; // get motor connected to most alphabetically early port

     // run motor for 10 seconds at 100 speed
     myMotor.run_for_seconds(10,100);
}

inner getDistanceSensors() → {array}

Get all distance sensor objects currently connected to SPIKE

Returns:
array -

All connected DistanceSensor objects

Example
var distanceSensors = serviceSPIKE.getDistanceSensors();

if (distanceSensors.length > 0) {

     var myDistanceSensor = distanceSensors[0]; // get DistanceSensor connected to most alphabetically early port

     // get distance in centimeters
     var distance = myDistanceSensor.get_distance_cm();
     console.log("distance in CM: ", distance);
}

inner getColorSensors() → {object}

Get all color sensor objects currently connected to SPIKE

Returns:
object -

All connected ColorSensor objects

Example
var colorSensors = serviceSPIKE.getColorSensors();

if (colorSensors.length > 0) {

     var color_sensor = colorSensors[0]; // get ColorSensor connected to most alphabetically early port

     // get detected color
     var color = color_sensor.get_color();
     console.log("detected color: ", color);
}

inner getForceSensors() → {object}

Get all force sensor objects currently connected to SPIKE

Returns:
object -

All connected ForceSensor objects

Example
var forceSensors = serviceSPIKE.getForceSensors();

if (forceSensors.length > 0) {

     var force_sensor = forceSensors[0]; // get ForceSensor connected to most alphabetically early port

     // when ForceSensor is pressed, indicate button state on console
     force_sensor.wait_until_pressed( function() {
          console.log("ForceSensor at port A was pressed");
     })
}

inner stopCurrentProgram()

Terminate currently running micropy program

async, inner writeProgram(projectName, data, slotid, callback)

Write a micropy program into a slot of the SPIKE Prime

Parameters:
Name Type Description
projectName string

name of the program

data string

the micropython source code (expecting an input tag's value). All characters must be ASCII

slotid integer

slot number to assign the program

callback function

function to run after program is written

inner executeProgram(slotid)

Execute a program in SPIKE Prime

Parameters:
Name Type Description
slotid integer

slot of which program to execute

Example
// execute program in slot 1 of SPIKE Prime hub
serviceSPIKE.executeProgram(1);