12
Getting Started
©2000-2008 Tibbo Technology Inc.
' 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
.
130