78 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
			
		
		
	
	
			78 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
| package.loaded["draw"] = nil
 | |
| 
 | |
| require("draw")
 | |
| 
 | |
| CamShader = nil
 | |
| 
 | |
| BotState = {
 | |
| 	lastMessage = 0,
 | |
| 	cpuBattery = nil,
 | |
| 	cpuBatteryCorrected = nil,
 | |
| 	servoBattery = nil,
 | |
| 	servoBatteryCorrected = nil,
 | |
| 	camfeed = nil,
 | |
| }
 | |
| 
 | |
| Ping = {
 | |
| 	timeSent = 0,
 | |
| 	latency = "unknown",
 | |
| 	payload = nil,
 | |
| }
 | |
| 
 | |
| function love.update2()
 | |
| 	local now = love.timer.getTime()
 | |
| 	if now - Ping.timeSent > 5 then
 | |
| 		Ping.payload = ""
 | |
| 		for i = 0, 10 do
 | |
| 			Ping.payload = Ping.payload .. string.char(love.math.random(65, 91))
 | |
| 		end
 | |
| 		Ping.timeSent = now
 | |
| 		love.mqtt.send("command/ping", Ping.payload)
 | |
| 		print("Sending ping")
 | |
| 	end
 | |
| end
 | |
| 
 | |
| function formatSafe(format, value, ...)
 | |
| 	if value == nil then
 | |
| 		return "unknown"
 | |
| 	end
 | |
| 	return string.format(format, value, ...)
 | |
| end
 | |
| 
 | |
| function love.load()
 | |
| 	CamShader = love.graphics.newShader("client/camshader.glsl")
 | |
| 
 | |
| 	love.graphics.setFont(love.graphics.newFont(20))
 | |
| 	love.window.setFullscreen(true)
 | |
| 	love.mqtt.subscribe("telemetry/#")
 | |
| end
 | |
| 
 | |
| function love.mqtt.message(topic, payload)
 | |
| 	local oldTime = BotState.lastMessage
 | |
| 	BotState.lastMessage = love.timer.getTime()
 | |
| 
 | |
| 	if topic == "telemetry/cpu_battery" then
 | |
| 		BotState.cpuBattery = tonumber(payload)
 | |
| 		BotState.cpuBatteryCorrected = BotState.cpuBattery / 2
 | |
| 	elseif topic == "telemetry/servo_battery" then
 | |
| 		BotState.servoBattery = tonumber(payload)
 | |
| 		BotState.servoBatteryCorrected = BotState.servoBattery / 2
 | |
| 	elseif topic == "telemetry/camfeed" then
 | |
| 		print("Got camfeed")
 | |
| 		fileData = love.filesystem.newFileData(payload, "camfeed")
 | |
| 		BotState.camfeed = love.graphics.newImage(fileData)
 | |
| 	elseif topic == "telemetry/pong" then
 | |
| 		if payload == Ping.payload then
 | |
| 			local timeReceived = love.timer.getTime()
 | |
| 			Ping.latency = math.floor((timeReceived - Ping.timeSent) * 1000) .. "ms"
 | |
| 		end
 | |
| 	else
 | |
| 		print("Got unknown telemetry at " .. topic)
 | |
| 		BotState.lastMessage = oldTime
 | |
| 	end
 | |
| end
 | |
| 
 | |
| function love.gamepadpressed(joystick, button)
 | |
| 	print("Pressed gamepad button " .. button .. " on joystick " .. joystick:getName())
 | |
| end
 |