API - micojs/micojs.github.io GitHub Wiki

Not implemented

MicoJS aims to implement the JavaScript language on microcontrollers, not the browser API. As such, none of the functions and objects you'd find in the Browser or Node.js are present. This means there is no Math, Uint_Array, and certainly no navigator. It also means that arrays, strings and objects don't have the expected built-ins like splice, charAt, or propertyIsEnumerable.

At the moment, arrays only have a length property, an indexOf(needle) and a fill(value) method. Strings only have length and a charCodeAt(index) method. Strings support the [] operator, for extracting a single character. Objects have no built-in methods.

Functions

debug(...args):void;

  • Prints messages into the console for debugging

getTime():number;

  • Returns a timestamp in milliseconds

rand():number;

  • Returns a floating-point value between 0 (inclusive) and 1 (exclusive)

rand(max:number):number;

  • Returns an integer value between 0 (inclusive) and max (exclusive)

rand(min:number, max:number):number;

  • Returns a floating-point value between min (inclusive) and max (exclusive)

rand(min:number, max:number, ignored:any):number;

  • Returns an integer value between min (inclusive) and max (exclusive)

hash(str:string):number;

  • Returns a 32-bit hash of the given string

sign(value:number):number;

  • Returns the sign of the value (-1, 0 or 1, for negative, 0, or positive values respectively)

abs(value:number):number;

  • Returns the absolute value of value

floor(value:number):number;

  • Returns value rounded down to the nearest integer

round(value:number):number;

  • Returns value rounded to the nearest integer

ceil(value:number):number;

  • Returns value rounded up to the nearest integer

sqrt(value:number):number;

  • Returns the square root of value

cos(angle:number):number;

  • Returns the cosine of angle, given in radians

sin(angle:number):number;

  • Returns the sine of angle, given in radians

atan2(y:number, x:number):number;

  • Returns the arc tangent of y/x

tan(angle:number):number;

  • Returns the arc tangent of angle, given in radians

clamp(value, min, max):number;

  • Returns value restricted to the range defined by min and max, inclusive.

min(...value):number;

  • Returns the lowest of the given values

max(...value):number;

  • Returns the highest of the given values

vectorLength(...args):number;

  • Returns the root of the sum of each argument squared

angleDifference(angleA, angleB):number

  • Returns the difference between two angles given in radians

setFPS(fps:number):void;

  • Sets the target framerate that the game should update at.

setPen(R:number, G:number, B:number):number;

  • Sets the current drawing color. Returns a color.

setPen(color:number):number;

  • Sets the current drawing color. Returns a color.

setFont(font:FontResource):void;

  • Sets the font used by the text() function.
  • The font should be one of:
    • R.fontMini R.fontTIC806x6 R.fontZXSpec R.fontAdventurer
    • R.fontDonut R.fontDragon R.fontC64 R.fntC64UIGfx
    • R.fontMonkey R.fontKarateka R.fontKoubit R.fontRunes
    • R.fontTight R.fontTiny

setLED(R:number, G:number, B:number):void;

  • Sets the LED color on supported hardware.

rect(x:number, y:number, width:number, height:number):void;

  • Fills a rectangle on the screen using the current pen color.

setTileMap(map:TileMapResource):void;

  • Sets the current tilemap.

getTileProperty(x:number, y:number, property:String):number;

  • Returns the value of the property of the tile under the given coordinates.
  • X and Y should be in pixel (not tile) coordinates.
  • Takes the current CAMERA_X and CAMERA_Y offset into consideration.
  • Always returns an integer. If the property is not found, returns zero.

scanTileMap(filter:object, callback:function):void;

  • Tests every tile in the tilemap to see if it matches the given filter.
  • Calls callback(x, y) for every tile that matches.
  • x and y are in pixel coordinates for the middle of each tile, taking CAMERA_X/Y into account.

setTexture(texture:ImageResource):void;

  • Sets the texture to be used by the image() function.

setMirrored(mirror:boolean):void;

  • Sets if the following image() calls should be flipped horizontally.

setFlipped(flip:boolean):void;

  • Sets if the following image() calls should be flipped vertically.

setTransparent(transparent:boolean):void;

  • Sets if the following image() calls should support transparency.

getWidth():number;

  • Returns the screen's width in pixels.

getHeight():number;

  • Returns the screen's height in pixels.

getWidth(image:ImageResource):number;

  • Returns the image's width in pixels.

getHeight(image:ImageResource):number;

  • Returns the image's height in pixels.

clear():void;

  • Fills the entire screen with the current pen's color.

image():void;

  • Draws the active texture onto the screen at position 0, 0.

image(texture:ImageResource):void;

  • Draws texture onto the screen at position 0, 0.

image(X:number, Y:number):void;

  • Draws the active texture onto the screen at position X, Y.

image(texture:ImageResource, X:number, Y:number, rotation?:number, scale?:number):void;

  • Draws texture onto the screen at position X, Y.

text(message:string, x?:number, y?:number):void;

  • Draws text onto the screen.

sound(audio:AudioResource, volume?:number, speed?:number):void;

  • Plays a sound file at the specified volume and speed. The default volume and speed is 1.

Global variables

UP:boolean;

  • True while button is pressed, false otherwise. Mapped to arrow up or I on PC.

DOWN:boolean;

  • True while button is pressed, false otherwise. Mapped to arrow down or K on PC.

LEFT:boolean;

  • True while button is pressed, false otherwise. Mapped to arrow left or J on PC.

RIGHT:boolean;

  • True while button is pressed, false otherwise. Mapped to arrow right or L on PC.

A:boolean;

  • True while button is pressed, false otherwise. Mapped to key A or Control on PC.

B:boolean;

  • True while button is pressed, false otherwise. Mapped to key S or Shift on PC.

C:boolean;

  • True while button is pressed, false otherwise. Mapped to key F or X on PC.

D:boolean;

  • True while button is pressed, false otherwise. Mapped to key D or Z on PC.

PI:number;

  • Constant value of PI

HALF_PI:number;

  • Constant value of PI divided by two

TWO_PI:number;

  • Constant value of PI multiplied by two