View difference between Paste ID: Yv5VESh1 and XbVETyjd
SHOW: | | - or go back to the newest paste.
1
threshold = 0.8
2
notifThreshold = 0.25
3
mon = peripheral.find("monitor")
4
modem = peripheral.find("modem")
5
notifSentFlag = false
6
7
cells = table.pack(peripheral.find("thermal:energy_cell"))
8
9
function notifSend (text)
10
  modem.transmit(1337,1,text)
11
end
12
13
function writeMonStatus (state,pct)
14
  mon.clear()
15
  if mon.setTextScale ~= nil then
16
    mon.setTextScale(1)
17
    local width, height = mon.getSize()
18
    if width < 15 or height < 5 then -- Small monitor - scale down
19
      mon.setTextScale(0.5)
20
    else -- Large monitor - scale up
21
      local scale = math.min(width / 14, height / 2, 5)
22
      scale = math.floor(scale * 2) / 2 -- multiple of 0.5
23
      mon.setTextScale(scale)
24
    end
25
  end
26
  mon.setCursorPos(1,1)
27
  mon.write("State: " .. (state and "On" or "Off"))
28
  mon.setCursorPos(1,2)
29
  mon.write("Energy: " .. pct .. "%")
30
end
31
32
function main ()
33
  local energyCur = 0
34
  local energyMax = 0
35
  
36
  for _,cell in ipairs(cells) do -- ipairs avoids returning count (key "n")
37
    energyCur = energyCur + cell.getEnergy()
38
    energyMax = energyMax + cell.getEnergyCapacity()
39
  end
40
41
  local energyFraction = energyCur/energyMax
42
  print(energyFraction)
43
  local energyPercent = string.format("%.2f",
44
    math.floor(energyFraction*10000)/100)
45
  
46
  if energyFraction < notifThreshold then
47
    if not notifSentFlag then
48
      notifSentFlag = true
49
      notifSend("Warning: Base power capacity at "
50
      ..energyPercent.."%")
51
    end
52
  else
53
    notifSentFlag = false
54
  end
55
  
56
  local state = energyFraction < threshold
57
  redstone.setOutput("top",state)
58
  writeMonStatus(state,energyPercent)
59
end
60
61
while true do
62
  status,err = pcall(main)
63
  if not status then print(err) end
64
  sleep(5)
65
end