diff --git a/controller-client/draw.lua b/controller-client/draw.lua index 0079224..eb6e8b0 100644 --- a/controller-client/draw.lua +++ b/controller-client/draw.lua @@ -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 diff --git a/controller-client/main.lua b/controller-client/main.lua index fc28b22..81dc88c 100644 --- a/controller-client/main.lua +++ b/controller-client/main.lua @@ -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 diff --git a/controller-client/socket-test.lua b/controller-client/socket-test.lua deleted file mode 100644 index 8b3e03e..0000000 --- a/controller-client/socket-test.lua +++ /dev/null @@ -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() diff --git a/controller-host/main.lua b/controller-host/main.lua index cd19bb8..11f9fb0 100644 --- a/controller-host/main.lua +++ b/controller-host/main.lua @@ -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