A Scientific Calculator with Persistent Memory written using only 5K of Mobile BASIC source code.
The program can be made into a Mobile Phone MIDlet or a Website Applet without modification. The Applet to the left is controlled by moving the cursor with the arrow keys. The highlighted calculator key is pressed by hitting the space bar.
Calculator WAP Browser Installation
To install Calculator onto your Mobile Phone over WAP simply start your Mobile Phones WAP Browser and visit one of the following links:-
http://www.mobilebasic.com/FreeStuff/Calculator-Nokia.wml (Nokia Phones)
http://www.mobilebasic.com/FreeStuff/Calculator.wml (Other Phones)
Calculator for Nokia Mobile Phones
Calculator-Nokia.jad (Nokia Only)
Calculator-Nokia.jar (Nokia Only)
Calculator for Other Mobile Phones
Calculator.jad (Standard J2ME Phones)
Calculator.jar (Standard J2ME Phones)
Mobile BASIC Source Code for the above program
REM Calculator Engine Rules
REM 1. Functions operate on displayed value
REM 2. Operators operate on Accumulator Values
S$=”Calculator – Powered By Mobile BASIC – The easy way to write mobile phone programs. See http://www.mobilebasic.com/ for Details.”
WHILE (INKEY()=0) AND (X%+W%>0)
IF (MENUFLAG% = 1) THEN
IF (MENUITEM() = EXIT$) THEN GOSUB Finish
IF M$<>”” THEN
IF (LEFT()<>0) THEN
IF (X% < 0) THEN X%=5
IF (RIGHT()<>0) THEN
IF (X% > 5) THEN X%=0
IF (UP()<>0) THEN
IF (Y% < 0) THEN Y%=5
IF (DOWN()<>0) THEN
IF (Y% > 5) THEN Y%=0
IF (FIRE()<>0) THEN
IF (ErrorState%) THEN
IF (Y%=0 AND X%=0) THEN
OPCODE%=-1 DIGIT%=-2 IF (Y%=0) THEN IF (X%=0) THEN DISPLAY=0.0:NEW_NUMBER_FLAG%=1:ACCUMULATOR=0.0:PREV_OPCODE%=0:ErrorState%=0 IF (X%=1) THEN DISPLAY=0.0:NEW_NUMBER_FLAG%=1 IF (X%=2) THEN MEMORY=DISPLAY SETPROPERTY "M0",STR$(MEMORY) NEW_NUMBER_FLAG%=1 ENDIF IF (X%=3) THEN DISPLAY=MEMORY:NEW_NUMBER_FLAG%=1 IF (X%=4) THEN MEMORY=MEMORY+DISPLAY:NEW_NUMBER_FLAG%=1 IF (X%=5) THEN MEMORY=MEMORY-DISPLAY:NEW_NUMBER_FLAG%=1 ENDIF IF (Y%=1) THEN IF (X%=0) THEN DIGIT%=7 IF (X%=1) THEN DIGIT%=8 IF (X%=2) THEN DIGIT%=9 IF (X%=3) THEN OPCODE%=1 IF (X%=4) THEN DISPLAY=SIND(DISPLAY):NEW_NUMBER_FLAG%=1 IF (X%=5) THEN DISPLAY=ASIND(DISPLAY):NEW_NUMBER_FLAG%=1 ENDIF IF (Y%=2) THEN IF (X%=0) THEN DIGIT%=4 IF (X%=1) THEN DIGIT%=5 IF (X%=2) THEN DIGIT%=6 IF (X%=3) THEN OPCODE%=2 IF (X%=4) THEN DISPLAY=COSD(DISPLAY):NEW_NUMBER_FLAG%=1 IF (X%=5) THEN DISPLAY=ACOSD(DISPLAY):NEW_NUMBER_FLAG%=1 ENDIF IF (Y%=3) THEN IF (X%=0) THEN DIGIT%=1 IF (X%=1) THEN DIGIT%=2 IF (X%=2) THEN DIGIT%=3 IF (X%=3) THEN OPCODE%=3 IF (X%=4) THEN DISPLAY=TAND(DISPLAY):NEW_NUMBER_FLAG%=1 IF (X%=5) THEN DISPLAY=ATAND(DISPLAY):NEW_NUMBER_FLAG%=1 ENDIF IF (Y%=4) THEN IF (X%=0) THEN DIGIT%=0 IF (X%=1) THEN DIGIT%=-1 IF (X%=2) THEN OPCODE%=5 IF (X%=3) THEN OPCODE%=4 IF (X%=4) THEN DISPLAY=LOG(DISPLAY):NEW_NUMBER_FLAG%=1 IF (X%=5) THEN DISPLAY=EXP(DISPLAY):NEW_NUMBER_FLAG%=1 ENDIF IF (Y%=5) THEN IF (X%=0) THEN DISPLAY=-DISPLAY IF (X%=1) THEN DISPLAY=3.1415926:NEW_NUMBER_FLAG%=1 IF (X%=2) THEN DISPLAY=DISPLAY*DISPLAY:NEW_NUMBER_FLAG%=1 IF (X%=3) THEN DISPLAY=SQR(DISPLAY):NEW_NUMBER_FLAG%=1 IF (X%=4) THEN DISPLAY=LOG(DISPLAY)/LOG(10):NEW_NUMBER_FLAG%=1 IF (X%=5) THEN DISPLAY=EXP(DISPLAY*LOG(10)):NEW_NUMBER_FLAG%=1 ENDIF IF (DIGIT% <> -2) THEN IF (NEW_NUMBER_FLAG%) THEN DISPLAY=0.0 MULT=10.0 DIV=1.0 NEW_NUMBER_FLAG% = 0 ENDIF IF (DIGIT% = -1) THEN IF (DIV = 1.0) THEN MULT=1.0 DIV=10.0 ENDIF ELSE DISPLAY=DISPLAY*MULT+DIGIT%/DIV IF (DIV <> 1.0) THEN DIV=DIV*10 ENDIF ENDIF IF (OPCODE% <> -1) THEN IF (PREV_OPCODE%=0) THEN ACCUMULATOR=DISPLAY IF (PREV_OPCODE%=1) THEN ACCUMULATOR=ACCUMULATOR*DISPLAY:DISPLAY=ACCUMULATOR IF (PREV_OPCODE%=2) THEN ACCUMULATOR=ACCUMULATOR/DISPLAY:DISPLAY=ACCUMULATOR IF (PREV_OPCODE%=3) THEN ACCUMULATOR=ACCUMULATOR+DISPLAY:DISPLAY=ACCUMULATOR IF (PREV_OPCODE%=4) THEN ACCUMULATOR=ACCUMULATOR-DISPLAY:DISPLAY=ACCUMULATOR NEW_NUMBER_FLAG%=1 IF (OPCODE%=5) THEN PREV_OPCODE%=0 ELSE PREV_OPCODE%=OPCODE% ENDIF ENDIF GOSUB UpdateDisplay GOTO NoError
IF (MENUFLAG% = 1) THEN
IF (MENUITEM() = EXIT$) THEN GOSUB Finish
IF (MENUFLAG% = 1) THEN
IF I%=0 THEN END
Mobile BASIC 2.1 is the next version of our BASIC programming language allowing you to easily create your own applications for Java enabled Mobile Phones.
This version introduces a new project workspace which makes all of your source file visible at a glance.
Each source file is placed within one of the folders depending on what the file contains.
The Integrated Tile Editor enables you to draw tiles that are used as building blocks by the Map Editor. The Tile Editor’s user interface has been improved and made more intuitive.
The Map Editor uses a set of Tiles to create screens for your applications. Mobile BASIC fully supports scrolling Maps so they can even be larger than your phones display.
- Project Workspace (NEW)
- Multiple Source Files(NEW)
- Global and Local Variables (NEW)
- Develop and Test Mobile Phone Programs using your Desktop Computer.
- Generates Mobile Phone MIDlets (JAD and JAR files).
- Generate Website Applets (Demonstrate your Mobile Phone Programs on the Internet).
- Publish MIDlets – Integrated utility to upload your MIDlets to MobileBASIC.com so that they can be immediately installed on your phone using over-the-air installation. Simply enter the given URL into your mobile phones WAP Browser.
- Sprite Graphics with Sprite Priorities and Collision Detection.
- Integer and Floating Point Arithmetic.
- Trigonometry and Logarithm Functions.
- String Handling.
- Form Creation Commands.
- Random Access Files.
- Internet Access from your Mobile Phone Programs.
- Syntax Highlighting.
- Integrated Map, Tile and Image Editors.
- Integrated Utility for Creating Nokia OTT Ring Tones.
- Integrated Help Systems.
- Debug facilities include break and single stepping.
- Add Icons to your Midlets giving them a professional look.
- Nokia option to install in Game or Application Category.
- Support for Nokia Sound, Vibrate and Backlight facilities.
- Sample Programs – Click Here to see some running as Applets now.
- Available for Windows 95/98/2000/NT/XP, Linux and Unix.
This article discusses how to access, create, update and delete data held on SQL databases using your Mobile Phone. This is achieved by two pieces of software, which are described in this article. The first is the SQLClient program written in Mobile BASIC for use on the mobile phone. The second piece of software is the SQLServer program written in PHP and installed on the server. This article uses PHP and MySQL because of their wide popularity however the same techniques could be used for other scripting / CGI languages and databases.
- The mobile phone must be Java Enabled with a copy of Mobile BASIC installed. A trial version of Mobile BASIC can be downloaded from http://www.mobilebasic.com/
- The database you need to access must reside on a server connected to the Internet.
- This server must have a running Web Server supporting PHP.
- You must have access to the server in order to install the server side PHP script.
Establishing Communication with your server
One of the first things to establish before writing any form of database access functionality is that you can establish a communications channel with your server, send it some data and receive a response back.
This is essential and gives you the confidence that everything is configured and working correctly.
Although it is possible for your Mobile BASIC programs to send and receive string, floating point and integer data to the server, it is best to limit yourself to text data. Where it is essential to send integer or floating point data it should be converted to a string using the STR$() function.
The reason for this is that it avoids having to decode the individual bytes making up the integer or float number. A floating point number would be additionally difficult since the environment which your Mobile BASIC runs within doesn’t support IEEE floating point numbers – consequently floating point numbers use a proprietary format.
The program presented below simply asks the mobile phone user to enter their name. Once this has been entered the program opens a channel to HelloServer.php at the URL specified in the OPEN statement. The channel must be opened as “OUTPUT” since we are outputting data.
The entered name is sent to the server using the PRINT statement in line 1020. The INPUT statement on line 1030 is used to obtain the response from the server. Line 1040 closes the channel and line 1050 prints out the response from the server.
Note: This program uses the HTTP protocol and is subject to the limitations imposed by that protocol. Chiefly, you must send all data to the server before you attempt to read a response back. Whilst you can perform OUTPUT 1, OUTPUT 2, INPUT 1, INPUT 2 you cannot perform OUTPUT 1, INPUT 1, OUTPUT 2, INPUT 2. In most cases this is easy to achieve once you are aware of the limitation, in the event that this cannot be done you must close the channel and reopen a new channel to the script.
Mobile BASIC sends data to Internet Servers as HTTP POST data. A PHP script can access this data using the variable $HTTP_RAW_POST_DATA. Unfortunately the data needs to be processed since Mobile BASIC sends text data in Utf8 Format, likewise it also expects text data to be returned in Utf8 format. Fortunately, Utf8 format is quite simple, consisting of a two byte (16 bit) byte count (MSB first) followed by the bytes making up the text string.
This PHP script should be installed on your web server at the same URL as specified in SQLClient.bas
SQL Client / Server Programs
We now go on to illustrate how to implement a simple client program in Mobile BASIC that allows you to enter SQL commands and view there result of those commands on a mobile phone.
Before we embark on writing the program we need to decide on a mutual protocol that both the client and the server understand. Fortunately this is relatively straightforward.
Firstly, in order for the server to be able to perform an operation it needs four items:- The database username and password, the database name and the SQL command to execute. These items are simply sent to the server as four text strings.
Secondly, the server needs someway of send the results back to the client. The contents of the response depend largely on the command that was issued. Our server will send two types of results back:- A status message, and row / column data returned by the database.
The first string read from the server indicates the type of the result. If it’s numerical then it contains the number of rows returned. If it is non-numeric then it is either the string “OK” or an error message generated by the SQL command.
Assuming that the result was numeric then this is the number of rows of data returned.
The next item in the stream is a string containing the number of columns, followed by that number of strings defining the column names.
There then follows a series of text strings – one for each column in every row. For example, if there are 5 rows and 4 columns then there will be 20 (5 x 4) strings to input. This is organized as the data for all columns in row 1, followed by all columns in row 2, etc.
As an example, let’s assume that the SQL command “SELECT Username, Email from UserTable” returned the following data: –
The source for SQLClient.bas is listed below. It is also available under the applications category at http://www.mobilebasic.com/ and can be downloaded directly into a registered version of Mobile BASIC.
Lines 1000-1280 Used to set up initial values for your searches. Due to security considerations you should set USERNAME$ and PASSWORD$ to blank in production programs. Depending on your application you may also want to set DATABASE$ to blank.
If you have a frequent SQL command that you frequently use then you may like to set it as the default. Lines 1300-1440 Allows the user to edit the values before sending the request to SQLServer.php.
This is implemented as a series of dialog boxes that show the current value. The password field uses EDITFORM’s password mode. Lines 2000-2090 Sends the username, password, database and SQL command to the server for processing. Lines 2100-2200 Receove the first string back from the server.
If you recall we said that the first value would either be a numerical value (indicating rows and column data follows) or it is a text string containing either “OK” or an error message generated by the SQL command. This is handled by setting an error handler using the TRAP statement and then attempting to convert the string into a number using the VAL() function.
If the string doesn’t contain a number then the error handler will be invoked resulting in the program resuming at line 9050. In addition, if the number of rows is 0 then we invoke the “No Data” handler at line 9080. Lines 2300-2430 Reads in the number of columns / fields and then proceeds to read the returned column / field names.
These are concatenated together into a comma-separated list stored in FIELDNAMES$ Lines 2500-2660 Reads in the data for the rows and columns.
This is done by defining an array, ROWDATA$, data contains an array element for each row. Note that arrays are indexed from 0 so data for the first row is stored in ROWDATA$(0). All data values for the same row are concatenated together into a comma-separated list. Lines 3000-3060 Simply presents the user with a message containing the column / field names.
Lines 3100-3190 Allow the user to move through the data one row at a time. Note the POP statement in line 3170 this is necessary if you terminate a FOR/NEXT loop abnormally.
If the POP is missing then you will eventually get a stack overflow if you break out of the loop enough times. Lines 9000 onwards Define various message-handling routines that are invoked by the program.
1000 REMRead More
J2ME/BASIC is an easy to learn mobile phone programming language that allows you to create your own mobile phone applications through the convenience of the Internet.
Installation of your J2ME/BASIC applications onto your mobile phone couldn’t be simpler, and you can choose from SMS Short Code (Where Available), WAP Installation Page, or even a traditional manual install using Infrared, Bluetooth or a Data Cable.
If you would like to try J2ME/BASIC then you can either register for your own FREE account or take a look through the Example Programs in the Read Only Guest Account.
- Write programs in BASIC for your Java Enabled Mobile Phone.
- Install programs using phones WAP browser direct from this website
- Install programs using datacable / Infrared or Bluetooth connection
- Produces Fast Java Byte Code suitable for Mobile Phones
- Boolean, Integer, Long, Float, Double, and String Data Types.
- Multi-Dimensional Arrays.
- Graphic Facilities
- Sprite Graphics
- Upload your PNG Images
- Mathematical Functions
- Date and Time Functions
- String Handling Functions
Mobile BASIC is a Mobile Phone MIDlet that allows you to easily develop programs directly on Java enabled Mobile Phones.
The Mobile BASIC MIDlet can be used to write Business Applications, Personal Productivity Programs, Programs for Hobbies and Special Interest or simply use it as a powerful programmable calculator.
The language supports many convenient features including Colour Graphics (on Colour Phones), Sprite Graphics with collision detection, Mathematical functions including Logarithm and Trigonometry, Random Access Files, and a range of Forms. The language also supports several datatypes including Integer, Float and String.
The Mobile BASIC MIDlet can also be programmed to send and received data from Internet Services. Imagine the possibilities for Sales Force Automation – You can obtain customer details, order parts and raise invoices directly from your Mobile Phone. Say goodbye to your expensive cumbersome laptops!
To install the Mobile BASIC MIDlet simply visit http://wap.mobilebasic.com/ using your mobile phones WAP browser.Read More