An attempt at getting image data back
This commit is contained in:
43
controller-client/draw.lua
Normal file
43
controller-client/draw.lua
Normal file
@@ -0,0 +1,43 @@
|
||||
|
||||
local function getTextY(line)
|
||||
return 15 + 25 * line
|
||||
end
|
||||
|
||||
function love.draw2()
|
||||
-- Set background
|
||||
love.graphics.setBackgroundColor(0, 0, 0)
|
||||
love.graphics.setColor(1, 1, 1)
|
||||
|
||||
if BotState.camfeed then
|
||||
love.graphics.draw(BotState.camfeed)
|
||||
end
|
||||
|
||||
-- Draw time
|
||||
local time
|
||||
if BotState.lastMessage == 0 then
|
||||
time = "Never"
|
||||
else
|
||||
time = math.floor(love.timer.getTime() - BotState.lastMessage) .. "s ago"
|
||||
end
|
||||
love.graphics.print("Last message received: " .. time, 5, 5)
|
||||
|
||||
-- Draw cpu battery
|
||||
if BotState.cpuBatteryCorrected == nil or BotState.cpuBatteryCorrected <= 3 then
|
||||
love.graphics.setColor(1, 0, 0)
|
||||
else
|
||||
love.graphics.setColor(1, 1, 1)
|
||||
end
|
||||
love.graphics.print("CPU Batt: " .. formatSafe("%.02f (%.02f) V", BotState.cpuBattery, BotState.cpuBatteryCorrected), 5, getTextY(1))
|
||||
|
||||
-- Draw servo battery
|
||||
if BotState.servoBatteryCorrected == nil or BotState.servoBatteryCorrected <= 3 then
|
||||
love.graphics.setColor(1, 0, 0)
|
||||
else
|
||||
love.graphics.setColor(1, 1, 1)
|
||||
end
|
||||
love.graphics.print("Servo Batt: " .. formatSafe("%.02f (%.02f) V", BotState.servoBattery, BotState.servoBatteryCorrected), 5, getTextY(2))
|
||||
|
||||
-- Draw latency
|
||||
love.graphics.setColor(1, 1, 1)
|
||||
love.graphics.print("Latency: " .. Ping.latency, 5, getTextY(3))
|
||||
end
|
||||
@@ -1,45 +1,70 @@
|
||||
local lastMessage = 0
|
||||
package.loaded["draw"] = nil
|
||||
|
||||
local botState = {
|
||||
require("draw")
|
||||
|
||||
BotState = {
|
||||
lastMessage = 0,
|
||||
cpuBattery = nil,
|
||||
cpuBatteryCorrected = nil,
|
||||
servoBattery = nil,
|
||||
servoBatteryCorrected = nil,
|
||||
camfeed = nil,
|
||||
}
|
||||
|
||||
function love.draw2()
|
||||
love.graphics.setBackgroundColor(0, 0, 0)
|
||||
Ping = {
|
||||
timeSent = 0,
|
||||
latency = "unknown",
|
||||
payload = nil,
|
||||
}
|
||||
|
||||
local time
|
||||
if lastMessage == 0 then
|
||||
time = "Never"
|
||||
else
|
||||
time = math.floor(love.timer.getTime() - lastMessage) .. "s ago"
|
||||
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
|
||||
love.graphics.print("Last message received: " .. time, 5, 5)
|
||||
|
||||
love.graphics.print("CPU Batt: " .. formatSafe("%.02f V", botState.cpuBattery), 5, 30)
|
||||
love.graphics.print("Servo Batt: " .. formatSafe("%.02f V", botState.servoBattery), 5, 45)
|
||||
end
|
||||
|
||||
function formatSafe(format, value)
|
||||
function formatSafe(format, value, ...)
|
||||
if value == nil then
|
||||
return "unknown"
|
||||
end
|
||||
return string.format(format, value)
|
||||
return string.format(format, value, ...)
|
||||
end
|
||||
|
||||
function love.load()
|
||||
love.graphics.setFont(love.graphics.newFont(15))
|
||||
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)
|
||||
lastMessage = love.timer.getTime()
|
||||
BotState.cpuBattery = tonumber(payload)
|
||||
BotState.cpuBatteryCorrected = BotState.cpuBattery / 2
|
||||
elseif topic == "telemetry/servo_battery" then
|
||||
botState.servoBattery = tonumber(payload)
|
||||
lastMessage = love.timer.getTime()
|
||||
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user