pepeknamornik

Printer API

Dec 23rd, 2020 (edited)
685
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.67 KB | None | 0 0
  1. verze = "21.0629.R1"
  2. local printer = nil
  3. local tArgs = { ... }
  4. local sound = false
  5. local w, h = term.getSize()
  6. local opakovat = false
  7.  
  8. if fs.exists("/C/system/RAM/devices/speaker.tmp") then
  9.     sound = true
  10.     end
  11.    
  12. function song(type)
  13. if sound then shell.run("/C/system/api/sound.api "..type) end
  14. end
  15.  
  16. local function load(name)
  17.     local file = fs.open(name,"r")
  18.     local data = file.readAll()
  19.     file.close()
  20.     return textutils.unserialize(data)
  21. end
  22.  
  23. local f=fs.open("/C/system/RAM/user","r")
  24. user=f.readLine()
  25. pass=f.readLine()
  26. Fname=f.readLine()
  27. Lname=f.readLine()
  28. f.close()
  29.  
  30.     if fs.exists ("/C/system/Users/"..user.."/.Registry/colors") then
  31.     local data = fs.open("/C/system/Users/"..user.."/.Registry/colors", "r")
  32.     BarPoz = tonumber(data.readLine())
  33.     BarTxt = tonumber(data.readLine())
  34.     data.close()
  35.     end
  36.  
  37. if fs.exists("/C/system/RAM/devices/printer.tmp") then
  38.     printers = {}
  39.     printers = load("/C/system/RAM/devices/printer.tmp")
  40.     printer = peripheral.wrap(tostring(printers[0]))
  41.     if printer == nil then printer = peripheral.wrap(tostring(printers[1])) end
  42. else
  43.     printer = peripheral.find("printer")
  44. end
  45.  
  46. function chyba(duvod)
  47.    
  48.     myWindow = window.create(term.current(),w/2-8,h/2-2,20,6)
  49.     myWindow.setBackgroundColor(colors.white)
  50.     myWindow.setTextColor(colors.white)
  51.     myWindow.clear()
  52.     myWindow.setBackgroundColor(colors.red)
  53.     myWindow.setCursorPos (20, 1)
  54.     myWindow.write"X"
  55.     myWindow.setCursorPos (1, 1)
  56.     myWindow.setBackgroundColor(BarPoz)
  57.     myWindow.setTextColor(BarTxt)
  58.     myWindow.write"    Print Error!   "
  59.     myWindow.setBackgroundColor(colors.white)
  60.     myWindow.setTextColor(colors.red)
  61.    
  62.     if duvod == "papir" then
  63.     myWindow.setCursorPos (1, 3)
  64.     myWindow.write"Please check printer"
  65.     myWindow.setTextColor(colors.black)
  66.     myWindow.setBackgroundColor(colors.lightGray)
  67.     myWindow.setCursorPos (3, 5)
  68.     myWindow.write" Retry "
  69.     myWindow.setCursorPos (11, 5)
  70.     myWindow.write" Storno "
  71.     end
  72.    
  73.     song("stop")
  74.  
  75.      while true do
  76.             local event, button, x, y = os.pullEvent("mouse_click")
  77.             xy = x..","..y
  78.            
  79.             if event == "mouse_click" and x == math.floor(w/2+11) and  y == math.floor(h/2-2) and button == 1 then
  80.             error()
  81.             elseif event == "mouse_click" and x >= math.floor(w/2-6) and x <= math.floor(w/2) and  y == math.floor(h/2+2) and button == 1 then
  82.             myWindow.setBackgroundColor(colors.gray)
  83.             myWindow.setCursorPos (3, 5)
  84.             myWindow.write" Retry "
  85.             success = printer.newPage()
  86.             if not radek == nil then i = radek opakovat = true end
  87.             sleep(0.2)
  88.             rozlozit(tArgs[1])
  89.             elseif event == "mouse_click" and x >= math.floor(w/2+2) and x <= math.floor(w/2+9) and  y == math.floor(h/2+2) and button == 1 then
  90.             myWindow.setBackgroundColor(colors.gray)
  91.             myWindow.setCursorPos (11, 5)
  92.             myWindow.write" Storno "
  93.             sleep(0.2)
  94.             error()
  95.             end
  96.     end
  97. end
  98.  
  99. function rozlozit(dokument)
  100.  
  101.     if not opakovat then
  102.         file = fs.open(dokument, "r")
  103.  
  104.             if fs.exists(dokument) then
  105.                 if file then
  106.                   i = 0
  107.                   while file.readLine() do
  108.                     i = i + 1
  109.                   end
  110.                   file.close()
  111.                 end
  112.         end
  113.  
  114.         q = 1
  115.         else
  116.         opakovat = false
  117.     end
  118.          success = printer.newPage()
  119.          
  120.          if success then
  121.             local data = fs.open (dokument, "r")
  122.  
  123.             for a=1,i do
  124.                 if success then
  125.                     printer.setCursorPos(1, q)
  126.                     text = data.readLine()
  127.                     printer.write(text)
  128.                     q = q+1
  129.                         if q > 21 then
  130.                         q = 1
  131.                         printer.endPage()
  132.                         success = printer.newPage()
  133.                         end
  134.                 end
  135.                
  136.                 if not success then
  137.                 radek = i
  138.                 chyba("papir")
  139.                 end
  140.             end
  141.            
  142.             printer.endPage()
  143.             song("print")
  144.             error()
  145.     else
  146.         chyba("papir")
  147.     end
  148.     end
  149.  
  150.  
  151.  
  152. if #tArgs > 0 then
  153.     rozlozit(tArgs[1])
  154.     else
  155.     song("error")
  156. end
Add Comment
Please, Sign In to add comment