Code: Select all
local serialib = {
message = "";
port = "";
mode = "";
}
function serialib.openPort(self,port,mode) -- Name of PORT and a mode "r" for reading and "w" for writing
local m = mode or "r"
local p = assert(io.open(port,m),"The port "..port.." could not be found.");
self.port = p;
self.mode = m;
end
function serialib.closePort() -- Closes the port
self.port:close();
end
function serialib.read(self,length) -- Read from the port, length specifies the length of the message
local l = length or 1
self.message = self.port:read(l);
self.port:flush();
end
function serialib.write(self,message) -- Writes to the port, message is the message being sent
local m = message or "Hello World!"
self.port:write(m.."\n");
self.port:flush();
end
function serialib.getMessage(self)-- If the port is in reading mode, this function returns the message it received from the port
if(self.mode == "r") then return self.message; else return "Not reading!" end
end
return serialib;Lua code
Code: Select all
function love.load()
s = require("Serialib");
s:openPort("/dev/cu.wchusbserial1410","r");
end
function love.update(dt)
s:read(5);
end
function love.draw()
love.graphics.print(s:getMessage());
end
function love.keypressed(key)
if(key == "escape") then
love.quit();
end
endCode: Select all
void setup() {
Serial.begin(9600);
}
String message = "1:1:2";
boolean tix = false;
void loop() {
delay(1000);
Serial.print(message);
if(tix){
message = "1:1:2";
tix = !tix;
}else{
message = "2:1:1";
tix = !tix;
}
}
Happy coding!