Programming with
Hello, World!
By now, you know how to compile and run 'hiworld.c'. What makes this file tick?
This file contains the following code:
#include <hpgcc49.h> //the standard HP lib
int main(void){
// code begins execution from here
clear_screen(); //clear the screen
printf("Hello, World!!"); //prints "hello world", starting from position 0,0
while(!keyb_isON()); //loop until ON pressed
}
The first line, #include <hpgcc49.h>, included the needed header files. It should almost always be there.
Clear_screen() obviously clears the calculators screen.
Now there is code to loop until the ON key is pressed. Without it, the program would quit so quickly you couldn't see the text. As you might guess keyb_isON() returns TRUE if the ON key is being held down.
Other useful keyboard related functions are keyb_isRS(), keyb_isLS() and keyb_isAlpha(). These return the state of the Right Shift, Left Shift and Alpha keys respectively. ON, Alpha and the Shift keys are all connected directly to the CPU.
The remaining keys are connected in a matrix fashion. To test for a key use keyb_isKeyPressed(unsigned char col, unsigned char row). This returns TRUE if the key at (col,row) is pressed. Unfortunately the row and column numbers don't match the physical position of the keys. Use the following table to determine the coordinates for any key.
There is also keyb_isAnyKeyPressed(). It returns TRUE if any key is being held down.
Optimising for Size: The many faces of Hello World
There is a minor issue with this program: It's over 10kb! Thats huge for such a simple program.
The main reason is that printf is a very general-purpose function. By default, using printf has to include support for handling many different data types. After all, printf must be able to print integers, strings, and floating point numbers (floats and doubles).
One simple optimisation is to leave out floating point support. If you know that you won't need to print floats or doubles, add #define TINY_PRINTF to the top of your file.
#define TINY_PRINTF
#include <hpgcc49.h> //the standard HP lib
int main(void){
// code begins execution from here
clear_screen(); //clear the screen
printf("Hello, World!!"); //prints "hello world", starting from position 0,0
while(!keyb_isON()); //loop until ON pressed
}
This version works exactly the same as the last, but now its only 6.5kb. We can still do better.
Printf() has to handle printing the values of different data types, but we only want to print a string. The Puts() function is intended to print only strings to the standard console. Replacing printf() with puts() gives a 4.5kb file.
If size is really an issue, we can bypass the standard output functions and write directly to the screen. The following program is about 3.5kb
#include <hpgcc49.h>
int main(){
clearScreen(); //clear the screen
print("Hello, World!!",0,0); //prints "hello world", starting from position 0,0
while(!keyb_isON()){ //loop until ON pressed
}
The first difference is the use of the clearScreen() function to clear the screen.
In the earlier version, we were clearing the screen with clear_screen() - now we're using clearScreen(). This is a bit confusing. Why have 2 clear screen functions?
Print() is a dumb function. It doesn't do any text formatting. It just dumps a string to a given position on the screen. Printf on the other hand is much more complicated. It has a cursor, which keeps track for the next position to put text.
ClearScreen() actually calls a routine in ROM to erase the screen. So it is fairly tiny. Clear_screen() on the other hand clears the screen and also resets the cursor to the upper-left of the screen. It is a little bit bigger. A rule of thumb is if you are using standard C functions output functions, such as puts or printf, use clear_screen(). Otherwise use clearScreen(). If you don't care about size, its simpler to just always use clear_screen().
Note that both ClearScreen() and clear_screen() only work if you are not using a graphics library.
Next we have Print(). It takes in a string, and X and Y parameters. Note that the X and Y axis is as follows:
So, (0,0) is actually the upper left corner, not the lower left.
print("Hello, World!!",0,0);
will print the text "Hello World" in the upper left corner of the screen.