RoboRally/nodemcu_firmware/motor.lua.2

48 lines
754 B
Groff

motor = {}
motor[1] = {}
motor[1][1] = 0
motor[1][2] = 1
motor[1][3] = 2
motor[2] = {}
motor[2][1] = 3
motor[2][2] = 4
motor[2][3] = 5
for i,m in ipairs(motor) do
for i,g in ipairs(m) do
gpio.mode(g, gpio.OUTPUT)
gpio.write(g, 1)
end
end
function motor_dir(m, dir)
if dir ~= 0 then
gpio.write(motor[m][3], 1)
gpio.write(motor[m][1], (1-dir)/2)
gpio.write(motor[m][2], (1+dir)/2)
else
gpio.write(motor[m][3], 0)
end
end
Peer = {}
function Peer:create(sck)
local peer = {}
setmetatable(peer, Peer)
peer.buffer = {}
return peer
end
function Peer:receive(sck, data)
self.buffer = self.buffer .. data
if #self.buffer >= 4 then
sck:close()
end
end
function(conn)
peer = Peer:create(conn)
conn:on("receive", peer:receive)
end