Controller works quite well now
This commit is contained in:
parent
3eee21480b
commit
6d147ee5d0
@ -1,8 +1,41 @@
|
||||
local showUI = true
|
||||
|
||||
local function getTextY(line)
|
||||
return 15 + 25 * line
|
||||
end
|
||||
|
||||
local function addLineToTextBox(box, text, color)
|
||||
color = color or {1, 1, 1}
|
||||
box.lines = box.lines or {}
|
||||
box.lines[#box.lines + 1] = {text=text, color=color}
|
||||
|
||||
local font = love.graphics.getFont()
|
||||
local width = font:getWidth(text)
|
||||
local boxWidth = box.width or 0
|
||||
if width > boxWidth then
|
||||
box.width = width
|
||||
end
|
||||
end
|
||||
|
||||
local function drawTextBox(box, x, y, margin, padding)
|
||||
x = x or 0
|
||||
y = y or 0
|
||||
margin = margin or 10
|
||||
padding = padding or 3
|
||||
|
||||
local font = love.graphics.getFont()
|
||||
local lineHeight = font:getHeight() + padding
|
||||
|
||||
love.graphics.setColor(0, 0, 0, 0.5)
|
||||
love.graphics.rectangle("fill", x, y, box.width + margin * 2, #box.lines * lineHeight - padding + margin * 2)
|
||||
|
||||
love.graphics.setColor(1, 1, 1)
|
||||
for index, line in ipairs(box.lines) do
|
||||
love.graphics.setColor(line.color)
|
||||
love.graphics.print(line.text, x + margin, y + margin + (index - 1) * lineHeight)
|
||||
end
|
||||
end
|
||||
|
||||
function love.draw2()
|
||||
local width, height = love.graphics.getDimensions()
|
||||
|
||||
@ -31,32 +64,36 @@ function love.draw2()
|
||||
love.graphics.setShader(nil)
|
||||
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)
|
||||
local textbox = {}
|
||||
|
||||
-- Draw cpu battery
|
||||
if BotState.cpuBatteryCorrected == nil or BotState.cpuBatteryCorrected <= 3 then
|
||||
love.graphics.setColor(1, 0, 0)
|
||||
else
|
||||
if UIState.showUI then
|
||||
-- Draw time
|
||||
local time
|
||||
if BotState.lastMessage == 0 then
|
||||
time = "Never"
|
||||
else
|
||||
time = math.floor(love.timer.getTime() - BotState.lastMessage) .. "s ago"
|
||||
end
|
||||
addLineToTextBox(textbox, "Last message received: " .. time)
|
||||
|
||||
-- Draw cpu battery
|
||||
local color = {1, 1, 1}
|
||||
if BotState.cpuBatteryCorrected == nil or BotState.cpuBatteryCorrected <= 3 then
|
||||
color = {1, 0, 0}
|
||||
end
|
||||
addLineToTextBox(textbox, "CPU Batt: ".. formatSafe("%.02f (%.02f) V", BotState.cpuBattery, BotState.cpuBatteryCorrected), color)
|
||||
|
||||
-- Draw servo battery
|
||||
local color = {1, 1, 1}
|
||||
if BotState.servoBatteryCorrected == nil or BotState.servoBatteryCorrected <= 3 then
|
||||
color = {1, 0, 0}
|
||||
end
|
||||
addLineToTextBox(textbox, "Servo Batt: ".. formatSafe("%.02f (%.02f) V", BotState.servoBattery, BotState.servoBatteryCorrected), color)
|
||||
|
||||
-- Draw latency
|
||||
love.graphics.setColor(1, 1, 1)
|
||||
end
|
||||
love.graphics.print("CPU Batt: " .. formatSafe("%.02f (%.02f) V", BotState.cpuBattery, BotState.cpuBatteryCorrected), 5, getTextY(1))
|
||||
addLineToTextBox(textbox, "Latency: ".. Ping.latency)
|
||||
|
||||
-- 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)
|
||||
drawTextBox(textbox)
|
||||
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
|
||||
|
@ -4,6 +4,10 @@ require("draw")
|
||||
|
||||
CamShader = nil
|
||||
|
||||
UIState = {
|
||||
showUI = true,
|
||||
}
|
||||
|
||||
BotState = {
|
||||
lastMessage = 0,
|
||||
cpuBattery = nil,
|
||||
@ -71,6 +75,9 @@ function love.mqtt.message(topic, payload)
|
||||
end
|
||||
end
|
||||
|
||||
function love.gamepadpressed(joystick, button)
|
||||
function love.gamepadpressed2(joystick, button)
|
||||
print("Pressed gamepad button " .. button .. " on joystick " .. joystick:getName())
|
||||
if button == "back" then
|
||||
UIState.showUI = not UIState.showUI
|
||||
end
|
||||
end
|
||||
|
@ -1,19 +0,0 @@
|
||||
local socket = require("socket")
|
||||
|
||||
local conn = socket.connect("localhost", 1234)
|
||||
print("connected")
|
||||
|
||||
conn:settimeout(3)
|
||||
|
||||
local data, err, part = conn:receive(3)
|
||||
print("Data:", data)
|
||||
print("Err:", err)
|
||||
print("Part:", part)
|
||||
|
||||
|
||||
local data, err, part2 = conn:receive(3, part)
|
||||
print("Data:", data)
|
||||
print("Err:", err)
|
||||
print("Part:", part2)
|
||||
|
||||
conn:close()
|
@ -45,8 +45,8 @@ function love.draw(...)
|
||||
local centerY = love.graphics.getHeight() / 2
|
||||
|
||||
-- Calculate textX and textY
|
||||
--local textX = math.floor(centerX - (font:getWidth(text) / 2))
|
||||
--local textY = math.floor(centerY - (font:getHeight(text) / 2))
|
||||
local textX = math.floor(centerX - (font:getWidth(text) / 2))
|
||||
local textY = math.floor(centerY - (font:getHeight(text) / 2))
|
||||
local textX, textY = 10, 10
|
||||
|
||||
local realText
|
||||
@ -77,6 +77,15 @@ function love.update(...)
|
||||
end
|
||||
end
|
||||
|
||||
function love.gamepadpressed(joystick, button)
|
||||
if button == "guide" then
|
||||
love.event.quit()
|
||||
end
|
||||
if love.gamepadpressed2 then
|
||||
safeCall(love.gamepadpressed2, joystick, button)
|
||||
end
|
||||
end
|
||||
|
||||
function love.mqtt.onError(message)
|
||||
print("MQTT error: " .. message)
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user