Espruino Reference

Version 1v28

Contents

Global Functions

Array

Double

I2C

Integer

JSON

LCD

Math

Object

OneWire

Pin

SPI

Serial

String

console

fs

Detail

Global Functions

(top)

Methods and Fields

function eval

(top)

Call type:

function eval(code)

Description

Evaluate a string containing JavaScript code

Parameters

code

Returns

The result of evaluating the string

function parseInt

(top)

Call type:

function parseInt(string,radix)

Description

Convert a string representing a number into an integer

Parameters

string

radix The Radix of the string (optional)

Returns

The value of the string

function parseFloat

(top)

Call type:

function parseFloat(string)

Description

Convert a string representing a number into an float

Parameters

string

Returns

The value of the string

function bitRead

(top)

Call type:

function bitRead(value,bit)

Description

Get the specified bit from the value. Lowest significance bit is 0

Parameters

value

bit

Returns

See description above

function bitWrite

(top)

Call type:

function bitWrite(value,bit,data)

Description

Set the given bit in the value. Lowest significance bit is 0

Parameters

value

bit

data

Returns

No return value (undefined)

function bitSet

(top)

Call type:

function bitSet(value,bit)

Description

Write the specified bit from the value. Lowest significance bit is 0

Parameters

value

bit

Returns

No return value (undefined)

function bitClear

(top)

Call type:

function bitClear(value,bit)

Description

Clear the given bit in the value. Lowest significance bit is 0

Parameters

value

bit

Returns

No return value (undefined)

function bit

(top)

Call type:

function bit(bit)

Description

Get the value of the specified bit (0->1, 1->2, 2->4, 3->8 etc). Lowest significance bit is 0

Parameters

bit

Returns

See description above

function highByte

(top)

Call type:

function highByte(value)

Description

Return the high (second) byte of the value

Parameters

value

Returns

See description above

function lowByte

(top)

Call type:

function lowByte(value)

Description

Return the low byte of the value

Parameters

value

Returns

See description above

function setBusyIndicator

(top)

Call type:

function setBusyIndicator(pin)

Description

When Espruino is busy, set the pin specified here high. Set this to undefined to disable the feature.

Parameters

pin

Returns

No return value (undefined)

function setSleepIndicator

(top)

Call type:

function setSleepIndicator(pin)

Description

When Espruino is asleep, set the pin specified here high. Set this to undefined to disable the feature.

Parameters

pin

Returns

No return value (undefined)

function trace

(top)

Call type:

function trace()

Description

Output debugging information

Parameters

No parameters

Returns

No return value (undefined)

function dump

(top)

Call type:

function dump()

Description

Output current interpreter state such that it can be copied to a new device

Parameters

No parameters

Returns

No return value (undefined)

function load

(top)

Call type:

function load()

Description

Load program memory out of flash

Parameters

No parameters

Returns

No return value (undefined)

function save

(top)

Call type:

function save()

Description

Save program memory into flash

Parameters

No parameters

Returns

No return value (undefined)

function reset

(top)

Call type:

function reset()

Description

Reset everything - clear program memory

Parameters

No parameters

Returns

No return value (undefined)

function print

(top)

Call type:

function print(text)

Description

Print the supplied string

Parameters

text

Returns

No return value (undefined)

function memory

(top)

Call type:

function memory()

Description

Return an object containing information on memory usage at the start of the call

Parameters

No parameters

Returns

Information about memory usage

function edit

(top)

Call type:

function edit(funcName)

Description

Fill the console with the contents of the given function, so you can edit it.

Parameters

funcName The name of the function to edit (either a string or just the unquoted name)

Returns

No return value (undefined)

function echo

(top)

Call type:

function echo(echoOn)

Description

Should TinyJS echo what you type back to you? true = yes (Default), false = no. When echo is off, the result of executing a command is not returned. Instead, you must use 'print' to send output.

Parameters

echoOn

Returns

No return value (undefined)

function getTime

(top)

Call type:

function getTime()

Description

Return the current system time in Seconds (as a floating point number)

Parameters

No parameters

Returns

See description above

function register

(top)

Call type:

function register(code)

Description

Call this function with your registration code to register Espruino. Calling the function without a code will display your serial number.

Parameters

code The registration code. This is an 8 character string made out of the numbers 0-9 and letters A-F

Returns

No return value (undefined)

function peek32

(top)

Call type:

function peek32(addr)

Description

