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

ComputerCraft & BigReactor, Auto-Refresh bug

Started by trafgan, 29 April 2017 - 03:45 PM
trafgan #1
Posted 29 April 2017 - 05:45 PM
Bug:
Hello, the auto refresh of my program does not work (It does not automatically update)

My code:

ReactorName = "BigReactors-Reactor"
Nom = "Mon reactor"
NomColor = colors.yellow
BackGroundColor = colors.lightGray
InfoColor = colors.gray







ecran = peripheral.wrap("monitor_0")
machine = peripheral.find(ReactorName)
print("Loading...")
sleep(1)
print("Systeme by trafgan")

while true do
    ecran.setBackgroundColor(BackGroundColor)
    ecran.clear()
    ecran.setCursorPos(17.5, 1)
    ecran.setTextColor(NomColor)
    ecran.write(Nom)
    ecran.setCursorPos(1, 5)
    ecran.setTextColor(InfoColor)
    ecran.write("Energie Stockee: "..machine.getEnergyStored().." Rf")
    ecran.setCursorPos(1, 6)
    ecran.write("Energie par tick: "..machine.getEnergyProducedLastTick().." Rf/T")
    ecran.setBackgroundColor(colors.red)
    ecran.setCursorPos(9, 12)
    ecran.setTextColor(colors.white)
    ecran.write("Eteindre")
    ecran.setBackgroundColor(colors.green)
    ecran.setCursorPos(1, 12)
    ecran.setTextColor(colors.white)
    ecran.write("Allumer")
    event, side, x, y = os.pullEvent("monitor_touch")
    if x > 1 and x < 7 and y == 12 then
	    ecran.clear()
	    machine.setActive(true)
    end
    if x > 9 and x < 17 and y == 12 then
	    ecran.clear()
	    machine.setActive(false)
    end
    sleep(0.2)
end

Hello, the auto refresh of my program does not work (It does not automatically update)
Lyqyd #2
Posted 29 April 2017 - 09:02 PM
Moved to Ask a Pro.

Your script waits for a monitor_touch event on each iteration of the loop.
Bomb Bloke #3
Posted 30 April 2017 - 04:25 AM
Instead of this sort of thing:

    event, side, x, y = os.pullEvent("monitor_touch")
    if x > 1 and x < 7 and y == 12 then
            ecran.clear()
            machine.setActive(true)
    end
    if x > 9 and x < 17 and y == 12 then
            ecran.clear()
            machine.setActive(false)
    end
    sleep(0.2)

You're really wanting to do something along these lines:

    local myTimer = os.startTimer(0.2)
    event, side, x, y = os.pullEvent()
    if event == "monitor_touch" then
        if x > 1 and x < 7 and y == 12 then
            ecran.clear()
            machine.setActive(true)
        elseif x > 9 and x < 17 and y == 12 then
            ecran.clear()
            machine.setActive(false)
        end
    end

This'll hence allow your loop to repeat on any event, using os.startTimer() to ensure that one's always pending. It could be neatened up a bit to only repeat in response to your timers / clicks (as opposed to all the other event sources, eg rednet messages, keyboard input, etc), though.

I'd say five updates a second is a bit excessive. Personally I'd be going at something like one per five.
trafgan #4
Posted 30 April 2017 - 07:46 AM
AAAA thanks :)/>
I'm going to try

I'm going to try

Okeeey good, Why my screens blink?
Why my screens blink
Edited on 30 April 2017 - 06:28 AM
Bomb Bloke #5
Posted 30 April 2017 - 08:31 AM
Because machine.getEnergyStored() and machine.getEnergyProducedLastTick() don't execute immediately. Because you call them while redrawing the screen, the process is slowed enough that you can see it happening.

If you called them before clearing the display, that wouldn't be such a problem:

while true do
    local energy, energyLastTick = machine.getEnergyStored(), machine.getEnergyProducedLastTick()
    ecran.setBackgroundColor(BackGroundColor)
    ecran.clear()
    .
    .
    .
    ecran.write("Energie Stockee: "..energy.." Rf")
    .
    .
    .