nanoFORTH
v2.2
|
The emerging micro-controller communities are being built no more around specific hardware form factor, but rather around higher level languages. Without introducing an interactive shell like Javascript or microPython supported by ESP or Raspberry, the once popular Arduino platform will gradually lose out the market. However, no matter how the hardware environment evolved, on the edge of IoT universe, a minimalist system resembles the Aruino UNO will always have its value of existence provided that some of the form-factor might one-day shrunk down to micro or even nano-scale. Being chip agnostic, the Arduino IDE does serve as an excellent learning tool for future systems to come. Factor software development time into the mix, an interactive/interpreted language is not only cheaper but more fun than the good old static compiled C code in many cases. Check out NASA and contemplate why FORTH is still running on a number of space probes today.
Following the footsteps of Nakagawa and circuit4u@medium.com's TinyForth, a light-weight protothreaded FORTH with 3-character keywords for Arduino, I got an idea!
1 5 OUT ⏎
![]()
0 6 OUT ⏎
: red 1 5 OUT 0 6 OUT ; ⏎
> the symbol : starts the definition, and ; ends the function (or word) definition
: blu 0 5 OUT 1 6 OUT ; ⏎
blu ⏎
> a function is defines in the 'Compile Mode', and executed in 'Interpreter Mode'. The differece is at the leading ':' (colon) sign.
: xy FOR red 500 DLY blu 500 DLY NXT ; ⏎
10 xy ⏎
> so, 10 FOR ... NXT is to loop 10 times, (counting down from 10, 9, 8, ..., 2, 1)
FGT xy ⏎
> that erased xy from memory, we can redefine it now
> actually, multiple definition of the same function is allowed, the latest one takes precedence.
> also, FGT a word that is an interrupt service (see page3) might cause undefined behaviour: xy FOR red 200 DLY blu 300 DLY I . NXT ; ⏎
20 xy ⏎ ⇨ 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 ok
> so, you've probably noticed that I is the loop counter and . (dot) prints it
1 AIN ⏎
⇨ 258_ok> 258 is the value nanoFORTH read from photoresister, then place it on top of data stack > a photoresister or potentiometer returns value between 0 and 1023
DRP ⏎
⇨ ok> 258 is gone now
: lit 1 AIN 200 > ; ⏎
lit ⏎
⇨ 1_ok
: ?z IF red ELS blu THN ; ⏎
> ?z is our newly defined function. Unlike most of the other languages, you can create some really strange function names in FORTH.
1 ?z ⏎
0 ?z ⏎
lit ?z ⏎
: xyz BGN lit ?z 7 IN UTL ; ⏎
xyz ⏎> Try blocking the photoresister to see the LED toggles.
> Can this become a trigger i.e. mouse trap or something useful? Why not!
WRD ⏎
![]()
> See the latest, they include xyz, ?z, xy, lit, blu, red that we've just created.
Behold! This is nanoFORTH in its entirety. It's a short list of 'words' which should be rather easy to master. Note that the steps illustrated above has been the way Forth programmers building their applications. One small word at a time. Debug each well interactively then combine them into a "bigger" word. If a bug found, FGT the word, redefine it. Next word!
OK! If the process shown above has captured the essense, we should have an idea of what nanoFORTH is trying to do. Let's just stop and contemplate for a while. We did all of the above without any recompilation. Instead, we "talked" directly with the nanoFORTH uploaded only once via the USB cable. Should you code these in C, how do you go about doing it?
The interactive nature is different from the way we are so used to on Arduino platform. Just consider how many times you have to compile your C code to go through the functions shown above. So, move forward, let's envision how we can control robots or what we can do using Bluetooth with our Nano...
2. How - Ready to get nanoFORTH for a trial?
3. What - References and all the details...