BASIC4PPC  COMPILER
PROGRAM  EXAMPLE:


RF RANGE TESTER
This program example will
use a BlueSmiRF module in "Loop Back" mode to test
how far away the PDA can
be moved from the Bluetooth
module before the RF link
stops communicating.
The Pocket PC used in this example is an HP IPAQ H2200 PDA with:

     64 MB of system RAM
     32 MB of system flash ROM  ( Dated: 5/22/2003 )
     OS Version:  Windows CE 4.20
     Display Resolution:  240 horizontal pixels  by  320 vertical pixels
     Processor Type:  Intel PXA255
     Bluetooth
     1 SDIO Card Slot
     1 Compact Flash Card Slot
     Headset / Microphone Jack

This HP H2200 PDA was manufactured in 2003.  Any of the newer Pocket PCs
with Bluetooth capability running the latest version of the Basic4ppc Compiler
should be able to run this simple program example.

The embedded bluetooth hardware module used in this example is a BlueSmiRF
module.  You can search the Internet and find a variety of embedded bluetooth
module boards and fully packaged plug-and-go bluetooth dongles.

The microcontroller used in this example is the: Athena.  The Athena micro is a
PIC microcontroller that is pre-programmed with a BASIC Interpreter.  The Athena
can be programmed easily in BASIC using the IDE software written to run on a
Windows compatible desktop PC or laptop.
This program example will allow you to put the BlueSmiRF module in a stationary
position and then see how far you can walk away from it, in any direction, while
holding the PDA before the Bluetooth RF link stops communicating.  This Basic4ppc
Software Application will allow you to test the working "Range" between your Pocket
PC PDA and a BlueSmiRF Bluetooth module in your home or office environment.

The practical working range will match the Bluetooth specifications of your Pocket PC
PDA.  The HP IPAQ H2200 used in this example has a range of 10 meters which equals
about 32.5 feet.

                    (10 meters x 39 inches per meter) / 12 inches per foot = 32.5 feet

With this program example you can establish a Bluetooth link between a BlueSmiRF
module and your Pocket PC PDA then move around and visually see on the PDA screen
when the RF link stops communicating as you move out of range.  When you move
towards the BlueSmiRF module again you can visually see on the PDA when the RF link
starts to communicate again as you move back in range.  This program will allow you to
move around and explore your environment.  This will give you a good idea of where your
PDA can be and still have a reliable Bluetooth RF link with a stationary BlueSmiRF
Bluetooth module.
BASIC4PPC PROGRAM CODE FOR THIS EXAMPLE.
This is what the screen looks like at program start-up.

At start-up the Bluetooth Serial Port connection is
"Closed" and the "RF Range Test" is OFF.
The first thing we need to do is to "Open" the Bluetooth
Serial Port connection between the Pocket PC PDA and
the BlueSmiRF Bluetooth module.
Now, we can turn the "RF Range Test" ON.  The test will
transmit the ASCII character "T" repeatedly at 500 ms
intervals.  The BlueSmiRF module will loop the character
back to the Pocket PC PDA where it will be displayed in
the RX Textbox window.

If the RF link is good, the Pocket PC PDA will receive
back the ASCII "T" character that it previously
transmitted to the BlueSmiRF module and the
message:  
                     RF DEVICE IN RANGE

will be displayed on the PDA screen.
If the RF Link is bad, then the Pocket PC PDA is out of
range and the message:

                      RF DEVICE OUT OF RANGE

will be displayed on the PDA screen.

This means the Pocket PC PDA tried to transmit the
ASCII "T" character and did not receive the ASCII
"T" character back from the BlueSmiRF module.
To stop the RF Range Test, press the "RF RANGE TEST OFF" button.  This will clear the messages from the PDA screen.
To transmit individual ASCII characters or strings of ASCII
characters, you can use the PDA's Soft Input Panel keyboard
to enter the character(s) in to the TX TextBox window.
After you have entered the ASCII character(s) that you
wish to transmit, press the "SEND TX" button on the PDA screen to actually transmit the character(s).
Press the "CLEAR" button to clear out the ACII characters
in the TX and RX TextBox windows.
When you are finished, press the "CLOSE" button to turn off
the Bluetooth Serial Port connection between the PDA and the
BlueSmiRF Bluetooth module.
This Basic4ppc program example uses three Timer Controls for the "Range Test".

Timer 1 is used as a heartbeat clock which fires every 500 mS.  The subroutine for
Timer 1 actually transmits the ASCII "T" character and updates the TX TextBox
window.  When the Pocket PC PDA and the BlueSmiRF Bluetooth module are
"In Range" of each other the ASCII "T" character will be received by the PDA and
the serial_OnCom subroutine will be entered.  The serial_OnCom routine will
update the tmr_count variable and the RX TextBox window.

Timer 2 fires every 1000 mS.  The Timer 2 subroutine will update the variable
tmr_count_1 by setting it equal to the current value of variable tmr_count.

Timer 3 fires every 2000 mS.  The Timer 3 subroutine will update the variable
tmr_count_2 by setting it equal to the current value of variable tmr_count.  Next,
variable tmr_count_1 will be compared to variable tmr_count_2 to see if they are
equal.  If they are NOT EQUAL then the PDA and BlueSmiRF are still "In Range" of
each other and the RF link is still communicating.  If they are EQUAL then the PDA
and BlueSmiRF are "Out of Range" of each other and the RF link is NOT
communicating properly.

