Palmos Development Tutorial - 2

zhaozj2021-02-08  213

Chapter II Create an app Whether you are a programming expert or beginner, you will soon learn the Palm OS program through this chapter. In this process, you first have to create a development environment that can stabilize work. It is worth mentioning that the development environment is not just a simple combination of programming tools, and it is a system structure that can help developers better completion. Once the source code can be properly compiled and run in the development environment, this means you have taken the most important step throughout the program. During the modification after the program, some other attempts and find B UG work, of course, completing these tasks, it is also necessary to repeatedly debug in the development environment. After a simple review of the basic structure of the Palm OS application, we will create such a development environment. Then we will carefully study the essence of resource programming and the code of Palm OS. We finally get it will be a simple and practical Palm OS application. Programming Environment We almost all C language to write application code because Palm OS application overhead written in C is the smallest. Most examples in the book use the MetrowerKs Code Warrior programming environment. In general, we assume that your computer operating system is Windows. In the attached CD, a free MetrowerKs Code Warrior Lite version can be run in Windows 9x and NT. Use MAC as a development platform if you are using a Mac operating system, then you need more jobs, after all, the example is written for the Windows operating system. Of course, Mac version of Mac version is also provided in the CD. In fact, the MAC version and the Windows version of Code Warrior are very similar. Before the C language should know how much is started to do this book, suppose you can write simple programs with C language. I suggest you have at least a good book about C language. If there is yet, Herbert Schildt writes "Teach YourSelf C" (Osborne / McGraw-Hill, 1997) is a very good choice. Palm OS Application Analysis Palm OS C language programming and normal C language programming are very similar. The source code is compiled and connected to some library files that control the internal operation mechanism of the PALM device. Figure 2-1 shows how a P Alm OS executable is compiled and connected by a variety of different files. In addition, the resource file is compiled and connected separately. They are used to represent graphical components in the program, such as forms, buttons, edit boxes, and more. Many modern graphics applications such as M AC OS and Windows are also using resource files to describe their user interfaces. Each form contains a large number of buttons or other controls. All information for all forms of the application is included in the resource file. You can edit resource files through MetrowerKs Constructionor (constructor) - Add, change, or delete forms and buttons, etc.. Y deep in-depth What is a control? The control is a special area used to accept the screen of user information. It can be easily, such as buttons (through finger or input pen) to complex, such as forms (which contains various other controls). You can create and define controls through the resource editor. Palm OS memory traditional memory is generally divided into two types: a RAM that is fast but power-down, the other is a hard disk memory for low speed but permanently saved data. Most computers spend a lot of time when transferring data from storage devices such as hard drives, C d to RAM. In this regard, Palm's memory has a great difference than conventional computer equipment, which uses another form of memory: fast and permanently saved memory. This makes it possible to increase the speed of data. At the same time, P Alm is no file system, its working principle actually organizes the memory into the form of a database. Basically, all things in the memory are saved into a database form. Even applications are also saved in this form.

The database where the application is located is filled by the executable code and other resources edited by the resource editor. For traditional computer applications, in order to make the program faster, there is inevitable copy of multiple data blocks, such as a copy in the RAM, a copy on the hard disk. But in P Alm OS, if you move data or make copies, it is completely unnecessary, because no matter where your data block is in place, Palm OS can access it directly, which is the benefit of organizing memory into a database form. One. Figure 2-1: How is a Palm OS application generated a source file (Source File * .c) text editor (INCOME FILE * .H) Compiler (Librarian) object file (Object Files .Obj or .o) Resource Editor Library File (LINKER) Resource File (EXECUTABLE FILE * .PRC) Palm The execution of the OS application and debug PALM can only perform one program at a time. You can run the program by selecting an application chart in the main menu or pressing one of the four buttons at the bottom of the Palm. Once a program begins, it will only stop when another program begins. When you close the P alm, the program stops, but after opening, the program will continue to execute at the breakpoint. When you test the program, the Code Warrior development environment runs a program called Console to initialize debugging information. Code Warrior will pass the code and other debug information to Console, then Code Warrior uses console to execute the program, single-step test, set breakpoint, check data, etc. When you complete the program, it is best to restart your Palm, you press the X button on the test panel or insert the hole behind the PUSH-PIN (or other pointed things) to restart the hole behind Palm. In this way, you can clear the data stored in the memory work area. The console file is saved as a hidden file in Palm, and you don't need to do anything to transfer Console. What is the debugger? The debugger is a computer application used to find problems in the program. For the Code Warrior and Palm OS, the debugger is embedded in the PC and associated with the console file to check if the program is running. The debugger allows you to observe code and variables step by step. You can also set breakpoints to observe what problems will occur when the program is running here. The installation of the development environment Installs Code Warrior according to the steps pointed out of the book CD. Code Warrior Lite and Code Warrior are almost the same, but in order to urge certain programmers using genuine software, it has certain restrictions on the size and complexity of the program. If you want to be a real developer, I think it still bought a full version of C Ode Warrior as well. Even if you don't want to buy now, you should have a set after reading this book. After installing the Code Warrior or Code Warrior Lite on a Windows computer, you may find that there is a problem with your constructor, that is, the font on the form is too big.

