View difference between Paste ID: ihYDUd7s and LGNUpDzz
SHOW: | | - or go back to the newest paste.
1-
-- Analogue Clock
1+
-- side of the computer that the monitor is attached to
2-
-- (c) Ramdor 2013
2+
local monitor = peripheral.wrap("back")
3-
-- v1.6
3+
4-
--
4+
local nClockRad = 18
5-
-- changes
5+
6-
-- v1.6 - added non filled face option (bFaceFilled = true  ... is the default)
6+
7-
-- v1.5 - now uses computercraft paintutils line, re-worked circle code, auto find monitor
7+
8-
-- v1.4 - colours added and some loop derps fixed
8+
9-
-- v1.3 - added resize/auto scale and 0.5 textsize
9+
10-
--        to double the effective resolution
10+
11-
-- v1.2 - changed timer code
11+
12-
-- v1.1 - tidy up
12+
13-
-- v1.0 - initial release
13+
print("Orologio fatto da Mr.Alko99 2013")
14-
--
14+
print("Premi 'Q' per spegnere.")
15-
-- You may edit/copy/redistribute/whatever this
15+
16-
-- code in any way.  Just a credit listing this
16+
term.setBackgroundColor(colors.black)
17-
-- pastebin link (pastebin.com/LGNUpDzz) will be fine :)
17+
18-
-- Have fun, I did :)
18+
19
-- some variables
20-
-- see  http://www.youtube.com/watch?v=RrgOIGW0C-s   for some more info
20+
21
local m = 0 -- parsed minute
22
local nOldHX = -1
23
local nOldHY = -1
24
local nOldMX = -1
25
local nOldMY = -1
26-
local bFaceFilled = true
26+
27
local function plot(s,x,y)
28-
-- coloured clocks anyone?
28+
29-
local nBackColour = colors.blue
29+
30-
local nFaceColour = colors.white
30+
31-
local nHandColour = colors.blue
31+
32-
local nTickColour = colors.lightBlue
32+
local function approx(v)
33
  if(v>=0) then
34-
-- find the monitor and set it up
34+
    return math.floor(v+0.5)
35-
local monitor = nil
35+
  else
36-
local i,side
36+
    return math.ceil(v+0.5)
37-
for i, side in pairs(rs.getSides()) do
37+
38-
  if peripheral.getType(side) == "monitor" then
38+
39-
    monitor = peripheral.wrap(side)
39+
40-
    break
40+
local function line(x0, y0, x1, y1)
41
  if x0==x1 and y0==y1 then
42
    return false
43-
if monitor==nil then
43+
44-
  print("NO MONITOR FOUND")
44+
45-
  print("Please attach a monitor and try again.")
45+
  if x0==x1 then
46-
  return
46+
    if y0>y1 then
47
      inc=-1
48
    else
49-
-- calculate scaling
49+
      inc=1
50-
monitor.setTextScale(0.5)
50+
51
   
52-
-- hackish fix to enable large monitor arrays enough time to initialise?
52+
    for y=y0,y1,inc do
53-
os.sleep(1)
53+
      plot(".",x0,y)
54
    end
55-
local mw,mh = monitor.getSize()
55+
56-
mw = mw / (15/10) -- 15x10 is w,h of 0.5 scale single monitor
56+
    return true
57-
local minSize = (math.min(mw,mh) / 2) - 2
57+
58
59
  if y0==y1 then
60-
-- or you can use minSize which attempts to fit
60+
    if x0>x1 then
61-
-- to monitor size automatically
61+
      inc=-1
62-
local nClockRad = minSize
62+
63
      inc=1
64
    end
65
 
66
    for x=x0,x1,inc do
67-
print("Clock by Ramdor (c) 2013")
67+
      plot(".",x,y0)
68-
print("Press 'Q' to terminate.")
68+
69
  
70-
term.setBackgroundColor(nBackColour)
70+
    return true
71
  end
72
 
73
  if x0~=x1 and y0~=y1 then
74
    m=(y1-y0)/(x1-x0)
75
   
76
    if m<1 and m>-1 then
77
      n=y0-m*x0
78
      if x0>x1 then
79
        inc=-1
80
      else
81
        inc=1
82
      end
83
     
84
      for x=x0,x1,inc do
85
        y=m*x+n
86
        y=approx(y)
87
        plot(".",x,y)
88
      end
89
      return true
90
    else
91
      m=(x1-x0)/(y1-y0)
92
      n=x0-m*y0
93
      if y0>y1 then
94-
  local nOutRadius = nClockRad-2
94+
        inc=-1
95
      else
96
        inc=1
97
      end
98
     
99
      for y=y0,y1,inc do
100
        x=m*y+n
101
        x=approx(x)
102
        plot(".",x,y)
103
      end
104
      return true
105
    end
106
  end
107
end
108-
    paintutils.drawLine(xi,yi,x,y,nTickColour)
108+
109
-- the clock face ticks
110
local function ticks(nW,nH)
111
  local nIndex
112
  local x
113-
-- by working out a quarter of the points
113+
114-
-- then drawing lines from oposite quarter
114+
115-
-- results in much faster circle code
115+
116
  local nInRadius = nClockRad-(nClockRad/4)
117
  local nOutRadius = nClockRad-1
118
119
  local nxOutRad = nOutRadius + (nOutRadius/2)
120
  local nyOutRad = nOutRadius
121-
  local nyRad = nRadius
121+
122
  local nyInRad = nInRadius
123
124
  for nIndex=0,359,(360/12) do
125-
  local points = {}
125+
126-
  local xy
126+
    term.setBackgroundColor(colors.black)
127-
  local n = 1
127+
128
    x = nW + math.sin(math.rad(nIndex)) * nxOutRad
129-
  for nIndex=0,89 do
129+
130-
    x = math.sin(math.rad(nIndex)) * nxRad
130+
131-
    y = math.cos(math.rad(nIndex)) * nyRad
131+
132-
    x = math.floor(x)
132+
133-
    y = math.floor(y)
133+
134
    line(xi,yi,x,y)
135-
    -- only store positions if they are different from last time  
135+
136
end
137-
      xy = {}
137+
138-
      xy[1] = x
138+
139-
      xy[2] = y
139+
-- which is essentially a line from
140-
      points[n] = xy
140+
-- centre to all points on the circle
141
-- in character terms.  Who cares about
142-
      if y~=nOldY or not bFaceFilled then
142+
-- overlap ;)
143-
        n=n+1
143+
144
  local nIndex
145
  local x
146
  local y
147
  local nxRad = nRadius + (nRadius/2)
148
  local nyRad = nRadius 
149
  local nOldX = -1
150
  local nOldY = -1
151-
  -- plot the circle
151+
152-
  for nIndex=1,#points do
152+
  for nIndex=0,359 do
153-
    x = points[nIndex][1]
153+
    x = nW + math.sin(math.rad(nIndex)) * nxRad
154-
    y = points[nIndex][2]
154+
    y = nH + math.cos(math.rad(nIndex)) * nyRad
155
156-
    if bFaceFilled then
156+
    -- the circle
157-
      paintutils.drawLine(nW-x,nH+y,nW+x,nH+y,nFaceColour)
157+
    -- only do it if the positions are different from last time
158-
      paintutils.drawLine(nW-x,nH-y,nW+x,nH-y,nFaceColour)
158+
    -- speeds things up a bit    
159
    if x~=nOldX or y~=nOldY then
160-
      paintutils.drawPixel(nW-x,nH+y,nFaceColour)
160+
      term.setTextColor(colors.white)
161-
      paintutils.drawPixel(nW+x,nH+y,nFaceColour)
161+
      term.setBackgroundColor(colors.white)
162-
      paintutils.drawPixel(nW-x,nH-y,nFaceColour)
162+
163-
      paintutils.drawPixel(nW+x,nH-y,nFaceColour)
163+
      line(nW,nH,x,y)
164
165
      nOldX = x
166
      nOldY = y
167
    end
168
  end
169
end
170
 
171
-- parse the time into H and M
172
-- these are stored globally in h and m
173
local function parseTime()
174
  local t = os.time()
175
  local sT = textutils.formatTime(t, true)
176
 
177
  local n = string.find(sT, ":")
178
 
179
  if(n>0) then
180
    h = tonumber(string.sub(sT,1,n-1))
181
    m = tonumber(string.sub(sT,n+1))
182
  end
183
end
184
 
185
-- plot minute hand
186-
  local nTmpColour
186+
187
-- then draws the new one
188
local function plotM(nW,nH,nX,nY)
189
  if(nOldMX==nX and nOldMY==nY) then
190
    return
191
  end
192
193
  -- remove previous by drawing it again
194
  -- in the face colour.  Also remove the
195
  -- hand tag
196-
    if bFaceFilled then
196+
197-
      nTmpColour = nFaceColour
197+
    term.setTextColor(colors.white)
198
    term.setBackgroundColor(colors.white)
199-
      nTmpColour = nBackColour
199+
    line(nW,nH,nOldMX,nOldMY)
200
201-
    paintutils.drawLine(nW,nH,nOldMX,nOldMY,nTmpColour)
201+
202
      plot(".",nOldMX,nOldMY)
203
    end
204
  end
205
206
  nOldMX = nX
207
  nOldMY = nY
208
209
  term.setTextColor(colors.black)
210
  term.setBackgroundColor(colors.black)
211-
  paintutils.drawLine(nW,nH,nX,nY,nHandColour)
211+
  line(nW,nH,nX,nY)
212
213
  -- draw the centre O
214
  term.setTextColor(colors.black)
215
  term.setBackgroundColor(colors.lightGray)
216
  plot("O",nW,nH)
217
218
  if(bShowMinuteChar) then
219
    -- draw the minute hand tag
220
    term.setTextColor(colors.black)
221
    term.setBackgroundColor(colors.lightGray)
222
    plot("M",nX,nY)
223
  end
224
end
225
226
-- plot hour hand
227
-- it removes the previous hour hand
228-
  local nTmpColour
228+
229
local function plotH(nW,nH,nX,nY)
230
  if(nOldHX==nX and nOldHY==nY) then
231
    return
232
  end
233
234
  -- remove previous by drawing it again
235
  -- in the face colour.  Also remove the
236
  -- hand tag
237-
    if bFaceFilled then
237+
238-
      nTmpColour = nFaceColour
238+
    term.setTextColor(colors.white)
239
    term.setBackgroundColor(colors.white)
240-
      nTmpColour = nBackColour
240+
    line(nW,nH,nOldHX,nOldHY)
241
242-
    paintutils.drawLine(nW,nH,nOldHX,nOldHY,nTmpColour)
242+
243
      plot(".",nOldHX,nOldHY)
244
    end
245
  end
246
247
  nOldHX = nX
248
  nOldHY = nY
249
250
  term.setTextColor(colors.black)
251
  term.setBackgroundColor(colors.black)
252-
  paintutils.drawLine(nW,nH,nX,nY,nHandColour)
252+
  line(nW,nH,nX,nY)
253
254
  if(bShowHourChar) then
255
    -- draw the hour hand tag
256
    term.setTextColor(colors.black)
257
    term.setBackgroundColor(colors.lightGray)
258
    plot("H",nX,nY)
259
  end
260
end
261
 
262
local fH
263
local fM
264
local fXH
265
local fYH
266
local fXM
267
local fYM
268
local nyMRadius = nClockRad - (nClockRad / 4)
269
local nyHRadius = nClockRad - (nClockRad / 2)
270
271
local nxMRadius = nyMRadius + (nyMRadius / 2)
272
local nxHRadius = nyHRadius + (nyHRadius / 2)
273
local nW, nH = term.getSize()
274
 
275
nW = (nW / 2)+1
276
nH = (nH / 2)+1
277
278
-- plot the circle
279
face(nW,nH,nClockRad + 1)
280
281
local bRunning = true
282
local nTimer
283
nTimer = os.startTimer(0.25) -- the timer interval to update the clock
284
285
while(bRunning) do
286
  parseTime()
287
 
288
  fH = (((h % 12)/12) + (m/60/12)) * 360
289
  fM = (m/60) * 360
290
 
291
  -- shift by half a circle
292
  fH = fH + 180
293
  fM = fM + 180
294
 
295
  fXH = math.sin(math.rad(-fH))
296
  fYH = math.cos(math.rad(fH))
297
  fXM = math.sin(math.rad(-fM))
298
  fYM = math.cos(math.rad(fM))
299
300
  plotH(nW,nH,  nW + fXH * nxHRadius, nH + fYH * nyHRadius)
301
  plotM(nW,nH, nW + fXM * nxMRadius, nH + fYM * nyMRadius)
302
 
303
  -- draw the ticks on
304
  ticks(nW,nH)
305
306
  local event, param1 = os.pullEvent()
307
308
  while (not (event=="timer" and param1==nTimer) and event~="char") do
309
    event, param1 = os.pullEvent()
310
  end
311
  
312
  if(event=="char") then
313
     if(param1=="q" or param1=="Q") then
314
       bRunning=false
315
     end
316
  elseif(event=="timer" and param1==nTimer) then
317
     -- timer timed out, lets start again
318
     nTimer = os.startTimer(0.25)
319
  end
320
end
321
322
term.restore()
323
print("Ended.")