Meine Lösung zur Aufgabe: LCD Numbers

Dieser code ist nur eine Übung und löst die Quiz-Frage zumindest teilweise. Keine Garantien. Lief ok mit gForth im Terminal des Mac OSX. Viel Vergnügen. Michael

\ LCD Numbers

\ Simulates an LCD display with 4 digits on terminal, no-scale.

[IFDEF] mycode mycode [ENDIF] marker mycode   \ Forget mycode and try again

vocabulary test   test definitions  decimal


\ As there is no way to find out cursor position at terminal,
\ I have to keep track of the displayed digits.

variable pos-x
variable pos-y

: pos! ( x y -- )
  pos-y !  pos-x !  ;

: pos@ ( -- x y )
  pos-x @ pos-y @ ;

: pos+ ( -- ) \ set position to next line in same row
  1 pos-y +!  pos@ at-xy ;

: .bl ( -- )
  pos+ ."     "
  pos+ ."     "
  pos+ ."     " ;

: .0 ( -- )
  pos+ ."  _  "
  pos+ ." | | "
  pos+ ." |_| " ;

: .1 ( -- )
  pos+ ."     "
  pos+ ."   | "
  pos+ ."   | " ;

: .2 ( -- )
  pos+ ."  _  "
  pos+ ."  _| "
  pos+ ." |_  " ;

: .3 ( -- )
  pos+ ."  _  "
  pos+ ."  _| "
  pos+ ."  _| " ;

: .4 ( -- )
  pos+ ."     "
  pos+ ." |_| "
  pos+ ."   | " ;

: .5 ( -- )
  pos+ ."  _  "
  pos+ ." |_  "
  pos+ ."  _| " ;

: .6 ( -- )
  pos+ ."  _  "
  pos+ ." |_  "
  pos+ ." |_| " ;

: .7 ( -- )
  pos+ ."  _  "
  pos+ ."   | "
  pos+ ."   | " ;

: .8 ( -- )
  pos+ ."  _  "
  pos+ ." |_| "
  pos+ ." |_| " ;

: .9 ( -- )
  pos+ ."  _  "
  pos+ ." |_| "
  pos+ ."   | " ;


\ "LCD" Display with 4 digits

: pos0 ( -- ) \ 0. position in display
  12 0 pos! ;
: pos1 ( -- ) \ 1. position in display
   8 0 pos! ;
: pos2 ( -- ) \ 2. position in display
   4 0 pos! ;
: pos3 ( -- ) \ 3. position in display
   0 0 pos! ;

: display-digit ( i -- )  \ i is a digit
  CASE 
  0 OF .0 ENDOF 
  1 OF .1 ENDOF 
  2 OF .2 ENDOF 
  3 OF .3 ENDOF 
  4 OF .4 ENDOF 
  5 OF .5 ENDOF 
  6 OF .6 ENDOF 
  7 OF .7 ENDOF 
  8 OF .8 ENDOF 
  9 OF .9 ENDOF 
       .bl
  ENDCASE ; 

: display ( i3 i2 i1 i0 -- ) \ display 4 digits
  pos0 display-digit
  pos1 display-digit
  pos2 display-digit
  pos3 display-digit ;
  
: n>i ( n - i3 i2 i1 i0 ) \ convert number to 4 digits, drop upper digits.
  >r
  r@ 1000 / 10 mod
  r@  100 / 10 mod
  r@   10 / 10 mod
  r@        10 mod
  r> drop ;

: .lcd  ( n -- ) \ display number to "LCD"
  page 0 0 pos! 
  n>i display
  cr cr ;