Skip to content

Commit

Permalink
Added time zones, a world clock and improved time measurements
Browse files Browse the repository at this point in the history
  • Loading branch information
Jos-Ven committed Dec 5, 2023
1 parent c6e3518 commit 78a832d
Show file tree
Hide file tree
Showing 12 changed files with 593 additions and 169 deletions.
15 changes: 6 additions & 9 deletions src/app/esp32-extra/app.fth
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,24 @@ alias m-init noop
then #3 /
;

f# 0 fvalue us-start \ Must be updated after set-system-time

: system-time>f ( us seconds -- ) ( f: -- us )
s" s>d d>f f# 1000000 f* s>d d>f f+ " evaluate ; immediate

: usf@ ( f: -- us )
s" dup dup sp@ get-system-time! system-time>f" evaluate ; immediate

: ms@ ( -- ms )
f# .001 usf@ us-start f- f* f>d drop ;
: ms@ ( -- ms ) f# .001 usf@ f* f>d drop ;

alias get-msecs ms@

: ms ( ms -- )
s>d d>f f# 1000 f* usf@ f+
: fus ( f: us - )
usf@ f+
begin fdup usf@ f- f# 100000000 f>
while #100000000 us
repeat
usf@ f- f>d drop abs us
;
usf@ f- f>d drop abs us ;

: ms ( ms -- ) s>d d>f f# 1000 f* fus ;

fl wifi.fth

Expand All @@ -83,7 +81,6 @@ fl tools/extra.fth
: load-startup-file ( -- ior ) " start" ['] included catch ;

: app ( - ) \ Sometimes SPIFFS or a wifi connection causes an error. A reboot solves that.
usf@ to us-start
banner hex interrupt? 0=
if s" start" file-exist?
if load-startup-file
Expand Down
111 changes: 111 additions & 0 deletions src/app/esp32-extra/tests/test_time.fth
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
\ test_time.fth 05-12-2023

DECIMAL

: code-in-minutes ( $hrs count - coded-min ) single? #60 * 16bneg + ;
: add-coded-w ( n - ) s" #" tmp$ +lplace (.) tmp$ +lplace s" w, " tmp$ +lplace ;
: add-coded-c ( n - ) s" #" tmp$ +lplace (.) tmp$ +lplace s" c, " tmp$ +lplace ;

: List-weekdays
." 0:sunday 1:monday 2:tuesday 3:wednesday"
cr ." 4:thursday 5:fryday 6:saterday" ;

: .last-input-tz ( - ) \ As long as tmp$ is not changes
cr ." The new summertime would last for: " cr
.tz-header cr
tmp$ lcount s" create " nip /string bl NextString evaluate
2023 .summer-time
cr cr ." If OK then: Paste the following code into timezones.f"
cr tmp$ lcount type cr ;

: Dst-input ( - )
." change at LOCAL [UUMM] : " 4 enter-input extract-time
60 * + 16bneg + nip add-coded-w \ 5) Add change time that day. In local time
List-weekdays cr 9 to-column
." Day number of the involved weekday : "
1 enter-input single? \ Ask #weekday
." Index of the involved weekday in it's month : "
4 enter-input single? 16bneg + add-coded-w \ 6) Add occurence in month
add-coded-c 35 to-column \ 7) Add #weekday
." In month : " 2 enter-input single? add-coded-c \ 8) Add month
." Number of days before the involved weekday : "
1 enter-input single? add-coded-c ; \ 9) Add subtract #weekdays

: init-timezone ( - coded-minutes )
tmp$
if tmp$ off
else 255 allocate drop to tmp$
then
cr s" create " tmp$ lplace \ 1) Add create
cr ." Max 34 pos for name : " 34 enter-input tmp$ +lplace \ 2) Add name
s" incr-tz " tmp$ +lplace 10 to-column \ Increment #tz during compiing
." Utc offset : " 3 enter-input code-in-minutes ;

: input-tz-rule ( - ) \ Input for extra timezones that observe DST
init-timezone add-coded-w \ 3) Add Utc offset
5 to-column ." Shift in minutes: " 3 enter-input
single? 16bneg + add-coded-w \ 4) Add shift
cr ." For Dst START, " Dst-input
cr ." For Dst END, " Dst-input
tmp$ lcount evaluate \ Test the code of the input
-1 #tz +! .last-input-tz ; \ Show the Forth code for timezones.f

: time-list-zoom-meeting ( sec mm hh dd mm yyyy-GMT -- )
cr utctics-from-time&date greenwich-mean-time
0 lmargin ! #60 rmargin ! #13 tabstops ! ??cr
." Local times for the zoom meeting at:"
fdup .time-from-utctics
fdup space .date-from-utctics ." UTC" cr
tz-Endlist #tz @ 0
do ?cr >link link@
2dup >body fdup convert-to-tz bold .time-from-utctics norm space
dup >name$ shorten-tz-name type
loop
2drop fdrop cr ;

\ Use : 0 0 13 9 12 2023 time-list-zoom-meeting


: world-clock ( - )
0 lmargin ! #86 rmargin ! #13 tabstops ! ??cr
greenwich-mean-time tz-Endlist
#tz @ 0
do ?cr >link link@ tuck >name$ shorten-tz-name type ." :"
2dup swap >body @time convert-to-tz
fdup .date-from-utctics
bold .time-from-utctics norm
swap 30 .tab
fdepth 0< abort" Floating Point Stack Underflow"
loop
2drop cr ;

: watch-world-clock ( - )
hide-cursor 0 7 at-xy world-clock show-cursor ;

\ Use: cls 18 set-precision f# 1 fsec>fus ' watch-world-clock execute-until-escape

0 [if]
Notes:
2023 .list-summer-times \ To list all timezones in timezones.fth
Europe/Amsterdam 2023 .summer-time \ Lists just 1

\ Conversions between time zones at the current local time:
local-time-now europe/amsterdam america/chicago convert-to-tz .time-from-utctics
local-time-now europe/amsterdam america/chicago convert-to-tz .Date-from-utctics

\ Conversions between time zones at a specified time:
0 3 13 28 8 2023 utctics-from-time&date greenwich-mean-time
europe/amsterdam convert-to-tz .time-from-utctics

2038 tests:
0 14 07 19 1 2037 UtcTics-from-Time&Date f.s fdup .Date-from-utctics .time-from-utctics
0 14 07 19 1 2038 UtcTics-from-Time&Date f.s fdup .Date-from-utctics .time-from-utctics
0 14 07 19 1 2100 UtcTics-from-Time&Date f.s fdup .Date-from-utctics .time-from-utctics

Under esp-idf v3.x.x the time of the esp32 is not handled right beyond 2038
From: https://github.com/espressif/esp-idf/issues/584
Subtract 883612800 seconds and dates and weekdays are the same again!
The 2038 problem is solved in v5

[then]

52 changes: 43 additions & 9 deletions src/app/esp32-extra/tools/extra.fth
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
marker -extra.fth cr lastacf .name #19 to-column .( 11-11-2023 ) \ By J.v.d.Ven
marker -extra.fth cr lastacf .name #19 to-column .( 25-11-2023 ) \ By J.v.d.Ven
\ Additional words I often use.

alias b bye
Expand Down Expand Up @@ -104,9 +104,6 @@ test-1second
: .elapsed ( - )
usf@ (time-start) 2@ us-elapsed fus>fsec fe. ." sec." ;

: us-to-deadline ( us-start us-later - us-to-later )
f+ usf@ f- f# 0 fmax ;

create-timer: tTotal
0 value stages-
f# 0 fvalue tcycle
Expand All @@ -132,8 +129,11 @@ f# 180e3 fvalue next-measurement

[then]

: bold ( -- ) .esc[ '1' (emit 'm' (emit ; \ VT100
: norm ( -- ) .esc[ '0' (emit 'm' (emit ; \ VT100
: bold ( -- ) .esc[ '1' (emit 'm' (emit ; \ VT100
: norm ( -- ) .esc[ '0' (emit 'm' (emit ;
: hide-cursor ( -- ) #out @ .esc[ s" ?25l" type #out ! ;
: show-cursor ( -- ) #out @ .esc[ s" ?25h" type #out ! ;

: lcount ( addr -- addr' count ) dup cell + swap @ ;
: +lplace ( addr len dest -- ) 2dup >r >r lcount + swap cmove r> r> +! ;
: lplace ( addr len dest -- ) 0 over ! +lplace ;
Expand Down Expand Up @@ -190,11 +190,46 @@ patch check-conditional here <resolve
does> ( adr -- adr' ) @ + ;

: field: ( n1 <"name"> -- n2 ) ( addr -- 'addr ) aligned cell +field ;
: bfield: ( n1 <"name"> -- n2 ) ( addr -- 'addr ) 1 +field ;
: wfield: ( n1 <"name"> -- n2 ) ( addr -- 'addr ) 2 +field ;
: xfield: ( n1 <"name"> -- n2 ) ( addr -- 'addr ) 8 +field ;

: f2dup ( fs: r1 r2 -- r1 r2 r1 r2 ) fover fover ;
: perform ( adr - ) s" @ execute " evaluate ; immediate

#27 constant escape
: escape? ( - flag )
key?
if key escape =
if true
else
begin key?
while key drop
repeat
false
then
else 0
then ;

: us-to-deadline ( f: us-base us-incr - us-wait )
f+ usf@ f- f# 0 fmax ; \ us-base + us-incr should be > then usf@

: find-deadline ( addr-fus-base addr-fus-timeout - ) ( f: - us-to-deadline )
over f@ f@ f2dup f+ f! us-to-deadline ;

: execute-until-escape ( xt -- ) ( f: #ms-timeout - )
0 0 { xt &#ms-timeout &usstrt -- }
/f allocate drop dup to &#ms-timeout f!
/f allocate drop dup usf@ fus>fsec fround fsec>fus to &usstrt f!
begin xt execute
&usstrt &#ms-timeout find-deadline
fus \ No drift in the software
\ fdrop 1000 ms \ Replace the previous line with this line and it drifts
\ usf@ &usstrt f@ f- fe. ." us deviation. " \ Show the drift
escape?
until
&#ms-timeout free drop &usstrt free drop ;

begin-structure /circular
field: >(cbuf-count)
field: >max-records
Expand Down Expand Up @@ -370,7 +405,7 @@ char , value seperator

: SendHtmlPage ( - )
HtmlPage$ lcount dup 0>
if lsock lwip-write drop 20 ms
if lsock lwip-write drop
else 2drop
then ;

Expand Down Expand Up @@ -492,14 +527,13 @@ char , value seperator

create TcpPort$ ," 8080" create UdpPort$ ," 8899"


: UdpWrite ( send$ cnt ip-server$ - )
count UdpPort$ count 2swap udp-connect
>r r@ lwip-write 50 ms r> lwip-close drop ;

: TcpWrite ( bufer cnt ip-server$ - )
>r #1000 TcpPort$ count r> count stream-connect >r
r@ lwip-write drop
r@ lwip-write drop 50 ms
r> lwip-close ;

\ After "WiFi station connection failed":
Expand Down
17 changes: 8 additions & 9 deletions src/app/esp32-extra/tools/schedule-tool.f
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
marker -schedule-tool.f s" cforth" ENVIRONMENT?
[IF] drop cr lastacf .name #19 to-column .( 11-11-2023 ) \ By J.v.d.Ven
[IF] drop cr lastacf .name #19 to-column .( 05-12-2023 ) \ By J.v.d.Ven
[THEN]

0 [if]
Expand Down Expand Up @@ -69,7 +69,6 @@
f# 0.0e0 fvalue boot-time


: .mmhh ( mmhh - ) s>d <# # # [char] : hold # # #> type ;
: next-scheduled-time ( - mmhh ) scheduled @ 1+ n>sched.time@ ;

: /schedule-file ( - #records )
Expand Down Expand Up @@ -244,19 +243,19 @@
60 60 * value seconds-before-sunset

: sleep-seconds-before-sunset ( SecondsBeforeSunset - )
UtcSunSet @time f- s>f f- fdup f0>
UtcSunSet local-time-now f- s>f f- fdup f0>
if cr .date .time ." Needed sleep " f>d drop #seconds-deep-sleeping
else fdrop cr .date .time ." No sleep needed."
false to WaitForSleeping-
then
;
then ;

: pass-this-second ( - ) usf@ f# 1e6 us-to-deadline f>d drop us ;
: ftime-till-next-second ( - )
usf@ fus>fsec fround fsec>fus [ f# 1 fsec>fus ] fliteral us-to-deadline ;

: scheduled-wakeup ( - #seconds )
cr .date .time ." sleep-schedule deep-sleep time:"
next-scheduled-time 2359 min #NsTill Nanoseconds f/ f>s
pass-this-second ;
next-scheduled-time 2359 min UtcTill f>s
ftime-till-next-second fus ;

: (sleeping-schedule) ( - )
next-scheduled-time 2359 min time>mmhh >
Expand Down Expand Up @@ -289,7 +288,7 @@

: (sleep-at-boot) ( - )
boot-time date-from-utc-time date>jjjjmmdd
@time date-from-utc-time date>jjjjmmdd = \ Only today
local-time-now date-from-utc-time date>jjjjmmdd = \ Only today
if 0 n>sched.option@
Sleep-till-sunset-option = \ Is sleep-at-boot the FIRST entry in the schedule?
if (sleep-till-sunset)
Expand Down
Loading

0 comments on commit 78a832d

Please sign in to comment.