Advertisement
FakoTheGreat

Rainy Day CC Clock

Jul 28th, 2014
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.40 KB | None | 0 0
  1. local monitorName = "top"
  2. local rainSensor = "right"
  3.  
  4. --Basic Variables
  5. local textSize = 1
  6. local militaryTime = true
  7. local updateDelay = 2
  8.  
  9. --Customize Notifications
  10. local dayMessage = "Day"
  11. local nightMessage = "Night"
  12. local clearMessage = "Clear"
  13. local rainMessage = "Storm"
  14.  
  15. --Used on Advanced Monitors
  16. local timeColor = colors.white
  17. local dayTextColor = colors.lightBlue
  18. local nightTextColor = colors.blue
  19. local dayBackColor = colors.black
  20. local nightBackColor = colors.black
  21. local clearWeatherColor = colors.yellow
  22. local rainyWeatherColor = colors.red
  23.  
  24. --Used Internally
  25. local monitor = nil
  26. local rainOn = false
  27. local isAm = true
  28. local tTime
  29. local x = 1
  30. local y = 1
  31.  
  32. local function getSetup()
  33.   print("Connecting to monitor.")
  34.   while monitor == nil do
  35.     monitor = peripheral.wrap(monitorName)
  36.     if monitor == nil then
  37.       print("Waiting for monitor.")
  38.       os.sleep(2)
  39.     end
  40.   end
  41.   print("Monitor found. Establishing base variables.")
  42.    
  43.   monitor.clear()
  44.   monitor.setTextScale(textSize)
  45.   x, y = monitor.getSize()
  46.    
  47.   if y < 3 then
  48.     print("Text size too large for current monitor. Please adjust.")
  49.   end
  50. end
  51.  
  52. local function centerText(cString)
  53.   local sSize = string.len(cString)
  54.   if sSize < x then
  55.     offset = math.floor((x - sSize) / 2) + 1
  56.   else
  57.     offset = 1
  58.   end
  59.  
  60.   return offset
  61. end
  62.  
  63. local function formatTime(curTime)
  64.   local curHour = 1
  65.   local curMin = 1
  66.   local strTime = ""
  67.  
  68.   curHour = math.floor(curTime)
  69.   curMin = math.floor((curTime - curHour) * 60)
  70.  
  71.   if curHour < 12 then
  72.     isAm = true
  73.   else
  74.     isAm = false
  75.     if militaryTime == false then
  76.       curHour = curHour - 12
  77.     end
  78.   end
  79.  
  80.   if curMin < 10 then
  81.     curMin = "0"..curMin
  82.   end
  83.  
  84.   if curHour < 10 then
  85.     strTime = "0"..curHour..":"..curMin
  86.   else
  87.     strTime = tostring(curHour)..":"..curMin
  88.   end
  89.  
  90.   return strTime
  91. end
  92.  
  93. getSetup()
  94.  
  95. if y >= 3 then
  96.   print("Starting main loop.")
  97.  
  98.   while true do
  99.     tTime = formatTime(os.time())
  100.     if monitor.isColor() then
  101.       if isAm then
  102.         monitor.setBackgroundColor(dayBackColor)
  103.       else
  104.         monitor.setBackgroundColor(nightBackColor)
  105.       end
  106.     end
  107.  
  108.     monitor.clear()
  109.    
  110.     if militaryTime == false then
  111.       if isAm then
  112.         tTime = tTime.." AM"
  113.       else
  114.         tTime = tTime.." PM"
  115.       end
  116.     end
  117.    
  118.     monitor.setCursorPos(centerText(tTime),1)
  119.     if monitor.isColor() then
  120.       monitor.setTextColor(timeColor)
  121.     end
  122.     monitor.write(tTime)
  123.    
  124.     if os.time() < 6 or os.time() > 18 then
  125.       if monitor.isColor() then
  126.         monitor.setTextColor(nightTextColor)
  127.       end
  128.       monitor.setCursorPos(centerText(nightMessage),2)
  129.       monitor.write(nightMessage)
  130.     else
  131.       if monitor.isColor() then
  132.         monitor.setTextColor(dayTextColor)
  133.       end
  134.       monitor.setCursorPos(centerText(dayMessage),2)
  135.       monitor.write(dayMessage)
  136.     end
  137.    
  138.     if redstone.getInput(rainSensor) then
  139.       if monitor.isColor() then
  140.         monitor.setTextColor(rainyWeatherColor)
  141.       end
  142.       monitor.setCursorPos(centerText(rainMessage),3)
  143.       monitor.write(rainMessage)
  144.     else
  145.       if monitor.isColor() then
  146.         monitor.setTextColor(clearWeatherColor)
  147.       end
  148.       monitor.setCursorPos(centerText(clearMessage),3)
  149.       monitor.write(clearMessage)
  150.     end
  151.    
  152.     os.sleep(updateDelay)
  153.   end
  154. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement