Serial Port Love2d-Arduino

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
AdrianN
Prole
Posts: 35
Joined: Wed Mar 28, 2018 5:13 pm
Location: Lima

Serial Port Love2d-Arduino

Post by AdrianN » Tue Dec 18, 2018 10:59 pm

Hi all, I'm currently playing with a bit of arduino, I'm trying to make a joystick for my video game, using a bluetooth module for communication, the problem is the libraries. I'm looking for a good library, I tried to implement luars232 but I don't understand how to work, and another libraries like serialib only work in linux (I work in windows 10, Love 11.2,Arduino UNO with the COM6 port)

Arduino code

Code: Select all

#include <SoftwareSerial.h>

#define px A0
#define py A1

float x;
float y;
String pos;
SoftwareSerial BT(10, 11);

void setup() {
  pinMode(px,INPUT);
  pinMode(py,INPUT);
  Serial.begin(9600);  
  BT.begin(9600); 
}

void loop() {
  x=analogRead(px);
  y=analogRead(py);

  Serial.print(x);
  Serial.print(" , ");
  Serial.println(y);

  pos=String(x)+","+String(y);
  BT.println(pos);
}
Love2d game example

Code: Select all

local cadena="a"--this get BT.println(pos) 

function love.load()

end

function love.update(dt)

end

function love.draw()
	love.graphics.print(cadena,100,100)
end
PD: I tried to use luars232 https://github.com/grytole/luars232 but doesn't work

Code: Select all

local rs232 = require "luars232"
function love.load()



local p, e = rs232.port('COM6',{
  baud         = '_9600';
  data_bits    = '_8';
  parity       = 'NONE';
  stop_bits    = '_1';
  flow_control = 'OFF';
  rts          = 'ON';
})

p:open()
print(p:write('AT\r\n'))
print(p:read(64, 5000))
p:close()
end
Error main.lua.6:attempt to call field 'port' (a nil value)

PD2: Error with 'luars232 expected, got userdata''

Code: Select all

local rs232 = require "luars232"


function love.load(  )

port_name = "COM6"

local out = io.stderr

-- open port
local e, p = rs232.open(port_name)
if e ~= rs232.RS232_ERR_NOERROR then
	-- handle error
	out:write(string.format("can't open serial port '%s', error: '%s'\n",
			port_name, rs232.error_tostring(e)))
	return
end

-- set port settings
assert(p:set_baud_rate(rs232.RS232_BAUD_115200) == rs232.RS232_ERR_NOERROR)
assert(p:set_data_bits(rs232.RS232_DATA_8) == rs232.RS232_ERR_NOERROR)
assert(p:set_parity(rs232.RS232_PARITY_NONE) == rs232.RS232_ERR_NOERROR)
assert(p:set_stop_bits(rs232.RS232_STOP_1) == rs232.RS232_ERR_NOERROR)
assert(p:set_flow_control(rs232.RS232_FLOW_OFF)  == rs232.RS232_ERR_NOERROR)

out:write(string.format("OK, port open with values '%s'\n", tostring(p)))

-- read with timeout
local read_len = 20 -- read one byte
local timeout = 100 -- in miliseconds
for i=1,10000,1 do
	local err, data_read, size = p:read(read_len, timeout)
	print(data_read)
end
assert(e == rs232.RS232_ERR_NOERROR)
			

-- close
assert(p:close() == rs232.RS232_ERR_NOERROR)

end

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests