Post Reply 
Controlling program exit
11-13-2020, 05:03 PM
Post: #1
Controlling program exit
Hi all,
For a new program I created a REPEAT loop with an exit option in it, see the box below. The intention is to end the program when the backspace key is pressed. But in practice, I have to press this key twice to achieve this. And that is not the intention. I have also tried a WHILE loop, but it gives the same result. How can I improve this? I also wonder if the touch screen portion is properly processed. Any comments or improvements to this program are welcome. Sincerely, Karel.

Code:

....
LOCAL input,cx,cy;
REPEAT
 input:=WAIT(-1);  //Wait for input.
 CASE
  IF TYPE(input)==0 THEN  // Key is pressed.
     CASE
        IF input==-1 THEN  END;  // Capturing a minute timeout.
        ....
     END;
  END;
  IF TYPE(input)==6 THEN  // Touch screen pressed.
    IF input(1)==#0h THEN
       cx:=B->R(input(2));  // Convert x part.
       cy:=B->R(input(3));  // Convert y part.
    END;
  END;
 END;
UNTIL input==19;  // Backspace key pressed.
.....

I use HP-16C, WP-34S emulator, HP-35s, HP-48GX, HP-50g, and HP Prime G2.
Find all posts by this user
Quote this message in a reply
11-13-2020, 09:19 PM
Post: #2
RE: Controlling program exit
Hello,
did you try running the program
from the toolbox?
BR
Stefan
Find all posts by this user
Quote this message in a reply
11-13-2020, 10:16 PM
Post: #3
RE: Controlling program exit
(11-13-2020 09:19 PM)Stefan_Titan2944A Wrote:  Hello,
did you try running the program
from the toolbox?
BR
Stefan

Dear Stefan,
No, I ran the program from the program catalog. Sincerely, Karel.

I use HP-16C, WP-34S emulator, HP-35s, HP-48GX, HP-50g, and HP Prime G2.
Find all posts by this user
Quote this message in a reply
11-13-2020, 10:44 PM (This post was last modified: 11-13-2020 10:44 PM by Han.)
Post: #4
RE: Controlling program exit
I recommend using a "flag" variable to exit. For example,
Code:

local exit_flag:=0;

REPEAT
   // your code here; set exit_flag to 1 if desired key is pressed
UNTIL exit_flag; // (set exit_flag to non-zero to exit)
I have not tested your code, but it looks like you have at least one potential issue related to the test condition input==19. If input is a list, then what does input==19 return?

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
11-13-2020, 11:08 PM
Post: #5
RE: Controlling program exit
(11-13-2020 10:44 PM)Han Wrote:  I recommend using a "flag" variable to exit. For example,
Code:

local exit_flag:=0;

REPEAT
   // your code here; set exit_flag to 1 if desired key is pressed
UNTIL exit_flag; // (set exit_flag to non-zero to exit)
I have not tested your code, but it looks like you have at least one potential issue related to the test condition input==19. If input is a list, then what does input==19 return?

Dear Han,
It makes no difference, I have to press backspace twice to exit. Sincerely, Karel.

I use HP-16C, WP-34S emulator, HP-35s, HP-48GX, HP-50g, and HP Prime G2.
Find all posts by this user
Quote this message in a reply
11-14-2020, 10:00 AM
Post: #6
RE: Controlling program exit
(11-13-2020 10:16 PM)cahlucas Wrote:  
(11-13-2020 09:19 PM)Stefan_Titan2944A Wrote:  Hello,
did you try running the program
from the toolbox?
BR
Stefan

Dear Stefan,
No, I ran the program from the program catalog. Sincerely, Karel.

Hello Karel,
what I actually wanted to point out is
that a program start via the toolbox
or by entering the program name (ENTER)
in the input line will not demand the second
keystroke.
Best regards
Stefan
Find all posts by this user
Quote this message in a reply
11-14-2020, 10:58 AM
Post: #7
RE: Controlling program exit
(11-13-2020 10:16 PM)cahlucas Wrote:  
(11-13-2020 09:19 PM)Stefan_Titan2944A Wrote:  Hello,
did you try running the program
from the toolbox?
BR
Stefan

Dear Stefan,
No, I ran the program from the program catalog. Sincerely, Karel.

Hello Karel,
what I actually wanted to point out is
that a program start via the toolbox
or by entering the program name (ENTER)
in the input line will not demand the second
keystroke.
Best regards
Stefan
Find all posts by this user
Quote this message in a reply
11-14-2020, 04:31 PM
Post: #8
RE: Controlling program exit
I use these types of UI loops quite a bit and have come up with a couple of support routines I use in many different programs. The TESTUI example program is a demo of how these can be used.

Code:
#pragma mode( separator(.,;) integer(h32) )
// support routines
ResetTExit();
GetMK_in();
FlushKeysMouse();
ClearMouse();

// global variables
TExit:=2; // number of minutes before auto exit
TExit_count; // timeout tracker
MK_in,MK_mx,MK_my; // input variable and touch input x and y coordinates

EXPORT TESTUI()
BEGIN
  LOCAL my_str:="",exit_cond:=0;
  TExit_count:=0; // initialize auto exit counter
  REPEAT
    RECT_P(); // clear the screen
    TEXTOUT_P("TESTUI",G0,0,0); // some static text
    TEXTOUT_P(my_str,G0,0,50);
    DRAWMENU("","","","","Cancel","OK"); // basic menu
    GetMK_in(); // get input
    IF TYPE(MK_in)==0 THEN // keyboard input
      IF MK_in==-1 THEN // timeout after 60 seconds
        TExit_count:=TExit_count+1; // increment the exit counter
        my_str:="Exit counter is at " + STRING(TExit_count);
      ELSE
        ResetTExit(); // input was provided so clear the exit counter
        CASE
          IF MK_in==19 THEN // backspace
            MSGBOX("Exit condition met");
            exit_cond:=1;
          END;
          IF MK_in==30 THEN // ENTER
            my_str:="ENTER was pressed";
          END;
          IF MK_in==400 THEN // timed out
            MSGBOX("Timed out");
            exit_cond:=1;
          END;
          DEFAULT
          my_str:="Key " + STRING(MK_in) + " was pressed";
        END;
      END;
    ELSE // touch input
      CASE
        IF MK_my>219 THEN // menu
          CASE
            IF 265>MK_mx>212 THEN // Cancel
              MSGBOX("Exit condition met");
              exit_cond:=1;
            END;
            IF MK_mx>264 THEN // OK
              my_str:="OK menu button was touched";
            END;
            DEFAULT
            my_str:="Menu area left of cancel was touched";
          END;
        END;
        DEFAULT // above menu area
        my_str:="X: " + STRING(B→R(MK_mx)) + "  Y: " + STRING(B→R(MK_my));
      END;
    END;
  UNTIL exit_cond;        
END;

ResetTExit()
BEGIN
  IF TYPE(MK_in)==0 THEN
    IF MK_in<>400 THEN
      TExit_count:=0;
    END;
  END;
END;

GetMK_in()
BEGIN
  IF TExit_count==TExit THEN
    MK_in:=400;
  ELSE
    REPEAT
      IFERR MK_in:=WAIT(-1); THEN
        MK_in:=4;
        FlushKeysMouse();
      END;
    UNTIL TYPE(MK_in)==0 OR IFTE(SIZE(MK_in)>1,MK_in(1)==3,0);
    ClearMouse();
    IF TYPE(MK_in)==6 THEN // list
      MK_mx:=MK_in(2);
      MK_my:=MK_in(3);
      TExit_count:=0;
    END;
  END;
END;

// get all keys and mouse events out of the system
FlushKeysMouse()
BEGIN
  LOCAL j,ksum;
  REPEAT
    ksum:=0;
    FOR j FROM 0 TO 50 DO
      ksum:=ksum+ISKEYDOWN(j);
    END;
  UNTIL ksum==0;
  ClearMouse();
END;

ClearMouse()
BEGIN
  LOCAL m;
  REPEAT
    m:=MOUSE();
  UNTIL SIZE(m(1))==0;
END;
Visit this user's website Find all posts by this user
Quote this message in a reply
11-15-2020, 07:55 PM
Post: #9
RE: Controlling program exit
Dear Mr. Wall,
The local variable 'my_str' gives an error. I don't know why, I can't find a fault with it. Sincerely, Karel.

I use HP-16C, WP-34S emulator, HP-35s, HP-48GX, HP-50g, and HP Prime G2.
Find all posts by this user
Quote this message in a reply
11-15-2020, 10:08 PM
Post: #10
RE: Controlling program exit
That compiled OK for me on the Android version.
Perhaps try downloading the code again?

Stephen Lewkowicz (G1CMZ)
https://my.numworks.com/python/steveg1cmz
Visit this user's website Find all posts by this user
Quote this message in a reply
11-15-2020, 10:58 PM
Post: #11
RE: Controlling program exit
(11-15-2020 07:55 PM)cahlucas Wrote:  Dear Mr. Wall,
The local variable 'my_str' gives an error. I don't know why, I can't find a fault with it. Sincerely, Karel.

I copied the code directly from CK after testing it, which calculator and firmware are you using?

Attached also in case character conversions were a factor.


Attached File(s)
.hpprgm  TESTUI.hpprgm (Size: 2.79 KB / Downloads: 5)
Visit this user's website Find all posts by this user
Quote this message in a reply
11-17-2020, 05:31 PM
Post: #12
RE: Controlling program exit
(11-15-2020 10:58 PM)Jacob Wall Wrote:  I copied the code directly from CK after testing it, which calculator and firmware are you using?

Dear Mr. Wall,
As you can see at the bottom of my comment, I am using a G2, and the firmware is 2.1.14433 (2020-01-21). As far as I know, this is the last update, so it cannot be due to the hardware and software. I cannot try out the new version you sent, because the CK crashes while loading. Sincerely, Karel.

I use HP-16C, WP-34S emulator, HP-35s, HP-48GX, HP-50g, and HP Prime G2.
Find all posts by this user
Quote this message in a reply
11-17-2020, 09:08 PM
Post: #13
RE: Controlling program exit
I just drag & dropped the .hpprgm file over to the Programs folder of a connected G2 calculator and everything worked completely fine.

Are you on Windows or Mac?

Starting to sound like something else is at play here. My recommendation would be to:
- Create a backup of your calculator
- Format C (or factory reset) by
-- Hold simultaneously C F O keys on the keyboard
-- Insert paper clip in reset hole on the back
-- Keep holding C F O keys until DIAGNOSTIC screen pops up
-- 4. FLS Utility
-- 3. Format Disk C

If the problem goes away then something was corrupt with your calculator, if the same problem exists after reset then you can always restore your Backup and possibly look at what could be causing the problem on the CK side
Visit this user's website Find all posts by this user
Quote this message in a reply
11-17-2020, 10:15 PM
Post: #14
RE: Controlling program exit
If you place a DEBUG command right before the UNTIL command, you can then view the contents of all the variables right before the UNTIL command is executed. That may give you some insight into to state of the program. It is possible you may have even found a bug (can't say either way since I don't have my calculator with me).

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
11-18-2020, 05:55 PM
Post: #15
RE: Controlling program exit
(11-17-2020 09:08 PM)Jacob Wall Wrote:  I just drag & dropped the .hpprgm file over to the Programs folder of a connected G2 calculator and everything worked completely fine.

Are you on Windows or Mac?

Dear Mr. Wall,
My CK runs on Mac. Perhaps is that the problem, but I refuse to run Windows. Sincerely, Karel.

I use HP-16C, WP-34S emulator, HP-35s, HP-48GX, HP-50g, and HP Prime G2.
Find all posts by this user
Quote this message in a reply
11-18-2020, 09:41 PM
Post: #16
RE: Controlling program exit
When I ran your code (deleting the ... and changing -> to the right-arrow symbol), it works just fine. That is, it exited as soon as I hit the backspace key.

What happens when you change the detection to a different key? Does it exit properly? (This is to check if there is a hardware issue with the backspace key).

When you add DEBUG just before the UNTIL statement, what is the content of your 'input' variable?

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
11-20-2020, 01:59 AM
Post: #17
RE: Controlling program exit
(11-18-2020 09:41 PM)Han Wrote:  When I ran your code (deleting the ... and changing -> to the right-arrow symbol), it works just fine. That is, it exited as soon as I hit the backspace key.

What happens when you change the detection to a different key? Does it exit properly? (This is to check if there is a hardware issue with the backspace key).

When you add DEBUG just before the UNTIL statement, what is the content of your 'input' variable?
Dear Mr. Han,
First, the backspace key is not broken, the machine is less than six months old, and not very much used. Second, I took your advice to use an exit flag. And yes, the program is stopped as soon as the backspace key is pressed. But in my case, the display wasn't refreshed right away, and I didn't notice that at first. That's why I have placed a "STARTVIEW(-7,1);" command after the UNTIL statement afterwards, to refresh the display. This is not really ideal, I am still looking for something better. Maybe you can help me with that? Sincerely, Karel.

