Player APIs (BrightScript & Ja...
...
BrightScript
Object Reference
Global Functions
48min
brightscript provides a set of standard, module scope functions that are stored in the global object if a global function is referenced, the compiler directs the runtime to call the appropriate global object member when calling a global function, you do not need to use the operators docid\ pl6ydtpgctydca5q2bw45 to reference the roglobal object global trigonometric functions accept and return values in radians, not degrees ifglobal createobject(name as string) as object creates a brightscript object corresponding to the specified class name this method returns invalid if object creation fails some objects have parameters in their constructor, which must be passed after the class name in a comma separated list sw = createobject("rogpiocontrolport") serial = createobject("roserialport", 0, 9600) restartscript() as void exits the current script the system then scans for a valid autorun brs file to run restartapplication() as void restarts the brightsign application sleep(milliseconds as integer) instructs the script to pause for a specified amount of time without wasting cpu cycles the sleep interval is specified in milliseconds asc(letter as string) as integer returns the ascii code for the first character of the specified string a null string argument will cause an error chr(character as integer) as string returns a one character string containing a character reflected by the specified ascii or control for example, because quotation marks are normally used as string delimiters, you can pass ascii code 34 to this function to add quotes to a string len(target string as string) as integer returns the number of characters in a string str(value as double) as string converts a specified float value to a string this method also returns a string equal to the character representation of a value for example, if a is assigned a value of 58 5, then calling str(a) will return "58 5" as a string stri(value as integer) as string converts a specified integer value to a string this method returns a string equal to the character representation of a value, and it will always add a leading space to non negative numbers for example, if a is assigned a value of 58 5, then calling stri(a) will return " 58" as a string see roint, rofloat, rostring docid\ lbho0lm71t5ojmdhwj 1i if you need an alternate method that will return the string without the prepended space val(target string as string) as double returns a number represented by the characters in the string argument this is the opposite of the str() function for example, if a is assigned the string "58", and b is assigned the string "5", then calling val(a+" "+b) will return the float value 58 5 strtoi(target string as string) as integer converts the target string to an integer any non integer characters (including decimal points and spaces), and any numbers to the right of a non integer character, will not be part of the integer output abs(x as double) as double returns the absolute value of the argument x atn(x as double) as double returns the arctangent (in radians) of the argument x (i e atn(x) returns "the angle whose tangent is x ") to get the arctangent in degrees, multiply atn(x) by 57 29578 csng(x as integer) as float returns a single precision float representation of the argument x cdbl(x as integer) as double returns a double precision float representation of the argument x cint(x as double) as integer returns an integer representation of the argument x by rounding to the nearest whole number cos(x as double) as double returns the cosine of the arugment x the argument must be in radians to obtain the cosine of x when x is in degrees, use cos(x 01745329) exp(x as double) as double returns the natural exponential of x this is the inverse of the log() function fix(x as double) as integer returns a truncated representation of the argument x all digits to the right of the decimal point are removed so that the resultant value is an integer for non negative values of x , fix(x) is equal to int(x) for negative values of x , fix(x) is equal to int(x)+1 int(x as double) as integer returns an integer representation of the argument x using the largest whole number that is not greater than the argument for example, int(2 2) returns 2, while int( 2 5) returns 3 log(x as double) as double returns the natural logarithm of the argument i (i e loge(x) ) this is the inverse of the exp() function to find the logarithm of a number to a base b , use the following formula logb(x) = loge(x)/loge(b) sgn(x as double) as integer returns an integer representing how the float argument x is signed 1 for negative, 0 for zero, and 1 for positive sgni(x as integer) as integer returns an integer representing how the integer argument x is signed 1 for negative, 0 for zero, and 1 for positive sin(x as double) as double returns the sine of the argument x the argument must be in radians to obtain the sine of x when x is in degrees, use sin(x 01745329) tan(x as double) as double returns the tangent of the argument x the argument must be in radians to obtain the tangent of x when x is in degrees, use tan(x 01745329) sqr(x as double) as double returns the square root of the argument x this function is the same as x^(1/2) , but calculates the result faster stableenumeration(array as object) as object returns an object that represents the iteration this function does not affect the original object must use this function to safely enumerate containers more than once in parallel left(target string as string, n as integer) as string returns the first n characters of the specified string right(target string as string, n as integer) as string returns the last n characters of the specified string stringi(n as integer, character as integer) as string returns a string composed of a character symbol repeated n times the character symbol is passed to the method as an ascii code integer string(n as integer, character as string) as string returns a string composed of a character symbol repeated n times the character symbol is passed to the method as a string mid(target string as string, start position as integer, length as integer) as string returns a substring of the target string the first integer passed to the method specifies the starting position of the substring, and the second integer specifies the length of the substring the start position of a string begins with 1 instr(start position as integer, search text as string, substring as string) as integer returns the position of a substring within a string this function is case sensitive and returns 0 if the specified substring is not found the start position of a string begins with 1 the string object also offers an instr() method (though it uses a zero based index) see the docid\ lbho0lm71t5ojmdhwj 1i documentation for more details findmemberfunction(object as object, funname as string) as interface returns the interface from the object that provides the specified function, or else returns invalid it is only available if rodeviceinfo hasfeature("findmemberfunction") returns true it can be used as shown below print findmemberfunction({}, "count") '= \<interface ifassociativearray> getinterface(object as object, ifname as string) as interface returns a value of the type interface all objects have one or more interfaces in most cases, you can skip interface specification when calling an object component this will not cause problems as long as the method names within a function are unique wait(timeout as integer, port as object) as object instructs the script to wait on an object that has an ifmessageport interface this method will return the event object that was posted to the message port if the timeout is specified as zero, wait() will wait indefinitely; otherwise, wait() will return invalid after the specified number of milliseconds if no messages have been received p = createobject("romessageport") sw = createobject("rogpiocontrolport") sw\ setport(p) msg=wait(0, p) print type(msg) ' should be rogpiobutton print msg getint() ' button number readasciifile(file path as string) as string reads the specified text file and returns it as a string writeasciifile(file path as string, buffer as string) as boolean creates a text file at the specified file path the text of the file is passed as the second parameter this method cannot be used to edit files a preexisting text file will be overwritten if it has the same name and directory path as the one being created the docid\ cxcldkjsnydzw4i ve ac object provides more flexibility if you need to create or edit files listdir(path as string) as object returns an docid\ leopepz51dstazh9tjhuq object containing the contents of the specified directory path file names are converted to all lowercase matchfiles(path as string, pattern in as string) as object takes a directory to look in (it can be as simple as " " or "/") and a pattern to be matched and then returns an docid\ leopepz51dstazh9tjhuq containing the results each listed result contains only the part of the filename that is matched against the pattern, not the full path the match is only applied in the specified directory; you will get no results if the pattern contains a directory separator the pattern is a case insensitive wildcard expression it may contain the following special characters ? – matches any single character – matches zero or more arbitrary characters \[…] – matches any single character specified within the brackets the closing bracket is treated as a member of the character class if it immediately follows the opening bracket (i e "\[]]" matches a single closed bracket) within this class, " " can be used to specify a range unless it is the first or last character (e g "\[a cf h"] is equivalent to "\[abcfgh]") a character class may be negated by specifying "^" as the first character to match a literal of this character, place it elsewhere in the class the special characters "?", " ", and "\[" lose their function if preceded by a single "\\", and a single "\\" can be matched using "\\\\" lcase(target string as string) as string converts the specified string to all lowercase ucase(target string as string) as string converts the specified string to all uppercase deletefile(file path as string) as boolean deletes the file at the specified file path this method returns false if the delete operation fails or if the file does not exist deletedirectory(directory as string) as boolean deletes the specified directory this method will recursively delete any files and directories that are necessary for removing the specified directory this method returns false if it fails to delete the directory, but it may still delete some of the nested files or directories for example dir = deletedirectory("/directory") ' check if directory deletion was successful print dir; ' true if successful or dir = deletedirectory("/directory") if not dir then print "could not delete directory" end if createdirectory(directory as string) as boolean creates the specified directory only one directory can be created at a time this method returns true upon success and false upon failure for example dir = createdirectory("/directory") ' check if directory creation was successful print dir; ' true if successful or dir = createdirectory("/directory") if not dir then print "could not create directory" end if rebootsystem() as void instructs the player to perform a soft reboot shutdownsystem() as void uptime(dummy as integer) as double returns the uptime of the system (in seconds) since the last reboot formatdrive(drive as string, fs type as string) as boolean formats the specified drive using one of the file systems listed below this function returns true upon success and false upon failure vfat (fat32, dos/windows file system) readable and writable by windows, linux, and macos exfat (dos/windows file system) supported on series 3 (xtx43, xdx33, hdx23, ls423, ho523) and series 4 (xtx44, xdx34, hdx24, ls424) players with firmware versions 6 2 94 and later secure boot is not supported with the exfat filesystem on firmware version 6 2 94 ext2 (linux file system) writable by linux and readable by windows and macos with additional software ext3 (linux file system) writable by linux and readable by windows and macos with additional software ext4 (linux file system) writable by linux and readable by windows and macos with additional software this is the recommended file system for ssd devices and usb hard drives ejectdrive(drive as string) as boolean ejects the specified drive (e g "sd ") and returns true if successful if the script is currently accessing files from the specified drive, the ejection process will fail copyfile(source as string, destination as string) as boolean copies the file at the specified source file path to the specified destination directory the function returns true if successful and false in the event of failure movefile(source as string, destination as string) as boolean moves the specified source file to the specified destination directory the function returns true if successful and false in the event of failure both path names must be on the same drive mapfilenametonative(path as string) as string converts the specified brightscript style path to the corresponding native path and returns it as a string (e g the path "sd /mydir" will be returned as "/storage/sd/mydir") rnd(a as dynamic) as dynamic rungarbagecollector() as roassociativearray destroys objects that are currently in a state of circular reference counting brightscript normally removes any objects that become unreferenced as part of its automated garbage collection algorithm however, objects that reference each other will never reach a reference count of zero, and will need to be destroyed manually using this method this method is useful when destroying old presentation data structures and generating a new presentation this method returns an associative array outlining the results of the garbage collection process getdefaultdrive() as string returns the current default drive complete with a trailing slash when running as autorun brs , the drive containing the autorun is designated as the current default setdefaultdrive(drive as string) sets the current default drive, which does not need to include a trailing slash this method does not fail; however, if the specified default drive does not exist, it will not be possible to retrieve anything this method accepts the following values "usb1 " – the drive for usb storage devices connected to the player "sd " – the primary sd or microsd drive on the player "sd2 " – the internal microsd drive on the player (4kx42, xdx32 models only) "ssd " – the internal ssd on the player (xtx44, xtx34, xdx34, xdx33 models only) enablezonesupport(enable as boolean) allows for display of multiple video, html, image, and text zones as of firmware 6 0 x, zone support is enabled by default enableaudiomixer(enable as boolean) pi() as double returns the value of pi as a double precision floating point number parsejson(json string as string) as object parses a string formatted according to the rfc4627 standard and returns an equivalent brightscript object, which can consist of the following booleans, integers, floating point numbers, strings, roarray objects, and roassociativearray objects the parsejson() method has the following properties invalid will be returned if the string is not syntactically correct any roassociativearray objects that are returned will be case sensitive an error will be returned if an roarray or roassociativearray is nested more than 256 levels deep the following script demonstrates how to use parsejson() to process a json object containing the titles and urls of a set of images json script { "photos" \[ { "title" "view from the hotel", "url" "http //example com/images/00012 jpg" }, { "title" "relaxing at the beach", "url" "http //example com/images/00222 jpg" }, { "title" "flat tire", "url" "http //example com/images/00314 jpg" } ] } brightscript searchrequest = createobject("rourltransfer") searchrequest seturl("http //api example com/services/rest/getphotos") response = parsejson(searchrequest gettostring()) for each photo in response photos getimage(photo title, photo url) end for formatjson(json as roassociativearray, flags as integer) as string converts an associative array to a json string (i e formatted according to the rfc4627 standard) the following are supported data types boolean, integer, float, string, roarray , and roassociativearray if the flags parameter is set to 0 or not specified, non ascii characters are escaped in the output string as “\uxxxx”, where “xxxx” is the hexadecimal representation of the unicode character value if the flags parameter is set to 1, non ascii characters are not escaped if arrays or associative arrays are nested more than 256 levels deep, an error will occur if an error occurs, an empty string will be returned by default, using object literal syntax (e g aa={relativepath "foo"} ) to generate an associative array will convert keys to all lower case to preserve camel case for json, use the roassociativearray addreplace() method instead of object literals or call roassociativearray setmodecasesensitive() before adding entries