• Aucun résultat trouvé

Creating the PDA Robot Project

Dans le document PDA Robotics (Page 180-192)

Start the CodeWarrior IDE. Clicking on “File/New” brings up the new project dialog that allows us to select the Palm OS Application Wizard and enter the project name and where it will be saved (see Figure 8.2) Enter PDARobot and select a directory where you want the source code to reside (see Figure 8.3).

Chapter 8 / PDA Robot Palm OS Software Using Code Warrior 8.0

157 Figure 8.2

Creating a new project.

Figure 8.3 Palm OS Application Wizard dialog.

Click OK after entering the information and the dialog shown in Figure 8.4is displayed, showing the application information.

The additional SDKs to add are grayed out because this is the evalua-tion version. No addievalua-tional SDKs are needed for this project anyway.

The Creator Code and Minimum OS Version is grayed out as well, but this will be changed in the code later.

Click Finish to create the project files and open the project window (see Figure 8.5).

Click on the source folder and then double click on pdarobotMain.c to open this file for editing. First, open the Constructor for the Palm OS 1.6 by clicking on the Resources folder and double-click the file pdaro-bot.rsrc. The constructor window will appear with the project

PDA Robotics

158 Figure 8.4 Application information.

Figure 8.5 The PDA Robot project window.

PDA 08 5/27/03 8:47 AM Page 158

resources viewable in a dialog. From here, we will create the buttons and labels that will make the graphical user interface (GUI) for PDARobot.prc.

Double click Main under Forms to bring up the main dialog, allowing us to place the buttons and labels, after which we can assign the IDs needed for tracking in the event loop of main.c (see Figures 8.6 and 8.7).

Clicking Window/Catalog will bring up the catalog window that con-tains the controls to be placed on the form (see Figure 8.8).

Figure 8.9 shows the form with the buttons and labels in place. The IDs and captions have been assigned to each. I made the Object

Chapter 8 / PDA Robot Palm OS Software Using Code Warrior 8.0

159 Figure 8.6 Por tion of the constructor menu.

Figure 8.7 Clean palette where the controls will be placed.

Identifier the same as the Label displayed on each Object. To generate the header file used when we compile and link the program, click File/Generate Header File or simply click File/Save.

PDA Robotics

160 Figure 8.8 UI Objects.

Figure 8.9 Form with the controls placed.

PDA 08 5/27/03 8:47 AM Page 160

If we switch back to the Metrowerks CodeWarrior IDE and click Project/Make or hit F7, the application will build and generate PDARobot.prc that can be loaded on the PDA and run (though nothing will happen when you press the buttons).

It can be run on the Windows desktop by starting the emulator pro-vided by Palm OS (that was installed with the evaluation version of CodeWarrior). To do this, start the emulator (after downloading or acquiring a ROM of the device) and in the IDE, click Project/Run or hit Ctrl+F5. Figure 8.11shows the program so far, running in the Palm OS Emulator. It looks exactly the same running on the device.

Chapter 8 / PDA Robot Palm OS Software Using Code Warrior 8.0

161 Figure 8.10 The Release and Debug executables in the OBJ director y.

Figure 8.11 PDA Robot running on the Palm OS Emulator.

The AppStart() function reads in any saved information and initializes the infrared libray by calling the function StartApplication, which I should have called InitializeInfrared().

// FUNCTION: AppStar t //

// DESCRIPTION: Get the current application's preferences.

//

// Read the saved preferences / saved-state information.

prefsSize = sizeof(pdarobotPreferenceType);

if (PrefGetAppPreferences(

appFileCreator, appPrefID, &g_prefs, &prefsSize, true) !=

noPreferenceFound) {

// FIXME: setup g_prefs with default values }

if (!Star tApplication()) return 0;

return errNone;

}

The AppStop() function saves any preferences and calls StopApplication() which shuts down the infrared communication.

// FUNCTION: AppStop //

// DESCRIPTION: Save the current state of the application.

static void AppStop(void) {

// Write the saved preferences / saved-state information. This // data will be saved during a HotSync backup.

PrefSetAppPreferences(

appFileCreator, appPrefID, appPrefVersionNum,

&g_prefs, sizeof(pdarobotPreferenceType), true);

StopApplication();

// Close all the open forms.

FrmCloseAllForms();

}

PDA Robotics

162

PDA 08 5/27/03 8:47 AM Page 162

StartApplication loads the IR library, opens and binds a port, saving the information in the variable irref so it can be used elsewhere. This is the firststep in creating the IrDA link.

//

// Loads the Infrared Librar y and opens and binds the por t.

//

static Boolean Star tApplication(void ) {

packet.buff = (unsigned char *)"Data";

packet.len = 4;

IrBind(irref,&connect,callback);

return true;

}