You must make the following steps to make your constructor run normal: 1. Find a folder containing the operating system, in general in the Windows directory on the C: disk; 2. Open the Windows folder; 3. Open the Fonts folder ; 4. Close the Fonts folder; 5. Turn off the Windows directory; now your constructor can work properly. Ides and Code Warrior When you need to turn program ideas to reality, a good programming environment is particularly important. Earlier (before the Out of Turbo Pascal), you have to use a text editor, a compiler, a connector to form your programming environment, which may have three different companies. product. The free compiler of Palm OS - GCC is an example of this. First create code files and resource files with a text editor, and then compile, connect, and debug code separately with GCC tools. In the early 1990s, a new programming environment - Ide (integrated development environment) has an development environment integrating editing, compiling, connection, commissioning. Code Warrior is such a programming environment, which saves you a lot of time and effort, so as not to be a conversion between all links; and you don't have to waste time for the establishment of your own development, because all The tools are integrated together. Check your development environment If you can guarantee that hardware and software are connected normally, you can save a lot of time, especially for the embedded environment such as Palm OS - compile, connection, and program operations are not on the same machine. I want to have more troublesome things that are more annoyed when I have a problem, hardware or my code is more annoying than I have problems. After years of programming practice, I try to avoid the impact of these uncertainties. Let's talk about how to check your development environment step by step: 1. If you haven't done anything, then the first step is to install the software on your PC and Palm communication software HotSync. 2. Keep your Palm device with the PC; Tips: If your device can't synchronize, you can try the following methods: Make sure your serial communication interface is inserted with the PC port selected in the HotSync software. The mouth is the same; ensuring that the port you choose is not occupied by other processes when the communication is started (this is the same as the same truth using M odem); according to the prompt in the help, check if your computer has hardware conflicts, this Will make your serial portless work properly; try other serial ports, if any, restart the PALM device by pressing the restart button behind the PALM device. Note: If your Palm device is not only for development, then before debugging, be sure to make Palm and PC data synchronize, otherwise it is possible to cause Palm's original data loss. 3. Before starting debug, right click on the HotSync icon on the PC and select Exitate on the pop-up menu. Since HotSync has a connection channel of the Palm device exclusively when running, you cannot download code or debug, so this step is completely necessary. But if you have forgotten, the situation will not be too serious, Code Warrior will pop up a beautiful message box to prompt you to close your hotsync. 4. Run the Code Warrior Integration Development Environment (IDE); 5. Select File | New; 6. Select Palm OS 3.1 on the Project tab; 7. Set the directory path by the SET button; 8. Type the project name Starter; Note: Put: Your routine and future procedures are a good way in the folder independent of the Code Warrior directory tree. Because this makes you easy backup and look up.

Moreover, when you need to reinstall the Code Warrior, you will not delete your hard-written program due to negligence. Don't ask why I think of telling you, it is an unpleasant experience. 9. On the PC, select Project | Enable Debugger; 10. On the PC, select Project | Debugger or press F5 to compile the connection project; 11. Set the PALM device to Console mode. The easiest way is to use Find to delete the original text in the Find input box and enter the shortcut symbol. The shortcut symbol is like a neoadrierant case "L", and you draw a connected "L" on the input area to generate shortcut symbols. Then write a period (slightly striking two times in the input area) and the number "2". After you do this, you will find that the PA LM device will make sounds and the content drawn will disappear. Figure 2-2 shows the graph whose Palm device is set to Console mode; Figure 2-2 In order to set the Palm to the Console mode to draw contents Cursive L: 草 的 英文 小 字 Letter "L" Two Taps: Two click Number Two: Number 2 12. On the PC, click OK to download the app to Palm. A dialog box as shown in Figure 2-3 will appear on Palm; 13. Click OK on Palm. Now you can start debugging the program from the first row of the function pilotmain (); 14. On the PC, click "" or press the Ctrl-r running application; 15. On the PALM, run the application, on the screen A blank form will appear. Press the "Menu Icon" will generate the menu bar. There is an optional entry on the menu bar, and another form with an Ok button will appear after the selection. If you press the OK button, it will return to the original form; 16. On the PC, press the X button of the debug window to restart Palm. (See Figure 2-4.) If you have turned off the Palm power, please reopen and then press the X button. This will restart the Palm and make it in the normal state of Console. You can also press the RESET button on the back of the P Alm by using the pushpins or other pointers. If you can get this step correctly, you should know that your development tools and download environments have enabled a Starter to run normally. You can stand around this program so that when it is an ignorant thing to attack your computer (I believe me, even if you are the luckiest, "bad luck" is also possible to visit you), then use this procedure to check your Development environment. When you view the application area on Palm, you will find more applications called Starter. If you use Palmed or later, you can choose the apps | delete to remove the downloaded application. The generation project is now in the time of generating its own project. In this project, there is a button to write "Hello". We can generate this project like the Starter program, delete the original code and resources of the S Tarter program and replace it with your own new code and resources. You can automatically generate by changing the project settings. Many settings are more interesting, they have direct answers, and they have been set in the S Tarter application. So this doesn't have many difficulties: 1. Run the Code Warrior integration development environment; 2. Select File | New; 3. Select Palm OS 3.1 in the Project tab; 4. Press the SET button to select the directory; 5. Click OK; 6. Give the project called Hello; Note: You can put your useful document on top-level directory or a special literature directory so that you can easily find yourself and others. When I reserved the application for backup, I often placed in a directory called I DEAS. When you want to add such a file, you can choose "Project | Add Files".

Note that the file type should be set to All Files so that the document file can be seen. 7. Click the AppSource tree to open; 8. Right-click Starter.c and select Delete. This is the original Starter code, you should use your own code will replace it; 9. Click AppResources to open it; 10. Right-click Starter.RSRC and select Delete. This is a planning information that includes the original Starter program for forms and controls. You should also replace it with your own resource files; 11. Enter the Windows browser to find the newly created project folder, it should call Hello. In this folder, there is a folder SRC. Open this folder, which is the source file in the S Tarter program, all selected and deleted. In the next section, you will build a new source file for the Hello program, which is saved here. Generating resource PALM OS resources is essentially the same, but it is simple to generate resources in nature and for Mac and Windows. Since the resource is from the resource format from Rezedit, it is closer to the resources of the Mac. The interface of the P ALM OS program is called a form. To produce this simple "Hello" program, you should establish a single form with a button above. The steps are as follows: 1. Run the Palm constructor, which is the MetrowerKs resource editor. The same menu will also be found in Code Warrior; 2. Select File | New Project File. This will generate an empty resource file. Now the project attribute will be available in the province. If the following chapters will tell what they can do, 3. Change Application Icon Name as the project name "Hello"; 4. Select the Forms line in the Resource Type and Name list box. ; 5. Select Edit New Form Resource or press Ctrl-K to create a new form. The new form will appear in the Forms line and be named "untricled"; 6. Click Name, after the edit box, take the name "Hello"; 7. Double-click the form to open, in the following chapter, detailed explanation The meaning of these properties discusses how they use; 8. Select Window | Catalog or press CTRL-Y to open the control toolbox; 9. Drag a button to the center of the form. Now the attribute of the button will replace the property of the form of the form appears on the left side of the Form dialog. These attributes can be default, will discuss them in detail later. If you are very eager to change the location of the button, you can change the L EFT Origin and Top Origin properties; 10. Click the Label property, change its name by OK to Hello; 11. Click on the panel on the right to reomex Attribute; 12. Select Layout | Hide Object IDs. This will make you really see a button with "Hello". If "Hello" looks bigger font, this is because you have encountered my strange font issues that I have said in the installation section. To solve this problem, turn off the constructor, open the Fonts folder in the W Indows folder, and then reopen the constructor. Your "Hello" form will be shown in Figure 2-5; 13. Select Fiel | Save to save engineering resources. Save it under the SRC subfolder of the Hello Engineering folder. And named hello.rsrc. 14. After you complete your new project, the next step is involved in the overall project.

