CPLua - ClassPad specific functions List of available functions at 19 apr 2006 ///////////////////////////////////////////////////////////////////////////// UPDATES HISTORY --- 06 apr 2006 --- Basis: input (update), gettime, settime, getdate, setdate Debug: debug.debug, debug.getfenv, debug.getinfo, debug.getlocal, debug.getregistry, debug.getmetatable, debug.getupvalue, debug.setfenv, debug.setlocal, debug.setmetatable, debug.setupvalue, debug.traceback (see Lua 5.1 Manual for details) IO: io.folders, fold:files (was fold:content), io.check() --- 30 jan 2006 --- Drawing: pict.new, pict.part, pict.load, pict.save, pict.size, pict.screen, pict.buffer, pict.capture, draw.pict, draw.onpict, draw.onwhat --- 11 dec 2005 --- Basis: statusbar, keypad (update), fullscreen (update), printf IO: io.folder() + folder management: fold:exists(), fold:create(), fold:remove(), fold:rename(), fold:name(), fold:content() io.file() + file management: fi:exists(), fi:create(), fi:remove(), fi:rename(), fi:copy(), fi:header(), fi:folder(), fi:name(), fi:size(), fi:open(), fi:isopen(), fi:write(), fi:read(), fi:eof(), fi:close(), f:content() --- 19 nov 2005 --- Mat: The 'mat' package has been completely disabled due to the need of major modifications. It will be again available as soon as possible ;) Cas: cas.elem() --- 24 oct 2005 --- Basis: doscript (update) Drawing: draw.pxltest Mat: MASK, mat.resize, mat.size, mat.sizedim, mat.dim, mat.maxval, mat.minval, mat.maxloc, mat.minloc, mat.sum, mat.new, mat.ident, mat.trans, mat.mul, mat.det, mat.invert, mat.solve, mat.solveTriUp, mat.solveTriLow, mat.totable --- 29 sept 2005 --- Drawing: draw.pixel Cas: REMOVED: cas.expr, cas.tostring, cas.integ ADDED: all functions of the Main mode =) --- 25 sept 2005 --- Basis: waitkey, waitpen, input Drawing: draw.text Cas: cas.expr, cas.tostring, cas.integ Table: table.copy Math: math.sign --- 16 sept 2005 --- Basis: require, doscript (update), export --- 07 sept 2005 --- Basis: loadfunc, doscript --- 01 sept 2005 --- Basis: print, clear, showconsole, showgraph, fullscreen, keypad, getpen, getkey, testkey Drawing: draw.onscreen, draw.onbuffer, draw.update, draw.clear, draw.point, draw.line, draw.rect, draw.circle ///////////////////////////////////////////////////////////////////////////// BASIS FUNCTIONS * print(...) // Display some text in the console window * printf(format, ...) // Prints formatted text in the console window // This give the the same result as calling "print(string.format(format,...))" * clear() // Clear the console window * showconsole(), showgraph() // Change the execution window type // 0 : console // 1 : graphics * fullscreen(yes) // Enable/Disable full screen mode or return the current state. // Use fullscreen(1) to turn this mode on and fullscreen(0) to turn it off. // s = fullscreen() returns the current state. * keypad(visible) // Hide/Show the keypad or return the current state. // Use keypad(1) to show the keypad and keypad(0) to hide it. // s = keypad() returns the current state (visible or not). * statusbar(text) // Print a short text in the status bar. * code = getkey() // Return the code of the currently pressed key (on hard or soft keyboard) // or 'nil' if no key is pressed * x,y = getpen() // Return the position of the pen on screen, or nil if the pen does not touch it * yes = testkey(code) // Test if the given key is currently pressed on the hard keyboard * key = waitkey(code1, code2, ...) // Wait for the user to press one of the key given as argument, and return the pressed key // If no code is specified, any key will be accepted // Note: "waitkey(K_EXE)" uses much less battery than "repeat until testkey(K_EXE)". // This is also true for all the other keys. * x, y = waitpen() // Wait for the user to tap the screen with the pen // Return the coordinates of the pen input * str = input([text]) // Open an input field at the bottom of the console window // Let the user enter a string and press EXE, then return that string without formatting it. // Entering an empty string has no effect (it must have at least one character). // You may pass a string as parameter: it will be displayed on the left of // the input field (For example "name:" or "X="). * export{ A=a, B=b, ... } // Export the elements 'a', 'b', ... to make them accessible to the calling script. // 'A', 'B', ... are their corresponding names in the calling script // Only the scripts called by the "require" function are able to export symbols. // Warning: be sure to use the correct syntax (beware of the brackets "{}") // If you don't want to change the names of the exported elements, // write "export{a=a, b=b}", not "export{a,b}"!! * require("pack") or require("path") // Load the package 'pack' (like 'cas', 'table', 'string' etc) // or load the exported elements by the file specified in 'path' // This function should only be used once for each external files or package you need; // if you want to execute some instructions in another file several times, use the doscript() // function. * doscript(path, args) // Load and run the file at given path // 'args' must be a table containing the arguments to pass to the script. // This will create a corresponding table 'args' inside of it. // Example: doscript("test", {x=3, y=4} ) will execute the script "test" in the // main directory, and this script will be able to use the variables args.x and args.y during execution. // A script may return several values. Those values are then returned // by the doscript() function at the end of the execution. This a better solution // than using the 'export' function to return some results to the calling chunk (and in fact // this function cannot be used by runnable scripts anymore). * h,m,s = gettime() // Return the current time (hour, minute, second). * settime(h,m) // Set the current time. Seconds are reset to 0. // Warning: has no effect on the emulator. * y,m,d = getdate() // Return the current date (year, month, day). * setdate(y,m,d) // Set the current date. // Warning: has no effect on the emulator. The following names can be used as key codes: K_BACK K_UP, K_DOWN, K_LEFT, K_RIGHT K_0, K_1, K_2, ..., K_9 K_EQUAL, K_x, K_y, K_z, K_POWER, K_DIV K_LPAR, K_RPAR, K_COMMA, K_TIMES, K_MINUS, K_PLUS, K_DOT, K_EXP, K_EXE ///////////////////////////////////////////////////////////////////////////// // DRAWING FUNCTIONS Note: You need to write 'require("draw")' in your code before calling those functions * pic = pict.new(width, height) // Create a new picture of given size and return it * pic2 = pict.part(pic, x1, y1, x2, y2) // Create a new picture 'pic2', which is a copy of the specified rectangle of picture 'pic' * pic = pict.load(folder, file) // Load a picture from the PICT variable at "folder/file". // If you don't specify the folder, the file will be searched in the "main" directory: // 'pict.load("foo")' is the same as 'pict.load("main", "foo")'. * pict.save(pic, folder, file) // Save a picture to a PICT variable. // 'pict.save(pic, "foo")' is the same as 'pict.save(pic, "main", "foo")'. * w,h = pict.size(pic) // Return the size (width and height) of the picture 'pic' * pic_capt = pict.capture() // Make a copy of the entire screen (160*240) and return it as a picture. * pic_scr = pict.screen() // Return the content of the graph window, as a picture. // This is not a simple copy: any drawing operation on this picture will // be visible on screen. * pic_buf = pict.buffer() // Return the content of the hidden buffer, as a picture. // This is not a simple copy: any drawing operation on this picture will // change the content of the buffer. * draw.onscreen() // Send all drawing operation directly on screen (default) * draw.onbuffer() // Send all drawing operation on the hidden buffer * draw.onpict(pic) // Send all drawing operation on the specified picture. * dst = draw.onwhat() // Return the current destination of the drawing operations, as a picture. * draw.clear() // Clear the Graph window (or clear the buffer, depending on the current target) * draw.update() // Copy the entire buffer on the visible screen * draw.point(x, y, color=COL_BLACK, width=1 ) // Draw a single point on the Drawings window * draw.pixel(x, y, color=COL_BLACK ) // Draw a single pixel on the Drawings window // Same as draw.point, but for one pixel only and slightly faster * on = draw.pxltest(x,y) // Return true if the pixel at coordinates (x,y) has black color * draw.line(x1, y1, x2, y2, color=COL_BLACK, width=1 ) // Draw a single line on the Graph window * draw.rect(x1, y1, x2, y2, width=1, colBorder=COL_BLACK, colFill=-1 {nofill} ) // Draw a rectangle on the Graph window * draw.circle(x, y, radius, width=1, colBorder=COL_BLACK, colFill=-1 {nofill} ) // Draw a circle on the Graph window * draw.text(x, y, text, color=COL_BLACK, size=9, style=0) // Draw the given text at the given position // 'style' can be one of the following: // * 0 : Default // * 1 : Bold // * 2 : Italic // * 3 : Bold & Italic * draw.pict(x, y, pic, rasterOp="copy") // Draw the picture 'pic' at the specified point. // 'rasterOp' is a string among the followings: "copy" (default), "inv", "dstinv", "or", "xor". // This modifies the way the picture is actually drawn; use this for different effects like // transparency, color inversion etc. * draw.dblpict(x, y, pic1, pic2) // Draw a "double" picture contained in 'pic1' and 'pic2' at the specified point. // This does the same as 'draw.pict(x,y,pic1,"or") draw.pict(x,y,pic2,"xor")', but it's faster. // It's useful when some of your pictures should have "transparent" and white pixels at the same time. // Suppose you want to draw the following picture, where '.' represents a transparent pixel: p pic1 pic2 ### ### ######### ### #..###..# # ### # .# #. = # ##### # + # ### # .# #. # ##### # # ### # #..###..# # ### # ### ### ######### ### 'pic1 and 'pic2' must have the same size. 'pic1' has black pixels where 'p' has white or black pixels, while 'pic2' has black pixels where 'p' has white pixels. The following names can be used as colors: BLACK, WHITE, INVERT ///////////////////////////////////////////////////////////////////////////// // CAS FUNCTIONS Note: You need to write 'require("cas")' in your code before calling those functions * Creating expressions The 'cas' table can be called to convert a string into an expression object. For example, f1 = cas("1/x") will return the CAS expression '1/x' This expression can then be evaluated, by writing y = f1(5) -- set 'y' to '1/5' Note that 'y' here will be the CAS expression '1/5', not the number 0.2 . To convert it into a simple number, you must "call" the expression: n = y() -- conversion to the number '0.2' When you evaluate an expression, it is supposed that you are specifying a value for the variable "x" of the expression. If you want to use another variable, you must specify this variable when evaluating the expression. For example: f2 = cas("y^2+y") n = f2("y",3) -- evaluates f2(y) = y^2+y with y=3 f3 = cas("sin(x)*cos(y)") f4 = f3("y",0) -- now f4 = f4(x) = sin(x) The expressions can be printed without explicit conversion: print( "f1(x)= " .. f1) * Manipulating expressions You can now use the common Main functions of the Classpad to manipulate this expression. For example, to compute the first deritative of 'x*sin(x)', you can write: f5 = cas("x*sin(x)") df5_dx = cas.diff(f5) -- 'cas.diff(f5,"x",1)' is the actual complete form print(df52_dx) Note that print( cas.diff("x*sin(x)") ) give the same result. * CAS functions names The names of the functions available with the 'cas' table are the same as in the Classpad's Main mode. However some functions use a name that contain special characters like Greek letters etc; those functions have a different name in CPLua. Those are: - cas.Sigma() -- the "Sum" function - cas.Pi() -- the "Product" function - cas.integ() -- the "Integration" function - cas.sqrt() -- the "Square Root" function * Note on lists and matrices You can create lists and matrices expressions like any other expression. For examples: l = cas("{1,2,3}") -- returns the list {1,2,3} m = cas("[[1,2][3,4]]") -- returns a 2*2 matrix You can now use the function 'cas.elem()' to access the elements of a list or a matrix. With the example above, x = cas.elem(m,2,1) would get the expression at m[2,1] (which is the value '3' here). y = cas.elem(l,2) would return the second element in the list 'l' (which is '2' here). ///////////////////////////////////////////////////////////////////////////// // INPUT/OUTPUT FUNCTIONS Note: You need to write 'require("io")' in your code before calling those functions // Misc: * b = io.check(name) // Checks if 'name' is a valid file or folder name // i.e. it has less than 9 characters and does not begin with a number // Folders Management: * fold = io.folder(folderName) // Returns a handle on the folder "folderName" ("fold" is an arbitrary name) // The following functions can be called on the returned handle * h = io.folders() // Iterates over all existing folders. // Example of use: -- lists all folders for h in io:folders() do print( h:name() ) -- "h" is a handle on each folder end * b = fold:exists() // Returns true if this folder exists * fold:create() // Creates the folder (does nothing if it already exists) // Raises an error in case of failure * fold:remove() // Removes the folder // Raises an error in case of failure * fold:rename(newName) // Rename the folder // Raises an error in case of failure * str = fold:name() // Returns the folder's name * h = fold:files() // Iterates over the files in the folder. // Example of use: foo = io.folder("foo") -- "foo" is a handle on the folder "foo" -- lists all files in folder "foo" for h in foo:files() do print( h:name() ) -- "h" is a handle on each file in the folder end // Files Management: * fi = io.file(folderName,fileName) // Returns a handle on the file "folderName/fileName" ("fi" is an arbitrary name) // If the folder name is unspecified (by writing 'io.file(fileName)'), the file // will be searched in the "main" folder. // The following functions can be called on the returned handle * b = fi:exists() // Returns true if this file exists * fi:create(appName, typeName, majv, minv) // Create or initialize the file, by specifying its header // (application name, type of data, major version, minor version) // Raises an error in case of failure. // The application name and the type of data are both strings of 8 characters max. // It should give indications about the content of the file. // The major and minor versions are integers between 0 and 255. * fi:remove() // Removes the file // Raises an error in case of failure * fi:rename(folderName,fileName) // Renames the file (or move it) // If the folder name is unspecified, the file will remain in the same folder. // Raises an error in case of failure * fi:copy(dirName,fileName) // Makes a copy of the file in "dirName/fileName" // If the folder name is unspecified, the copy will be placed in in the same folder. // Raises an error in case of failure * h = fi:header() // h = { app=s1, type=s2, majv=s3, minv=s4 } * s = fi:folder() * s = fi:name() * n = fi:size() // Returns the header (as a table), the folder name, the name, the size of the file * fi:open(mode) // Opens the file. Mode = // 'r' -> Reading (can read numbers, strings, tables, booleans, or nil) // 'w' -> Writing (the file's content is erased first) // 'a' -> Appending (write at the end of the file's content) // Raises an error in case of failure * b = fi:isopen() // Returns true if 'open' was already called * fi:write(sth_1,sth_2,...) // Writes something in the file (modes 'w' or 'a' only) // You may write numbers, strings, booleans, or even (embedded) tables, // but there cannot be functions, userdatas or threads (as key or as value) in it. // It is not possible to write a table which use some other tables as keys. // The written data are copied in an internal buffer until the file is closed. // The real file will then be updated at once. * sth_1,sth_2,...,sth_n = fi:read(n) // Reads "n" things in the file (mode 'r' only) // "n" is an optional argument with default value 1. * b = fi:eof() // Returns true if the end of the file was reached (nothing more to read) // (mode 'r' only) * fi:close() // Closes the file. // If the file was opened in 'w' mode, this performs the real copy of // the data's in the file. // This function is called automatically if the file was still open when // the garbage collector wants to delete the handle, but you should allways // call it yourself after writing your data's: if an error occurs before // the file get closed, all written data's could be lost. * sth_1,sth_2,...,sth_n = fi:content(n) // Iterates over the elements in the file (mode 'r' only) // "n" is an optional argument with default value 1. It can be used to // read more than one element at each step (e.g. player name + score etc :)) // Examples of use: foo = io.file("main","foo") -- "foo" is an handle on the file "main/foo" for sth in foo:content() do print(sth) -- prints each element of the file end ///////////////////////////////////////////////////////////////////////////// [end]