DaikiKaminari

DateTimeSpawn

Aug 9th, 2020 (edited)
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.50 KB | None | 0 0
  1. local mon   -- table : peripheral, monitor
  2. local width -- int : width of the monitor
  3.  
  4. --- INIT ---
  5. local function loadAPIs()
  6.     if not fs.exists("lib/datetime") then
  7.         error("[lib/datetime] file not found.")
  8.     end
  9.     os.loadAPI("lib/datetime")
  10.     datetime.init()
  11. end
  12.  
  13. local function init()
  14.     mon = peripheral.find("monitor")
  15.     if mon == nil then
  16.         error("Monitor peripheral not found.")
  17.     end
  18.     width,_ = mon.getSize()
  19.     mon.clear()
  20.     mon.setTextScale(2)
  21. end
  22.  
  23. --- UTILS ---
  24. local function writeCenter(str, width, y, color)
  25.     if color then
  26.         mon.setTextColor(color)
  27.     end
  28.     local x =  (width - string.len(str) + 1) / 2
  29.     mon.setCursorPos(x, y)
  30.     mon.write(str)
  31. end
  32.  
  33. --- FUNCTIONS ---
  34. local function displayTime()
  35.     local infos = datetime.getDatetime2()
  36.     if infos then
  37.         mon.clear()
  38.         writeCenter(string.upper(infos["dayOfWeek"]), width, 1, colors.blue)
  39.         writeCenter(infos["day"] .. "/" .. infos["month"] .. "/" .. infos["day"], width, 3, colors.blue)
  40.         writeCenter(infos["hour"] .. ":" .. infos["min"], width, 5, colors.blue)
  41.  
  42.         if string.upper(infos["dayOfWeek"]) == "DIMANCHE" and tonumber(infos["hour"]) >= 20 then
  43.             writeCenter("RESET", width, 2, colors.red)
  44.             writeCenter("BIENTOT", width, 4, colors.red)
  45.         end
  46.     end
  47. end
  48.  
  49. --- MAIN CALL ---
  50. local function main()
  51.     loadAPIs()
  52.     init()
  53.     while true do
  54.         displayTime()
  55.         sleep(1)
  56.     end
  57. end
  58.  
  59. main()
Add Comment
Please, Sign In to add comment