Skip to content

Instantly share code, notes, and snippets.

@CheezusChrust
Last active April 8, 2023 04:31
Show Gist options
  • Save CheezusChrust/16ffb50a29bde3433ec436b597bd1fcd to your computer and use it in GitHub Desktop.
Save CheezusChrust/16ffb50a29bde3433ec436b597bd1fcd to your computer and use it in GitHub Desktop.
Helps you find what axis is what with the joystick module in Starfall, spawn on a Starfall screen
--@name Joystick Helper
--@author Cheezus
--@shared
if SERVER then
local ent = chip():isWeldedTo()
if isValid(ent) and ent:getClass() == "starfall_screen" then
ent:linkComponent(chip())
end
else
if not joystick then
print("Joystick module not installed")
return
end
if joystick.numJoysticks() == 0 then
print("No usable devices connected")
return
end
-- Returns a table containing only keys which have a value different from the old table
local function getDiff(old, new)
local diff = {}
for k, v in pairs(new) do
if old[k] ~= v then
diff[k] = v
end
end
return diff
end
local function getJoystickData(deviceID)
local data = {}
data.axes = {}
data.buttons = {}
data.povs = {}
for i = 0, 31 do
data.axes[i] = joystick.getAxis(deviceID, i)
end
for i = 0, 31 do
data.buttons[i] = joystick.getButton(deviceID, i)
end
for i = 0, 31 do
data.povs[i] = joystick.getPov(deviceID, i)
end
return data
end
local devices = {}
-- Get default device values to compare against later
for i = 0, 2 do
devices[i] = {}
devices[i].joystickData = getJoystickData(i)
local detectedInputs = {}
detectedInputs.axes = {}
detectedInputs.buttons = {}
detectedInputs.povs = {}
devices[i].detectedInputs = detectedInputs
end
timer.create("detectAxes", 0.1, 0, function()
for deviceID, device in pairs(devices) do
local newData = getJoystickData(deviceID)
local axesDiff = getDiff(device.joystickData.axes, newData.axes)
local buttonsDiff = getDiff(device.joystickData.buttons, newData.buttons)
local povsDiff = getDiff(device.joystickData.povs, newData.povs)
for k, v in pairs(axesDiff) do
device.detectedInputs.axes[k] = v
end
for k, v in pairs(buttonsDiff) do
device.detectedInputs.buttons[k] = v
end
--for k, v in pairs(povsDiff) do
-- device.detectedInputs.povs[k] = v
--end
end
end)
local WHITE = Color(255, 255, 255)
local RED = Color(255, 100, 100)
local GREEN = Color(100, 255, 100)
local BLUE = Color(100, 100, 255)
hook.add("render", "data", function()
for deviceID, device in pairs(devices) do
render.setColor(WHITE)
render.drawText(deviceID * 256, 0, "Device " .. deviceID, deviceID)
render.drawText(deviceID * 256, 14, joystick.getName(deviceID), deviceID)
render.drawLine(0, 30, 512, 30)
local row = 0
for dataSetName, dataSet in pairs(device.detectedInputs) do
for inputID in pairs(dataSet) do
local value
if dataSetName == "axes" then
value = string.format("Axis %s: %d", inputID, joystick.getAxis(deviceID, inputID))
render.setColor(RED)
elseif dataSetName == "buttons" then
value = string.format("Button %s: %d", inputID, joystick.getButton(deviceID, inputID) > 0 and 1 or 0)
render.setColor(GREEN)
elseif dataSetName == "povs" then
value = string.format("POV %s: %d", inputID, joystick.getPov(deviceID, inputID))
render.setColor(BLUE)
end
render.drawText(deviceID * 256, 32 + row * 12, value, deviceID)
row = row + 1
end
end
end
end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment