Autres articles / Other articles

Entering a character string

published: 21 November 2020 / updated 23 November 2020

Lire cette page en français

 


The word STRINPUT

Even on a microcontroller, you may need to enter a character string from the terminal or another source.

Here, we use the pad buffer which goes through the corresponding tib to the input area of the FORTH interpreter.

When you use the terminal to communicate with FlashForth, the characters entered go through the buffer pointed to by tib

-input 
marker -input 
 
\ accept n characters from terminal FORTH buffer 
\ leave addr n  
: strinput  ( n --- addr n) 
    pad swap 
    accept 
    pad swap 
  ; 

The execution of strinput must be preceded by the maximum number of characters to enter:

Timed text entry

In principle, in an ARDUINO assembly, once the application has been finalized, there is no no longer supposed to communicate with the terminal. However, some device, a LoRa transmitter for example, connected to the serial port rx0 / tx0, will feed itself to the terminal.

If a signal or a string is transmitted over this serial link, the device can respond. For the case of a LoRa transmitter, a transmission by AT + SEND may be subject to a response that will be retrieved by strinput. But if the transmission is poorly received, you can wait a very long time, if ever, for a response on the serial port.

The idea is therefore to delay this entry attempt:

2000 value IN_DELAY \ increase or decrase if necessary 
: temp.rx0 
    IN_DELAY 
    begin 
        1- 1 ms 
        rx0?    \ char recevied from rx0 ? 
        if   
            drop 
            rx0  exit 
        then 
    dup 0= until 
    drop 13     \ if no char, leave $0a (cr) 
  ; 
 
\ temporised input 
: tempInput ( n --- addr n) 
    [']  temp.rx0 'key ! 
    strinput 
    ['] rx0 'key ! 
  ; 

The word temp.rx0 acts like the word rx0 except that the wait is limited in time by the content of the IN_DELAY value. You can modify the content of this value if necessary.

The word tempInput will act like the word strinput. If the waiting time is exceeded at the start or during entry, this entry string is interrupted.

In the scenario of a LoRa transmission for example:

Interpretation of a character string

FORTH is an interpreter AND a compiler !

FlashForth is no exception to the rule. It has both features in 12 KB of flash memory, excluding user application.

When you install FlashForth on an ARDUINO card, then you plug this card on the USB port of your computer, the interpreter is accessible from the terminal. Voir: Install and use the Tera Term terminal

The terminal goes through the USB interface which is connected to the USB port rx0 / tx0 and managed by FlashForth.

What does that mean?

This means that, to receive characters from the serial port rx0 / tx0, there is no nothing to do!

If you create a stand-alone application, i.e. disconnected from the terminal, any reception of characters from serial port rx0 / tx0 will be interpreted by FlashForth exactly as if this chain of catacères were transmitted by the terminal!

Ahhh .....

We can have several cases:

This is the solution that will be preferred when you can transmit data and commands in FORTH language.

A very practical example

We have an ARDUINO board connected to a loRa transmitter. This card must activate or deactivate a relay.

Without going into the details of a FORTH program, when you have the card connected to the terminal, you can activate or deactivate the relay with:

RELAY high 
RELAY low 

Once the assembly is operational, if you want to activate the relay remotely, the transmitter will only have to send the string RELAY high

The receiver application will receive this string in the receive buffer managed by strinput. To run the contents of the buffer, just run interpret after receiving the string. The word interpret just needs the address and number of characters in the string to parse.

Here is a test to do with the terminal:

: test ( ---) 
    s" words" ; 
test interpret      \ execute words 

Executing the word test drops the coordinates of the string on the stack characters containing words .

The word interpret will act exactly the same on a string transmitted via the serial port received in a serial buffer.