This API has been in part influenced by the CC:Tweaked API in order to facilitate porting of existing software.
sys- System functionskeys- Keyboard handling functionsfs- Filesystemopen(path, mode)list(path)makeDir(path)getSize(path)isDir(path)isReadOnly(path)attributes(path)exists(path)delete(path)move(source, target)copy(source, target)getFreeSpace(path)FileHandle:read([size])FileHandle:readAll()FileHandle:readLine()FileHandle:seek([whence, [offset]])FileHandle:write(bytes)FileHandle:writeLine(bytes)FileHandle:flush()FileHandle:close()
term- Text terminal functionsdraw- Drawing functionstext(x, y, text, [fg], [bg], [align])clear()point(x, y, color)rect(x, y, width, height, color)rectFill(x, y, width, height, color)line(x1, y1, x2, y2, color)circle(x, y, radius, color)circleFill(x, y, radius, color)polygon(points, color)polygonFill(points, color)triangle(c1, x1, y1, c2, x2, y2, c3, x3, y3)enableBuffer(mode, [dirty])blitBuffer()loadBMPSprites(filename, [width], [height], [mask])loadSprites(filename)newSprites([width], [height], [count], [mask])Spritesheet:blit(x, y, [id], [flip])Spritesheet:getSize()Spritesheet:getPixel(x, y, [id])Spritesheet:setPixel(x, y, [id])Spritesheet:getMask()Spritesheet:setMask()Spritesheet:save(filename)- Constants
colors- Color functions and constantssound- Programmable Sound Generatorinstrument([instrument|wave], [volume], [attack], [decay], [sustain], [release], [table_mode], [table_start], [table_playrate], [table_end])play(channel, note, instrument)playPitch(channel, pitch, instrument)off(channel)stop(channel)stopAll()volume(channel, volume, [relative])pitch(channel, pitch, [relative])Instrument- Constants
Returns the amount of unused memory
Returns
number- Amount of memory in bytes
Returns the amount of total system memory
Returns
number- Amount of memory in bytes
Resets the Pico
Resets the Pico in BOOTSEL mode
Sets a GPIO pin as output or input
Parameters
pin : number- The desired GPIO pindir : boolean- True for output, false for input
Gets the current state of an input pin
Parameters
pin : number- The desired GPIO pin
Returns
boolean- False for low, true for high
Sets the output of a pin
Parameters
pin : number- The desired GPIO pinstate : boolean- False for low, true for high
Gets the current state of charge of the battery
Returns
number- The battery state in 0-100 percentageboolean- Whether or not the charger is connected
Gets the current CPU clock speed
Returns
number- Current clock speed in MHz
Sets the CPU clock speed
Parameters
speed : number- The desired clock speed in MHz
Returns
boolean- Whether or not the CPU clock was able to be set
Sets up a repeating timer to run a Lua function. Only one timer can be set at a time and calling this function will cancel the previous timer if one was set
Parameters
interval : number- The interval in milliseconds to run the timer at. Positive values means time between each function execution (end to start), negative values means time between each function call (start to start)callback : function- The Lua function to execute when the timer fires
Stops an existing repeating timer
Returns latest keyboard event without blocking, clearing it from the queue
Returns
number- One ofkeys.statesnumber- Bitfield maskable bykeys.modifiersstring- Key code or character, 0 if no key was pressed
Same as poll() but does not clear it from the queue
Returns
number- One ofkeys.statesnumber- Bitfield maskable bykeys.modifiersstring- Key code or character, 0 if no key was pressed
Same as poll() but halts execution until a key is pressed
Parameters
nomod : boolean- Ignore key events from modifier keys, default falseonlypressed : boolean- Ignore key events other thankeys.state.pressed, default true
Returns
number- One ofkeys.statenumber- Bitfield maskable bykeys.modifiersstring- Key code or character
Discard all unused keyboard buffer
Checks if a specific key is currently pressed
Parameters
code : string- One ofkeysconstants, or key code
Returns
boolean- True if the key is currently held down, false otherwise
Checks if there are key events available to be polled without blocking. Will swallow events to be ignored if specified
Parameters
nomod : boolean- Ignore key events from modifier keys, default falseonlypressed : boolean- Ignore key events other thankeys.state.pressed, default true
Returns
boolean- Whether or not there are key events available
Checks if a character is an ASCII printable character
Returns
boolean- Whether or not the character is printable
altleftShiftrightShiftcontrolescleftupdownrightbackspaceentercapslockpausehomedeleteendpageUppageDowntab
idlepressedreleasedholdlongHold
controlaltshiftleftShiftrightShift
Opens a file for usage
Parameters
path : string- Path to the filemode : string- Same modes as standard libcfopen()
Returns
FileHandle- Object for file operations, throws error if unable to open
List the contents of a directory, throws error if unable to read or not a directory
Parameters
path : string- Path to search in
Returns
table- Nested table of entries inside search pathname : string- Filenamesize : number- File sizeisDir : boolean- Whether entry is a directory
Create directory at specified path. Does not create recursively
Parameters
path : string- Path of directory to be created
Returns the size of a file
Parameters
path : string- Path to the file
Returns
number- Size of the file in bytes
Checks if a path is a directory
Parameters
path : string- Path to be checked
Returns
boolean- Whether or not path is a directory
Checks if a path is a read-only
Parameters
path : string- Path to be checked
Returns
boolean- Whether or not path is read-only
Returns a list of attributes for a path
Parameters
path : string- Path to be checked
Returns
table:size : number- Size of the file in bytesisDir : boolean- Whether path is a directoryisReadOnly : boolean- Whether path is a read-onlydate : number- Date, as recorded by pico_fatfstime : number- Time, as recorded by pico_fatfs
Returns whether or not the path exists
Parameters
path : string- Path to be checked
Returns
boolean- Whether or not path exists
Deletes the specified path from disk
Parameters
path : string- Path to be deleted
Moves a path to another, can be used to rename files
Parameters
source : string- Path to be movedtarget : string- Destination of the new path
Copies a file to another, does not work on directories
Parameters
source : string- File to be copiedtarget : string- Destination of the new file
Gets the available disk space
Parameters
path : string- Path to be checked, if ommitted uses "" for the root folder
Returns
number- The available free space in clustersnumber- The total disk size in clusters
Reads an amount of bytes from an open file
Parameters
size : number- The amount of bytes to be read from the file, defaults to 1
Returns
string | nil- The bytes read from the file, nil if none available (end of file)
Reads the entire open file into a string
Returns
string | nil- The file contents, nil if nothing available to read
Reads the open file until the next newline character. Does not return the newline
Returns
string | nil- The line read, nil if nothing available to read
Seek to a new position within the file. The new position is an offset given by offset, relative to a start position determined by whence:
"set":offsetis relative to the beginning of the file."cur": Relative to the current position. This is the default."end": Relative to the end of the file.
Parameters
whence : string- Where the offset is relative tooffset : number- The offset to seek to
Returns
number- The new position in the file
Writes a string of bytes to an open file
Parameters
bytes : string- The bytes to be written
Returns
number- The number of bytes successfully written
Same as write(bytes), but adds a newline character at the end of the string written
Parameters
bytes : string- The bytes to be written
Returns
number- The number of bytes successfully written
Commits an open file's written data to disk without closing it
Closes an open file
Gets the current terminal cursor position
Returns
number- The horizontal cursor position in charactersnumber- The vertical cursor position in characters
Sets the terminal cursor position, starting at (1, 1) for top left
Parameters
x : number- The horizontal cursor position in charactersy : number- The vertical cursor position in characters
Gets whether the terminal cursor currently blinks
Returns
boolean- Whether or not the cursor blink is active
Sets the terminal cursor blinking
Parameters
blink : boolean- Whether or not the cursor blink should be active
Gets the terminal size
Returns
number- The terminal width in charactersnumber- The terminal height in characters
Clears the entire terminal and resets cursor to position (0, 0)
Clears the line indicated by the current cursor position
Returns the foreground color the text will currently be written in
Returns
number- Thecolorin a 16-bit format
Sets the foreground color for the text to be written in
Parameters
color : number- Thecolorin a 16-bit format
Returns the background color the text will currently be written in
Returns
number- Thecolorin a 16-bit format
Sets the background color for the text to be written in
Parameters
color : number- Thecolorin a 16-bit format
Produces a prompt for user text entry which echoes to the screen, waiting until the Enter key is pressed
Parameters
prompt : string- An optional prompt to be shown where the user enters text
Returns
string- The user's input
Writes text to the terminal screen at the current cursor position with the current text colors, updating the cursor position to the end of the text. Unlike print(), does not automatically line break at the end
Parameters
text : string- The text to be written
Similar to write(text) but controlling the foreground and background colors per-character. In case the color strings are shorter than the text string, they will repeat back from the start
Parameters
text : string- The text to be writtenfg : string- A string of hexadecimal values0tofmatching ANSI colors for the foreground colorbg : string- A string of hexadecimal values0tofmatching ANSI colors for the background color
Loads a font from the SD card, falling back to the default built-in font in case of failure.
Parameters
filename : string- The filename on SD card of the font to load
Returns
boolean- Whether or not loading the font was successful
Checks which font file is currently being used by the terminal
Returns
string | nil- The filename of the font, nil if no font is loaded and the terminal is using the built-in default
Returns the size of each glyph of the font currently being used by the terminal
Returns
number- The width of each glyph in pixelsnumber- The height of each glyph in pixels
Draws text on the screen at a determined position with determined colors
Parameters
x : number- The horizontal position in pixelsy : number- The vertical position in pixelstext : string- The text to be writtenfg : number- The foregroundcolor, defaults to whitebg : number- The backgroundcolor, defaults to blackalign : number- Which way to align text, see Constants. Defaults toalign_left
Clears the drawn screen. This does not affect the terminal cursor
Draws a single pixel on the screen
Parameters
x : number- The horizontal position in pixelsy : number- The vertical position in pixelscolor : number- Thecolorto be drawn
Draws the outline of a rectangle on the screen
Parameters
x : number- The horizontal position of the top left corner in pixelsy : number- The vertical position of the top left corner in pixelswidth : number- The width in pixelsheight : number- The height in pixelscolor : number- Thecolorto be drawn
Draws a filled rectangle on the screen
Parameters
x : number- The horizontal position of the top left corner in pixelsy : number- The vertical position of the top left corner in pixelswidth : number- The width in pixelsheight : number- The height in pixelscolor : number- Thecolorto be drawn
Draws a line between two points on the screen
Parameters
x1 : number- The horizontal position of the first point in pixelsy1 : number- The vertical position of the first point in pixelsx2 : number- The horizontal position of the second point in pixelsy2 : number- The vertical position of the second point in pixelscolor : number- Thecolorto be drawn
Draws the outline of a circle on the screen
Parameters
x : number- The horizontal position of the center of the circle in pixelsy : number- The vertical position of the center of the circle in pixelsradius : number- The radius of the circle in pixelscolor : number- Thecolorto be drawn
Draws a filled circle on the screen
Parameters
x : number- The horizontal position of the center of the circle in pixelsy : number- The vertical position of the center of the circle in pixelsradius : number- The radius of the circle in pixelscolor : number- Thecolorto be drawn
Draws the outline of a polygon to the screen
Parameters
points : table- A sequence of coordinates for the polygon's points. The table must have an even number of values, each pair of values representing the horizontal and the vertical positions in pixels of each point respectivelycolor : number- Thecolorto be drawn
Draws a filled polygon to the screen
Parameters
points : table- A sequence of coordinates for the polygon's points. The table must have an even number of values, each pair of values representing the horizontal and the vertical positions in pixels of each point respectivelycolor : number- Thecolorto be drawn
Draw a triangle with each vertex shaded by a different color
Parameters
c1 : number- Thecolorto shade the first vertexx1 : number- The horizontal position of the first vertex in pixelsy1 : number- The vertical position of the first vertex in pixelsc2 : number- Thecolorto shade the second vertexx2 : number- The horizontal position of the second vertex in pixelsy2 : number- The vertical position of the second vertex in pixelsc3 : number- Thecolorto shade the third vertexx3 : number- The horizontal position of the third vertex in pixelsy3 : number- The vertical position of the third vertex in pixels
Enables or disables a framebuffer mode. While the framebuffer is enabled, no drawing functions will be reflected on the screen until the framebuffer is blitted, or the framebuffer is disabled. Valid mode values:
0: Direct LCD drawing (disable framebuffer)1: PSRAM, slower access but does not use system RAM2: RAM, faster than PSRAM but with lower color depth (RGB565 is converted to RGB233 automatically) and uses ~100KB of RAM
The RAM framebuffer is dynamically allocated when enabled and freed when disabled.
Parameters
mode : integer | boolean- The framebuffer mode to enable, boolean values are accepted for enabling PSRAM (legacy)
Returns
boolean- Whether or not setting the mode was successful
Blit the contents of the framebuffer to the screen
Loads a spritesheet to memory for blitting sprites to the screen. Formats supported are 24bit and 32bit BMP, sprites are indexed top left to bottom right as an atlas
Parameters
filename : string- The path for the BMP on disk to be loadedwidth : number- The width of each individual sprite, defaults to the entire width of the image if omittedheight : number- The height of each individual sprite, defaults to the entire width of the image if omittedmask : number- Thecolorto be used as transparent pixels for the sprites. Defaults toRGB(255, 0, 255)
Returns
spritesheet- Spritesheet object
Loads a spritesheet to memory as saved by Spritesheet:save
Parameters
filename : string- The path for the Spritesheet on disk to be loaded
Returns
spritesheet- Spritesheet object
Loads a spritesheet to memory for blitting sprites to the screen. Formats supported are 24bit and 32bit BMP, sprites are indexed top left to bottom right as an atlas
Parameters
filename : string- The path for the BMP on disk to be loadedwidth : number- The width of each individual sprite, defaults to 16height : number- The height of each individual sprite, defaults to 16height : number- The number of sprites in the sheet, defaults to 1mask : number- Thecolorto be used as transparent pixels for the sprites. Defaults toRGB(255, 0, 255)
Returns
spritesheet- Spritesheet object
Blits a sprite from a spritesheet onto the screen
Parameters
x : number- The horizontal position on the screen to blit the sprite toy : number- The vertical position on the screen to blit the sprite toid : number- The index of the desired sprite within the spritesheet, defaults to 0flip : number- Bitmask for drawing the sprite flipped, see Constants
Returns the sizes of the spritesheet
Returns
number- The width of each individual spritenumber- The height of each individual spritenumber- How many sprites are in the sheet
Reads the color of a pixel in a sprite
Parameters
x : number- The horizontal position of the pixel to ready : number- The vertical position of the pixel to readid : number- The index of the desired sprite within the spritesheet, defaults to 0
Returns
number- Thecolorof the pixel
Writes the color of a pixel in a sprite
Parameters
x : number- The horizontal position of the pixel to ready : number- The vertical position of the pixel to readmask : number- Thecolorto write onto the pixelid : number- The index of the desired sprite within the spritesheet, defaults to 0
Gets the mask color used for sprite transparency in a sheet
Returns
number- Thecolorused as transparency
Sets the mask color used for sprite transparency in a sheet
Parameters
mask : number- Thecolorto be used as transparency
Saves a spritesheet from memory onto disk
Parameters
filename : string- The path for the file to be saved on disk
flip_horizontalflip_verticalflip_bothalign_leftalign_centeralign_right
Convert RGB values into a 16-bit color value
Parameters
R : number- The input color's red value from 0 to 255G : number- The input color's green value from 0 to 255B : number- The input color's blue value from 0 to 255
Returns
number- The resulting 16-bit color value
Convert a 16-bit color vaalue into RGB values
Parameters
color : number- The input 16-bit color value
Returns
number- The resulting red value from 0 to 255number- The resulting green value from 0 to 255number- The resulting blue value from 0 to 255
Convert HSV values into a 16-bit color value
Parameters
hue : number- The input color's hue from 0 to 255saturation : number- The input color's saturation from 0 to 255value : number- The input color's value (brightness) from 0 to 255
Returns
number- The resulting 16-bit color value
Convert a 16-bit color vaalue into HSV values
Parameters
color : number- The input 16-bit color value
Returns
number- The resulting hue from 0 to 255number- The resulting saturation from 0 to 255number- The resulting value (brightness) from 0 to 255
Add two 16-bit colors together
Parameters
color1 : number- The first 16-bit color to addcolor2 : number- The second 16-bit color to add
Returns
number- The resulting 16-bit color from the addition
Subtract two 16-bit colors
Parameters
color1 : number- The 16-bit color to subtract fromcolor2 : number- The 16-bit color to be subtracted
Returns
number- The resulting 16-bit color from the subtraction
Multiply two 16-bit colors together
Parameters
color1 : number- The first 16-bit color to multiplycolor2 : number- The second 16-bit color to multiply
Returns
number- The resulting 16-bit color from the multiplication
whiteorangemagentalightBlueyellowlimepinkgraylightGraycyanpurplebluebrowngreenredblack
instrument([instrument|wave], [volume], [attack], [decay], [sustain], [release], [table_mode], [table_start], [table_playrate], [table_end])
Create or clone an instrument object to be used to play sounds
Parameters
instrument | wave : integer- The wave sample number to assign to the instrument. If anInstrumentobject is passed instead, all other parameters are ignored and a clone of that instrument is returned. Defaults to 0 (pulse-width wavetable)volume : number- Overall instrument volume 0.0-1.0. Defaults to 1.0attack : integer- ADSR envelope attack value in milliseconds. Defaults to 0decay : integer- ADSR envelope decay value in milliseconds. Defaults to 1000sustain : number- ADSR envelope sustain value in 0.0-1.0. Defaults to 0.0release : integer- ADSR envelope release value in milliseconds. Defaults to 0table_mode : integer- Sets how the wavetable will be swept, seesound.tableModes. Defaults tosingletable_start : integer- Wavetable slice to start sweep at. Defaults to 0table_playrate : integer- The number of samples to wait before advancing the wavetable sweep, lower numbers mean faster sweep, can be negative. Defaults to 500table_end : integer- Wavetable slice to end sweep at. Defaults to last slice of the selected wavetable
Returns
Instrument- Userdata object of an instrument which can be played
Plays a note value using a specified instrument at a specified channel
Parameters
channel : integer- The channel to play at. 8 channels are available at values 0-7note : integer- The note value to pitch the instrument at. Value 36 corresponds to C-3instrument : Instrument- The instrument to be played
Plays an absolute pitch value using a specified instrument at a specified channel
Parameters
channel : integer- The channel to play at. 8 channels are available at values 0-7pitch : number- The rate at which to play the instrument or sample. 1.0 means normal playbackinstrument : Instrument- The instrument to be played
Triggers a release of an instrument currently playing on a specified channel
Parameters
channel : integer- The channel to trigger the release at
Hard stops any sound playing on a specified channel
Parameters
channel : integer- The channel to stop playing
Stops all sounds playing
Changes the volume of an instrument currently playing on a specified channel
Parameters
channel : integer- The currently playing channel to changevolume : number- The volume value ranging 0.0-1.0relative : boolean- Whether the value should be added relative to the current volume or not. Defaults to false
Changes the pitch of an instrument currently playing on a specified channel
Parameters
channel : integer- The currently playing channel to changepitch : number- The pitch valuerelative : boolean- Whether the value should be added relative to the current pitch or not. Defaults to false
Instrument objects have the following attributes which can be read and set:
wave : integer- The wave sample number assigned to the instrumentvolume : number- Overall instrument volume in range 0.0-1.0attack : integer- ADSR envelope attack value in millisecondsdecay : integer- ADSR envelope decay value in millisecondssustain : number- ADSR envelope sustain value in range 0.0-1.0release : integer- ADSR envelope release value in millisecondstable_mode : integer- How the wavetable will be swepttable_start : integer- Wavetable slice to start sweep attable_playrate : integer- The number of samples to wait before advancing the wavetable sweep, lower numbers mean faster sweep, can be negativetable_end : integer- Wavetable slice to end sweep at
squarethinstringviolinvoicepluckpulsewarpsinetriangle
kick1kick2snare1snare2hihattomcowbell
single- Treats the wavetable as a single sample, like drums. Playback ends once the first slice is playedoneShot- Sweeps the wavetable from start to end, then stops sweep at the endpingPong- Sweeps the wavetable, switching directions whenever end or start are reachedloop- Sweeps the wavetable in a single direction, resetting the sweep from the other side of the loop once the end is reached