0 pay> = Command for destination 1 pay> = Destination node
2 pay> = Origin node 3 pay> = Address of sub node
4 pay> = Administration byte 5 to 31 pay> = Data 0x1A (#26) bytes
===== Time out on network commands =====
Since network commands can be lost, a time-out on the response to the transmitted command is necessary. This time-out can be constructed with a built-in timer, or alternatively by using a built-in cycle counter.\\
This code example is for the GD32VF103 Risc-V microcontroller:
hex
code TICK ( -- u ) \ Read half (low 32-bits) of 64-bit rdcycle counter
sp -) tos .mov
tos B00 zero csrrs \ Read low counter
next
end-code
decimal
: MS ( ms -- )
104000 * \ Convert MS to CPU ticks (CPU-clock/1000)
tick >r \ Save tick counter
begin
tick r@ - \ Calc. ticks passed
over u< 0= until \ Larger or equal to given ticks?
r> 2drop ; \ Yes ready
This structure is used in the word ''%%
hex 0 value NR create BUF 20 allot
: GET? ( -- 0|1B )
0 key? if \ Key pressed?
key dup 1B = if or exit then \ Yes, exit on escape char!
dup 0D = if \ Is it ENTER?
drop space buf nr evaluate \ Yes, interpret string
0 to nr cr \ New line
t? 0= if ." N>" then \ Display prompt when tracer is off
else
dup 8 = if \ Is it backspace?
emit bl emit 8 emit -1 \ Yes, remove char
else
dup emit buf nr + c! 1 \ Normal char, show & save
then
+to nr \ Adjust number of chars
then
then ;
: NODE ( -- )
startnode ( tron ) troff 0 to nr
begin begin handler? until get? until ;
This program is originally written in noForth. In noForth the serial input and output is vectored. Replacing the key vector does the same as the ''%%NODE%%''program.
: XKEY) ( -- c ) begin handler? until key) ;
The last line in the word ''%%STARTNODE%%'' contains this line, so after startnode is executed the interactive node is ready.
['] xkey) to 'key \ Add KEY & node handler to KEY
===== NODE command set =====
If you want to try out this mesh network implementation, these are the words to play with. Note that each node also contains a primitive event handler. This handler uses a pin as input for a switch to ground. This switch alternately activates the words ''%%ALL-ON%%'' or ''%%ALL-OFF%%''.
^Word ^Stack ^Description ^
|''%%.STATUS%%'' |( -- ) |Show most important RF data |
|''%%.ALL%%'' |( -- ) |Show network connection data |
|''%%REGISTER%%''|( -- ) |Connect myself to an existing network |
|''%%ON%%'' |( +n -- ) |Activate power output on node +n |
|''%%OFF%%'' |( +n -- ) |Deactivate power output on node +n |
|''%%ALL-ON%%'' |( -- ) |Activate all outputs on the network |
|''%%ALL-OFF%%'' |( -- ) |Deactivate all outputs on the network |
|''%%STOP%%'' |( +n -- ) |Halt any free running program on node +n|
|''%%>F%%'' |( +n ccc -- )|Execute the forth word ccc on node +n |
|''%%SCANX%%'' |( -- ) |Scan & note direct accessible nodes |
----
{{https://user-images.githubusercontent.com/11397265/157905223-70621a30-4d84-4d40-b706-f30acef52bed.jpg|scanx}}\\
**SCANX result**
===== Network tools =====
The mesh network code adds some additional words for constructing new functions above the basic node command interpreter. These are:
^Word ^Stack ^Function ^
|''%%ALL%%'' |( -- a ) |Address of a BIT-table with all found nodes |
|''%%DIRECT%%'' |( -- a ) |Address of a BIT-table with direct accessable nodes |
|''%%INDIRECT%%''|( -- a ) |Address of a BIT-table with indirect accessable nodes |
|''%%#MAP%%'' |( -- +n ) |Leave the size of a bitmap |
|''%%GET*%%'' |( u a -- 0|b ) |Check if node +n present in bit-array a? |
|''%%*CLR%%'' |( u a -- ) |Remove node +n from bit-array a |
|''%%*COUNT%%'' |( a -- +n ) |Leave number of nodes found in bitmap a |
|''%%>USER%%'' |( a -- ) |Copy nodes to node accu for app programmer |
|''%%>WORK%%'' |( a -- ) |Copy nodes to node accu for internal functions |
|''%%NEXT?%%'' |( a -- 0|u -1 )|Leave node number of the first used node in bitmap a & erase the bit |
|''%%RUN%%'' |( -- ) |Allow a program to run free |
|''%%HALT?%%'' |( -- f ) |Alternative KEY? to stop node programs |
|''%%
hex
: RUN-FORW ( -- ) \ Running light on all outputs in my network
run begin \ Reset stop flag
all >user \ Copy all nodes table to user table
begin user next? while \ Get next available node number
dup on 100 \ Activate output on that node shortly
off 30 repeat
halt? until ; \ Until a key is pressed or a STOP command was received
----
[[https://github.com/project-forth-works/project-forth-works/blob/main/Communication-Protocols/Wireless-Communication/Mesh-network/Generic-Forth/Tools/Build%20(3.9d).f|BUILD]], constructs a (hopping) mesh network
[[https://github.com/project-forth-works/project-forth-works/blob/main/Communication-Protocols/Wireless-Communication/Mesh-network/Generic-Forth/Tools/Ping-2.f|PING]], check node connection/availability
[[https://github.com/project-forth-works/project-forth-works/blob/main/Communication-Protocols/Wireless-Communication/Mesh-network/Generic-Forth/Tools/Mesh-demos.f|DEMO's]], that activate the node outputs in different ways
[[https://github.com/project-forth-works/project-forth-works/blob/main/Communication-Protocols/Wireless-Communication/Mesh-network/Generic-Forth/Tools/automesh.f|Simple demo]], primitive network build routine & running light demo
----