Experimental Version of UBDebug - with String Support
UBDebug is a PC-based IDE for CHDK ubasic scripts that runs under Windows and Mac OSX (10.3 "Panther" or later). It lets you create and edit scripts, then step through them line by line while inspecting and setting variables. You can also set the values to be returned by some CHDK functions (such as get_usb_power) and any property. You can learn more about it here.
However uBASIC as currently supplied with CHDK only support integer variables and expressions (as does Adam Dunkels original uBASIC). I've recently extended the original uBASIC to add string support (see here). The obvious next step is to add this support to the CHDK version. However before trying to add the new code to the trunk, it seems a good idea to let people play with it safely in an upgraded version of UBDebug. That upgraded version is available for downloading below.
- 52 string variables, named a$-z$, A$-Z$
- string literals, variables and expressions can be used in assignments and print statements
- string expressions can be compared for equality in if statements
- string expressions can be concatenated using '+'
- ability to use string expressions in 'press', 'release' and 'click' statements and in 'is_pressed' and 'is_key' functions
- ability to string expressions in goto and gosub statments ('computed' goto and gosub)
- the following 'GWBASIC' string functions:
- left$(s$, i)
- returns the first i characters of s$
- mid$(s$, i, j)
- returns the substring of length j starting at position i in s$
- right(s$, i, j)
- returns the last i characters of s$
- returns the integer value i as a string - opposite of val()
- returns the i'th ASCII character - opposite of asc()
- returns the numeric value of the string of digits s$ - opposite of str()
- returns the number of characters in s$
- instr$(t$, s$)
- returns the position of the string t$ in s$ (or 0)
- returns the ASCII code for the first character in s$ - opposite of chr$()
- the following additional general string functions:
- returns s$ with any leading and trailing blanks removed
- returns the number of (blank-delimited) words in s$
- word(s$, n)
- returns the n'th word in s$
- the following additional CHDK functions:
- returns a string containing build information - e.g. "a570 100a CHDK 0.7.4"
- returns the full name of the latest image taken - e.g. "A/DCIM/103CANON/IMG_0343.JPG"
- returns the number of the directory containing the latest image - e.g. 103
- returns the number of the latest image - e.g. 343
- returns the full name of the image whose number is n. Thus get_fullfilename(343) would return "A/DCIM/103CANON/IMG_0343.JPG". Note that a null string is returned if the image does not exist.
- deletes the file whose full name is f$
- renamefile(f$, n$)
- renames the file f$ as n$
The values of string variables and the new CHDK-related functions can be inspected and modfied while the script is running.
|Supplied Test Program||Program Output|
s$ = " start of test " for i = 1 to words(s$) print "'" + word(s$, i) + "'" next i a$= "abcdefghi" b$="123456789" print "Length of a$=", len(a$) print "Length of b$=", len(b$) if len(a$) = len(b$) then print "same length" if a$ = trim(" abcdefghi ") then print "same string" else print "strings differ" endif c$=left$(a$+ b$,12) print "left 12 of a$ + b$=" + c$ c$=right$(a$+b$, 12) print "right 12 of a$+b$=", c$ c$=mid$(a$+b$, 8,8) print "mid 8,8 of a$+b$=", c$ c$=str$(13+42) print "str$(13+42)=", c$ print "len(c$)=", len(c$) print len("this" + "that") c$ = chr$(54) print "chr$(54)='" + c$ + "'" j = asc(c$) print j print "val('12345')=",val("12345") i=instr(3, "123456789", "67") print "position of '67' in '123456789' is", i print get_buildinfo l$="sub1" gosub l$ k$="leftdown" press left$(k$,4) press right$(k$,4) print get_lastdirnum print get_lastfilenum print get_lastfilename print "end of test" end :sub1 print "in sub1" return
starting ... >>> 'start' >>> 'of' >>> 'test' >>> Length of a$= 9 >>> Length of b$= 9 >>> same length >>> same string >>> left 12 of a$ + b$=abcdefghi123 >>> right 12 of a$+b$= ghi123456789 >>> mid 8,8 of a$+b$= hi123456 >>> str$(13+42)= 55 >>> len(c$)= 2 >>> 8 >>> chr$(54)='6' >>> 54 >>> val('12345')= 12345 >>> position of '67' in '123456789' is 6 >>> a570 100a CHDK 0.7.4 >>> in sub1 *** button press 'left' *** *** button press 'down' *** >>> 100 >>> 1 >>> A/DCIM/100CANON/IMG_0001.JPG >>> end of test Script has ended!
- strings are limited to 255 characters
- the string buffer is 4000 bytes long (so you can't have 52 strings each 255 characters long)
These restrictions are trivially easy to amend. Note that operands are checked for errors (out of range etc) and there is a garbage collector, but my testing has not been exhaustive.
Although this is an experimental program, it is a proper superset of the original UBDebug (so any script which worked before should produce the same results). As a result, the names of the class, jar and dll files have not been changed. The zip file contains:
|UBDB.jar||this contains the five java classes which make up the debugger|
|ubengine.dll||the native methods and ubasic interpreter (Windows version)|
|libubengine.jnilib||the native methods and ubasic interpreter (Mac OSX version - supports both PowerPC and Intel Macs)|
|stringtest.bas||a simple test script|
Unzip the files into any convenient directory. To run the debugger simply double-click the .jar file.
Comments, suggestions and bug reports welcome. Dave@zenoshrdlu.com
See here for other KAP and CHDK stuff of mine.