Resource engineering is part of the general project, and the general project will include all aspects regarding graphical descriptions of user interfaces, such as: size, layout and forms of forms, etc .; 15. Open the Code Warrior integrated development environment, select File | Open recent | Hello.mcp; 16. Select Project | Add Files. Set the file type "All Files" to see the resource file. Find hello.rsrc open; 17. At this time, the resource file will appear at the top of the project. For the sake of simplicity, drag it to the next side of the appresource resource group. To do this, you can hold down the mouse to drag the file icon to the lower part of the application (A ppresource) resource group, loosen the mouse when there is a line below the resource group; 18. Here, you have successfully created And add new resources. Generating code To make your program to run, you must add C code, steps are as follows: 1. Establish a new file that can be written to code, you can choose File | New Text File creation; 2. Select File | Save AS Save File as Hello.c; 3. Select the Project window, select Project | Add Files. Find hello.c Add it; 4. If it is not in the Appsource folder, drag it into the folder. When you drag files, you will find a straight line, which allows you to insert files; enter the following code into the Hello.c file. A detailed explanation will be conducted in detail in the next part.

/ * The super-incrude for palm os * / #include / * ot resource file * / #include "hello_res.h" / * a prototype for out "; * / static boolean myhandleevent (eventtype * event); / * the Main entry point * / dWord Pilotmain (Word CMD, PTR, WORD) {Formptr form; / * a Pointer To Our form structure * / evenettype event; / * u Event structure * / / / * if this is not a Normal Launch, Don 't launch * / if (! cmd = sysAppLaunchCmdNormalLaunch) return (0); / * Initialize our form * / form = FrmInitForm (HelloForm); FrmSetEventHandler (form, myHandleEvent); (form) FrmSetActiveForm; FrmDrawForm (form); / * Our Event loop * / do {/ * get the next event * / evtgetEvent (& Event, -1); / * Handle System Events * / if (Syshandleevent (& Event)) Continue; / * Handle Form Events * / FRMDispatChevent (& Event) ; / * If it's a stop event, exit * /} while (event.type! = Appstopevent); / * We're done * / return (0);} / * ur form handler function * / static boolean myhandleevent (EventType) * Event) {/ * par Seate * / if (event-> eType == CTLSELECTEVENT) SNDPLAYSYSTEMSOUND (SNDAlarm); / * We're done * / return (false);} The code analysis source code consists of two functions. It creates a form and a button that can exit the application. Of course, in order to solve the actual problem, we must increase the deeper program structure. Here is the progressive explanation of the code. / * The super-include for palm os * / #include pilot.h includes all Palm OS-related header files. / * OUR Resource File * / #include "Hello_RES.H" In-depth (in Depth) Notice Pilot.h included in <> and Hello_RES.H is included in quotation marks? That is to say, the compiler will look for pilot.h in the system folder specified by Palm OS, and for Hello_RES.H, first look for in Hello / SRC, if you find it to all folders. When you create Hello.RSRC, hello_res.h has also been established. Hello_RES.H contains the Id identifier of forms and controls. Note: Before running, he should add hello_res.h to your project, which allows you to easily view and edit.

