Advertisement
fingercomp

[OpenComputers] Simple Clock

May 5th, 2015
5,691
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. MT_BG    = 0x000000
  2. MT_FG    = 0xFFFFFF
  3. DAY      = 0xFFFF00
  4. EVENING  = 0x202080
  5. NIGHT    = 0x000080
  6. MORNING  = 0x404000
  7. RT_BG    = 0x000000
  8. RT_FG    = 0xFFFFFF
  9. TIMEZONE = 0
  10. CORRECT  = 0
  11. W, H     = 40, 8
  12. REDSTONE = false
  13. TOUCH    = true
  14. KEY1     = 13
  15. KEY2     = 28
  16. SHOWSECS = true
  17. AUTOMODE = true
  18. SWDATEMT = true
  19. SWDATERT = true
  20. SWDTMMT  = true
  21. SWDTMRT  = true
  22.  
  23. local com     = require("component")
  24. local gpu     = com.gpu
  25. local unicode = require("unicode")
  26. local fs      = require("filesystem")
  27. local event   = require("event")
  28. local term    = require("term")
  29.  
  30. oldw, oldh = gpu.getResolution()
  31. gpu.setResolution(W, H)
  32. w, h = gpu.getResolution()
  33. mode = AUTOMODE
  34. noExit = true
  35.  
  36. tz = TIMEZONE + CORRECT
  37. local nums = {}
  38. nums[0] = {"███", "█ █", "█ █", "█ █", "███"}
  39. nums[1] = {"██ ", " █ ", " █ ", " █ ", "███"}
  40. nums[2] = {"███", "  █", "███", "█  ", "███"}
  41. nums[3] = {"███", "  █", "███", "  █", "███"}
  42. nums[4] = {"█ █", "█ █", "███", "  █", "  █"}
  43. nums[5] = {"███", "█  ", "███", "  █", "███"}
  44. nums[6] = {"███", "█  ", "███", "█ █", "███"}
  45. nums[7] = {"███", "  █", "  █", "  █", "  █"}
  46. nums[8] = {"███", "█ █", "███", "█ █", "███"}
  47. nums[9] = {"███", "█ █", "███", "  █", "███"}
  48.  
  49. dts = {}
  50. dts[1] = "Night"
  51. dts[2] = "Morning"
  52. dts[3] = "Day"
  53. dts[4] = "Evening"
  54.  
  55. local function centerX(str)
  56.   local len
  57.   if type(str) == "string" then
  58.     len = unicode.len(str)
  59.   elseif type(str) == "number" then
  60.     len = str
  61.   else
  62.     error("Number excepted")
  63.   end
  64.   local whereW, _ = math.modf(w / 2)
  65.   local whereT, _ = math.modf(len / 2)
  66.   local where = whereW - whereT + 1
  67.   return where
  68. end
  69.  
  70. local function centerY(lines)
  71.   local whereH, _ = math.modf(h / 2)
  72.   local whereT, _ = math.modf(lines / 2)
  73.   local where = whereH - whereT + 1
  74.   return where
  75. end
  76.  
  77. local t_correction = tz * 3600
  78.  
  79. local function getTime()
  80.     local file = io.open('/tmp/clock.dt', 'w')
  81.     file:write('')
  82.     file:close()
  83.     local lastmod = tonumber(string.sub(fs.lastModified('/tmp/clock.dt'), 1, -4)) + t_correction
  84.  
  85.     local year = os.date('%Y', lastmod)
  86.     local month = os.date('%m', lastmod)
  87.     local day = os.date('%d', lastmod)
  88.     local weekday = os.date('%A', lastmod)
  89.     local hour = os.date('%H', lastmod)
  90.     local minute  = os.date('%M', lastmod)
  91.     local sec  = os.date('%S', lastmod)    
  92.     return year, month, day, weekday, hour, minute, sec
  93. end
  94.  
  95. local function sn(num)
  96.   -- SplitNumber
  97.   local n1, n2
  98.   if num >= 10 then
  99.     n1, n2 = tostring(num):match("(%d)(%d)")
  100.     n1, n2 = tonumber(n1), tonumber(n2)
  101.   else
  102.     n1, n2 = 0, num
  103.   end
  104.   return n1, n2
  105. end
  106.  
  107. local function drawNumbers(hh, mm, ss)
  108.   local firstLine = centerY(5)
  109.   local n1, n2, n3, n4, n5, n6
  110.   n1, n2 = sn(hh)
  111.   n3, n4 = sn(mm)
  112.   if ss ~= nil then
  113.     n5, n6 = sn(ss)
  114.   end
  115. --print(n1, n2, n3, n4, n5, n6, type(n1))
  116.   for i = 1, 5, 1 do
  117.     local sep
  118.     if i == 2 or i == 4 then
  119.       sep = " . "
  120.     else
  121.       sep = "   "
  122.     end
  123.     local lineToDraw = ""
  124.     if ss ~= nil then
  125.       lineToDraw = nums[n1][i] .. "  " .. nums[n2][i] .. sep .. nums[n3][i] .. "  " .. nums[n4][i] .. sep .. nums[n5][i] .. "  " .. nums[n6][i]
  126.     else
  127.       lineToDraw = nums[n1][i] .. "  " .. nums[n2][i] .. sep .. nums[n3][i] .. "  " .. nums[n4][i]
  128.     end
  129.     gpu.set(centerX(lineToDraw), firstLine + i - 1, lineToDraw)
  130.   end
  131. end
  132.  
  133. local function setDaytimeColor(hh, mm)
  134.   local daytime
  135.   if (hh == 19 and mm >= 30) or (hh > 19 and hh < 22) then
  136.     daytime = 4
  137.     gpu.setForeground(EVENING)
  138.   elseif hh >= 22 or hh < 6 then
  139.     daytime = 1
  140.     gpu.setForeground(NIGHT)
  141.   elseif hh >= 6 and hh < 12 then
  142.     daytime = 2
  143.     gpu.setForeground(MORNING)
  144.   elseif (hh >= 12 and hh < 19) or (hh == 19 and mm < 30) then
  145.     daytime = 3
  146.     gpu.setForeground(DAY)
  147.   end
  148.   return daytime
  149. end
  150.  
  151. local function drawMT()
  152.   local year, month, day, hh, mm = os.date():match("(%d+)/(%d+)/(%d+)%s(%d+):(%d+):%d+")
  153.   hh, mm = tonumber(hh), tonumber(mm)
  154.   gpu.fill(1, 1, w, h, " ")
  155.   drawNumbers(hh, mm)
  156.   if SWDTMMT then
  157.     local dtm = setDaytimeColor(hh, mm)
  158.     gpu.set(centerX(dts[dtm]), centerY(5) - 1, dts[dtm])
  159.   end
  160.   gpu.setForeground(MT_FG)
  161.   if SWDATEMT then
  162.     gpu.set(centerX(year .. "/" .. month .. "/" .. day), centerY(1) + 3, year .. "/" .. month .. "/" .. day)
  163.   end
  164. end
  165.  
  166. local function drawRT()
  167.   local year, month, day, wd, hh, mm, ss = getTime()
  168.   gpu.fill(1, 1, w, h, " ")
  169.   hh, mm, ss = tonumber(hh), tonumber(mm), tonumber(ss)
  170.   if not SHOWSECS then
  171.     ss = nil
  172.   end
  173.   drawNumbers(hh, mm, ss)
  174.   if SWDTMRT then
  175.     local dtm = setDaytimeColor(hh, mm)
  176.     gpu.set(centerX(dts[dtm]), centerY(5) - 1, dts[dtm])
  177.   end
  178.   gpu.setForeground(RT_FG)
  179.   local infoLine = wd .. ", " .. year .. "/" .. month .. "/" .. day .. "::GMT" .. TIMEZONE
  180.   if SWDATERT then
  181.     gpu.set(centerX(infoLine), centerY(1) + 3, infoLine)
  182.   end
  183. end
  184.  
  185. local function cbFunc()
  186.   if mode == true then mode = false gpu.setBackground(RT_BG) gpu.setForeground(RT_FG) else mode = true gpu.setBackground(MT_BG) gpu.setForeground(MT_FG) end
  187. end
  188.  
  189. local function checkKey(name, addr, key1, key2)
  190.   if key1 == KEY1 and key2 == KEY2 then
  191.     noExit = false
  192.   end
  193. end
  194.  
  195. gpu.fill(1, 1, w, h, " ")
  196. if TOUCH then
  197.   event.listen("touch", cbFunc)
  198. end
  199. if REDSTONE then
  200.   event.listen("redstone_changed", cbFunc)
  201. end
  202. event.listen("key_down", checkKey)
  203. term.setCursor(1, 1)
  204. while noExit do
  205.   if mode == true then
  206.     drawMT()
  207.   else
  208.     drawRT()
  209.   end
  210.   os.sleep(1)
  211. end
  212. gpu.setForeground(0xFFFFFF)
  213. gpu.setBackground(0x000000)
  214. gpu.setResolution(oldw, oldh)
  215. gpu.fill(1, 1, oldw, oldh, " ")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement