Writing Code

Top  Previous  Next

Once you have started your new project, you will be presented with a blank file (main.tbs).

We will now begin writing the actual code in this file. We will construct this project from beginning to end, step by step. For your convenience, the end of this section contains a complete copy of the project without comments. You can copy and paste the whole thing into TIDE, or just copy and paste the commented sections one by one as they appear below.

Here goes:

 

 

' Comments cannot spill over to the next line. If you see this happening in this manual, it is a result of the help system -- not an actual feature.

 

Dim hello_world As String ' define a variable which will hold the whole pattern we will play.

 

Dim length, play_position As Integer ' length is a calculated integer which will contain the whole length of the string we will play, and play_position will contain our current position in this string (how much we have played so far).

 

Const PAT_PLAY_CHUNK_LENGTH = 15 ' define a constant for the size of the chunk we will play. We will play one chunk of the pattern at a time, and then move on to the next chunk. Each chunk is 15 'steps' long.

 

Declare Sub play_Next ' let the compiler know that there is a sub called play_next. This sub will be used in code before being created so we must declare it.

 

 

Notice that we are defining a chunk above. The reason for this is that we are going to play quite a long and complex pattern (over 130 steps in length), but the pattern object (pat.) used to play the pattern only supports patterns of up to 16 steps. So we have to play our pattern in parts, one after the other, and track our progress through the pattern (this is what the counters are for).

So far, we have prepared the ground. Let us move to the first piece of executable code:

 

 

sub on_sys_init ' event handler for the init event. Fires whenever the device powers on.

       hello_world = ' here we define the contents of our string, in morse.

 

               'R is Red LED, G is Green LED. GGG means a long pulse of the green LED (line). R means a short pulse of the Red LED (dot). Line (-) means both off.

 

 

               'HELLO .... . .-.. .-.. ---

               "R-R-R-R---R---R-GGG-R-R---R-GGG-R-R---GGG-GGG-GGG" +

               "-------" + ' A period of silence between words

               'WORLD .-- --- .-. .-.. -..

               "R-GGG-GGG---GGG-GGG-GGG---R-GGG-R---R-GGG-R-R---GGG-R-R" +

               "-------" +                

               '! ..--..

               "R-R-GGG-GGG-R-R-"

       length = len(hello_world) ' Calculate total length of string.

       play_position = 1 ' Initialize play_position as we haven't played anything yet.

end sub

 

 

We will now write the event handlers for our code.

First, we want the pattern to start playing whenever you press the button. For this, our platform offers a button object, which generates an on_button_pressed event. Instead of typing, you can create the event handler for this event by double-clicking on the event name in the project tree.

 

 

sub on_button_pressed ' event handler fired whenever the button is pressed

       play_position = 1 ' start playing from the beginning of the pattern

       play_next ' call the routine which plays the next chunk (the first chunk, in this case)

end sub

 

 

Notice that the play_next routine is not yet defined. In our code, it is first used and then defined. This is why we have to declare it at the beginning.

Now, let us move on to the next event handler:

 

 

sub on_pat        ' this fires whenever a pattern (a chunk, in our case) finishes playing.

       play_next ' call the routine which plays the next chunk

end sub

 

 

We have now completed writing our event handlers. Our program now knows what it's supposed to do whenever you press the button, and whenever a chunk of the pattern finishes playing. It just doesn't know how to do it yet. This comes next:

 

 

sub play_next ' plays the next chunk of our large pattern.

       if length < play_position then exit sub ' if we have reached the end of the pattern, stop.

 

       dim chunk_len as integer ' internal integer for the length of current chunk to be played.

 

       chunk_len = length - play_position + 1 ' calculate how much of the large string is left.

 

       if chunk_len > PAT_PLAY_CHUNK_LENGTH then chunk_len = PAT_PLAY_CHUNK_LENGTH ' if too much is left, we bite off only a chunk we can process.

       

       dim chunk as string ' will contain the chunk which will actually play now.

       chunk = mid(hello_world, play_position, chunk_len) ' chunk is the part of hello_world which begins at play_position and is as long as chunk_len.

 

       pat.play(chunk, YES) ' Play this chunk. YES means the pattern may be interrupted -- you can press the button while the pattern is playing, and it will start again from the top.

 

       play_position = play_position + chunk_len ' advance play_position to account for the chunk we played.

 

end sub

 

 

Here is the whole project, without comments:

 

 

'===============================================================================

'                        HELLO WORLD IN MORSE CODE (for EM202-EV, DS202)

'===============================================================================

 

dim hello_world as string

dim length, play_position as integer

 

const PAT_PLAY_CHUNK_LENGTH = 15

 

declare sub play_next

 

'-------------------------------------------------------------------------------

sub on_sys_init

       hello_world =                

               "R-R-R-R---R---R-GGG-R-R---R-GGG-R-R---GGG-GGG-GGG" +

               "-------" +

               "R-GGG-GGG---GGG-GGG-GGG---R-GGG-R---R-GGG-R-R---GGG-R-R" +

               "-------" +                

               "R-R-GGG-GGG-R-R-"

       length = len(hello_world)

       play_position = 0

end sub

 

'-------------------------------------------------------------------------------

sub on_button_pressed

       play_position = 1

       play_next

end sub

 

'-------------------------------------------------------------------------------

sub on_pat        

       play_next

end sub

 

'-------------------------------------------------------------------------------

sub play_next

       if length < play_position then exit sub

 

       dim chunk_len as integer        

       chunk_len = length - play_position + 1

       if chunk_len > PAT_PLAY_CHUNK_LENGTH then chunk_len = PAT_PLAY_CHUNK_LENGTH

       

       dim chunk as string

       chunk = mid(hello_world, play_position, chunk_len)

       pat.play(chunk, YES)

       play_position = play_position + chunk_len

end sub