This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
luochen1990's profile picture

How to detect what device is running, turtle or pc or pad?

Started by luochen1990, 04 February 2020 - 08:08 AM
luochen1990 #1
Posted 04 February 2020 - 09:08 AM
Hello, I'm trying to automatic run a script on startup, and dispatch to different logic branch according to the device type. Following is a peice of code for demo, which cannot successfully run:


local dt = os.getDeviceType()
if dt == "turtle" then
    runTurtleScript()
elseif dt == "pc" then
    runPCScript()
elseif dt == "pad" then
    runPadScript()
end

I have checked the API doc but doesn't find such an API similar to `os.getDeviceType()`

Is there any solution to implement such a function?

Now I know that I can check if current device is turtle via `if turtle == nil`, but it seem not easy to distinguish a PC and a Pad.
Luca_S #2
Posted 04 February 2020 - 09:14 AM
Now I know that I can check if current device is turtle via `if turtle == nil`, but it seem not easy to distinguish a PC and a Pad.

You can distinguish a PC from a pocket pc by checking for "pocket" and you can check for a command computer using "commands" just like you do with "turtle". So your getDeviceType() function might look like this:


local function getDeviceType()
   if turtle then
    return "turtle"
  elseif pocket then
    return "pocket"
  elseif commands then
    return "command_computer"
  else
    return "computer"
  end
end
local dt = getDeviceType()
if dt == "turtle" then
  runTurtleScript()
elseif dt == "computer" or dt == "command_computer" then
  runPCScript()
elseif dt == "pocket" then
  runPadScript()
end
luochen1990 #3
Posted 04 February 2020 - 03:03 PM
@Luca_S Thank you! this reply is very helpful :)/>