Home

 

         Programming 101 by Example

Lesson 1

 

Programming can be very difficult but it can also be quite simple.  Here at Heximus we try to hide the very difficult aspects and present the simple.  Kiss Theory. To get started,  download the Heximus app from the playstore and run it.

 To get to the next screen touch the Heximus logo, the wizard hat, top left corner.  The following will appear.

 

In order to keep lesson one very simple, we are going to open a Terminal window.  This is similar to a Linux terminal window or a Windows command prompt.  So in this first lesson we are going to become

command line junkies

 So go ahead and click the Terminal option.

 

The following screen will appear.  When programming and debugging.  Quick detour here.  When you go to all the trouble to write a program,  you expect it to work the first time.  Unfortuately in real life, this rarely happens,  you have to go back and debug it.  Another quick detour,  Debug?,  Yes in the old days bugs would get on the circuit boards and cause short circuits.  So somebody had to remove the bugs.  We don't have problems with insects anymore, but the term debug stuck.  There is a lot of old computer jargon out there,  the orignal writers did have a sense of humour.  Okay back on track now.  When debugging or writing your program, you will want to know whats stored in your varaibles.  Using an on screen keyboard is nowhere nice as a full size mechanical keyboard on a desktop computer, so Heximus was designed to keep typing to a mimimum.  The keyboard presented is not the stock Android keyboard, but a custom one designed for programming in Heximus. Keys have been moved around to avoid having to use the shift or alt keys.

The first command we will learn is the Print command.  The Print command has two names,  P and Print.  We use "p" excusively since it much easier to type and takes up less space.

So for this lesson, we are going to add two numbers together.  2 and 2.  That is,  what is 2 + 2 ?  I know, thats a tough one.

Image A

So simply type p 2 + 2 and then hit enter.  Enter is the bottom right.  The enter key is also know as CR,  or carriage return.  History lesson, carriage return is an old term when we had manual typewriters.

Image B

You would type your sentences and then swing back the carriage return to start a new line.  So that is where CR came from.  Modern day, we call it the Enter key.  CR and Enter are both too long as well.  Look at image A, bottom right corner.  The arrow that points down and then swings left.  This is the new way of how Enter or CR is presented.  The funcationally is the same.  Hitting the enter key returns 13, which can be found an a ascii chart.

In the example above we added 2 and 2,  but we also computed the square root of two.  p sqr(2)

sqr(x),  sqr is a built in function.  There are many built in functions, a complete list is in the index.

A note on built in functions and commands.  P is the Print command and Sqr is the square root function.  So P and Sqr are reserved words, you cannot use these names for any other purpose.

For example if you key in

sqr = 44

You will get an error message "cannot assign a value to a function"

On the line j = sqr(2),  we assigned the computed square root of two into the variable j.  We also took the square root of two, and then raised it to the second power ^ 2.   Take the square root and then square it, you get the original number back.

Computers operate in binary,  while humans count to 10.  Since binary representation would be unreadble and take up a lot of room, computers usually use Hexadecimal,  they count to 16,  not 10 likes humans.

The binary representation of the number 255 is.   p bin(255)

11111111

Now if we use Hexadecimal,   p hex(255) we get

FF

which is much shorter and easier to read.  Other bases were used in the early days,  Octal for example, base 8.

 

At the command line we can also key in linux commands.  For example.

ls  will list the file directory contents

cp test.dat test.sav  // the copy command

mv test.dat newname.dat  // the move or rename command

Also notice the blue area in the middle.  As you are typing context sensitive items will pop up.  Simply click the item to save on typing.  The text will be inserted for you.

None of this is too exciting,  this is command line programming, and you are probably used to rich multi media apps with all there gadets and ads?  Did I mention ads?  Not here,  not ever.

But back in the old days, thats what we wrote,  terminal apps.  This we before the Graphical User Interface.  The GUI,  not invented by but made famous by Steve Jobs and Bill Gates.

 

Below is a very simple command line program.  It prints out all the prime numbers between 3 and 701

You will see //  in sample programs.  // means start of a comment till the end of the line.  Comments are for humans, the computer does not know what they are or how to use them.

// Computes prime numbers
// Trial by division
// HexBasic performance test

p time.init // init timer

for a = 3 till 701 step 2

call trialbydivision

next

p time.elapsed

halt  // tells the program to stop.


trialbydivision:
maxv = sqr(a)
for n = 3 to maxv
 if  a % n = 0 then
   return  // has factor, not prime
 endif
next

p "prime",a

return

The for loop runs from 3 to 701, but steps by the integer 2

The call statement calls the function trialbydivsion.  If trialbydivsion detects no factors, the number is prime and is printed.

 

 

Lesson 2, What you see is what you get

 

I hope you are still reading.  The last lesson was kind of dry but it also important to have a little background knowledge.  In this next lesson we are going to get into the meat and potatoes.  Since nobody likes typing,  simply hit the samples menu item, top left corner and scroll down to helloworld.hex.  Double click or do a long press.  The helloworld app and the source code will open.

 

 

 

Presented above, is the source code for hello world.  It may seem long, but this is no ordinary helloworld.  It has bling.

Click the running man above, next to the 3 vertical dots.  The running man is the run command.  When running the following will appear.

 

Notice Hello world is rotated and note all the flashing.

 

// Simple Hello World with pizzazz
// Display "Hello World" centered

w = canvas.getWidth / 2 // get center of the screen
h = canvas.getHeight / 2

outputstring = "Hello World"  // this is our message to type
s = min(w,h)
fs = s / len(outputstring)

paint.setTextSize(fs*2)

dowhile (1)

colour = color.rgb(random(255),random(255),random(255))
canvas.drawRGB colour  // clear screen

// pick a random color  picking a random color will be real flashy
colour = color.rgb(random(255),random(255),random(255))
paint.setColor(colour) // color is a reserved word

canvas.drawText outputstring, w,h,"CC"  // draw the text, center, center justified.

canvas.angle = canvas.angle + 30 // add 30 degrees to the angle

canvas.DRAW

endwhile

 

To read code, read it like a book.  Left to right and then on to the next line.  However most programs don't just run one time top to bottom.  There will be for loops, dowhile, and branches.  Even our simple example above contains a dowhile.

Dowhile 1  means run forever.  If we have dowhile (x=3),  the dowhile loop will only be processed if x is equal to 3.

This program above never finishes since it is in an infinite loop.  To stop it, click the red hand, same location where the running man was or simply back out.