Page 1 of 1

Minetest interface to the real world

PostPosted: Tue Oct 29, 2013 12:23
by Gorilla
I having been beating my head against a brick wall trying to figure out how to interface a read world device (Arduino via the serial port) to Minetest.

https://forum.minetest.net/viewtopic.php?id=7491 (Ignore the post by kaeza as it's a red herring... appreciate the humour though :-D )

I have followed through the chattybricks tutorial that Jeija released on github as minetest-modding-tutorial so some of that code is remaining for debugging purposes.

Hate to say it but I'm not comprehending how to do this. Ignore the Arduino side as that it basically done. Send the string "LED1:1" and the light turns on, LED1:0 will turn it off.. easy.

As I believe that I can't use an endless loop in a minetest lua script, I figure that the inbound data could be read using a register_globalstep() function with minimal processing. The outbound serial data is sent on events such as a player hitting a block. Current code below, I'm not sure how much I of the TODO code loop I need to include.

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local r = p.poll(set, -1)
for fd, d in pairs(set) do
    if d.revents and d.revents.IN then
         if fd == 0 then
             local d, err = p.read(0, 1024)
             if not d then exit(err) end
             if d == string.char(3) then exit("Bye") end
             local ok, err = p.write(fds, d)
             if not ok then exit(err) end
         end
         if fd == fds then
             local d, err = p.read(fds, 1024)
             if not d then exit(err) end
             local ok, err = p.write(1, d)
             if not ok then exit(err) end
        end
    end
end



More than happy to add a page to document this into the wiki when I get this example complete. Might fire someone's imagination...

PostPosted: Tue Oct 29, 2013 12:26
by kaeza
Gorilla wrote:Ignore the post by kaeza as it's a red herring

huh?

PostPosted: Tue Oct 29, 2013 12:29
by Gorilla
kaeza wrote:
Gorilla wrote:Ignore the post by kaeza as it's a red herring

huh?


Sorry I meant in the other forum topic. Not here. Sorry about any confusion caused.

PostPosted: Tue Oct 29, 2013 17:00
by leetelate
lol

look at minetest_register_on_dignode and just send the string out the port whenever a node is dug - no need for any endless loops since the endless loop is the game engine itself - whenever a node (anywhere in the game) is dug, the event fires and your code is executed - easy-peasy - i used it in gespesterwald mod to handle all the things that are being dug wherever by whomever, update the hud, etc.

PostPosted: Thu Oct 31, 2013 03:09
by Gorilla
leetelate wrote:lol

look at minetest_register_on_dignode and just send the string out the port whenever a node is dug - no need for any endless loops since the endless loop is the game engine itself - whenever a node (anywhere in the game) is dug, the event fires and your code is executed - easy-peasy - i used it in gespesterwald mod to handle all the things that are being dug wherever by whomever, update the hud, etc.


Ahhh yes, it was rather straight forward.

As a hello world, I have used a register_on_dignode() function to send a command to the physical device.

Also used a minetest.register_globalstep() function to read the serial port buffer and display a test message.

Speaking of which the dev wiki documentation register_globalstep() has a bug... as was previously written, it will print "Minetest" every globalstep rather than five seconds as the incremented counter is never reset. Added the line timer = 0.

As mentioned above, I'll tidy up the code and put it somewhere more public... probably github.

Docs for minetest.register_globalstep(): http://dev.minetest.net/minetest.register_globalstep