Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- Program : Clock app
- --- Author : LightKnight51
- --- last modification : 10/04/2023
- --- (In editing ...)
- --- Utils API
- os.loadAPI("MarquitoLuaUtils")
- -- Color
- local fontColor = colors.red
- -- Get second
- function GetSecond(currentTimeStamp)
- return os.date('%S', currentTimeStamp / 1000)
- end
- -- Get minute
- function GetMinute(currentTimeStamp)
- return os.date('%M', currentTimeStamp / 1000)
- end
- -- Get hour
- function GetHour(currentTimeStamp)
- return os.date('%H', currentTimeStamp / 1000)
- end
- --- Draw methods
- -- Draw digit
- function writeDigit(digit, fontColor, xDigitLeft, yDigitTop)
- -- Height 5, width 3
- if digit == 0 then
- -- Horizontal lines
- paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft + 2, yDigitTop, fontColor)
- paintutils.drawLine(xDigitLeft, yDigitTop + 4, xDigitLeft + 2, yDigitTop + 4, fontColor)
- -- Vertical lines
- paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft, yDigitTop + 4, fontColor)
- paintutils.drawLine(xDigitLeft + 2, yDigitTop, xDigitLeft + 2, yDigitTop + 4, fontColor)
- elseif digit == 1 then
- -- Horizontal lines
- paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft + 1, yDigitTop, fontColor)
- paintutils.drawLine(xDigitLeft, yDigitTop + 4, xDigitLeft + 2, yDigitTop + 4, fontColor)
- -- Vertical lines
- paintutils.drawLine(xDigitLeft + 1, yDigitTop, xDigitLeft + 1, yDigitTop + 4, fontColor)
- elseif digit == 2 then
- -- Horizontal lines
- paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft + 2, yDigitTop, fontColor)
- paintutils.drawLine(xDigitLeft, yDigitTop + 2, xDigitLeft + 2, yDigitTop + 2, fontColor)
- paintutils.drawLine(xDigitLeft, yDigitTop + 4, xDigitLeft + 2, yDigitTop + 4, fontColor)
- -- Vertical lines
- paintutils.drawLine(xDigitLeft + 2, yDigitTop, xDigitLeft + 2, yDigitTop + 2, fontColor)
- paintutils.drawLine(xDigitLeft, yDigitTop + 2, xDigitLeft, yDigitTop + 4, fontColor)
- elseif digit == 3 then
- -- Horizontal lines
- paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft + 2, yDigitTop, fontColor)
- paintutils.drawLine(xDigitLeft + 1, yDigitTop + 2, xDigitLeft + 2, yDigitTop + 2, fontColor)
- paintutils.drawLine(xDigitLeft, yDigitTop + 4, xDigitLeft + 2, yDigitTop + 4, fontColor)
- -- Vertical lines
- paintutils.drawLine(xDigitLeft + 2, yDigitTop, xDigitLeft + 2, yDigitTop + 4, fontColor)
- elseif digit == 4 then
- -- Horizontal lines
- paintutils.drawLine(xDigitLeft, yDigitTop + 3, xDigitLeft + 2, yDigitTop + 3, fontColor)
- -- Vertical lines
- paintutils.drawLine(xDigitLeft + 2, yDigitTop, xDigitLeft + 2, yDigitTop + 4, fontColor)
- -- Pixels
- paintutils.drawPixel(xDigitLeft, yDigitTop + 2, fontColor)
- paintutils.drawPixel(xDigitLeft + 1, yDigitTop + 1, fontColor)
- elseif digit == 5 then
- -- Horizontal lines
- paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft + 2, yDigitTop, fontColor)
- paintutils.drawLine(xDigitLeft, yDigitTop + 2, xDigitLeft + 2, yDigitTop + 2, fontColor)
- paintutils.drawLine(xDigitLeft, yDigitTop + 4, xDigitLeft + 2, yDigitTop + 4, fontColor)
- -- Vertical lines
- paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft, yDigitTop + 2, fontColor)
- paintutils.drawLine(xDigitLeft + 2, yDigitTop + 2, xDigitLeft + 2, yDigitTop + 4, fontColor)
- elseif digit == 6 then
- -- Horizontal lines
- paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft + 2, yDigitTop, fontColor)
- paintutils.drawLine(xDigitLeft, yDigitTop + 2, xDigitLeft + 2, yDigitTop + 2, fontColor)
- paintutils.drawLine(xDigitLeft, yDigitTop + 4, xDigitLeft + 2, yDigitTop + 4, fontColor)
- -- Vertical lines
- paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft, yDigitTop + 4, fontColor)
- paintutils.drawLine(xDigitLeft + 2, yDigitTop + 2, xDigitLeft + 2, yDigitTop + 4, fontColor)
- elseif digit == 7 then
- -- Horizontal lines
- paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft + 2, yDigitTop, fontColor)
- -- Vertical lines
- paintutils.drawLine(xDigitLeft + 2, yDigitTop, xDigitLeft + 2, yDigitTop + 4, fontColor)
- elseif digit == 8 then
- -- Horizontal lines
- paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft + 2, yDigitTop, fontColor)
- paintutils.drawLine(xDigitLeft, yDigitTop + 2, xDigitLeft + 2, yDigitTop + 2, fontColor)
- paintutils.drawLine(xDigitLeft, yDigitTop + 4, xDigitLeft + 2, yDigitTop + 4, fontColor)
- -- Vertical lines
- paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft, yDigitTop + 4, fontColor)
- paintutils.drawLine(xDigitLeft + 2, yDigitTop, xDigitLeft + 2, yDigitTop + 4, fontColor)
- elseif digit == 9 then
- -- Horizontal lines
- paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft + 2, yDigitTop, fontColor)
- paintutils.drawLine(xDigitLeft, yDigitTop + 2, xDigitLeft + 2, yDigitTop + 2, fontColor)
- paintutils.drawLine(xDigitLeft, yDigitTop + 4, xDigitLeft + 2, yDigitTop + 4, fontColor)
- -- Vertical lines
- paintutils.drawLine(xDigitLeft, yDigitTop, xDigitLeft, yDigitTop + 2, fontColor)
- paintutils.drawLine(xDigitLeft + 2, yDigitTop, xDigitLeft + 2, yDigitTop + 4, fontColor)
- end
- end
- -- Draw : delimiter
- function WriteTimeDelimiter(fontColor, xDigitLeft, yDigitTop)
- paintutils.drawPixel(xDigitLeft, yDigitTop + 1, fontColor)
- paintutils.drawPixel(xDigitLeft, yDigitTop + 3, fontColor)
- end
- -- Draw time
- function WriteTime(hour, minute, second, fontColor, xCenter, yCenter)
- local yStart = yCenter - 2
- local xStart = xCenter - 3 - 1 - 1 - 1 - 3 - 1 - 3
- -- Draw hour
- writeDigit(tonumber(string.sub(hour, 1,1)), fontColor, xStart, yStart)
- xStart = xStart + 4
- writeDigit(tonumber(string.sub(hour, 2,2)), fontColor, xStart, yStart)
- xStart = xStart + 4
- -- Draw first delimiter
- WriteTimeDelimiter(fontColor, xStart, yStart)
- xStart = xStart + 2
- -- Draw minute
- writeDigit(tonumber(string.sub(minute, 1,1)), fontColor, xStart, yStart)
- xStart = xStart + 4
- writeDigit(tonumber(string.sub(minute, 2,2)), fontColor, xStart, yStart)
- xStart = xStart + 4
- -- Draw second delimiter
- WriteTimeDelimiter(fontColor, xStart, yStart)
- xStart = xStart + 2
- -- Draw second
- writeDigit(tonumber(string.sub(second, 1,1)), fontColor, xStart, yStart)
- xStart = xStart + 4
- writeDigit(tonumber(string.sub(second, 2,2)), fontColor, xStart, yStart)
- end
- -- Clock program
- function RunClock()
- -- Find the screen
- local screen = MarquitoLuaUtils.FindScreen()
- term.redirect(screen)
- -- Clear the screen
- screen.setBackgroundColor(colors.black)
- screen.clear()
- screen.clearLine()
- while true do
- -- Get center position of the screen
- local xCenter, yCenter = MarquitoLuaUtils.GetCenterOfScreen(screen)
- -- Clear the screen
- screen.setBackgroundColor(colors.black)
- screen.clear()
- local currentTimeStamp = MarquitoLuaUtils.GetCurrentLocalTimeStamp()
- -- Write current system time on screen
- WriteTime(GetHour(currentTimeStamp), GetMinute(currentTimeStamp), GetSecond(currentTimeStamp), fontColor, xCenter, yCenter)
- sleep(1)
- end
- end
- -- Detect screen touch for change color
- function ChangeColor()
- while true do
- local event, side, x, y = os.pullEvent("monitor_touch")
- if fontColor == colors.red then
- fontColor = colors.orange
- elseif fontColor == colors.orange then
- fontColor = colors.magenta
- elseif fontColor == colors.magenta then
- fontColor = colors.lightBlue
- elseif fontColor == colors.lightBlue then
- fontColor = colors.yellow
- elseif fontColor == colors.yellow then
- fontColor = colors.lime
- elseif fontColor == colors.lime then
- fontColor = colors.cyan
- elseif fontColor == colors.cyan then
- fontColor = colors.purple
- elseif fontColor == colors.purple then
- fontColor = colors.blue
- elseif fontColor == colors.blue then
- fontColor = colors.green
- elseif fontColor == colors.green then
- fontColor = colors.red
- end
- end
- end
- -- Run clock program
- parallel.waitForAny(RunClock, ChangeColor)
Advertisement
Add Comment
Please, Sign In to add comment