Advertisement
Guest User

clock

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