daily pastebin goal
19%
SHARE
TWEET

mag_data.lua

dha_meo_in Jun 13th, 2018 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local args = {...}
  2. local event = require("event")
  3. local cmp = require("component")
  4. local read_card_event = "magData"
  5.  
  6. local function ask_user(question)
  7.   if question then
  8.     io.write(question..": ")
  9.     io.flush()
  10.     local result = io.read()
  11.     io.write("\n")
  12.     return result
  13.   end
  14. end
  15.  
  16. local function read_data()
  17.   local card_reader = cmp.os_magreader
  18.   print("Waiting for magnetic card")
  19.   local res = table.pack(event.pull(read_card_event))
  20.   print(table.unpack(res))
  21.   return res
  22. end
  23.  
  24. local function write_data()
  25.   local card_writer = cmp.os_cardwriter
  26.   local card_data = ask_user("Data string to write")
  27.   local disp_data = ask_user("Display string to write")
  28.   local finalize = ask_user("Type \"true\" if you want to finalize card")
  29.   finalize = (finalize == "true")
  30.   if card_data then
  31.     local result = card_writer.write(card_data,disp_data, finalize)
  32.     if result then print("Ok") end
  33.   end
  34. end
  35.  
  36. local actions = {read = read_data, write = write_data}
  37.  
  38. local action = actions[args[1]]
  39. if action then
  40.   action()
  41. else
  42.   print("Usage: mag_card read|write")
  43. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top