13
TIDE and Tibbo BASIC User Manual
©2000-2008 Tibbo Technology Inc.
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
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_po
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_po chunk_len
' advance play_position to
account for the chunk we played.
end
sub
Here is the whole project, without comments:
79