Advertisement
mrWhiskasss

Solar_Generator_Control

Oct 12th, 2021
931
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component         = require("component")
  2. local gpu               = component.gpu
  3. local unicode           = require("unicode")
  4. local event             = require("event")
  5. local term              = require("term")
  6. local computer          = require("computer")
  7.  
  8. computer.addUser("bogatyr4ik")
  9. computer.addUser("kudrin")
  10.  
  11. if false == component.isAvailable("redstone") then
  12.     print("Redstone not connected!")
  13.     os.exit()
  14. end
  15.  
  16. if false == component.isAvailable("extrautils_generatorsolar") then
  17.     print("SolarPanel not connected!")
  18.     os.exit()
  19. end
  20.  
  21. local rs            = component.redstone
  22. local panel         = component.extrautils_generatorsolar
  23. local EnergyStep    = 425000  -- шаг для батарейки
  24. local colorBack     = 0x151515
  25. local colorText     = 0xBC8F8F --0xFFFFFF --
  26. local colorRed      = 0xFF0000
  27. local colorBlack    = 0x000000
  28. local colorBlue     = 0x4682B4
  29. local colorGreen    = 0x00FF00
  30. local colorBattery  = 0xFFFFFF
  31. local l, v          = 4, 3 -- для основного текста l - отступ с лева v - отступ с верху
  32. local w, h          = 64, 20
  33. local a             = 100 -- Ёмкость в % для начала отдачи энергии
  34. local b             = 70  -- Ёмкость в % для начала зарядки панелей
  35. local step          = 5
  36.  
  37. gpu.setResolution(w, h) -- Заданное розрешение экрана
  38. gpu.setBackground(colorBack)
  39. gpu.setForeground(colorText)
  40. gpu.fill(1,1,w,h, " ") -- Очистка экрана перед запуском программы
  41.  
  42. -- Обводка экрана
  43. gpu.fill(1, 1, 1, 1, "┌")
  44. gpu.fill(1, h, 1, 1, "└")
  45. gpu.fill(1, 2 ,1, h-2, "│") -- левая пололса
  46. gpu.fill(w, 2, 1, h-2, "│") -- правая полоса
  47. gpu.fill(2, 1, w-2, 1, "─") --верхняя полоса
  48. gpu.fill(2, h, w-2, 1, "─") -- нижняя полоса
  49. gpu.fill(w, 1, 1, 1, "┐")
  50. gpu.fill(w, h, 1, 1, "┘")
  51.  
  52. gpu.set(l,v, "SolarPanel Control Ready!!")
  53. gpu.set(l+unicode.len("Status: "),v+2, "Expectation")
  54.  
  55. function buttom()
  56.   gpu.setBackground(0xFFFFFF)
  57.   gpu.setForeground(0x000000)
  58.   gpu.set(l+13, v+5, " + ")
  59.   gpu.set(l+17, v+5, " - ")
  60.   gpu.set(l+13, v+7, " + ")
  61.   gpu.set(l+17, v+7, " - ")
  62.   gpu.setBackground(colorBack)
  63.   gpu.setForeground(colorText)
  64. end
  65.  
  66. --[[gpu.fill(l+10, v+5, 2, 1, " ")
  67.   gpu.fill(l+10, v+7, 2, 1, " ")]]--
  68.  
  69. function touch(w, h)
  70.   if w >= l+13 and w <= l+15 and h == v+5 and a + step <= 100 then
  71.     a = a + step
  72.     gpu.setBackground(0x00FF00)
  73.     gpu.set(l+13, v+5, " + ")
  74.     os.sleep(0.1)
  75.     gpu.setBackground(colorBack)
  76.   elseif w >= l+17 and w <= l+19 and h == v+5 and a - step > b then
  77.     a = a - step
  78.     gpu.setBackground(0xFF0000)
  79.     gpu.set(l+17, v+5, " - ")
  80.     os.sleep(0.1)
  81.     gpu.setBackground(colorBack)
  82.   elseif w >= l+13 and w <= l+15 and h == v+7 and b + step < a then
  83.     b = b + step
  84.     gpu.setBackground(0x00FF00)
  85.     gpu.set(l+13, v+7, " + ")
  86.     os.sleep(0.1)
  87.     gpu.setBackground(colorBack)
  88.   elseif w >= l+17 and w <= l+19 and h == v+7 and b - step >= 0 then
  89.     b = b - step
  90.     gpu.setBackground(0xFF0000)
  91.     gpu.set(l+17, v+7, " - ")
  92.     os.sleep(0.1)
  93.     gpu.setBackground(colorBack)
  94.   end
  95. end
  96.  
  97. function turn(i)
  98.   for j = 0,5 do
  99.   rs.setOutput(j,i)
  100. end
  101. end
  102.  
  103.  
  104. while true do
  105.   stored = panel.getEnergyStored()
  106.   MaxStored = panel.getMaxEnergyStored()
  107.   EnergyUP = MaxStored/100*a
  108.   EnergyDown = MaxStored/100*b
  109.   MaxLevelEnergy = EnergyUP -- Уровени при котором начинает отдавать энергию
  110.   MinLevelEnergy = EnergyDown -- Уровень при котором начинает заряд
  111.   PercentStorage = math.ceil(stored*100/MaxStored)
  112.   if stored >= MaxLevelEnergy then
  113.     turn(1)
  114.     gpu.setForeground(colorBlue)
  115.     gpu.set(l+unicode.len("Status: "),v+2, "▼ Transmition")
  116.     gpu.setForeground(colorText)
  117.   end
  118.   if stored < MinLevelEnergy then
  119.     turn(0)
  120.     gpu.setForeground(colorGreen)
  121.     gpu.set(l+unicode.len("Status: "),v+2, "▲ Charging    ")
  122.     gpu.setForeground(colorText)
  123.   end
  124. -- Вывод строк данных
  125.   gpu.set(l, v+1, "Energy in Generator: ["..stored.."] RF "..PercentStorage.."%     ")
  126.   gpu.set(l, v+2, "Status: ")      
  127.   gpu.set(l, v+3, "Curent control:")
  128.   gpu.set(l, h-2, "by Bogatyr4ik")
  129.   gpu.set(l+2, v+5, "Max:")
  130.   gpu.set(l+2, v+7, "Min:")
  131.   if a < 100 then
  132.     gpu.fill(l+10, v+5, 2, 1, " ")
  133.   end
  134.   if b < 10 then
  135.     gpu.fill(l+9, v+7, 2, 1, " ")
  136.   end
  137.   gpu.setForeground(colorRed) -- красный
  138.   gpu.set(l+7, v+5, ""..a.."%")
  139.   gpu.setForeground(colorGreen) -- зелёный
  140.   gpu.set(l+7, v+7, ""..b.."%")
  141. -- Виз батарейки
  142.   verticalpos = 1 -- вертикаль gz
  143.   horizontalpos = 15 -- горизонталь vk
  144.   gpu.setForeground(colorText)
  145.   gpu.fill(w/2+5+horizontalpos, h/2-9+verticalpos, 6, 1, "█") -- контакт
  146.   gpu.fill(w/2+horizontalpos, h/2-8+verticalpos, 16, 17, "█") -- основа
  147.   gpu.setForeground(colorGreen)
  148.   gpu.fill(w/2+2+horizontalpos, h/2-7+verticalpos,12, 15, "█") -- заливка пустоты чёрная
  149. -- Вывод энергиии в батарейке
  150.   gpu.setForeground(colorBlack)
  151.   gpu.fill(w/2+2+horizontalpos, h/2-7+verticalpos, 12, (100-PercentStorage)*(15/100), "█") -- Ёмкость Энергии Зелёная
  152.   gpu.setForeground(colorText)
  153.   buttom()
  154.   term.setCursor(1,1)
  155.   local e,_,w,h,_,_ = event.pull(0.1, "touch")
  156.   if e == "touch" then
  157.     touch(w, h)
  158.   end
  159. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement