Advertisement
Guest User

h

a guest
Jul 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. local bShowHourChar = false
  2. local bShowMinuteChar = false
  3. local bFaceFilled = true
  4.  
  5. local nBackColour = colors.blue
  6. local nFaceColour = colors.white
  7. local nHandColour = colors.blue
  8. local nTickColour = colors.lightBlue
  9.  
  10. local monitor = nil
  11. local i,side
  12. for i, side in pairs(rs.getSides()) do
  13. if peripheral.getType(side) == "monitor" then
  14. monitor = peripheral.wrap(side)
  15. break
  16. end
  17. end
  18. if monitor==nil then
  19. print("NO MONITOR FOUND")
  20. print("Please attach a monitor and try again.")
  21. return
  22. end
  23.  
  24. monitor.setTextScale(0.5)
  25.  
  26. os.sleep(1)
  27.  
  28. local mw,mh = monitor.getSize()
  29. mw = mw / (15/10) -- 15x10 is w,h of 0.5 scale single monitor
  30. local minSize = (math.min(mw,mh) / 2) - 2
  31.  
  32. -- clock radius in character. 18 is good for a 6x6 advanced monitor
  33. -- or you can use minSize which attempts to fit
  34. -- to monitor size automatically
  35. local nClockRad = minSize
  36.  
  37. -- set up the screens
  38. term.clear()
  39. term.setCursorPos(1,1)
  40. print("Press 'Q' to terminate.")
  41. term.redirect(monitor)
  42. term.setBackgroundColor(nBackColour)
  43. term.clear()
  44.  
  45. -- some variables
  46. local h = 0 -- parsed hour
  47. local m = 0 -- parsed minute
  48. local nOldHX = -1
  49. local nOldHY = -1
  50. local nOldMX = -1
  51. local nOldMY = -1
  52.  
  53. local function plot(s,x,y)
  54. term.setCursorPos(x,y)
  55. term.write(s)
  56. end
  57.  
  58. -- the clock face ticks
  59. local function ticks(nW,nH)
  60. local nIndex
  61. local x
  62. local y
  63. local xi
  64. local yi
  65. local nInRadius = nClockRad-(nClockRad/4)
  66. local nOutRadius = nClockRad-2
  67.  
  68. local nxOutRad = nOutRadius + (nOutRadius/2)
  69. local nyOutRad = nOutRadius
  70. local nxInRad = nInRadius + (nInRadius/2)
  71. local nyInRad = nInRadius
  72.  
  73. for nIndex=0,359,(360/12) do
  74. x = nW + math.sin(math.rad(nIndex)) * nxOutRad
  75. y = nH + math.cos(math.rad(nIndex)) * nyOutRad
  76.  
  77. xi = nW + math.sin(math.rad(nIndex)) * nxInRad
  78. yi = nH + math.cos(math.rad(nIndex)) * nyInRad
  79.  
  80. paintutils.drawLine(xi,yi,x,y,nTickColour)
  81. end
  82. end
  83.  
  84. -- draw the face (circle)
  85. -- by working out a quarter of the points
  86. -- then drawing lines from oposite quarter
  87. -- results in much faster circle code
  88. local function face(nW,nH,nRadius)
  89. local nIndex
  90. local x
  91. local y
  92. local nxRad = nRadius + (nRadius/2)
  93. local nyRad = nRadius
  94. local nOldX = -1
  95. local nOldY = -1
  96.  
  97. local points = {}
  98. local xy
  99. local n = 1
  100.  
  101. for nIndex=0,89 do
  102. x = math.sin(math.rad(nIndex)) * nxRad
  103. y = math.cos(math.rad(nIndex)) * nyRad
  104. x = math.floor(x)
  105. y = math.floor(y)
  106.  
  107. -- only store positions if they are different from last time
  108. if x~=nOldX or y~=nOldY then
  109. xy = {}
  110. xy[1] = x
  111. xy[2] = y
  112. points[n] = xy
  113.  
  114. if y~=nOldY or not bFaceFilled then
  115. n=n+1
  116. end
  117.  
  118. nOldX = x
  119. nOldY = y
  120. end
  121. end
  122.  
  123. -- plot the circle
  124. for nIndex=1,#points do
  125. x = points[nIndex][1]
  126. y = points[nIndex][2]
  127.  
  128. if bFaceFilled then
  129. paintutils.drawLine(nW-x,nH+y,nW+x,nH+y,nFaceColour)
  130. paintutils.drawLine(nW-x,nH-y,nW+x,nH-y,nFaceColour)
  131. else
  132. paintutils.drawPixel(nW-x,nH+y,nFaceColour)
  133. paintutils.drawPixel(nW+x,nH+y,nFaceColour)
  134. paintutils.drawPixel(nW-x,nH-y,nFaceColour)
  135. paintutils.drawPixel(nW+x,nH-y,nFaceColour)
  136. end
  137. end
  138. end
  139.  
  140. -- parse the time into H and M
  141. -- these are stored globally in h and m
  142. local function parseTime()
  143. local t = os.time()
  144. local sT = textutils.formatTime(t, true)
  145.  
  146. local n = string.find(sT, ":")
  147.  
  148. if(n>0) then
  149. h = tonumber(string.sub(sT,1,n-1))
  150. m = tonumber(string.sub(sT,n+1))
  151. end
  152. end
  153.  
  154. -- plot minute hand
  155. -- it removes the previous minute hand
  156. -- then draws the new one
  157. local function plotM(nW,nH,nX,nY)
  158. local nTmpColour
  159.  
  160. if(nOldMX==nX and nOldMY==nY) then
  161. return
  162. end
  163.  
  164. -- remove previous by drawing it again
  165. -- in the face colour. Also remove the
  166. -- hand tag
  167. if(nOldMX~=-1) then
  168. if bFaceFilled then
  169. nTmpColour = nFaceColour
  170. else
  171. nTmpColour = nBackColour
  172. end
  173. paintutils.drawLine(nW,nH,nOldMX,nOldMY,nTmpColour)
  174.  
  175. if(bShowMinuteChar) then
  176. plot(".",nOldMX,nOldMY)
  177. end
  178. end
  179.  
  180. nOldMX = nX
  181. nOldMY = nY
  182.  
  183. paintutils.drawLine(nW,nH,nX,nY,nHandColour)
  184.  
  185. -- draw the centre O
  186. term.setTextColor(colors.black)
  187. term.setBackgroundColor(colors.lightGray)
  188. plot("O",nW,nH)
  189.  
  190. if(bShowMinuteChar) then
  191. -- draw the minute hand tag
  192. plot("M",nX,nY)
  193. end
  194. end
  195.  
  196. -- plot hour hand
  197. -- it removes the previous hour hand
  198. -- then draws the new one
  199. local function plotH(nW,nH,nX,nY)
  200. local nTmpColour
  201. if(nOldHX==nX and nOldHY==nY) then
  202. return
  203. end
  204.  
  205. -- remove previous by drawing it again
  206. -- in the face colour. Also remove the
  207. -- hand tag
  208. if(nOldHX~=-1) then
  209. if bFaceFilled then
  210. nTmpColour = nFaceColour
  211. else
  212. nTmpColour = nBackColour
  213. end
  214. paintutils.drawLine(nW,nH,nOldHX,nOldHY,nTmpColour)
  215.  
  216. if(bShowHourChar) then
  217. plot(".",nOldHX,nOldHY)
  218. end
  219. end
  220.  
  221. nOldHX = nX
  222. nOldHY = nY
  223.  
  224. paintutils.drawLine(nW,nH,nX,nY,nHandColour)
  225.  
  226. if(bShowHourChar) then
  227. -- draw the hour hand tag
  228. term.setTextColor(colors.black)
  229. term.setBackgroundColor(colors.lightGray)
  230. plot("H",nX,nY)
  231. end
  232. end
  233.  
  234. local fH
  235. local fM
  236. local fXH
  237. local fYH
  238. local fXM
  239. local fYM
  240. local nyMRadius = nClockRad - (nClockRad / 4)
  241. local nyHRadius = nClockRad - (nClockRad / 2)
  242.  
  243. local nxMRadius = nyMRadius + (nyMRadius / 2)
  244. local nxHRadius = nyHRadius + (nyHRadius / 2)
  245. local nW, nH = term.getSize()
  246.  
  247. nW = (nW / 2)+1
  248. nH = (nH / 2)+1
  249.  
  250. -- plot the circle
  251. face(nW,nH,nClockRad + 1)
  252.  
  253. local bRunning = true
  254. local nTimer
  255. nTimer = os.startTimer(0.25) -- the timer interval to update the clock
  256.  
  257. while(bRunning) do
  258. parseTime()
  259.  
  260. fH = (((h % 12)/12) + (m/60/12)) * 360
  261. fM = (m/60) * 360
  262.  
  263. -- shift by half a circle
  264. fH = fH + 180
  265. fM = fM + 180
  266.  
  267. fXH = math.sin(math.rad(-fH))
  268. fYH = math.cos(math.rad(fH))
  269. fXM = math.sin(math.rad(-fM))
  270. fYM = math.cos(math.rad(fM))
  271.  
  272. plotH(nW,nH, nW + fXH * nxHRadius, nH + fYH * nyHRadius)
  273. plotM(nW,nH, nW + fXM * nxMRadius, nH + fYM * nyMRadius)
  274.  
  275. -- draw the ticks on
  276. ticks(nW,nH)
  277.  
  278. local event, param1 = os.pullEvent()
  279.  
  280. while (not (event=="timer" and param1==nTimer) and event~="char") do
  281. event, param1 = os.pullEvent()
  282. end
  283.  
  284. if(event=="char") then
  285. if(param1=="q" or param1=="Q") then
  286. bRunning=false
  287. end
  288. elseif(event=="timer" and param1==nTimer) then
  289. -- timer timed out, lets start again
  290. nTimer = os.startTimer(0.25)
  291. end
  292. end
  293.  
  294. term.restore()
  295. print("Ended.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement