Page 1 of 1

Countdown Timer

Posted: Wed Aug 23, 2017 4:21 am
by rbytes
While this countdown timer does work, it is not easy to read the display. I look forward to the time when font size (and font name) are available as SPL commands.
But it should be easy to tell when the countdown reaches 0. The screen turns red.

Code: Select all

''
  Countdown Timer by rbytes
  August 2017
  Set s to the desired number of seconds
''
x=-1                  'actually x can be set to any non-zero value
s=10                  'number of seconds to count down
t = #.textbox         'textbox "t" is created
t.text=""
t.edit=0
t.height=40
t.width=60
t.x=100
t.y=100
#.show(t)             'textbox "t" is shown on the screen
#.drawrect(95,95,150,140)  'draw a box around the textbox
>
y = #.int(#.time())   'number of elapsed seconds
? y!=x                'if second has changed
  s-=1                'subtract 1 from elapsed time
  '#.output(y)
  t.text = s          'update text of textbox "t" to value s (seconds remaining)
  ? s=0               'if timer has run out
    #.scrclear(1,0,0) 'turn screen red
    #.drawrect(95,95,150,140)  'draw the box again
    <<                'break
  .
  x=y                 'store the current second to compare y to it
.
<
Screenshot (10).png
Screenshot (10).png (31.85 KiB) Viewed 2373 times

Re: Countdown Timer

Posted: Wed Aug 23, 2017 5:54 am
by Mr. Kibernetik
Inspired by your timer, I also made a timer:

Code: Select all

maxt = 10 'in seconds
#.scroff()
>
  t = #.time()
  ? t>maxt, t = maxt
  #.scrclear()
  #.drawtext(10,10,gett(t))
  #.drawtext(10,40,gett(maxt-t))
  #.scr()
< t<maxt

gett(t)=
  hr = #.lower(t/3600)
  min = #.lower((t-hr*3600)/60)
  sec = t%60
  <= #.str(hr,"00:")+#.str(min,"00:")+#.str(sec,"00.00")
.
Снимок.PNG
Снимок.PNG (1.48 KiB) Viewed 2368 times

Re: Countdown Timer

Posted: Wed Aug 23, 2017 2:02 pm
by rbytes
Nice! I see there are much more efficient ways to code with SPL.

Is there a way to derive year, month and day from the #.time() function?

Re: Countdown Timer

Posted: Wed Aug 23, 2017 2:10 pm
by Mr. Kibernetik
rbytes wrote:
Wed Aug 23, 2017 2:02 pm
Is there a way to derive year, month and day from the #.time() function?
No, #.time is just a timer (note that there can be unlimited amount of named timers using #.time function).
There will be dedicated date/time functions in SPL.

Your wish list sorted by priority is very welcome!
Also I found rosettacode.org to be very useful to find interesting tasks to facilitate development of SPL function set.

Re: Countdown Timer

Posted: Wed Aug 23, 2017 4:46 pm
by Mr. Kibernetik
Date function #.today and time function #.now will appear in version 0.0.12.
This is the code syntax example:

Code: Select all

#.scroff()
>
  #.scrclear()

  h,m,s = #.now() 'time
  t = #.str(h,"00:")+#.str(m,"00:")+#.str(s,"00.000")
  #.drawtext(10,10,t)

  d,m,y = #.today() 'date
  t = #.str(d,"00'.'")+#.str(m,"00'.'")+#.str(y)
  #.drawtext(10,40,t)

  #.scr()
<
And the output:
Снимок.PNG
Снимок.PNG (1.87 KiB) Viewed 2354 times

Re: Countdown Timer

Posted: Wed Aug 23, 2017 5:22 pm
by rbytes
Valuable addition! Thanks!

Re: Countdown Timer

Posted: Sat Aug 26, 2017 1:50 pm
by rbytes
I updated my SPL version last night, and this code is now working well.