What, Me Program? Are You Crazy?
It’s safe to say that most people who read this book have either never written a computer program or, if they have long ago, never intend to do so again. The majority of off-the-shelf software these days consists of thou- sands of lines of code written by teams of programmers. So why even bother trying to write your own program—why not just jump ahead to the next chapter? My first answer is that programming is fun and you should give it a try before saying no. If you’re not technically inclined, don’t laugh quite yet—you might like reason number two. The second reason to try pro- gramming your own code is that if you want it programmed right, you have to do it yourself. Sound familiar? Well, it’s really true when it comes to computer software, so keep reading.
If the thought of this much programming is too much to bear, then a custom software solution is another option, and might be your best bet. A list of potential freelance programmers for Palm OS devices is available at www.palm.com and other locations on the World Wide Web.
Programmable calculators meet the needs of specialized users by allow- ing them to further customize the software. There are two approaches to programmable calculators found in handheld calculator software. The first, sometimes called “plug-in” software, is difficult to implement but overall more powerful than other options. In this case, the user writes a software program using a desktop editor (such as Syncalc’s Software Development Kit for Palm OS or Napier’s VB Script editor for Pocket PC). The user may need to compile the program, and then installs the program onto a hand- held device. The programmable calculator interface is used to access the software. Although the programmable calculator software itself provides a graphical user interface (GUI) and some programming logic, a good deal of technical expertise is needed. The time investment can be significant, too.
The upside is that you can create complicated calculator functions without the hassles of programming a stand-alone application from scratch.
16
Programmable Calculators
Peter L. Reynolds and Mark H. Ebell
378
The “plug-in” approach is beyond the scope of this chapter and the inter- est of most everyday PDA users, but keep reading! The second approach is almost as powerful in most situations and much, much easier to master.
Anyone who has used macro functions with word processing and other office software will pick it up right away. Just as with macros, these pro- grammable calculators process a sequence of calculator functions provided by the user.
Programmable Calculators for the Palm and Pocket PC OS
SynCalc, one type of programmable calculator that accepts plug-in soft- ware, is found at http://www.installigent.com/software.php?productline_
id=8. It runs on the Palm OS. There are also several Pocket PC program- mable calculators. Napier is a very full-featured product and is the equiva- lent of SynCalc for the Palm in most ways (http://www.nca-corp.com/
Napier/Napier%20Start.htm); best of all, it only costs $25 at this writing.
A simpler program that is focused more on the health professions is MedFormulas, which is freeware (http://www.liesens.com/medformulas).
In the rest of the chapter, we’ll go through a step-by-step example using SynCalc. It’s really just a matter of translating the algorithm you wish to accomplish into a list of sequential calculator functions. Once you write a successful program, you can use it repeatedly. You can also modify it as needed over time. To download and use SynCalc, go to their site at http://www.installigent.com/software.php?productline_id=8 ($19.95). The general lessons that you learn in this chapter will also apply to Napier and MedFormulas for the Pocket PC.
Using SynCalc
As an example, let’s imagine you’re a physician or nurse practitioner seeing sick children in the wintertime. Many of the medications you prescribe are dosed based on the patient weight. To make you more efficient and less prone to errors, we’ll write a program called “URI Meds” that calculates the dosage of several commonly used medications for upper respiratory infection. It’s easier to begin programming if you start with some kind of template. We’ll base our first program on an existing shortcut and write the code to calculate the dosage of Tylenol only. Then, once we’ve learned the basic syntax, we’ll expand it to other medications.
Here’s what this program will look like on your PDA when finished. Select
“shortcuts” from the bottom right of the SynCalc Display (Figure 16.1).
Many of the shortcuts listed in Figure 16.2 can be downloaded free of
charge from the SynCalc website. As a medical professional, be sure to
check all programs for accuracy before using them in practice. Select the
URI Meds program, enter the patient’s weight (Figure 16.3), and the correct dosage of medications is calculated (Figure 16.4).
IMPORTANT: Leave the “Auto-activate console?” checkbox checked as seen in Figure 16.4. Otherwise, the Shortcuts will not properly display results. If you accidentally uncheck this checkbox, you can display the Console again via the SynCalc main menu discussed later.
Here’s the exact code as you’d enter it into the SynCalc editor:
C(in(Weight in pounds:)/2.2)@w=(w*15/160)@t=out(\n\nPatient weight
%f kgs,w)= out(\nTylenol: %f tsp Q4,t)=out(\nIbuprofen: %f tsp Q6,t)
=out(\nPen-Vee K: %f mg QID,(w*50/4))=out(\nAmoxicillin: %f mg TID, (w*40/3))=out(\nAugmentin: %f mg BID, (w*45/2))=
out(\nAzithromycin: %f mg/dose, (w*5))=
Figure 16.1. SynCalc for Palm OS. (Reprinted with permis- sion from Installigent Team, a Zero-sixty Corp.)
Figure 16.2. SynCalc pop-up with medical shortcuts.
(Reprinted with permission from Installigent Team, a Zero-sixty Corp.)
Figure 16.3. “URI Meds”
Shortcut: Enter patient’s weight. (Reprinted with per- mission from Installigent Team, a Zero-sixty Corp.)
Figure 16.4. “URI Meds”
Shortcut: Calculated dosage for common upper respiratory infection (URI) medications.
(Reprinted with permission from Installigent Team, a Zero-sixty Corp.)
We’ll go through the code line by line, but first let’s get oriented to the SynCalc program editor. If you haven’t already downloaded and installed SynCalc, you’ll have to do so now. Choose SynCalc from the Application launcher and select the Menu function from the main SynCalc screen. Tap on “Edit Shortcuts” in the “Calc” menu (Figure 16.5). You should see a list of currently installed Shortcuts (Figure 16.6).
Some versions of SynCalc include a shortcut for temperature conversion from Celsius (C) to Fahrenheit (F). This short bit of code can serve as a template. If you see this choice, select it and tap “Edit” at the bottom of the screen. If not, you can tap “New” and copy the code from Figure 16.7 onto your screen. You can also skip using a template and begin entering your own code immediately.
The Edit Shortcut screen is divided into two parts. The name of each shortcut is entered in the top half entitled “Name.” The algorithm used to calculate your solution is coded in the bottom half, entitled “Value.”
IMPORTANT: If you edit the Name or Value of a shortcut, the original shortcut is completely overwritten and the starting information is lost.
Figure 16.5. SynCalc program menu. (Reprinted with permis- sion from Installigent Team, a Zero-sixty Corp.)
Figure 16.6. “Edit Shortcuts”
menu option. (Reprinted with permission from Installigent Team, a Zero-sixty Corp.)
Figure 16.7. Shortcut code for temperature conversion. (Reprinted with permission from Installigent Team, a Zero-sixty Corp.)
Another option is to copy code from an existing shortcut to a new short- cut using the Copy and Paste functions from the Edit menu (Figure 16.8).
Try it!
We’ll use this example to discuss the basic syntax of a SynCalc “plug-in”
program. The temperature conversion code is as follows:
You will notice that the code is very compressed. The Palm OS environ- ment is shorter on space than the typical office computer platform. The screen is small as well as the memory. In this case, formatting conventions that take up more space have been left out. The more compact code is hard for people to read, but the handheld computer can understand it just fine.
Extra spaces and carriage return characters can lead to error messages, so be careful!
The principal commands used to write SynCalc shortcuts are summarized in Table 16.1. Note that the “C” command only clears the expression window on the main SynCalc calculator screen but not the Console pop-up window, where results are displayed by the “out” command.
C in Celsius Temp:
Celsius is %n Fahrenheit.,c, Ans
( )
( ) * ( ) +
= ( ) =
@
\ %
c out n n
9 5 32
Figure 16.8. Editing with “Copy” and “Paste.”
(Reprinted with permission from Installigent Team, a Zero-sixty Corp.)
Table 16.1. SynCalc command summary.
Command syntax Explanation
C At the beginning of a shortcut, tells computer to clear expression window
in(Prompt Message Here) Request a value from the user via a pop-up window
@ Tells computer to assign the current value to a memory location
= Tells computer to process all calculations up to that point in the program
out(Response Message Here) Outputs text and values to the user via the SynCalc Console
User-defined memory locations are identified with a single letter “a” to
“z”. There is an additional memory location used by SynCalc called “Ans”.
This system variable is extremely important as it contains the current numerical value being manipulated. It’s the equivalent of the number you see on the display line of a pocket calculator or the expression window of the SynCalc main screen. Each time a new value is written to the location
“Ans”, the old value is lost. When a calculation is executed using the “ =”
command, the result is always stored in the “Ans” location.
You can output text as well as calculated numerical value. Although for- matting is limited, values can be displayed in several different formats as listed in Table 16.2. “\n” and “\t” are special character strings indicating new line and tab, respectively.
The first command in our template code, “in(Celsius Temp:)”, prompts the user to enter a value in response to the message “Celsius Temp:”. The entered value is stored in “Ans”.The next command,“@c”, assigns this value to the memory location “c”. Finally, the conversion calculation is completed and assigned to “Ans” by the “ =” command. The original value of “Ans”, the temperature in Celsius entered by the user, would have been lost except that it was also stored in the memory location “c” as a user-defined variable.
The results are output to the user by the command “out(\n%n Celsius is %n Fahrenheit.,c,Ans)”, and the program is ended with a final “ =”.
The formatting commands used in the output command are confusing but not complicated. The message to be displayed is followed by a comma- separated list of values stored in memory that will be included in the message. A place is reserved in the message for each of these variables using the syntax in Table 16.2. “\n” tells the program to move to a new line. The first “%n” reserves a place in the text message for the first of two comma- separated variables, “c”, and the second “%n” reserves a place for the second variable listed, “Ans”.
Now that we have an understanding of what the code means, let’s edit the program to meet our needs. The original code, once again, is as follows:
We’ll prompt the user to enter a weight rather than a temperature. It’s a good idea to always specify the anticipated unit of measure in your prompt;
C in Celsius Temp c out n n Celsius
(
( ) ) * ( ) + =
( is %n Fahrenheit.,c, Ans) = : @
\ %
9 5 32
Table 16.2. Formatting commands for SynCalc.
Syntax Output format Example
%n Normal number “23”
%f Fixed “23.00”
%s Scientific “2.3000000e1”
%h Hexadecimal “00000049”
this will minimize errors later. As in the final code, I’ll immediately change the weight in pounds to weight in kilograms by dividing by 2.2, and I’ll store the resulting value in memory location “w”.
This code asks for weight and then returns a temperature, so we’re not finished yet. The pediatric dosage of Tylenol is 15 mg/kg/dose, and liquid Tylenol contains 160 mg per teaspoon. Therefore, the dose calculation is “w*15/160”. Note that as in most computer programming, multiplication is indicated with the “*”. If we change the output text to match the new calculations, our code looks like the following:
Try the program out and see how it works. If you enter a weight of 30 pounds, the output should look like that shown in Figure 16.9.
Because no one measures teaspoon medication dosages to the seventh decimal place, a different number format makes sense. Try changing the
“%n” in the “out” command to “%f”.
Now the output makes more sense, as shown in Figure 16.10.
Your program may not run the first time because of errors. Don’t forget to change the variable name in the “out” command from “c” to “w”. A common source of errors is the use of parentheses. Parentheses tell SynCalc which calculation to complete first following the standard order of mathe- matical operations. Consider the following examples:
C in Weight w
out (
( ) ) * =
( in pounds:
nA %f kg patient receives %f teaspoon of Tylenol per dose., w, Ans) =
2 2 . @ 15 160
\
C in Weight w
out (
( ) ) * =
( in pounds:
nA %n kg patient receives %n teaspoon of Tylenol per dose., w, Ans) =
2 2 . @ 15 160
\
C in Weight w
out n n Celsius (
( ) ) * ( ) + =
( in pounds:
is %n Fahrenheit.,c, Ans) = 2 2 . @ 9 5 32
\ %
Figure 16.9. Drug calculation with “normal” formatting.
(Reprinted with permission from Installigent Team, a Zero-sixty Corp.)
Figure 16.10. Drug calcula- tion with “fixed” formatting.
(Reprinted with permission from Installigent Team, a Zero-sixty Corp.)
Computer programs usually process multiplication and division operations before addition and subtraction. In the first line, the parentheses indicate that 2 and 4 are to be added together before dividing by 2. In the second line, the parentheses are left out, so 4 is divided by 2 and then added to 2.
The subject of nested operations gives many nonmathematical, nonpro- grammer types great heartburn. It suffices to say that an order of opera- tions error is often to blame when your calculation produces an incorrect result. Always, double-check your program several times before depending on it in clinical situations.
As it turns out, the pediatric dosage of ibuprofen is the same as Tylenol.
In the final “URI Meds” example, we store this value in the memory location “t” and use it for both.
After each step in the algorithm, the “ =” command tells SynCalc to com- plete the code up to that point. We’ve taken an existing SynCalc plug-in and modified it to calculate a new value. Once you become familiar with SynCalc plug-in programming, you’ll be able to start from scratch. Just develop your algorithm, break it down into simple but very specific steps, and then translate these steps into language that SynCalc can understand.
Table 16.3 shows one way you might proceed.
You can include calculations directly within the comma-separated list of values in an “out” command response message. For example, the command
both calculates the three-times-per-day dosage of Amoxicillin based on weight in kilograms and displays the result. The drug dosages for Pen-Vee K, Amoxicillin, Augmentin, and azithromycin are calculated and displayed this way. Other medications can be added to the list by adding an additional
“out” command following the same pattern. The final “URI Meds” program then looks like this:
C in Weight w
w t out n nPatient kgs w
out nTylenol Q t
out nIbuprofen Q t
out nPen QID w
out nAmoxicillin w
out nAugmentin w
(
( ) ) =
( * ) = ( ) =
( ) =
( ) =
( ( * )) =
( * ) =
( ( * ))
in pounds:
weight %f %f tsp
%f tsp - Vee K: % f mg
% f mg TID, % f mg BID,
2 2 15 160
4 6
50 4 40 3 50 4 . @
@ \ \ ,
\ : ,
\ : ,
\ ,
\ :
\ : ==
( ( * )) =
out nAzithromycin \ : % f mg dose , w 5 out \nAmoxicillin: %f mg TID, w 40 ( ( * 3 ) ) =
C in Weight w
w t out n nPatient kgs w
out nTylenol Q t out nIbuprofen Q t (
( ) ) =
( * ) = ( ) =
( ) = ( )
in pounds:
weight %f
%f tsp %f tsp
2 2 15 160
4 6
. @
@ \ \ ,
\ : , \ : ,
2 4 2 3 2 4 2 4 ( + ) =
+ =
That’s it! You’ve completed the “URI Meds” plug-in. Be sure to test the program and double-check the values it outputs against a trusted source.
Once you’re sure you’ve got it right, it’s ready for clinical practice. To learn more, download several Shortcuts from the SynCalc web page and read over the code. You’ll be amazed how quickly you pick up new program- ming techniques.
Summary
Programming your handheld computer to perform complex, repetitive tasks is actually not as hard as most people think. Although preconfigured calculators are available for common tasks like calculating the body mass index and creatinine clearance, physicians who perform highly specialized calculations may find it best to ‘roll their own’ with the handy tools discussed in this chapter.
Table 16.3. Steps in a SynCalc program.
Algorithm Simple steps SynCalc code
1. Input weight in in(Weight in pounds:) in(Weight in
pounds Æ Ans pounds:)/2.2)@w=
2. Change weight into Ans/2.2 Æ Ans kilograms and save Ans@w value
3. Calculate dosage of w*15/160 Æ Ans (w*15/160)@t= medications for Ans@t
Tylenol and ibuprofen
4. Output weight in out(\n\nPatient weight %f out(\n\nPatient weight %f
kilograms kgs,w) kgs,w)=
5. Output dosage of out(\n Tylenol: %f tsp Q4,t) out(\n Tylenol: %f tsp Q4,t)= Tylenol
6. Output dosage of out(\nIbuprofen: %f tsp Q6,t) out(\nIbuprofen: %f tsp Q6,t)= ibuprofen