Hello everyone,
I am looking for help on how to use a serial port as a means of translating sensor input into motion in my game. I have not created my final game as I am trying to figure out basic game mechanics first. I have a rotary encoder that is connected to an Arduino, that Arduino will then be connected to my Raspberry Pi which will have the game file. I am currently trying to make sure everything works on my computer (MSI, windows) before I begin to transfer everything to my Pi. Any tips on how to write this code would be greatly appreciated as I am working on this for a school project. It will be a little space game for kids at a local children's museum.
Thanks ahead of time!
Serial Port Communication With Love2D
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Serial Port Communication With Love2D
- Attachments
-
- serialib.lua
- (792 Bytes) Downloaded 65 times
-
- main.lua
- (217 Bytes) Downloaded 67 times
- BrotSagtMist
- Party member
- Posts: 657
- Joined: Fri Aug 06, 2021 10:30 pm
Re: Serial Port Communication With Love2D
For sensors i find it more proper to just use the linux "all hardware is a file" approach.
So on the system side you get drivers (or set them up yourself), until the sensor appears in /sys or /dev as a file which returns the value. On the Löve side you will end up with a thread that simply reads that file.
This is usually less messy when stuff is changed. I mean, you program expects a number, the driver returns a number. As opposed to "the game is hardwired to this exact device and wont work with other sensors until 50 config files are changed"
So on the system side you get drivers (or set them up yourself), until the sensor appears in /sys or /dev as a file which returns the value. On the Löve side you will end up with a thread that simply reads that file.
This is usually less messy when stuff is changed. I mean, you program expects a number, the driver returns a number. As opposed to "the game is hardwired to this exact device and wont work with other sensors until 50 config files are changed"
obey
Re: Serial Port Communication With Love2D
I'm still relatively new to programming in general, what exactly do you mean by the sensor appearing as a file? I tried something like coolterm if you know what that is. If not it reads the serial port wherever the Arduino is plugged into my computer. It would then write that information to a text file which my test game would read. My only problem was when you would load the game, it would read whatever was in that file before being loaded. If I tried turning the rotary sensor it would write to the text file but the game wouldn't see that update.
- BrotSagtMist
- Party member
- Posts: 657
- Joined: Fri Aug 06, 2021 10:30 pm
Re: Serial Port Communication With Love2D
Linux exposes hardware as simple readable files.
Say you want to write a hardware monitor that displays battery usage and cpu temp and whatnot.
Then you dont need much code for getting the values, you simply open /sys/class/power_supply/capacity and there you have it.
The actual code for my cpu monitor is:
Back to your game: Trying to include ardurino libs or sensor reading stuff inside the Löve file will bite you in the ass as later on, either when the pi breaks or when you want to run it on other devices. Keep it generic, then the you can plug in a ps3 controller anytime and simply point it to the new hardware and it just works (hopefully).
Note that it is also possible to open programs as files. See io.popen in the lua docs.
You can start _coolterm_ using io.popen and simply have it print the value. Then start a new thread with a simple reading loop on the file handle that popen returned:
See how to deal with threads in the wiki.
Say you want to write a hardware monitor that displays battery usage and cpu temp and whatnot.
Then you dont need much code for getting the values, you simply open /sys/class/power_supply/capacity and there you have it.
The actual code for my cpu monitor is:
Code: Select all
input = io.open("/proc/uptime", "r")
input:setvbuf("no")
--and for the reading loop:
input:seek("set") -- signals to read the file again.
val= input:read("*n")
val2 = input:read("*n")
--the differences in val compared to the last read return in fact the cpu usage.
See above you need setvbuf and seek for this trick.PackratSr wrote: ↑Thu Oct 27, 2022 6:04 pm It would then write that information to a text file which my test game would read. My only problem was when you would load the game, it would read whatever was in that file before being loaded. If I tried turning the rotary sensor it would write to the text file but the game wouldn't see that update.
Note that it is also possible to open programs as files. See io.popen in the lua docs.
You can start _coolterm_ using io.popen and simply have it print the value. Then start a new thread with a simple reading loop on the file handle that popen returned:
Code: Select all
thread = love.thread.newThread([[
prog=io.popen("coolterm and whatever arguments that need", "r")
repeat
love.thread.getChannel( 'sensor' ):push( prog:read(*) )
until false]])
obey
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 9 guests