I use HP-16C, WP-34S emulator, HP-35s, HP-48GX, HP-50g, and HP Prime G2.
Find all posts by this user
Quote this message in a reply
11-21-2020, 04:53 PM
Post: #18
RE: Controlling program exit
(11-20-2020 01:59 AM)cahlucas Wrote:  Dear Mr. Han,
First, the backspace key is not broken, the machine is less than six months old, and not very much used. Second, I took your advice to use an exit flag. And yes, the program is stopped as soon as the backspace key is pressed. But in my case, the display wasn't refreshed right away, and I didn't notice that at first. That's why I have placed a "STARTVIEW(-7,1);" command after the UNTIL statement afterwards, to refresh the display. This is not really ideal, I am still looking for something better. Maybe you can help me with that? Sincerely, Karel.

This will generally be the case if your code exits the REPEAT loop and continues onward to any remaining code in your program. The screen will only update if you issue any commands to alter the screen -- be it to print a message, display an image, or simply to refresh to screen. On the other hand, if your code exits the program completely upon reach the UNTIL command, then the display should return to the Home/CAS view as well as the result of the very last command. In the case of the code snippet I used, I see a 1 returned, which is the result of the equality test within the UNTIL command. Could you share a little more of what your goals are for your code? It's hard to provide more feedback without knowing what you want your code to do.

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
11-21-2020, 11:54 PM (This post was last modified: 11-22-2020 12:01 AM by cahlucas.)
Post: #19
RE: Controlling program exit
(11-21-2020 04:53 PM)Han Wrote:  This will generally be the case if your code exits the REPEAT loop and continues onward to any remaining code in your program. The screen will only update if you issue any commands to alter the screen -- be it to print a message, display an image, or simply to refresh to screen. On the other hand, if your code exits the program completely upon reach the UNTIL command, then the display should return to the Home/CAS view as well as the result of the very last command. In the case of the code snippet I used, I see a 1 returned, which is the result of the equality test within the UNTIL command. Could you share a little more of what your goals are for your code? It's hard to provide more feedback without knowing what you want your code to do.
Dear Mr. Han,
The general idea is that when the program ends, you go back to where you originally came from. So if you started the program from the program catalog, you will also return there. This is what my program does when you close the program. however if you started the program from e.g. home, you will still end up in the program catalog. And actually that is not the intention. The 1 that comes back is actually unwanted. But as I already wanted to clarify in my previous comment, I don't have a solution for that yet. I hope this clarifies the problem definition. Maybe someone can help me with this? Sincerely, Karel.

I use HP-16C, WP-34S emulator, HP-35s, HP-48GX, HP-50g, and HP Prime G2.
Find all posts by this user
Quote this message in a reply
11-30-2020, 01:57 AM
Post: #20
RE: Controlling program exit
(11-21-2020 11:54 PM)cahlucas Wrote:  
(11-21-2020 04:53 PM)Han Wrote:  This will generally be the case if your code exits the REPEAT loop and continues onward to any remaining code in your program. The screen will only update if you issue any commands to alter the screen -- be it to print a message, display an image, or simply to refresh to screen. On the other hand, if your code exits the program completely upon reach the UNTIL command, then the display should return to the Home/CAS view as well as the result of the very last command. In the case of the code snippet I used, I see a 1 returned, which is the result of the equality test within the UNTIL command. Could you share a little more of what your goals are for your code? It's hard to provide more feedback without knowing what you want your code to do.
Dear Mr. Han,
The general idea is that when the program ends, you go back to where you originally came from. So if you started the program from the program catalog, you will also return there. This is what my program does when you close the program. however if you started the program from e.g. home, you will still end up in the program catalog. And actually that is not the intention. The 1 that comes back is actually unwanted. But as I already wanted to clarify in my previous comment, I don't have a solution for that yet. I hope this clarifies the problem definition. Maybe someone can help me with this? Sincerely, Karel.

Out of curiosity, are you using a STARVIEW command near the exit of your program? (I'm asking in reference to mentioning that even when running the program from Home, it returns to the program catalog.)

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)