Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #35117

Re: Program or be Programmed !

From Paul E Bennett <Paul_E.Bennett@topmail.co.uk>
Newsgroups comp.lang.forth
Subject Re: Program or be Programmed !
Followup-To comp.lang.forth
Date 2014-12-23 01:05 +0000
Organization Hidecs Consultancy
Message-ID <cfrte2Frt26U1@mid.individual.net> (permalink)

Followups directed to: comp.lang.forth

Show all headers | View raw


Paul E Bennett wrote:

> WJ wrote:
>
>> Paul E Bennett wrote:
>> 
>>> I was displaying a sine-wave of
>>> asterix characters scrolling continuously down the screen. 3 lines of
>>> code generated the sine-wave (and cosine) numbers and the other 30 or so
>>> lines did scaling and arrangements for its display. I was showing the
>>> source code in a separate window.
>> 
>> Gauche Scheme:
>> 
>> (define line-length 79)
>> (define mid-point (div line-length 2))
>> (define one-line (string-append
>>   (make-string mid-point #\space) "|" (make-string mid-point #\space)))
>> (define (pause) (sys-nanosleep 5e7))
>> (let loop ((x 0.0))
>>   (when (< x 22)
>>     (let ((this-line (string-copy one-line))
>>           (p (round->exact (+ mid-point (* mid-point (sin x))))))
>>       (string-set! this-line p #\*)
>>       (print this-line)
>>       (pause))
>>     (loop (+ 0.1 x))))
>> 
>> Partial output:
>
> [%X]
> 
> Ok, now do that without using the sin x function (which I did not in my 
> example). I shall post the full source of my example shortly. I have just 
> had a quick look and there is a total of 65 lines defining 7 words
> including the comments and a more vertical style than most use. You will 
> see (eventually) that had I used a more horizontal style it would probably
> have been just seven lines.
> 
> The reason I started playing with the central sine-wave generation was for
> a small robotic project that required me to generate such a waveform. The 
> three line sine-wave generation code is all that I am using from this 
> example for the robotics project.

In the light of no answer from WJ I am going to leave the following code as 
a gift. The code has a few controls.

n TO OFFSET will shift the centre-line of the displayed waveform.
n TO AMPLITUDE will adjust the scaling
CYCLE will produce one waveform cycle
n CYCLES will perform n cycles.
CONTINUOUS will continuously produce the waveform until any key is pressed. 
It will always stop at the end of a completed cycle.

\ Sample Code

(( This code generates a sine-wave to the screen displayed as a star 
character 
eat a distance from th left hand edge proportional to the amplitude of that 
point of the sine wave. ))

Vocabulary TRACE ONLY Forth Also Trace trace definitions

: (DDA) ( S: sin\cos -- sin'\cos' )
( This code is the main sine wave generator )
  tuck over 
  256 / + -rot
  swap 256 / - swap
;

: DDA      \ "<spaces> name"
\ G: Create a DDA Instance with local storage for 2 Cells to 
\    accommodate the SINE and COSINE values at each step.
  CREATE 0 , 32768 ,  \ S: " spaces name" Sine-seed\Cosine-seed
  DOES> DUP 2@ (DDA) ROT 2!  \ S: --
;

: DDA-RESET   \ S: 'addr --
  32768 0 
  rot >Body 2! ;

DDA SINE   \ The child word SINE will perform the DDA actions when called.

\ **************************************************************
\ Screen Oscilloscope Functions
\ This lexicon is for a single channel Oscilloscope function that
\ takes a value and plots the wave-form to the screen. The plotted
\ character is an asterix.
\ **************************************************************

36 VALUE OFFSET
32 VALUE AMPLITUDE

: level ( S: n -- 'n )
( This code adjusts the scale of the signal to ensure it will be in )
( range for printing to screen )
  AMPLITUDE 32768 */ OFFSET + 0 MAX
;
  
: .scope ( S: n -- )
( This code is the portion that plots the text on the screen at the )
( appropriate distance from the left hand edge )
  cr level spaces ." *"
;

: chunk ( S: -- )
( This performs several blind cycles to reduce the height of screen )
( over which the waveform is displayed. )
  35 0
  do   SINE
  loop
  100 ms
;

: cycle ( S: -- )
\ Perform one full cycle
  46 0 
  do   chunk              \ 46 * 35 = 1610; the no. of steps in full cycle.
       ['] sine >body @ 
       .scope
  loop
  ['] SINE DDA-RESET
;

: cycles ( S: n -- )
( Displays the n cycles of the sine wave )
  0
  do   cycle
  loop
;

: continuous ( S: -- )
( Display the sine wave as a continous wave. Any key press will exit )
( the routine )
  begin cycle
        key?
  until
  key drop
;


-- 
********************************************************************
Paul E. Bennett IEng MIET.....<email://Paul_E.Bennett@topmail.co.uk>
Forth based HIDECS Consultancy.............<http://www.hidecs.co.uk>
Mob: +44 (0)7811-639972
Tel: +44 (0)1235-510979
Going Forth Safely ..... EBA. www.electric-boat-association.org.uk..
********************************************************************

Back to comp.lang.forth | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Re: Program or be Programmed ! Paul E Bennett <Paul_E.Bennett@topmail.co.uk> - 2014-12-23 01:05 +0000
  Re: Program or be Programmed ! Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-12-23 09:30 -0600
    Re: Program or be Programmed ! Paul E Bennett <Paul_E.Bennett@topmail.co.uk> - 2014-12-23 22:30 +0000
      Re: Program or be Programmed ! Paul Rubin <no.email@nospam.invalid> - 2014-12-24 00:13 -0800
        Re: Program or be Programmed ! Paul E Bennett <Paul_E.Bennett@topmail.co.uk> - 2014-12-24 14:33 +0000

csiph-web