Read 32 bits of memory at the given location - DANGEROUS!

Parameters

addr The address in memory to read

Returns

The value of memory at the given location

function poke32

(top)

Call type:

function poke32(addr,value)

Description

Write 32 bits of memory at the given location - VERY DANGEROUS!

Parameters

addr The address in memory to read

value The value to write

Returns

No return value (undefined)

function analogRead

(top)

Call type:

function analogRead(pin)

Description

Get the analog value of the given pin

This is different to Arduino which only returns an integer between 0 and 1023

However only pins connected to an ADC will work (see the datasheet)

Parameters

pin The pin to use

Returns

The analog Value of the Pin between 0 and 1

function analogWrite

(top)

Call type:

function analogWrite(pin,value,options)

Description

Set the analog Value of a pin. It will be output using PWM

Parameters

pin The pin to use

value A value between 0 and 1

options An object containing options.
Currently only freq (pulse frequency in Hz) is available: analogWrite(LED1,0.5,{ freq : 10 });
Note that specifying a frequency will force PWM output, even if the pin has a DAC

Returns

No return value (undefined)

function digitalPulse

(top)

Call type:

function digitalPulse(pin,value,time)

Description

Pulse the pin with the value for the given time in milliseconds

eg. pulse(A0,1,500); pulses A0 high for 500ms

Parameters

pin The pin to use

value Whether to pulse high (true) or low (false)

time A time in milliseconds

Returns

No return value (undefined)

function digitalWrite

(top)

Call type:

function digitalWrite(pin,value)

Description

Set the digital value of the given pin

If pin is an array of pins, eg. [A2,A1,A0] the value will be treated as an integer where the first array element is the MSB

Parameters

pin The pin to use

value Whether to pulse high (true) or low (false)

Returns

No return value (undefined)

function digitalRead

(top)

Call type:

function digitalRead(pin)

Description

Get the digital value of the given pin

If pin is an array of pins, eg. [A2,A1,A0] the value will be treated as an integer where the first array element is the MSB

Parameters

pin The pin to use

Returns

The digital Value of the Pin

function setInterval

(top)

Call type:

function setInterval(function,timeout)

Description

Call the function specified REPEATEDLY after the timeout in milliseconds.

The function may also take an argument, which is an object containing a field called 'time', which is the time in seconds at which the timer happened

This can also be removed using clearInterval

Parameters

function A Function or String to be executed

timeout The time between calls to the function

Returns

An ID that can be passed to clearInterval

function setTimeout

(top)

Call type:

function setTimeout(function,timeout)

Description

Call the function specified ONCE after the timeout in milliseconds.

The function may also take an argument, which is an object containing a field called 'time', which is the time in seconds at which the timer happened

This can also be removed using clearTimeout

Parameters

function A Function or String to be executed

timeout The time until the function will be executed

Returns

An ID that can be passed to clearTimeout

function setWatch

(top)

Call type:

function setWatch(function,pin,options)

Description

Call the function specified when the pin changes

The function may also take an argument, which is an object containing a field called 'time', which is the time in seconds at which the pin changed state

This can also be removed using clearWatch

Parameters

function A Function or String to be executed

pin The pin to watch

options If this is a boolean or integer, it determines whether to call this once (false = default) or every time a change occurs (true)
If this is an object, it can contain the following information: { repeat: true/false(default), edge:'rising'/'falling'/'both'(default)}

Returns

An ID that can be passed to clearWatch

function clearInterval

(top)

Call type:

function clearInterval(id)

Description

Clear the Interval that was created with setInterval, for example:

var id = setInterval(function () { print('foo'); }, 1000);

clearInterval(id);

If no argument is supplied, all timers and intervals are stopped

Parameters

id The id returned by a previous call to setInterval

Returns

No return value (undefined)

function clearTimeout

(top)

Call type:

function clearTimeout(id)

Description

Clear the Timeout that was created with setTimeout, for example:

var id = setTimeout(function () { print('foo'); }, 1000);

clearTimeout(id);

If no argument is supplied, all timers and intervals are stopped

Parameters

id The id returned by a previous call to setTimeout

Returns

No return value (undefined)

function changeInterval

(top)

Call type:

function changeInterval(id,time)

Description

Change the Interval on a callback created with setInterval, for example:

var id = setInterval(function () { print('foo'); }, 1000); <-- every second

changeInterval(id, 1500); <-- now runs every 1.5 seconds

This takes effect the text time the callback is called (so it is not immediate).

Parameters

id The id returned by a previous call to setInterval

time The new time period in ms

Returns

No return value (undefined)

function clearWatch

(top)

Call type:

function clearWatch(id)

Description

Clear the Watch that was created with setWatch. If no parameter is supplied, all watches will be removed.

Parameters

id The id returned by a previous call to setWatch

Returns

No return value (undefined)

Array Class

(top)

This is the built-in JavaScript class for arrays.

Arrays can be defined with '[]', 'new Array()', or 'new Array(length)'

Methods and Fields

function Array.contains

(top)

Call type:

function Array.contains(value)

Description

Return true if this array contains the given value

Parameters

value The value to check for

Returns

Whether value is in the array or not

function Array.indexOf

(top)

Call type:

function Array.indexOf(value)

Description

Return the index of the value in the array, or -1

Parameters

value The value to check for

Returns

the index of the value in the array, or undefined

function Array.join

(top)

Call type:

function Array.join(separator)

Description

Join all elements of this array together into one string, using 'separator' between them. eg. [1,2,3].join(' ')=='1 2 3'

Parameters

separator The separator

Returns

A String representing the Joined array

function Array.push

(top)

Call type:

function Array.push(value)

Description

Push a new value onto the end of this array'

Parameters

value The value to add

Returns

The new size of the array

function Array.pop

(top)

Call type:

function Array.pop(value)

Description

Pop a new value off of the end of this array

Parameters

value The value to add

Returns

The value that is popped off

function Array.map

(top)

Call type:

function Array.map(function,thisArg)

Description

Return an array which is made from the following: A.map(function) = [function(A[0]), function(A[1]), ...]

Parameters

function Function used to map one item to another

thisArg if specified, the function is called with 'this' set to thisArg (optional)

Returns

The value that is popped off

function Array.splice

(top)

Call type:

function Array.splice(index,howMany,element1,element2,element3,element4,element5,element6)

Description

Pop a new value off of the end of this array

Parameters

index Index at which to start changing the array. If negative, will begin that many elements from the end

howMany An integer indicating the number of old array elements to remove. If howMany is 0, no elements are removed.

element1 A new item to add (optional)

element2 A new item to add (optional)

element3 A new item to add (optional)

element4 A new item to add (optional)

element5 A new item to add (optional)

element6 A new item to add (optional)

Returns

An array containing the removed elements. If only one element is removed, an array of one element is returned.

Double Class

(top)

Methods and Fields

function Double.doubleToIntBits

(top)

Call type:

function Double.doubleToIntBits(x)

Description

Convert the floating point value given into an integer representing the bits contained in it

Parameters

x A floating point number

Returns

The integer representation of x

I2C Class

(top)

This class allows use of the built-in I2C ports. Currently it allows I2C Master mode only.

All addresses are in 7 bit format. If you have an 8 bit address then you need to shift it one bit to the right.

Instances

Methods and Fields

function I2C.setup

(top)

Call type:

function I2C.setup(options)

Description

Set up this I2C port

Parameters

options An optional structure containing extra information on initialising the I2C port
{scl:pin, sda:pin}

Returns

No return value (undefined)

function I2C.writeTo

(top)

Call type:

function I2C.writeTo(address,data)

Description

Transmit to the slave device with the given address. This is like Arduino's beginTransmission, write, and endTransmission rolled up into one.

Parameters

address The 7 bit address of the device to transmit to

data The Data to send - either a byte, an array of bytes, or a string

Returns

No return value (undefined)

function I2C.readFrom

(top)

Call type:

function I2C.readFrom(address,quantity)

Description

Request bytes from the given slave device, and return them as an array. This is like using Arduino Wire's requestFrom, available and read functions. Sends a STOP

Parameters

address The 7 bit address of the device to request bytes from

quantity The number of bytes to request

Returns

The data that was returned - an array of bytes

Integer Class

(top)

Methods and Fields

function Integer.parseInt

(top)

Call type:

function Integer.parseInt(x)

Description

Convert a string representing a number into a number

Parameters

x A string to convert to an Integer

Returns

The integer value of x

function Integer.valueOf

(top)

Call type:

function Integer.valueOf(character)

Description

Given a string containing a single character, return the numeric value of it

Parameters

character A string containing a single character

Returns

The integer value of char

JSON Class

(top)

Methods and Fields

function JSON.stringify

(top)

Call type:

function JSON.stringify(d)

Description

Convert the given object into a JSON string which can subsequently be parsed with JSON.parse or eval

Parameters

d t

Returns

A JSON string

function JSON.parse

(top)

Call type:

function JSON.parse(s)

Description

Parse the given JSON string into a JavaScript object

Parameters

s r

Returns

The JavaScript object created by pasring the data string

LCD Class

(top)

This class handles interfacing to an LCD screen

It is currently only available on boards that contain a LCD screen, such as the HY 2.4 inch board. It can not currently be added to boards that did not ship with an LCD.

Methods and Fields

function LCD.WIDTH

(top)

Call type:

function LCD.WIDTH

Description

The width of the LCD

Parameters

No parameters

Returns

The width of the LCD

function LCD.HEIGHT

(top)

Call type:

function LCD.HEIGHT

Description

The height of the LCD

Parameters

No parameters

Returns

The height of the LCD

function LCD.clear

(top)

Call type:

function LCD.clear(col)

Description

Clear the LCD

Parameters

col The colour to clear to

Returns

No return value (undefined)

function LCD.fillRect

(top)

Call type:

function LCD.fillRect(x1,y1,x2,y2,col)

Description

Fill a rectangular area

Parameters

x1 The left

y1 The top

x2 The right

y2 The bottom

col The colour to fill to

Returns

No return value (undefined)

function LCD.drawRect

(top)

Call type:

function LCD.drawRect(x1,y1,x2,y2,col)

Description

Draw an unfilled rectangle 1px wide

Parameters

x1 The left

y1 The top

x2 The right

y2 The bottom

col The colour to fill to

Returns

No return value (undefined)

function LCD.getPixel

(top)

Call type:

function LCD.getPixel(x,y)

Description

Set a pixel's colour

Parameters

x The left

y The top

Returns

The colour

function LCD.setPixel

(top)

Call type:

function LCD.setPixel(x,y,col)

Description

Get a pixel's colour

Parameters

x The left

y The top

col The colour

Returns

No return value (undefined)

function LCD.col

(top)

Call type:

function LCD.col(r,g,b)

Description

Colvert into a colour format suitable for the LCD

Parameters

r Red (between 0 and 1)

g Green (between 0 and 1)

b Blue (between 0 and 1)

Returns

The colour

function LCD.drawString

(top)

Call type:

function LCD.drawString(str,x,y,colbg,colfg)

Description

Draw a string

Parameters

str The string

x The left

y The top

colbg The colour of the background

colfg The colour of the foreground

Returns

No return value (undefined)

function LCD.drawVectorString

(top)

Call type:

function LCD.drawVectorString(str,x,y,size,col)

Description

Draw a string

Parameters

str The string

x The left

y The top

size The height in pixels of the font

col The colour of the foreground

Returns

No return value (undefined)

function LCD.drawLine

(top)

Call type:

function LCD.drawLine(x1,y1,x2,y2,col)

Description

Draw a line between x1,y1 and x2,y2

Parameters

x1 The left

y1 The top

x2 The right

y2 The bottom

col The colour to draw with

Returns

No return value (undefined)

function LCD.fillPoly

(top)

Call type:

function LCD.fillPoly(poly,col)

Description

Draw a filled polygon

Parameters

poly An array of vertices, of the form [x1,y1,x2,y2,x3,y3,etc]

col The colour of the foreground

Returns

No return value (undefined)

Math Class

(top)

This is a standard JavaScript class that contains useful Maths routines

Methods and Fields

function Math.E

(top)

Call type:

function Math.E

Parameters

No parameters

Returns

The value of E - 2.71828182846

function Math.PI

(top)

Call type:

function Math.PI

Parameters

No parameters

Returns

The value of PI - 3.14159265359

function Math.abs

(top)

Call type:

function Math.abs(x)

Parameters

x A floating point value

Returns

The absolute value of x (eg, Math.abs(2)==2, but also Math.abs(-2)==2)

function Math.acos

(top)

Call type:

function Math.acos(x)

Parameters

x The value to get the arc cosine of

Returns

The arc cosine of x, between 0 and PI

function Math.asin

(top)

Call type:

function Math.asin(x)

Parameters

x The value to get the arc sine of

Returns

The arc sine of x, between -PI/2 and PI/2

function Math.atan

(top)

Call type:

function Math.atan(x)

Parameters

x The value to get the arc tangent of

Returns

The arc tangent of x, between -PI/2 and PI/2

function Math.atan2

(top)

Call type:

function Math.atan2(y,x)

Parameters

y The Y-part of the angle to get the arc tangent of

x The X-part of the angle to get the arc tangent of

Returns

The arctangent of Y/X, between -PI and PI

function Math.cos

(top)

Call type:

function Math.cos(theta)

Parameters

theta The angle to get the cosine of

Returns

The cosine of theta

function Math.pow

(top)

Call type:

function Math.pow(x,y)

Parameters

x The value to raise to the power

y The power x should be raised to

Returns

x raised to the power y (x^y)

function Math.random

(top)

Call type:

function Math.random()

Parameters

No parameters

Returns

A random number between 0 and 1

function Math.round

(top)

Call type:

function Math.round(x)

Parameters

x The value to round

Returns

x, rounded to the nearest integer

function Math.sin

(top)

Call type:

function Math.sin(theta)

Parameters

theta The angle to get the sine of

Returns

The sine of theta

function Math.sqrt

(top)

Call type:

function Math.sqrt(x)

Parameters

x The value to take the square root of

Returns

The square root of x

function Math.ceil

(top)

Call type:

function Math.ceil(x)

Parameters

x The value to round up

Returns

x, rounded upwards to the nearest integer

function Math.floor

(top)

Call type:

function Math.floor(x)

Parameters

x The value to round down

Returns

x, rounded downwards to the nearest integer

function Math.exp

(top)

Call type:

function Math.exp(x)

Parameters

x The value raise E to the power of

Returns

E^x

function Math.log

(top)

Call type:

function Math.log(x)

Parameters

x The value to take the logarithm (base E) root of

Returns

The log (base E) of x

Object Class

(top)

Methods and Fields

function Object.length

(top)

Call type:

function Object.length

Description

Find the length of the object

Parameters

No parameters

Returns

The value of the string

function Object.toString

(top)

Call type:

function Object.toString()

Description

Convert the Object to a string

Parameters

No parameters

Returns

A String representing the object

function Object.clone

(top)

Call type:

function Object.clone()

Description

Copy this object completely

Parameters

No parameters

Returns

A copy of this Object

function Object.on

(top)

Call type:

function Object.on(event,listener)

Description

Register an event listener for this object, for instance 'http.on('data', function(d) {...})'. See Node.js's EventEmitter.

Parameters

event The name of the event, for instance 'data'

listener The listener to call when this event is received

Returns

No return value (undefined)

function Object.emit

(top)

Call type:

function Object.emit(event,v1,v2)

Description

Call the event listeners for this object, for instance 'http.emit('data', 'Foo')'. See Node.js's EventEmitter.

Parameters

event The name of the event, for instance 'data'

v1 Optional argument 1

v2 Optional argument 2

Returns

No return value (undefined)

function Object.removeAllListeners

(top)

Call type:

function Object.removeAllListeners(event)

Description

Removes all listeners, or those of the specified event.

Parameters

event The name of the event, for instance 'data'

Returns

No return value (undefined)

OneWire Class

(top)

This class provides a software-defined OneWire master. It is designed to be similar to Arduino's OneWire library.

Methods and Fields

constructor OneWire

(top)

Call type:

constructor OneWire(pin)

Description

Create a software OneWire implementation on the given pin

Parameters

pin The pin to implement OneWire on

Returns

A OneWire object

function OneWire.reset

(top)

Call type:

function OneWire.reset()

Description

Perform a reset cycle

Parameters

No parameters

Returns

True is a device was present (it held the bus low)

function OneWire.select

(top)

Call type:

function OneWire.select(rom)

Description

Select a ROM - reset needs to be done first

Parameters

rom The rom to select

Returns

No return value (undefined)

function OneWire.skip

(top)

Call type:

function OneWire.skip()

Description

Skip a ROM

Parameters

No parameters

Returns

No return value (undefined)

function OneWire.write

(top)

Call type:

function OneWire.write(data,power)

Description

Write a byte

Parameters

data A byte to write

power Whether to leave power on after write (default is false)

Returns

No return value (undefined)

function OneWire.read

(top)

Call type:

function OneWire.read()

Description

Read a byte

Parameters

No parameters

Returns

The byte that was read

function OneWire.search

(top)

Call type:

function OneWire.search()

Description

Search for devices

Parameters

No parameters

Returns

An array of devices that were found

Pin Class

(top)

This is the built-in class for Pins, such as D0,D1,LED1, or BTN

You can call the methods on Pin, or you can use Wiring-style functions such as digitalWrite

Methods and Fields

function Pin.read

(top)

Call type:

function Pin.read()

Description

Returns the input state of the pin as a boolean

Parameters

No parameters

Returns

Whether pin is a logical 1 or 0

function Pin.set

(top)

Call type:

function Pin.set()

Description

Sets the output state of the pin to a 1

Parameters

No parameters

Returns

No return value (undefined)

function Pin.reset

(top)

Call type:

function Pin.reset()

Description

Sets the output state of the pin to a 0

Parameters

No parameters

Returns

No return value (undefined)

function Pin.write

(top)

Call type:

function Pin.write(value)

Description

Sets the output state of the pin to the parameter given

Parameters

value Whether to set output high (true/1) or low (false/0)

Returns

No return value (undefined)

SPI Class

(top)

This class allows use of the built-in SPI ports. Currently it is SPI master only.

Instances

Methods and Fields

function SPI.setup

(top)

Call type:

function SPI.setup(options)

Description

Set up this SPI port. Master, MSB first, no checksum

Parameters

options An optional structure containing extra information on initialising the SPI port
Please note that baud rate is set to the nearest that can be managed - which may be -+ 50%
{sck:pin, miso:pin, mosi:pin, baud:integer}

Returns

No return value (undefined)

function SPI.send

(top)

Call type:

function SPI.send(data,nss_pin)

Description

Send data down SPI, and return the result

Parameters

data The data to send - either an integer, array, or string

nss_pin An nSS pin - this will be lowered before SPI output and raised afterwards (optional)

Returns

The data that was returned

function SPI.send4bit

(top)

Call type:

function SPI.send4bit(data,bit0,bit1,nss_pin)

Description

Send data down SPI, using 4 bits for each 'real' bit (MSB first). This can be useful for faking one-wire style protocols

Parameters

data The data to send - either an integer, array, or string

bit0 The 4 bits to send for a 0 (MSB first)

bit1 The 4 bits to send for a 1 (MSB first)

nss_pin An nSS pin - this will be lowered before SPI output and raised afterwards (optional)

Returns

No return value (undefined)

function SPI.send8bit

(top)

Call type:

function SPI.send8bit(data,bit0,bit1,nss_pin)

Description

Send data down SPI, using 8 bits for each 'real' bit (MSB first). This can be useful for faking one-wire style protocols

Parameters

data The data to send - either an integer, array, or string

bit0 The 8 bits to send for a 0 (MSB first)

bit1 The 8 bits to send for a 1 (MSB first)

nss_pin An nSS pin - this will be lowered before SPI output and raised afterwards (optional)

Returns

No return value (undefined)

Serial Class

(top)

This class allows use of the built-in USARTs

Methods may be called on the USB, Serial1, Serial2, Serial3, Serial4, Serial5 and Serial6 objects. While different processors provide different numbers of USARTs, you can always rely on at least Serial1 and Serial2

Instances

Methods and Fields

function Serial.setConsole

(top)

Call type:

function Serial.setConsole()

Description

Set this Serial port as the port for the console

Parameters

No parameters

Returns

No return value (undefined)

function Serial.setup

(top)

Call type:

function Serial.setup(baudrate,options)

Description

Setup this Serial port with the given baud rate and options

Parameters

baudrate The baud rate - the default is 9600

options An optional structure containing extra information on initialising the serial port.
{rx:pin,tx:pin}
Note that even after changing the RX and TX pins, if you have called setup before then the previous RX and TX pins will still be connected to the Serial port as well - until you set them to something else using digitalWrite

Returns

No return value (undefined)

function Serial.print

(top)

Call type:

function Serial.print(string)

Description

Print a string to the serial port - without a line feed

Parameters

string A String to print

Returns

No return value (undefined)

function Serial.println

(top)

Call type:

function Serial.println(string)

Description

Print a line to the serial port (newline character sent are ' ')

Parameters

string A String to print

Returns

No return value (undefined)

function Serial.onData

(top)

Call type:

function Serial.onData(function)

Description

When a character is received on this serial port, the function supplied to onData gets called.

Only one function can ever be supplied, so calling onData(undefined) will stop any function being called

Parameters

function A function to call when data arrives. It takes one argument, which is an object with a 'data' field

Returns

No return value (undefined)

String Class

(top)

Methods and Fields