The idea here is simple, the tmr_count variable will be incremented by 1 every time
the serial_OnCom subroutine is executed.  As long as the PDA and the BlueSmiRF are
"In Range" the tmr_count variable will be updated every pass through the
serial_OnCom subroutine.

If the PDA and the BlueSmiRF are "Out of Range" the serial_OnCom subroutine will
NOT get executed and the variable tmr_count will NOT be updated.

So, we have 3 Timer Controls that fire at 3 different time intervals: 500 mS, 1000mS
and 2000mS.  When the PDA and BlueSmiRF are "In Range" the variable tmr_count
will be updated continuously at 500 mS intervals.  Both Timer 2 and Timer 3 will read
the current value of the tmr_count variable when they fire every 1000 mS and 2000 mS. 
Since Timer 2 and Timer 3 fire at different intervals, the value of variable tmr_count
should be different each time that it is read.  So, the value of variable tmr_count_2
should NOT EQUAL the value of variable tmr_count_3 as long as the PDA and
BlueSmiRF are "In Range" of each other.

When the PDA and BlueSmiRF are "Out of Range" of each other the serial_OnCom
subroutine will NOT get executed and the variable tmr_count will NOT be updated. 
When Timer 2 fires at 1000 mS intervals it will read the current value of the tmr_count variable.  When Timer 3 fires at 2000 mS intervals it will read the current value of the tmr_count variable.  Since the variable tmr_count is NOT being updated at 500 mS
intervals because the PDA and BlueSmiRF are "Out of Range" the value of tmr_count_1
and the value of tmr_count_2 will be EQUAL to each other.

When the PDA and BlueSmiRF move back "In Range" of each other the serial_OnCom subroutine will start executing again and the value of variable tmr_count will be
updated again.
Sub Globals
    tmr_flag=0
    tmr_count=0
    tmr_count_1=0
    tmr_count_2=0
    portflag = 0
    strlabel0="COM 8: 9600,N,8,1"
    strlabel1=" [OFF]"
    strlabel2=" [ ON]"
    strlabeltemp=""
    strval=""
End Sub

Sub App_Start
    Form1.show

    textbox1.Text=""
    textbox2.Text=""
    label1.Text = strlabel0 & strlabel1
    strlabeltemp  = strlabel0 & strlabel1
     
    serial.New2 (8,9600,"N",8,1)

    textbox1.FontSize=8
    textbox2.FontSize=8

    ' Turn the Port Closed indicator ON
    ' since the Bluetooth port is closed
    ' when the program first starts up.

    image7.Image="sr.jpg"

    image16.Image="w28w35.jpg"
    image17.Image="r28w35.jpg"

    Sip=false
    Form1.Focus=true

    label4.Visible=false
    label5.Visible=false

End Sub

Sub ImageButtonClear_Click
    Textbox1.Text=""
    Textbox2.Text=""
    Form1.Focus=true
End Sub

Sub ImageButtonClear_ButtonDown
    image8.Image="sr.jpg"
End Sub

Sub ImageButtonClear_ButtonUp
    image8.Image="sw.jpg"
End Sub

Sub ImageButton1_Click
    image6.Image="sr.jpg"
    image7.Image="sw.jpg"

    If portflag = 0 Then
       serial.PortOpen=true
       portflag = 1
       label1.Text = strlabel0 & strlabel2
       strlabeltemp=label1.Text
       serial.enableoncomm=true
       serial.Output ("*")
End If
End Sub

Sub ImageButton2_Click
    image6.Image="sw.jpg"
    image7.Image="sr.jpg"

    If portflag = 1 Then
       TextBox1.Text=""
       serial.Output ("^")
       serial.enableoncomm=false
       serial.PortOpen=false
       portflag = 0
       label1.Text = strlabel0 & strlabel1
       strlabeltemp=label1.Text
    End If
End Sub

Sub serial_OnCom
    strval=serial.InputString
    TextBox2.Text=TextBox2.Text & strval

    If tmr_flag=1 Then
       If strval = "T" Then
          label4.Visible=true
          label5.Visible=false
          Sound("beep.wav")
          tmr_count=tmr_count + 1
       End If
    End If
End Sub

Sub ImageButtonSendTX_Click
    serial.Output (Textbox1.Text)
End Sub

Sub ImageButtonSendTX_ButtonDown
    image9.Image="sr.jpg"
End Sub

Sub ImageButtonSendTX_ButtonUp
    image9.Image="sw.jpg"
End Sub

Sub Timer1_Tick
    serial.Output ("T")
    textbox1.Text=textbox1.Text & "T"
End Sub

Sub Timer2_Tick
    tmr_count_1=tmr_count
End Sub

Sub Timer3_Tick
    tmr_count_2=tmr_count
    If tmr_count_1=tmr_count_2 Then
       label4.Visible=false
       label5.Visible=true
    End If
End Sub

Sub ImageButtonRangeTestOn_Click
    image16.Image="r28w35.jpg"
    image17.Image="w28w35.jpg"

    timer1.Interval=500
    timer1.Enabled=true

    timer2.Interval=1000
    timer2.Enabled=true

    timer3.Interval=2000
    timer3.Enabled=true
    tmr_flag=1
End Sub

Sub ImageButtonRangeTestOff_Click
    image16.Image="w28w35.jpg"
    image17.Image="r28w35.jpg"
    timer1.Enabled=false
    timer2.Enabled=false
    timer3.Enabled=false
    textbox1.Text=""
    textbox2.Text=""
    label4.Visible=false
    label5.Visible=false
    tmr_flag=0
End Sub
BLUESMIRF MODULE WIRED-UP IN "LOOP BACK" MODE.