sleep in Z88

Discussion about other targets
Post Reply
Jbizzel
Member
Posts: 14
Joined: Tue Jun 29, 2021 7:20 pm

sleep in Z88

Post by Jbizzel »

Hi,

I have this bit of code, I'm expecting it to sleep for 18 seconds, then print to screen and loop back etc. When I run the code the length of time the sleep happens reduces each time it loops. So the first time is 18 seconds, but towards the end the sleep is only 2-3 seconds. I can't understand why this would be the case, can anyone help?


Code: Select all


//test sleep
int main () {

for (int i = 11; i > 0; --i){
      sleep(18);
      printf("%d,",i-1);
      }

}
User avatar
dom
Well known member
Posts: 2076
Joined: Sun Jul 15, 2007 10:01 pm

Re: sleep in Z88

Post by dom »

That is weird, I can see the speeding up happen on OZVM (though I'm not convinced the first pause is for 18 seconds), does it happen on real hardware as well?

EDIT: The header defines the function as __z88dk_fastcall, but the code isn't so it was taking the loop variable as the sleep time.
Jbizzel
Member
Posts: 14
Joined: Tue Jun 29, 2021 7:20 pm

Re: sleep in Z88

Post by Jbizzel »

Hi Dom,

Yes it happens on real hardware too.

I was after a 3 minute timer. I've tried a different approach and this seems to work on OZvm OK. Maybe this is a better method?

Code: Select all


#include <stdio.h>
#include <time.h>

int main () {
   time_t seconds;
   time_t after15;
   int i;

printf("press a key");
   getchar();
   after15 = time(NULL);
   seconds = time(NULL);
   seconds = seconds + 18;
   

   printf("3 minute timer");

for( i = 1 ; i < 11 ; i++ ){
  while (after15 < seconds){
           after15 = time(NULL);
  }
   printf("#");
   seconds = seconds + 18;
}


   return(0);
}
User avatar
dom
Well known member
Posts: 2076
Joined: Sun Jul 15, 2007 10:01 pm

Re: sleep in Z88

Post by dom »

I've fixed the sleep() code now so it should sleep for the right amount of time.

However....it does use os_dly which has this return condition:

Code: Select all

        Fc=1
        BC - remaining time out
        A - error code
        RC.ESC(&01) - escape
        RC.SUSP(&69) - process suspended
        RC.TIME(&02) - timeout
So suspension/resuming or pressing the escape key (if detection isn't disabled) will terminate the sleep. So, you'll need to check the return value and loop until the value is 0.

In general on the z88 you should never busy loop (as your second snippet is doing) - it will drain the battery and also prevents application switching.
Jbizzel
Member
Posts: 14
Joined: Tue Jun 29, 2021 7:20 pm

Re: sleep in Z88

Post by Jbizzel »

Thanks Dom,

So sleep is the better way, as it is kinder to the battery? That's good to know.

I'll give it a whirl with sleep again.

Cheers

Jamie
Post Reply