Advertisement
pedrosgali

Pedro's 7Seg Clock

Nov 24th, 2013
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.61 KB | None | 0 0
  1. m = peripheral.wrap("right")
  2. m.setTextScale(2)
  3. clockTop = 3
  4. segLen = 3
  5. lit = colors.lime
  6. unlit = colors.green
  7.  
  8. function drawLine(stx, sty, len, dir, col)
  9.     m.setBackgroundColor(col)
  10.     m.setCursorPos(stx, sty)
  11.     if dir == "a" or dir == "across" then
  12.         for i = 1, len do
  13.             m.write(" ")
  14.         end
  15.     elseif dir == "d" or dir == "down" then
  16.         for i = 1, len do
  17.             m.write(" ")
  18.             m.setCursorPos(stx, sty + i)
  19.         end
  20.     end
  21.     m.setBackgroundColor(colors.black)
  22. end
  23.  
  24. function sevSeg(stx, seg, col)
  25.     if seg == 1 then
  26.         drawLine(stx + 1, clockTop, segLen, "a", col)
  27.     elseif seg == 2 then
  28.         drawLine(stx, clockTop + 1, segLen, "d", col)
  29.     elseif seg == 3 then
  30.         drawLine(stx + 4, clockTop + 1, segLen, "d", col)
  31.     elseif seg == 4 then
  32.         drawLine(stx + 1, clockTop + 4, segLen, "a", col)
  33.     elseif seg == 5 then
  34.         drawLine(stx, clockTop + 5, segLen, "d", col)
  35.     elseif seg == 6 then
  36.         drawLine(stx + 4, clockTop + 5, segLen, "d", col)
  37.     elseif seg == 7 then
  38.         drawLine(stx + 1, clockTop + 8, segLen, "a", col)
  39.     end
  40. end
  41.  
  42. function drawNum(stx, num, col)
  43.     if num == 0 then
  44.         sevSeg(stx, 1, col)
  45.         sevSeg(stx, 2, col)
  46.         sevSeg(stx, 3, col)
  47.         sevSeg(stx, 5, col)
  48.         sevSeg(stx, 6, col)
  49.         sevSeg(stx, 7, col)
  50.     elseif num == 1 then
  51.         sevSeg(stx, 3, col)
  52.         sevSeg(stx, 6, col)
  53.     elseif num == 2 then
  54.         sevSeg(stx, 1, col)
  55.         sevSeg(stx, 3, col)
  56.         sevSeg(stx, 4, col)
  57.         sevSeg(stx, 5, col)
  58.         sevSeg(stx, 7, col)
  59.     elseif num == 3 then
  60.         sevSeg(stx, 1, col)
  61.         sevSeg(stx, 3, col)
  62.         sevSeg(stx, 4, col)
  63.         sevSeg(stx, 6, col)
  64.         sevSeg(stx, 7, col)
  65.     elseif num == 4 then
  66.         sevSeg(stx, 2, col)
  67.         sevSeg(stx, 3, col)
  68.         sevSeg(stx, 4, col)
  69.         sevSeg(stx, 6, col)
  70.     elseif num == 5 then
  71.         sevSeg(stx, 1, col)
  72.         sevSeg(stx, 2, col)
  73.         sevSeg(stx, 4, col)
  74.         sevSeg(stx, 6, col)
  75.         sevSeg(stx, 7, col)
  76.     elseif num == 6 then
  77.         sevSeg(stx, 1, col)
  78.         sevSeg(stx, 2, col)
  79.         sevSeg(stx, 4, col)
  80.         sevSeg(stx, 5, col)
  81.         sevSeg(stx, 6, col)
  82.         sevSeg(stx, 7, col)
  83.     elseif num == 7 then
  84.         sevSeg(stx, 1, col)
  85.         sevSeg(stx, 3, col)
  86.         sevSeg(stx, 6, col)
  87.     elseif num == 8 then
  88.         for i = 1, 7 do
  89.             sevSeg(stx, i, col)
  90.         end
  91.     elseif num == 9 then
  92.         sevSeg(stx, 1, col)
  93.         sevSeg(stx, 2, col)
  94.         sevSeg(stx, 3, col)
  95.         sevSeg(stx, 4, col)
  96.         sevSeg(stx, 6, col)
  97.         sevSeg(stx, 7, col)
  98.     else
  99.         drawLine(stx, clockTop + 3, 1, "a", lit)
  100.         drawLine(stx, clockTop + 6, 1, "a", lit)
  101.     end
  102. end
  103.  
  104. function getTime()
  105.     time = textutils.formatTime(os.time(), true)
  106.     if string.len(time) > 4 then
  107.         ht = string.sub(time, 1, 1)
  108.         hu = string.sub(time, 2, 2)
  109.         mt = string.sub(time, 4, 4)
  110.         mu = string.sub(time, 5, 5)
  111.     else
  112.         ht = 0
  113.         hu = string.sub(time, 1, 1)
  114.         mt = string.sub(time, 3, 3)
  115.         mu = string.sub(time, 4, 4)
  116.     end
  117.     ht = tonumber(ht)
  118.     hu = tonumber(hu)
  119.     mt = tonumber(mt)
  120.     mu = tonumber(mu)
  121. end
  122.  
  123. function display()
  124.     getTime()
  125.     drawNum(3, 8, unlit)
  126.     drawNum(9, 8, unlit)
  127.     drawNum(15, 10, lit)
  128.     drawNum(17, 8, unlit)
  129.     drawNum(23, 8, unlit)
  130.     drawNum(3, ht, lit)
  131.     drawNum(9, hu, lit)
  132.     drawNum(17, mt, lit)
  133.     drawNum(23, mu, lit)
  134. end
  135.  
  136. while true do
  137.     display()
  138.     sleep(1)
  139. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement