Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.37 KB | None | 0 0
  1. ---
  2. --- mag.lua - Simple mag card reader and writer utility
  3. --- Author: Rolando Islas
  4. --- Version: 0.1
  5. --- License: GPLv2
  6. ---
  7.  
  8. local Mag = {}
  9. Mag.__index = Mag
  10.  
  11. ---
  12. --- Create new mag reader/writer instance
  13. ---
  14. function Mag:new()
  15.     local mag = {}
  16.     setmetatable(mag, Mag)
  17.     -- ComputerCraft
  18.     if peripheral then
  19.         mag.mag = peripheral.find("mag_reader_writer")
  20.         -- OpenComputers
  21.     else
  22.         mag.mag = require("component").mag_reader_writer
  23.         os.pullEvent = require("event").pull
  24.     end
  25.     if not mag.mag then
  26.         error("Mag Card Reader/Writer not found")
  27.     end
  28.     return mag
  29. end
  30.  
  31. ---
  32. --- Main command line entry point
  33. --- @param args table command line arguments
  34. ---
  35. function Mag:run(args)
  36.     self:check_argument_count(args, 1, "argument", false)
  37.     local arg = args[1]
  38.     if arg == "read" then
  39.         local event, address, track_one, track_two, track_three
  40.         if peripheral then
  41.             event, track_one, track_two, track_three = os.pullEvent("mag_swipe")
  42.         else
  43.             event, address, track_one, track_two, track_three = os.pullEvent("mag_swipe")
  44.         end
  45.         print(string.format("1: %s\n2: %s\n3: %s", track_one, track_two, track_three))
  46.     elseif arg == "write" then
  47.         self:check_argument_count(args, 2, "track index", false)
  48.         self:check_argument_count(args, 3, "data buffer", true)
  49.         self.mag.write(tonumber(args[2]), args[3])
  50.         print(string.format("Wrote to track buffer %d", args[2]))
  51.     elseif arg == "clear" then
  52.         self.mag.clear()
  53.         print("Cleared buffers")
  54.     else
  55.         error(string.format("No argument \"%s\" found. Try \"help mag\".", arg))
  56.     end
  57. end
  58.  
  59. ---
  60. --- Check the argument count matches the bounds passed
  61. --- Errors with a reason message.
  62. ---
  63. --- @param args table arguments
  64. --- @param index number minimum amount of arguments
  65. --- @param name string noun to use for error messages
  66. --- @param max boolean should the arguments end at the index passed
  67. ---
  68. function Mag:check_argument_count(args, index, name, max)
  69.     if max == nil then
  70.         max = true
  71.     end
  72.     if #args < index then
  73.         error("Missing " .. name .. ". Try Try \"help mag\".")
  74.     elseif max and #args > index then
  75.         error("Too many " .. name .. "s passed. Try \"help mag\".")
  76.     end
  77. end
  78.  
  79. -- Init
  80. local mag = Mag:new()
  81. mag:run({...})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement