On 12/13/2014 05:03 AM, Bernd Paysan wrote:
--- snip ---

: (task1)  begin counter1 @ 1+ counter1 ! pause again ;
: (task2)  begin counter2 @ 1+ counter2 ! pause again ;

1 counter1 +!

would be more idiomatic.  And try to write a stack effect even for definitions
where it is ( -- ).


This is a fun little example for folks to learn with. Set cal to zero and execute calibration. This will show the average of 100 one second wait calls in microseconds. Subtract 1000000 from the displayed result and set cal accordingly. On my machine here I am getting within +- 50 usec for a 1000 wait. (not too shabby really and can be adjusted as the application grows.)

The 1 ms in front of pause in 'wait' releases the round robin execution of the tasker to the underlying OS so the infinite loops don't burn CPU cycles uselessly.

Keep in mind I am just playing with this to see what I can do and it is much less complicated than using pthread. It just depends on what you are trying to do and how sophisticated you think you need to be.

For my purposes I am going to be monitoring the status of an incoming signal on an RS-232 port status line and taking some action with that in the background while in the foreground interacting with the user in a terminal window. (Its related to amateur radio)

When I show people this code they are usually pretty amazed at how simple it "looks". No surprise.

Enjoy and Merry Christmas to All!

Jerry


[IFDEF] my-code
  my-code
[ENDIF]
marker my-code
require ./tasker.fs

0 value task1-id
0 value task2-id

variable counter1
variable counter2

: (task1) ( -- )
    begin 1 counter1 +! pause again ;
: (task2) ( -- )
    begin 1 counter2 +! pause again ;

: start-task1 ( -- )
    task1-id activate (task1) ;
: start-task2 ( -- )
    task2-id activate (task2) ;

: show ( -- )
    counter1 ? ."    " counter2 ? ;

: commands ( -- flag ) \ Execute commands, return a flag for quit.
    key
    case
      [char] 1 of task1-id sleep false endof
      [char] 2 of task2-id sleep false endof
      [char] 3 of task1-id wake false endof
      [char] 4 of task2-id wake false endof
      [char] q of true endof
      false swap
    endcase ;

630 value cal  \ Calibration of the wait delay set for my machine.

variable wait-end-time
: wait ( n -- )
    \ Wait about n millisecs.
    1000 * utime drop + wait-end-time !
    begin utime drop cal + wait-end-time
     @ <=
    while 1 ms pause repeat ; \ 1 ms before pause to give back to CPU.

: calibration ( -- )
    \ Displays the average of 100 one second wait calls in u-seconds.
    0 ( accumulator -- )
    100 0 do utime drop 1000 wait utime drop swap - + loop 100 / . ;

: run  ( -- )
    0 counter1 !
    0 counter2 !
    page 0 23 at-xy ." Press 'q' to quit."
    1000 newtask to task1-id
    1000 newtask to task2-id
    start-task1 task1-id sleep
    start-task2 task2-id sleep
    begin  10 10 at-xy show
           key? if commands else false then  ( flag -- )
           250 wait  \ wait about .25 seconds
    until
    task1-id kill task2-id kill
    page  ." Done" ;

\ run bye 

Reply via email to