Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- side of the computer that the monitor is attached to
- local monitor = peripheral.wrap("back")
- -- clock radius in character. 18 is good for a 6x6 advanced monitor
- local nClockRad = 18
- -- these two settings will hide/show
- -- the M and H that is on the end of the hand
- local bShowHourChar = false
- local bShowMinuteChar = false
- -- set up the screens
- term.clear()
- term.setCursorPos(1,1)
- print("Orologio fatto da Mr.Alko99 2013")
- print("Premi 'Q' per spegnere.")
- term.redirect(monitor)
- term.setBackgroundColor(colors.black)
- term.clear()
- -- some variables
- local h = 0 -- parsed hour
- local m = 0 -- parsed minute
- local nOldHX = -1
- local nOldHY = -1
- local nOldMX = -1
- local nOldMY = -1
- local function plot(s,x,y)
- term.setCursorPos(x,y)
- term.write(s)
- end
- local function approx(v)
- if(v>=0) then
- return math.floor(v+0.5)
- else
- return math.ceil(v+0.5)
- end
- end
- local function line(x0, y0, x1, y1)
- if x0==x1 and y0==y1 then
- return false
- end
- if x0==x1 then
- if y0>y1 then
- inc=-1
- else
- inc=1
- end
- for y=y0,y1,inc do
- plot(".",x0,y)
- end
- return true
- end
- if y0==y1 then
- if x0>x1 then
- inc=-1
- else
- inc=1
- end
- for x=x0,x1,inc do
- plot(".",x,y0)
- end
- return true
- end
- if x0~=x1 and y0~=y1 then
- m=(y1-y0)/(x1-x0)
- if m<1 and m>-1 then
- n=y0-m*x0
- if x0>x1 then
- inc=-1
- else
- inc=1
- end
- for x=x0,x1,inc do
- y=m*x+n
- y=approx(y)
- plot(".",x,y)
- end
- return true
- else
- m=(x1-x0)/(y1-y0)
- n=x0-m*y0
- if y0>y1 then
- inc=-1
- else
- inc=1
- end
- for y=y0,y1,inc do
- x=m*y+n
- x=approx(x)
- plot(".",x,y)
- end
- return true
- end
- end
- end
- -- the clock face ticks
- local function ticks(nW,nH)
- local nIndex
- local x
- local y
- local xi
- local yi
- local nInRadius = nClockRad-(nClockRad/4)
- local nOutRadius = nClockRad-1
- local nxOutRad = nOutRadius + (nOutRadius/2)
- local nyOutRad = nOutRadius
- local nxInRad = nInRadius + (nInRadius/2)
- local nyInRad = nInRadius
- for nIndex=0,359,(360/12) do
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.black)
- x = nW + math.sin(math.rad(nIndex)) * nxOutRad
- y = nH + math.cos(math.rad(nIndex)) * nyOutRad
- xi = nW + math.sin(math.rad(nIndex)) * nxInRad
- yi = nH + math.cos(math.rad(nIndex)) * nyInRad
- line(xi,yi,x,y)
- end
- end
- -- draw the face (circle)
- -- which is essentially a line from
- -- centre to all points on the circle
- -- in character terms. Who cares about
- -- overlap ;)
- local function face(nW,nH,nRadius)
- local nIndex
- local x
- local y
- local nxRad = nRadius + (nRadius/2)
- local nyRad = nRadius
- local nOldX = -1
- local nOldY = -1
- for nIndex=0,359 do
- x = nW + math.sin(math.rad(nIndex)) * nxRad
- y = nH + math.cos(math.rad(nIndex)) * nyRad
- -- the circle
- -- only do it if the positions are different from last time
- -- speeds things up a bit
- if x~=nOldX or y~=nOldY then
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.white)
- line(nW,nH,x,y)
- nOldX = x
- nOldY = y
- end
- end
- end
- -- parse the time into H and M
- -- these are stored globally in h and m
- local function parseTime()
- local t = os.time()
- local sT = textutils.formatTime(t, true)
- local n = string.find(sT, ":")
- if(n>0) then
- h = tonumber(string.sub(sT,1,n-1))
- m = tonumber(string.sub(sT,n+1))
- end
- end
- -- plot minute hand
- -- it removes the previous minute hand
- -- then draws the new one
- local function plotM(nW,nH,nX,nY)
- if(nOldMX==nX and nOldMY==nY) then
- return
- end
- -- remove previous by drawing it again
- -- in the face colour. Also remove the
- -- hand tag
- if(nOldMX~=-1) then
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.white)
- line(nW,nH,nOldMX,nOldMY)
- if(bShowMinuteChar) then
- plot(".",nOldMX,nOldMY)
- end
- end
- nOldMX = nX
- nOldMY = nY
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.black)
- line(nW,nH,nX,nY)
- -- draw the centre O
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.lightGray)
- plot("O",nW,nH)
- if(bShowMinuteChar) then
- -- draw the minute hand tag
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.lightGray)
- plot("M",nX,nY)
- end
- end
- -- plot hour hand
- -- it removes the previous hour hand
- -- then draws the new one
- local function plotH(nW,nH,nX,nY)
- if(nOldHX==nX and nOldHY==nY) then
- return
- end
- -- remove previous by drawing it again
- -- in the face colour. Also remove the
- -- hand tag
- if(nOldHX~=-1) then
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.white)
- line(nW,nH,nOldHX,nOldHY)
- if(bShowHourChar) then
- plot(".",nOldHX,nOldHY)
- end
- end
- nOldHX = nX
- nOldHY = nY
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.black)
- line(nW,nH,nX,nY)
- if(bShowHourChar) then
- -- draw the hour hand tag
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.lightGray)
- plot("H",nX,nY)
- end
- end
- local fH
- local fM
- local fXH
- local fYH
- local fXM
- local fYM
- local nyMRadius = nClockRad - (nClockRad / 4)
- local nyHRadius = nClockRad - (nClockRad / 2)
- local nxMRadius = nyMRadius + (nyMRadius / 2)
- local nxHRadius = nyHRadius + (nyHRadius / 2)
- local nW, nH = term.getSize()
- nW = (nW / 2)+1
- nH = (nH / 2)+1
- -- plot the circle
- face(nW,nH,nClockRad + 1)
- local bRunning = true
- local nTimer
- nTimer = os.startTimer(0.25) -- the timer interval to update the clock
- while(bRunning) do
- parseTime()
- fH = (((h % 12)/12) + (m/60/12)) * 360
- fM = (m/60) * 360
- -- shift by half a circle
- fH = fH + 180
- fM = fM + 180
- fXH = math.sin(math.rad(-fH))
- fYH = math.cos(math.rad(fH))
- fXM = math.sin(math.rad(-fM))
- fYM = math.cos(math.rad(fM))
- plotH(nW,nH, nW + fXH * nxHRadius, nH + fYH * nyHRadius)
- plotM(nW,nH, nW + fXM * nxMRadius, nH + fYM * nyMRadius)
- -- draw the ticks on
- ticks(nW,nH)
- local event, param1 = os.pullEvent()
- while (not (event=="timer" and param1==nTimer) and event~="char") do
- event, param1 = os.pullEvent()
- end
- if(event=="char") then
- if(param1=="q" or param1=="Q") then
- bRunning=false
- end
- elseif(event=="timer" and param1==nTimer) then
- -- timer timed out, lets start again
- nTimer = os.startTimer(0.25)
- end
- end
- term.restore()
- print("Ended.")
Advertisement
Add Comment
Please, Sign In to add comment