enderpro100

Clock app

Apr 10th, 2023 (edited)
559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.70 KB | Gaming | 0 0
  1. --- Program : Clock app
  2. --- Author : LightKnight51
  3. --- last modification : 10/04/2023
  4. --- (In editing ...)
  5.  
  6.  
  7. --- Utils API
  8. os.loadAPI("MarquitoLuaUtils")
  9.  
  10. -- Color
  11. local fontColor = colors.red
  12.  
  13. -- Get second
  14. function GetSecond(currentTimeStamp)
  15.     return os.date('%S', currentTimeStamp / 1000)
  16. end
  17. -- Get minute
  18. function GetMinute(currentTimeStamp)
  19.     return os.date('%M', currentTimeStamp / 1000)
  20. end
  21. -- Get hour
  22. function GetHour(currentTimeStamp)
  23.     return os.date('%H', currentTimeStamp / 1000)
  24. end
  25.  
  26. --- Draw methods
  27.  
  28. -- Draw digit
  29. function writeDigit(digit, fontColor, xDigitLeft, yDigitTop)
  30.     -- Height 5, width 3
  31.     if digit == 0 then
  32.         -- Horizontal lines
  33.         paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft + 2, yDigitTop, fontColor)
  34.         paintutils.drawLine(xDigitLeft, yDigitTop + 4, xDigitLeft + 2, yDigitTop + 4, fontColor)
  35.         -- Vertical lines
  36.         paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft, yDigitTop + 4, fontColor)
  37.         paintutils.drawLine(xDigitLeft + 2, yDigitTop, xDigitLeft + 2, yDigitTop + 4, fontColor)
  38.     elseif digit == 1 then
  39.         -- Horizontal lines
  40.         paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft + 1, yDigitTop, fontColor)
  41.         paintutils.drawLine(xDigitLeft, yDigitTop + 4, xDigitLeft + 2, yDigitTop + 4, fontColor)
  42.         -- Vertical lines
  43.         paintutils.drawLine(xDigitLeft + 1, yDigitTop, xDigitLeft + 1, yDigitTop + 4, fontColor)
  44.     elseif digit == 2 then
  45.         -- Horizontal lines
  46.         paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft + 2, yDigitTop, fontColor)
  47.         paintutils.drawLine(xDigitLeft, yDigitTop + 2, xDigitLeft + 2, yDigitTop + 2, fontColor)
  48.         paintutils.drawLine(xDigitLeft, yDigitTop + 4, xDigitLeft + 2, yDigitTop + 4, fontColor)
  49.         -- Vertical lines
  50.         paintutils.drawLine(xDigitLeft + 2, yDigitTop, xDigitLeft + 2, yDigitTop + 2, fontColor)
  51.         paintutils.drawLine(xDigitLeft, yDigitTop + 2, xDigitLeft, yDigitTop + 4, fontColor)
  52.     elseif digit == 3 then
  53.         -- Horizontal lines
  54.         paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft + 2, yDigitTop, fontColor)
  55.         paintutils.drawLine(xDigitLeft + 1, yDigitTop + 2, xDigitLeft + 2, yDigitTop + 2, fontColor)
  56.         paintutils.drawLine(xDigitLeft, yDigitTop + 4, xDigitLeft + 2, yDigitTop + 4, fontColor)
  57.         -- Vertical lines
  58.         paintutils.drawLine(xDigitLeft + 2, yDigitTop, xDigitLeft + 2, yDigitTop + 4, fontColor)
  59.     elseif digit == 4 then
  60.         -- Horizontal lines
  61.         paintutils.drawLine(xDigitLeft, yDigitTop + 3, xDigitLeft + 2, yDigitTop + 3, fontColor)
  62.         -- Vertical lines
  63.         paintutils.drawLine(xDigitLeft + 2, yDigitTop, xDigitLeft + 2, yDigitTop + 4, fontColor)
  64.         -- Pixels
  65.         paintutils.drawPixel(xDigitLeft, yDigitTop + 2, fontColor)
  66.         paintutils.drawPixel(xDigitLeft + 1, yDigitTop + 1, fontColor)
  67.     elseif digit == 5 then
  68.         -- Horizontal lines
  69.         paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft + 2, yDigitTop, fontColor)
  70.         paintutils.drawLine(xDigitLeft, yDigitTop + 2, xDigitLeft + 2, yDigitTop + 2, fontColor)
  71.         paintutils.drawLine(xDigitLeft, yDigitTop + 4, xDigitLeft + 2, yDigitTop + 4, fontColor)
  72.         -- Vertical lines
  73.         paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft, yDigitTop + 2, fontColor)
  74.         paintutils.drawLine(xDigitLeft + 2, yDigitTop + 2, xDigitLeft + 2, yDigitTop + 4, fontColor)
  75.     elseif digit == 6 then
  76.         -- Horizontal lines
  77.         paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft + 2, yDigitTop, fontColor)
  78.         paintutils.drawLine(xDigitLeft, yDigitTop + 2, xDigitLeft + 2, yDigitTop + 2, fontColor)
  79.         paintutils.drawLine(xDigitLeft, yDigitTop + 4, xDigitLeft + 2, yDigitTop + 4, fontColor)
  80.         -- Vertical lines
  81.         paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft, yDigitTop + 4, fontColor)
  82.         paintutils.drawLine(xDigitLeft + 2, yDigitTop + 2, xDigitLeft + 2, yDigitTop + 4, fontColor)
  83.     elseif digit == 7 then
  84.         -- Horizontal lines
  85.         paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft + 2, yDigitTop, fontColor)
  86.         -- Vertical lines
  87.         paintutils.drawLine(xDigitLeft + 2, yDigitTop, xDigitLeft + 2, yDigitTop + 4, fontColor)
  88.     elseif digit == 8 then
  89.         -- Horizontal lines
  90.         paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft + 2, yDigitTop, fontColor)
  91.         paintutils.drawLine(xDigitLeft, yDigitTop + 2, xDigitLeft + 2, yDigitTop + 2, fontColor)
  92.         paintutils.drawLine(xDigitLeft, yDigitTop + 4, xDigitLeft + 2, yDigitTop + 4, fontColor)
  93.         -- Vertical lines
  94.         paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft, yDigitTop + 4, fontColor)
  95.         paintutils.drawLine(xDigitLeft + 2, yDigitTop, xDigitLeft + 2, yDigitTop + 4, fontColor)
  96.     elseif digit == 9 then
  97.         -- Horizontal lines
  98.         paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft + 2, yDigitTop, fontColor)
  99.         paintutils.drawLine(xDigitLeft, yDigitTop + 2, xDigitLeft + 2, yDigitTop + 2, fontColor)
  100.         paintutils.drawLine(xDigitLeft, yDigitTop + 4, xDigitLeft + 2, yDigitTop + 4, fontColor)
  101.         -- Vertical lines
  102.         paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft, yDigitTop + 2, fontColor)
  103.         paintutils.drawLine(xDigitLeft + 2, yDigitTop, xDigitLeft + 2, yDigitTop + 4, fontColor)
  104.     end
  105. end
  106.  
  107. -- Draw : delimiter
  108. function WriteTimeDelimiter(fontColor, xDigitLeft, yDigitTop)
  109.     paintutils.drawPixel(xDigitLeft, yDigitTop + 1, fontColor)
  110.     paintutils.drawPixel(xDigitLeft, yDigitTop + 3, fontColor)
  111. end
  112.  
  113. -- Draw time
  114. function WriteTime(hour, minute, second, fontColor, xCenter, yCenter)
  115.     local yStart = yCenter - 2
  116.     local xStart = xCenter - 3 - 1 - 1 - 1 - 3 - 1 - 3
  117.     -- Draw hour
  118.     writeDigit(tonumber(string.sub(hour, 1,1)), fontColor, xStart, yStart)
  119.     xStart = xStart + 4
  120.     writeDigit(tonumber(string.sub(hour, 2,2)), fontColor, xStart, yStart)
  121.     xStart = xStart + 4
  122.     -- Draw first delimiter
  123.     WriteTimeDelimiter(fontColor, xStart, yStart)
  124.     xStart = xStart + 2
  125.     -- Draw minute
  126.     writeDigit(tonumber(string.sub(minute, 1,1)), fontColor, xStart, yStart)
  127.     xStart = xStart + 4
  128.     writeDigit(tonumber(string.sub(minute, 2,2)), fontColor, xStart, yStart)
  129.     xStart = xStart + 4
  130.     -- Draw second delimiter
  131.     WriteTimeDelimiter(fontColor, xStart, yStart)
  132.     xStart = xStart + 2
  133.     -- Draw second
  134.     writeDigit(tonumber(string.sub(second, 1,1)), fontColor, xStart, yStart)
  135.     xStart = xStart + 4
  136.     writeDigit(tonumber(string.sub(second, 2,2)), fontColor, xStart, yStart)
  137. end
  138.  
  139. -- Clock program
  140. function RunClock()
  141.     -- Find the screen
  142.     local screen = MarquitoLuaUtils.FindScreen()
  143.     term.redirect(screen)
  144.     -- Clear the screen
  145.     screen.setBackgroundColor(colors.black)
  146.     screen.clear()
  147.     screen.clearLine()
  148.  
  149.     while true do
  150.         -- Get center position of the screen
  151.         local xCenter, yCenter = MarquitoLuaUtils.GetCenterOfScreen(screen)
  152.         -- Clear the screen
  153.         screen.setBackgroundColor(colors.black)
  154.         screen.clear()
  155.         local currentTimeStamp = MarquitoLuaUtils.GetCurrentLocalTimeStamp()
  156.         -- Write current system time on screen
  157.         WriteTime(GetHour(currentTimeStamp), GetMinute(currentTimeStamp), GetSecond(currentTimeStamp), fontColor, xCenter, yCenter)
  158.  
  159.         sleep(1)
  160.     end
  161. end
  162.  
  163. -- Detect screen touch for change color
  164. function ChangeColor()
  165.     while true do
  166.         local event, side, x, y = os.pullEvent("monitor_touch")
  167.         if fontColor == colors.red then
  168.             fontColor = colors.orange
  169.         elseif fontColor == colors.orange then
  170.             fontColor = colors.magenta
  171.         elseif fontColor == colors.magenta then
  172.             fontColor = colors.lightBlue
  173.         elseif fontColor == colors.lightBlue then
  174.             fontColor = colors.yellow
  175.         elseif fontColor == colors.yellow then
  176.             fontColor = colors.lime
  177.         elseif fontColor == colors.lime then
  178.             fontColor = colors.cyan
  179.         elseif fontColor == colors.cyan then
  180.             fontColor = colors.purple
  181.         elseif fontColor == colors.purple then
  182.             fontColor = colors.blue
  183.         elseif fontColor == colors.blue then
  184.             fontColor = colors.green
  185.         elseif fontColor == colors.green then
  186.             fontColor = colors.red
  187.         end
  188.     end
  189. end
  190.  
  191. -- Run clock program
  192. parallel.waitForAny(RunClock, ChangeColor)
Advertisement
Add Comment
Please, Sign In to add comment