When you add a file, first select Project | Add Files. Locate hello_res.h to drag it into the Appsource folder. / * A prototype for out ;lelery function * / static boolean myhandleevent (eventtype * Event); The above is the prototype of the callback function of the form event and control event, the so-called callback function is called in a system in the application, it is general Used to respond to system or user-defined events, such as the event you generated in the form in the form. Function pilotmain () / * The main entry point * / dword pilotmain (Word CMD, PTR, WORD) {pilotmain () is a function that contains a startup code. Variable CMD provides an entry for activating applications. Next I will tell you what kind of role will play after starting the startup code. The pointer to the form is defined as follows. Formptr form; / * a Pointer to out form structure * / The pointer to the current event is defined as follows. EventType is a data structure that contains all information about the current event. EventType Event; / * OUR Event Structure * / Tip: General We will first check if there is some code that identifies the Palm OS version to ensure that the program has any incompatible phenomena in the discovery operating system. Safety and decent exit. In order to achieve this simple purpose, I first separated this part of the code as follows. It is to be described in advance. The application now provided is indeed possible to run on any P Alm device because it only uses few system resources, but there are some unpredictable problems when running other complex programs. Let us look from the code below. Dword ROMVersion; // Get the ROM version ROMVersion = 0; FtrGet (sysFtrCreator, sysFtrNumROMVersion, & ROMVersion); // Alert and bail if the ROM version is too low if (ROMVersion

/ * If this is not a Normal launch, don't launch * / if (cmd! = Sysapplaunchcmdnormal) Return (0); below is some function calls for the Palm OS Inline Form Manager. First, call the frMinitForm () function to enable our form to enter the standby state. By using the fmsetendhandler () function as the Form Handling Function. Then, use the fmsetActiveForm () function to activate the form. Finally, call the FRMDRAWFORM () to display the form. / * Initialize outform * / form = frMinitform (Helloform); FRMSETEVENTHANDLER (Form, MyHandleevent); FRMSetActiveForm (Form); frmdrawform (form); below is the beginning of the event cycle. / * OUR Event loop * / do {Deextile: What is an event cycle? Event cycle is a program loop for processing application and user, and interacting between applications, such as processing input pen motion, buttons are pressed and otherwishes. Many modern graphical user interfaces (G ui), such as Mac OS, Windows, etc. are all event-driven operating systems. Event is a means of operating system and your application to interact, so that users can do what users are doing some of the applications. Generally, such as a simple operating system such as P Alm OS, typically saving events in the order of occurrence, and users can request events and handling events. The following functions call the event processor (Event Manager) from the ROM and put the old event in the queue to handle the event in the order of the operating system. / * Get the next event * / esvent (& Event, -1); The following function calls the system manager to handle system events. / * Handle System Events * / IF (Syshandleevent (& Event)) Continue; The following function not only calls the callback function processing event, but also has a lot of other tasks. For example, using both Penupevent and PENDOWNEvent to generate new events, this is like a CTLSelectEvent event, we use it in the Form Event Processing function to decide whether the button is pressed. This is why we use the system callback function to handle events rather than put all events in a huge event loop. / * Handle Form Events * / FRMDISPATCHEVENT; once another application is selected by the user, it will handle the following code to handle all the good work before exiting. There is no need to clear in this example program, so we can exit the execution in the execution by ending the loop. These codes often appear at the bottom of the loop because we always need to add some code to the form event processor (or other processing function) when you exit, so let this exit event receive cycle throughout the program structure. have benefits. / * If it's a stop event, exit * /} while (event.type! = Appstopevent); / * We're done * / return (0);} HelloHandleEvent () function still remember? We call the frMseTeventHandler () function to be implemented by passing the port address of the HelloHandleEvent () function to Palm OS.

This function processes the Forms event by calling the frmdispatchool () function. / * OUR FORM HANDLER FUNCTION * / static boolean myhandleevent (evening "" Hello "button on the form is pressed, in the example, I simultaneously make a warning. / * Parse the Event * / if (Event-> ETYPE == CTLSELECTEVENT) SNDPLAYSYSTEMSOUND (SNDAlarm); / * We're done * / return (false);} My programming style is through more than 500,000 lines of code, 16 years of program career is developed. However, I saw my program didn't mean learning my style. I think that you should not grant the contents of the book, but you should use your own way to program, and don't feel uneasy to try your own unique programming style. Write your own sample programs with your own way, often make people learn more and understand those inner principles, so I am very agreeable with the programmer's code. Debug: Make it work in this section You can start compiling and linking this project. First choose Run Project | make. If you do it like me, you may have some bugs (of course, not all aspects are perfect). I wish you good luck. Okay, Okay, we have made a lot of progress. You can now use the Compare Files to make more comparative and more complaints. If you accidentally delete library files when you create an engineering, you will generate a connection error. Your best handling method is to do a good job in these documents as soon as possible. The best way to find run time errors is the progressive debugger. You can do some of the debugging steps below in the Code Warrior IDE integration environment: 1. Select Project | Debug on the PC to click F5 to compile and connect the project, then call up the debugger. 2. On Palm, set the device first to the Console mode. The easiest way is to use the find box to remove the original text in the Find input box and enter the shortcut symbol. The shortcut symbol is like a neoadrierant case "L", and you draw a connected "L" on the input area to generate shortcut symbols. Then write a period (slightly striking two times in the input area) and the number "2". After you do this, you will find that the PA LM device will make sounds and the content drawn will disappear. Figure 2-2 shows graphics to which Palm devices are set to Console mode; 3. On the PC, click OK to download the application to the Palm device. Click OK to remove the Code Warrior Lite warning box that appears on Palm, now you should stop in the Code Warrior debugger, the first line of the Pilotmain () function. 4. On the PC, click on the top of the debugger window looks like a button to the right arrow. This will allow you to run the program in a single step, after entering the function, the arrow will stay on the statement to be executed each time. Click another button like an upward arrow that will give you a function in any execution. 5. On the PC, press the "X" button at the top of the debugger window to reset the PALM device. If your PALM device is already turned off, then open, then press the "X" button, the device will be placed in a good C Onsole mode close. Of course, you can also achieve the same purpose by pressing the RESET button on the back of the PALM by using the pushpins or other pointers. Fortunately, my early source code comments will be useful to you, especially when you are trying to speculate on each code meaning in the program.

Of course, if your form does not display, then you have to make sure you have properly connected the current resource file (* .RSRC), and make it easy to set breakpoints and single step tracking by frmdrawform () function portal. Whether it is called. You can also set breakpoints by clicking on the blank of the left on each row. If a breakpoint is set, a small red point will appear on the left of the page on the row. When you remove the breakpoint, just click on the small red point displayed on the left side of the row. Figure 2-6 shows that the sample program runs like this sample program is displayed. This sample program is displayed. When your Hello sample program is completely debugged, you will find: 1 In Palm's application area will appear "Hello" icon 2 Once the program is loaded, it will display its form and "hello" button 3 When the "Hello" button is pressed, the program will exit 4 When the four real buttons on the PALM are pressed or any one "Silkscreen Bottom" on the edge of the Grafti Pad, the program will exit from your congratulations. You have already written your first Palm OS application. This is a good program that can be used to test whether there is some unsecured settings in the current code. What happened when you run your program? Now you have a Palm OS program that can work, let us set up a breakpoint, one step back, so that it is more in-depth understanding how it works. 1. At the PC, start the debugger. Choose Project | Debug or press F5 to start compiling and connect engineering and pop up the debugger. 2. Set your device to the Console status on Palm. Use the find box to delete the original text in the Find input box and enter the shortcut symbol. The shortcut symbol is like a neoadrierant case "L", and you draw a connected "L" on the input area to generate shortcut symbols. Then write a period (slightly striking two times in the input area) and the number "2". After you do this, you will find that the PA LM device will make sounds and the content drawn will disappear. Figure 2-2 shows graphics to which Palm devices are set to Console mode; 3. On the PC, after you click OK, download the application to Palm. Now you need to set breakpoints in the first line of the pilotmain () function. 4. On the PC, found from the debugger window, as follows: / * Parse the evenet * / if (Event-> ETYPE == CTLSELECTEVENT) SNDPLAYSYSTEMSOUND (SNDALARM); 5. On the PC, set breakpoints before the IF statement. 6. The entire program will be debugged by a single step by repeatedly pressing the button to execute a row of code. Note that the call of the frMdispatChevent () function stays on the Form Event Processor (M YHandleEvent ()), but since SndPlaySystemSound () is not running due to CTLSELECTEVENT, SndPlaySystemSound () is not running. After skipping the sndplaysystemsound () function, the program enters the g eTevent () state, waiting for other events. 7. Click the "Hello" button on Palm, note that the getEvent () function responded that the form event processor was repeatedly called many times, and finally CTLSELECTEVE NT has occurred, so SndPlaySystemSound () is called. You have witnessed your code allocation and handling some events. 8. On the PC, press the "X" button at the top of the debugger window to reset the PALM device.

转载请注明原文地址:https://www.9cbs.com/read-2747.html

New Post(0)