Controller works quite well now

This commit is contained in:
Sebastiaan de Schaetzen 2024-07-20 20:18:01 +02:00
parent 3eee21480b
commit 6d147ee5d0
4 changed files with 80 additions and 46 deletions

View File

@ -1,8 +1,41 @@
local showUI = true
local function getTextY(line) local function getTextY(line)
return 15 + 25 * line return 15 + 25 * line
end 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() function love.draw2()
local width, height = love.graphics.getDimensions() local width, height = love.graphics.getDimensions()
@ -31,32 +64,36 @@ function love.draw2()
love.graphics.setShader(nil) love.graphics.setShader(nil)
end end
-- Draw time local textbox = {}
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 UIState.showUI then
if BotState.cpuBatteryCorrected == nil or BotState.cpuBatteryCorrected <= 3 then -- Draw time
love.graphics.setColor(1, 0, 0) local time
else 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) love.graphics.setColor(1, 1, 1)
end addLineToTextBox(textbox, "Latency: ".. Ping.latency)
love.graphics.print("CPU Batt: " .. formatSafe("%.02f (%.02f) V", BotState.cpuBattery, BotState.cpuBatteryCorrected), 5, getTextY(1))
-- Draw servo battery drawTextBox(textbox)
if BotState.servoBatteryCorrected == nil or BotState.servoBatteryCorrected <= 3 then
love.graphics.setColor(1, 0, 0)
else
love.graphics.setColor(1, 1, 1)
end 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 end

View File

@ -4,6 +4,10 @@ require("draw")
CamShader = nil CamShader = nil
UIState = {
showUI = true,
}
BotState = { BotState = {
lastMessage = 0, lastMessage = 0,
cpuBattery = nil, cpuBattery = nil,
@ -71,6 +75,9 @@ function love.mqtt.message(topic, payload)
end end
end end
function love.gamepadpressed(joystick, button) function love.gamepadpressed2(joystick, button)
print("Pressed gamepad button " .. button .. " on joystick " .. joystick:getName()) print("Pressed gamepad button " .. button .. " on joystick " .. joystick:getName())
if button == "back" then
UIState.showUI = not UIState.showUI
end
end end

View File

@ -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()

View File

@ -45,8 +45,8 @@ function love.draw(...)
local centerY = love.graphics.getHeight() / 2 local centerY = love.graphics.getHeight() / 2
-- Calculate textX and textY -- Calculate textX and textY
--local textX = math.floor(centerX - (font:getWidth(text) / 2)) local textX = math.floor(centerX - (font:getWidth(text) / 2))
--local textY = math.floor(centerY - (font:getHeight(text) / 2)) local textY = math.floor(centerY - (font:getHeight(text) / 2))
local textX, textY = 10, 10 local textX, textY = 10, 10
local realText local realText
@ -77,6 +77,15 @@ function love.update(...)
end end
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) function love.mqtt.onError(message)
print("MQTT error: " .. message) print("MQTT error: " .. message)
end end