Advertisement
Slayster

ComputerCraft Segmented Clock Display

Feb 15th, 2013
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.30 KB | None | 0 0
  1. -- ======================================
  2. -- Digital Clock Script for ComputerCraft
  3. -- brendan-at-slayweb.com 2013-02-15
  4. -- ======================================
  5.  
  6. -- Expects first digit to be connected via bundled cable on the left side.
  7. -- Expects second digit to be connected via bundled cable on right side.
  8. -- Expects minute counter to be connected via bundled cable on the top.
  9. -- Bottom output sends a single on/off redstone signal indicating night/day.
  10.  
  11. -- One digit is twenty lights which will form a full eight shape.
  12.  
  13. -- ++++++++
  14. -- ++0000++
  15. -- ++0++0++
  16. -- ++0++0++
  17. -- ++0000++
  18. -- ++0++0++
  19. -- ++0++0++
  20. -- ++0000++
  21. -- ++++++++
  22.  
  23. -- This is split in to 13 sections to be wired to the following colours.
  24.  
  25. -- ++++++++
  26. -- ++1223++
  27. -- ++4++5++
  28. -- ++4++5++
  29. -- ++6778++
  30. -- ++9++A++
  31. -- ++9++A++
  32. -- ++BCCD++
  33. -- ++++++++
  34.  
  35. --  1: White
  36. --  2: Orange
  37. --  3: Magenta
  38. --  4: Light Blue
  39. --  5: Yellow
  40. --  6: Lime
  41. --  7: Pink
  42. --  8: Cyan
  43. --  9: Purple
  44. -- 10: (A) Blue
  45. -- 11: (B) Brown
  46. -- 12: (C) Green
  47. -- 13: (D) Red
  48.  
  49. -- The minute counter is a cross of lights which light up two at a time every 15 world minutes.
  50.  
  51. -- +++++++
  52. -- +++O+++
  53. -- ++OOO++
  54. -- +++O+++
  55. -- +++++++
  56.  
  57. -- Wired as follows
  58.  
  59. -- +++++++
  60. -- +++2+++
  61. -- ++513++
  62. -- +++4+++
  63. -- +++++++
  64.  
  65. -- Patterns for numbers
  66. -- 0: 1,2,3,4,5,6,8,9,10,11,12,13
  67. -- 1: 3,5,8,10,13
  68. -- 2: 1,2,3,5,8,7,6,9,11,12,13
  69. -- 3: 1,2,3,5,6,7,8,10,11,12,13
  70. -- 4: 1,4,6,7,3,5,8,10,13
  71. -- 5: 1,2,3,4,6,7,8,10,13,12,11
  72. -- 6: 1,2,3,4,6,7,8,10,9,11,12,13
  73. -- 7: 1,2,3,5,8,10,13
  74. -- 8: 1,2,3,4,5,6,7,8,9,10,11,12,13
  75. -- 9: 1,2,3,4,5,6,7,8,10,11,12,13
  76.  
  77.  
  78. -- ======================
  79. -- Variables
  80. -- ======================
  81.  
  82. server_time = 0
  83. old_hour = 0
  84. old_minute = 0
  85. old_q = 0
  86. current_hour = 0
  87. current_minute = 0
  88. current_q = 0
  89.  
  90. -- ======================
  91. -- Colour Groups for numbers
  92. -- Colours for bundled outputs are just combinations of ints. These have been pre-calculated.
  93. -- These could be changed to use colors.combine()
  94. -- ======================
  95.  
  96. n0 = 32319
  97. n1 = 18964
  98. n2 = 30327
  99. n3 = 31351
  100. n4 = 19069
  101. n5 = 31343
  102. n6 = 32367
  103. n7 = 18967
  104. n8 = 32383
  105. n9 = 31359
  106.  
  107. q1 = 3
  108. q2 = 5
  109. q3 = 9
  110. q4 = 17
  111.  
  112. -- ======================
  113. -- Functions
  114. -- ======================
  115.  
  116. function update_time()
  117.    server_time = os.time()
  118.    print("Server Timestamp: "..server_time)
  119.    old_hour = current_hour
  120.    old_minute = current_minute
  121.    local worldtime = server_time
  122.    worldtime = textutils.formatTime(worldtime, true)
  123.    if string.len(worldtime)<5 then
  124.       current_hour = string.sub(worldtime, 1, 1)
  125.       current_minute = string.sub(worldtime, 3, 4)
  126.    else
  127.       current_hour = string.sub(worldtime, 1, 2)
  128.       current_minute = string.sub(worldtime, 4, 5)
  129.    end
  130.    old_q = current_q
  131.    if current_minute>"0" then
  132.       current_q = 1
  133.    end
  134.    if current_minute>"15" then
  135.       current_q = 2
  136.    end
  137.    if current_minute>"30" then
  138.       current_q = 3
  139.    end
  140.    if current_minute>"45" then
  141.       current_q = 4
  142.    end
  143.    print("World Time: "..worldtime)
  144.    print("Hour: "..current_hour)
  145.    print("Minute: "..current_minute)
  146.    print("Quarter: "..current_q)
  147. end
  148.  
  149. function update_required()
  150.    if current_hour~=old_hour then
  151.       return true
  152.    end
  153.    if current_q~=old_q then
  154.       return true
  155.    end
  156.    return false
  157. end
  158.  
  159. function get_number_colors(num)
  160.    if num=="" then
  161.       return 0
  162.    end
  163.    if num=="0" then
  164.       return n0
  165.    end
  166.    if num=="1" then
  167.       return n1
  168.    end
  169.    if num=="2" then
  170.       return n2
  171.    end
  172.    if num=="3" then
  173.       return n3
  174.    end
  175.    if num=="4" then
  176.       return n4
  177.    end
  178.    if num=="5" then
  179.       return n5
  180.    end
  181.    if num=="6" then
  182.       return n6
  183.    end
  184.    if num=="7" then
  185.       return n7
  186.    end
  187.    if num=="8" then
  188.       return n8
  189.    end
  190.    if num=="9" then
  191.       return n9
  192.    end  
  193.    return 0
  194. end
  195.  
  196. function get_quarter_colors(num)
  197.    if num=="" then
  198.       return 0
  199.    end
  200.    if num==1 then
  201.       return q1
  202.    end
  203.    if num==2 then
  204.       return q2
  205.    end
  206.    if num==3 then
  207.       return q3
  208.    end
  209.    if num==4 then
  210.       return q4
  211.    end
  212.    return 0
  213. end
  214.  
  215. function reload_display()
  216.    local digit1 = ""
  217.    local digit2 = ""
  218.  
  219.    if string.len(current_hour)>1 then
  220.       -- Double digit hour
  221.       digit1 = string.sub(current_hour,1,1)
  222.       digit2 = string.sub(current_hour,2,2)
  223.    else
  224.       -- Single digit hour
  225.       digit1 = ""
  226.       digit2 = string.sub(current_hour,1,1)
  227.    end
  228.    
  229.    -- Left output: Digit 1
  230.    print("Digit 1: "..digit1)
  231.    rs.setBundledOutput("left",get_number_colors(digit1))
  232.    
  233.    -- Right output: Digit 2
  234.    print("Digit 2: "..digit2)
  235.    rs.setBundledOutput("right",get_number_colors(digit2))
  236.  
  237.    -- Top output: Quarter
  238.    rs.setBundledOutput("top",get_quarter_colors(current_q))
  239.  
  240.    -- Bottom output: Day/Night
  241.    -- Send redstone signal if night time
  242.    if (server_time>8) and (server_time<"20") then
  243.       print("Day")
  244.       rs.setOutput("bottom",false)
  245.    else
  246.       print("Night")
  247.       rs.setOutput("bottom",true)
  248.    end
  249.  
  250. end
  251.  
  252. -- ======================
  253. -- Main Program
  254. -- ======================
  255.  
  256. function display_loop()
  257.    term.clear()
  258.    term.setCursorPos(1,1)
  259.    update_time()
  260.    if update_required() then
  261.       reload_display()
  262.    end
  263. end
  264.  
  265. while true do
  266.    display_loop()
  267.    sleep(2)
  268. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement