This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
The_Cat's profile picture

Reactor Controlling Program

Started by The_Cat, 20 June 2015 - 02:32 PM
The_Cat #1
Posted 20 June 2015 - 04:32 PM
Hey Guys,

So I am making this program which lets me control and see different things using the touch screen monitor.

The problem which I am having is that the screen flashes (I think its because of the loop updating the writing of the buttons or what not) so I tried adding a timer event within the loop but had no luck as it seems to somewhat ignore the timer i.e. doesn't stop everything keeps flashing the display.

Here is my code (Reactor is connected via wired modem):

Spoiler

-- Reactor controller program
-- Variables
m = peripheral.wrap("right")
r = peripheral.wrap("BigReactors-Reactor_1") -- r = Reactor
e = peripheral.wrap("tile_blockcapacitorbank_name_0") -- e = Capacitor Bank
-- c = Capacitor, r = reactor
cCurrentEnergy = 0
cEnergyLastTick = 0
cEnergyUseage = 0
rRFProduced = 0
rRFTarget = 0
rControlRodAmount = 0
test = true
w, h = m.getSize()
textButton = {
{text="Reactor: ", xPos=1 , yPos=1, other=r.getActive(), txtColor=colors.yellow, otherColor=colors.green, func=1},
{text="Energy Stored: ", xPos=1, yPos=3, other=0, txtColor=colors.yellow, otherColor=colors.green, func=2},
{text="Energy Producing: ", xPos=1, yPos=5, other=math.ceil(r.getEnergyProducedLastTick()), txtColor=colors.yellow, otherColor=colors.green, func=3},
--{text="Energy Usage: ", xPos=1, yPos=7, other=cEnergyUseage, txtColor=colors.yellow, otherColor=colors.green, func=4},

}
-- Functions
function writeInfo(_table)
for i, v in pairs(_table) do -- For loop for a table
  m.setCursorPos(v.xPos, v.yPos) -- Within table takes the x and y pos to set the pos
  if v.func == 1 then -- checks for func as that is what the func is
   v.other = r.getActive() --gets if reactor is active to print out later
  elseif v.func == 2 then
   if isPercentage then --  Checks isPercentage if so returns a percentage using rPercentage()
    v.other = math.ceil(rPercentageAmount) .."%"
   else
    v.other = math.ceil(r.getEnergyStored()) .." RF" -- If not just returns energy stored
   end
  elseif v.func == 3 then
   v.other = math.ceil(r.getEnergyProducedLastTick()) .. " RF/T"
  end
  m.setTextColor(v.txtColor)
  m.write(v.text) -- writes text
  m.setTextColor(v.otherColor)
  m.write(v.other) -- writes info that changes
end
end
function checkClick(_table, touchX, touchY)
for i,v in pairs(_table) do
  if touchY == v.yPos then
   return true, v.func, v.other
  end
end
return false, nil
end
--[[function setVars() -- Sets some baribles for logic
-- Calculations for energy usage of base.
cEnergyLastTick = e.getEnergyStored()
cCurrentEnergy = e.getEnergyStored()
cEnergyUseage = cEnergyLastTick - cCurrentEnergy
end]]
function rPercentage() -- Works out current percentage in reactor
maxEnergy = 10000000
currEnergy = r.getEnergyStored()
rPercentageAmount = currEnergy / maxEnergy * 100
end
function changeActive()
if r.getActive() then
  r.setActive(false)
else
  r.setActive(true)
end
end
function changeEnergy(text)
if isPercentage then
  isPercentage = false
else
  isPercentage = true
end
end
m.clear()
myTimer=1
while true do
--display()
--getClickPos()
rPercentage() --Works out percentage for display
sleep(1)
writeInfo(textButton) -- Writes textButton on monitor
myTimer = os.startTimer(1) -- Starts timer so screen can update
event, but, x, y = os.pullEvent() -- Waits for a event to Pull
if event == "timer" and but == myTimer then
 
elseif event == "monitor_touch" then -- See if event monitor_touch was called
  clicked, option, other2 = checkClick(textButton, x, y) -- Checks if the monitor were the monitor was touched only if the event was called
  if option == 1 then -- sees if option is 1
   changeActive() -- Calls functions
  elseif option == 2 then
   changeEnergy(other2)
  end
  -- Checks if event is timer and is equal to myTimer
end
m.clear()
end
TheOddByte #2
Posted 20 June 2015 - 04:55 PM
You mean that it's flickering? That often happens when you clear the screen and redraw a lot of stuff all the time, there are different ways to fix this, but the most ideal way todo it is to use a buffer, which prevents the screen from flickering.
You can find a buffer in the APIs section, here's one

Another way you could fix this would be to not redraw as often, use a variable or something that redraws every 100th time or something.

local var = 0
while true do
    var = var + 1
    if var >= 100 then
        var = 0
        redraw()
    end
end
This is not a really good solution IMO, but it may work in your situation, depending on how fast you go through the loop.
Because if for example you go through the loop 100 times in one second, then this would only redraw one time each second, instead of 100 times :P/>
Lyqyd #3
Posted 20 June 2015 - 05:02 PM
Well, no, the problem is that the screen is getting cleared, and then appears to sleep for a second, and then updated values are fetched while things are being written to the screen. This should just require a re-ordering of the existing code, it won't require buffering or any other complex solutions. Simply fetch new data, and only then clear the screen and immediately re-write the new data.
TheOddByte #4
Posted 20 June 2015 - 06:20 PM
Well, no, the problem is that the screen is getting cleared, and then appears to sleep for a second, and then updated values are fetched while things are being written to the screen. This should just require a re-ordering of the existing code, it won't require buffering or any other complex solutions. Simply fetch new data, and only then clear the screen and immediately re-write the new data.
Ooops, my mistake, thought it was because of excessive drawing.
With the information Lyqyd gave you, you simply have to move "m.clear()" before you draw.
The_Cat #5
Posted 21 June 2015 - 11:42 AM
Ooops, my mistake, thought it was because of excessive drawing.
With the information Lyqyd gave you, you simply have to move "m.clear()" before you draw.

Works now!
Thanks Lyqyd and OddByte :)/>