Minetest interface to the real world

Post Reply
User avatar
Gorilla
Member
Posts: 12
Joined: Wed Oct 16, 2013 01:39
Location: Melbourne, Victoria, Australia

Minetest interface to the real world

by Gorilla » Post

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.

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.

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...
Attachments

[The extension lua has been deactivated and can no longer be displayed.]


User avatar
kaeza
Moderator
Posts: 2162
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza
Location: Montevideo, Uruguay
Contact:

by kaeza » Post

Gorilla wrote:Ignore the post by kaeza as it's a red herring
huh?
Your signature is not the place for a blog post. Please keep it as concise as possible. Thank you!

Check out my stuff! | Donations greatly appreciated! PayPal

User avatar
Gorilla
Member
Posts: 12
Joined: Wed Oct 16, 2013 01:39
Location: Melbourne, Victoria, Australia

by Gorilla » Post

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.

User avatar
leetelate
Member
Posts: 205
Joined: Thu Aug 29, 2013 18:07
Location: 한인 타운, Huntsville,Alabama,USA

by leetelate » Post

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.
Last edited by leetelate on Tue Oct 29, 2013 17:02, edited 1 time in total.
MT IS MC'S SMARTER BROTHER
minetest 0.4.8 compiled from latest git on linux mint 15 with qjoypad and wired 360 controller
freeminer, pilztest, buildcraft and next are the idea factories
my minetest page is http://1337318.zymichost.com if zymic isn't down - meh, it is free...

User avatar
Gorilla
Member
Posts: 12
Joined: Wed Oct 16, 2013 01:39
Location: Melbourne, Victoria, Australia

by Gorilla » Post

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
Last edited by Gorilla on Thu Oct 31, 2013 04:23, edited 1 time in total.

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests