Bluetooth & Remote Device User Interfaces 
by Richard Hoptroff

Listing One

// Bluetooth module is connected to serial port SER1
extern COM ser1_com;
#define BTH_BAUD 12 // 115,200 baud for SER1
#define BTH_INBUFF 1024 // SER1 Input buffer size
#define BTH_OUTBUFF 1024 // SER1 Output buffer size
unsigned char ser1_in_buf[BTH_INBUFF]; // SER1 Input buffer
unsigned char ser1_out_buf[BTH_OUTBUFF]; // SER1 Output buffer

// UI constants
#define CID_ATOD 1
#define CID_CHART_TY 2
#define NUM_CTRL 2
#define NUM_OPTION 2

// function prototypes
void ProcessFlexiPanelMessages(void);
void HaltProcesses(void);

void main(void)
{
   int16 loopcount;
   // initialize serial port (calls serial I/O library)
   s1_init( BTH_BAUD, ser1_in_buf, BTH_INBUFF,
      ser1_out_buf, BTH_OUTBUFF, &ser1_com );
   // initialize FlexiPanel library and give the UI a name
   FBVSInit( NULL, NULL, 1, &ser1_com, NUM_OPTION );
   FBVSSetDevNameAndCharSet( "Tern Demo" );
   
   // initiate control panel description
   FBVSStartControlList( NUM_CTRL );
   // numeric display of distance
   FBVSAddNumber( CID_ATOD, CTL_NUM_FIXEDPOINT, "Range", 0, 0, 0,
                                                  2, -2, "%% m", NULL);
      // graphical display of distance log
      FBVSAddMatrix(CID_CHART_TY, CTL_MTX_DATA_TY | CTL_MTX_Y_FIXEDPOINT |
              CTL_MTX_Y_2BYTE, "Data Log", 30, 0, NULL, 1, NULL, "Range",
              "Time", "%% m", "%HH%:%mm%:%ss%", 2, -2, 0, 0, NULL);
      // suggest how chart might be displayed
      FBVSSetOption( PPC_DEV_ID, CID_CHART_TY, PPC_ATT_STYLE,
                                           PPC_CST_MATRIX_POINTS );
      FBVSSetOption( WIN_DEV_ID, CID_CHART_TY, WIN_ATT_STYLE, 
                                           WIN_CST_MATRIX_POINTS ); 
   // complete control panel description
   FBVSPostControls( );
   // start control panel service
   FBVSConnect();

   // main program loop
   loopcount = 0;
   while (1)
   {
      // ensure each loop takes around 10ms
      delay_ms(10);
      // discover whether client has sent any messages
      ProcessFlexiPanelMessages();
      // every 3 seconds, ping
      loopcount ++;
      if (loopcount == 300)
      {
         loopcount = 0;
         // ping
         if ( FBVSIsClientConnected() && FBVSIsPingSupported() && FBVSPing() )
         {
            // lost contact with remote device; continue cautiously
            HaltProcesses();
         }
      }
      // every second, log proximity sensor
      if (loopcount%100==0)
      {
         int16 range;
         DateTimeU dt;
         // read A/D (calls A/D library)
         range = fb_ad16( 0xc6 );
         // update numeric display
         FBVSSetNumberControlData( CID_ATOD, range );
         // update chart
         SetToCurrentTime( &dt );
         FBVSAddMatrixControlData( CID_CHART_TY, &range, &dt );
         // send updated time to client
         FBVSUpdateControlsOnClient( );
      }
   }
}

void ProcessFlexiPanelMessages(void)
{
   // check for message
   switch (FBVSGetNotifyCode())
      {
         // nothing has happened
         case FBVSN_NoNotify:
            break;
         // Client has connected; clear the contents of the matrix control
         case FBVSN_ClientConnected:
            FBVSSetMatrixControlData( CID_CHART_TY, 0, NULL, NULL );
            break;
         // Client has modified a control. in this app, no controls are
         // modifiable by the client, so nothing to do
         case FBVSN_ClientData:
            break;
         // Client has disconnected
         case FBVSN_ClientDisconnected:
            break;
         // following messages are informational only and will be ignored
         case FBVSN_GotProfileRequest:
         case FBVSN_GotPinged:
         case FBVSN_GotPingReply:
         case FBVSN_GotAck:
         case FBVSN_IncompatibleVersion:
            break;
         case FBVSN_Abandon:
         // Error; generally only gets here during development
            HaltProcesses();
            Reset();
            break;
     }
}
void HaltProcesses(void)
{
   // In this function, anything controlled by the embedded
   // controller is put in a fail-safe state.
   // nothing being controlled in this app, so nothing to do
}





3