StopApplication unbinds the port, disconnects, and closes the IR con-nection.

//

// Shut down connections, close the librar y //

Chapter 8 / PDA Robot Palm OS Software Using Code Warrior 8.0

163

The callback(IrConnect *con, IrCallBackParms *parms) function is called whenever an infrared event happens, for example, when PDA Robot sends us some data, this function is automatically called with the event and data embedded in the parms parameter.

static void callback(IrConnect *con, IrCallBackParms *parms) {

char* event;

char out= 0;

FormType *frm; // = FrmGetActiveForm();

switch (parms->event) {

case LEVENT_DISCOVERY_CNF:

//

// This event was triggered by PDA Robot when // we broadcast a discover y to ALL IrDA compliant // devices. StoreDiscover y throws away all devices // except PDA Robot. My HP printer always responds to // the discover y request.

//

// PDA Robot has sent some data because we requested it.

// Let's copy the data to a global variable so it can be used // elsewhere.

StrPrintF((char *) range_data, "%u", out);//value);

current_range = out;

//

PDA Robotics

164

PDA 08 5/27/03 8:47 AM Page 164

// Display the range in the Range Label if we are in autonomous mode //

if( autonomous == true ) {

// StoreDiscover y goes through the devices list returned when we // sent out a Discover y request to all IrDA devices in the vicinity.

// It throws away all devices except PDA Robot and set the connection // information returned to us by it.

//

void StoreDiscover y(IrDeviceList* deviceList) {

UInt8 i;

Chapter 8 / PDA Robot Palm OS Software Using Code Warrior 8.0

165

char info[36];

// clear the label

StrCopy((char *)&info, (char *)"______________________________");

FrmCopyLabel (FrmGetActiveForm(), MainStatusLabel, (char*)&info);

if( deviceList->nItems == 0 ) {

StrCopy((char *)&info, (char *)"NO Devices Discovered ");

FrmCopyLabel (FrmGetActiveForm(), MainStatusLabel, (char*)&info);

return;

}

for (i = 0; i < deviceList->nItems; i++) {

//

// We don't want to recognize any device but PDA Robot // so ensure that the device name is 'Generic IrDA'. This // is the default name used by the MCP2150 chip. We will // connect with the first found

//

if( (StrCompare((char *)"Generic IrDA", (char *) &deviceList->dev[i].xid[3])) == 0) {

dev = deviceList->dev[i].hDevice;

connect.rLsap = deviceList->dev[i].xid[0];

StrCopy((char *)&info, (char *)"Discovered PDA Robot ");

FrmCopyLabel (FrmGetActiveForm(), MainStatusLabel, (char*)&info);

} } } //

// Information Access Ser vice Callback. This function // is called when we quer y PDA Robot for information.

// If we received the LSAP information then we connect // to to PDA Robot.

//

static void IrIasCallback(IrStatus status) { UInt8 b;

switch(i)

rlsap = connect.rLsap = IrIAS_GetIntLsap(&quer y);

}

connect.rLsap = rlsap;

packet.buff = (unsigned char *)&controlPacket;

packet.len = sizeof(controlPacket);

//

// Open a connection with PDA Robot //

// Unknown IAS Reply //

break;

} }

Please go to www.pda-robotics.com to download the entire source code and executable for this program.

Chapter 8 / PDA Robot Palm OS Software Using Code Warrior 8.0

167

This page intentionally left blank.

169 The software for the Pocket PC was designed and written using the eMbedded Visual Tools 3.0 IDE and compiler, in conjunction with the Pocket PC 2002 Software Development Kit (SDK). Both are provided free from Microsoft.

The Microsoft eMbedded Visual Tools 3.0 deliver a complete desktop development environment for creating applications and system com-ponents for Windows-powered devices, including the Pocket PC and Handheld PC.

The eMbedded Visual Tools include eMbedded Visual Basic and eMbedded Visual C++, including SDKs for the Pocket PC 2000, Palm-size PC, and Handheld PC. The eMbedded Visual Tools are the suc-cessor to the separate Windows CE Toolkits for VC++ and VB. This version is stand-alone and does not require Visual Studio. Read the specifications on the data sheet.

The Pocket PC 2002 SDK allows you to write enterprise and consumer applications for this innovative platform. The Pocket PC 2002 SDK provides a brand new Pocket PC 2002 device emulator, more docu-mentation, and more samples. In addition, this SDK includes all the necessary application programming interfaces (APIs) and documenta-tion for both Pocket PC 2002 and Pocket PC 2002 Phone Edidocumenta-tion devices.

PDA Robot

Dans le document PDA Robotics (Page 180-192)