function String.fromCharCode

(top)

Call type:

function String.fromCharCode(code)

Description

Return the character represented by the given character code.

Parameters

code The character code to create a character from (range 0-255)

Returns

The character

function String.charAt

(top)

Call type:

function String.charAt(pos)

Description

Return a single character at the given position in the String.

Parameters

pos The character number in the string. Negative values return characters from end of string (-1 = last char)

Returns

The character in the string

function String.charCodeAt

(top)

Call type:

function String.charCodeAt(pos)

Description

Return the integer value of a single character at the given position in the String.

Note that this returns 0 not 'NaN' for out of bounds characters

Parameters

pos The character number in the string. Negative values return characters from end of string (-1 = last char)

Returns

The integer value of a character in the string

function String.indexOf

(top)

Call type:

function String.indexOf(substring)

Description

Return the index of substring in this string, or -1 if not found

Parameters

substring The string to search for

Returns

The index of the string, or -1 if not found

function String.substring

(top)

Call type:

function String.substring(start,end)

Parameters

start The start character index

end The end character index

Returns

The part of this string between start and end

function String.substr

(top)

Call type:

function String.substr(start,len)

Parameters

start The start character index

len The number of characters

Returns

Part of this string from start for len characters

function String.split

(top)

Call type:

function String.split(separator)

Description

Return an array made by splitting this string up by the separator. eg. '1,2,3'.split(',')==[1,2,3]

Parameters

separator The start character index

Returns

Part of this string from start for len characters

console Class

(top)

Methods and Fields

function console.log

(top)

Call type:

function console.log(text)

Description

Print the supplied string

Parameters

text

Returns

No return value (undefined)

fs Class

(top)

This class handles interfacing with a FAT32 filesystem on an SD card. The API is designed to be similar to node.js's - However Espruino does not currently support asynchronous file IO, so the functions behave like node.js's xxxxSync functions. Versions of the functions with 'Sync' after them are also provided for compatibility.

Currently this provides minimal file IO - it's great for logging and loading/saving settings, but not good for loading large amounts of data as you will soon fill your memory up.

It is currently only available on boards that contain an SD card slot, such as the Olimexino and the HY. It can not currently be added to boards that did not ship with a card slot.

Methods and Fields

function fs.readdir

(top)

Call type:

function fs.readdir(path)

Description

List all files in the supplied directory, returning them as an array of strings.

NOTE: Espruino does not yet support Async file IO, so this function behaves like the 'Sync' version.

Parameters

path The path of the directory to list. If it is not supplied, '' is assumed, which will list the root directory

Returns

An array of filename strings

function fs.readdirSync

(top)

Call type:

function fs.readdirSync(path)

Description

List all files in the supplied directory, returning them as an array of strings.

Parameters

path The path of the directory to list. If it is not supplied, '' is assumed, which will list the root directory

Returns

An array of filename strings

function fs.writeFile

(top)

Call type:

function fs.writeFile(path,data)

Description

Write the data to the given file

NOTE: Espruino does not yet support Async file IO, so this function behaves like the 'Sync' version.

Parameters

path The path of the file to write

data The data to write to the file

Returns

No return value (undefined)

function fs.writeFileSync

(top)

Call type:

function fs.writeFileSync(path,data)

Description

Write the data to the given file

Parameters

path The path of the file to write

data The data to write to the file

Returns

No return value (undefined)

function fs.appendFile

(top)

Call type:

function fs.appendFile(path,data)

Description

Append the data to the given file, created a new file if it doesn't exist

NOTE: Espruino does not yet support Async file IO, so this function behaves like the 'Sync' version.

Parameters

path The path of the file to write

data The data to write to the file

Returns

No return value (undefined)

function fs.appendFileSync

(top)

Call type:

function fs.appendFileSync(path,data)

Description

Append the data to the given file, created a new file if it doesn't exist

Parameters

path The path of the file to write

data The data to write to the file

Returns

No return value (undefined)

function fs.readFile

(top)

Call type:

function fs.readFile(path)

Description

Read all data from a file and return as a string

NOTE: Espruino does not yet support Async file IO, so this function behaves like the 'Sync' version.

Parameters

path The path of the file to read

Returns

A string containing the contents of the file

function fs.readFileSync

(top)

Call type:

function fs.readFileSync(path)

Description

Read all data from a file and return as a string

Parameters

path The path of the file to read

Returns

A string containing the contents of the file